dsi_ctrl.c 106 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/of_device.h>
  6. #include <linux/err.h>
  7. #include <linux/regulator/consumer.h>
  8. #include <linux/clk.h>
  9. #include <linux/of_irq.h>
  10. #include <video/mipi_display.h>
  11. #include "msm_drv.h"
  12. #include "msm_kms.h"
  13. #include "msm_mmu.h"
  14. #include "dsi_ctrl.h"
  15. #include "dsi_ctrl_hw.h"
  16. #include "dsi_clk.h"
  17. #include "dsi_pwr.h"
  18. #include "dsi_catalog.h"
  19. #include "dsi_panel.h"
  20. #include "sde_dbg.h"
  21. #define DSI_CTRL_DEFAULT_LABEL "MDSS DSI CTRL"
  22. #define DSI_CTRL_TX_TO_MS 200
  23. #define TO_ON_OFF(x) ((x) ? "ON" : "OFF")
  24. #define CEIL(x, y) (((x) + ((y)-1)) / (y))
  25. #define TICKS_IN_MICRO_SECOND 1000000
  26. #define DSI_CTRL_DEBUG(c, fmt, ...) DRM_DEV_DEBUG(NULL, "[msm-dsi-debug]: %s: "\
  27. fmt, c ? c->name : "inv", ##__VA_ARGS__)
  28. #define DSI_CTRL_ERR(c, fmt, ...) DRM_DEV_ERROR(NULL, "[msm-dsi-error]: %s: "\
  29. fmt, c ? c->name : "inv", ##__VA_ARGS__)
  30. #define DSI_CTRL_INFO(c, fmt, ...) DRM_DEV_INFO(NULL, "[msm-dsi-info]: %s: "\
  31. fmt, c->name, ##__VA_ARGS__)
  32. #define DSI_CTRL_WARN(c, fmt, ...) DRM_WARN("[msm-dsi-warn]: %s: " fmt,\
  33. c ? c->name : "inv", ##__VA_ARGS__)
  34. struct dsi_ctrl_list_item {
  35. struct dsi_ctrl *ctrl;
  36. struct list_head list;
  37. };
  38. static LIST_HEAD(dsi_ctrl_list);
  39. static DEFINE_MUTEX(dsi_ctrl_list_lock);
  40. static const enum dsi_ctrl_version dsi_ctrl_v1_4 = DSI_CTRL_VERSION_1_4;
  41. static const enum dsi_ctrl_version dsi_ctrl_v2_0 = DSI_CTRL_VERSION_2_0;
  42. static const enum dsi_ctrl_version dsi_ctrl_v2_2 = DSI_CTRL_VERSION_2_2;
  43. static const enum dsi_ctrl_version dsi_ctrl_v2_3 = DSI_CTRL_VERSION_2_3;
  44. static const enum dsi_ctrl_version dsi_ctrl_v2_4 = DSI_CTRL_VERSION_2_4;
  45. static const enum dsi_ctrl_version dsi_ctrl_v2_5 = DSI_CTRL_VERSION_2_5;
  46. static const enum dsi_ctrl_version dsi_ctrl_v2_6 = DSI_CTRL_VERSION_2_6;
  47. static const struct of_device_id msm_dsi_of_match[] = {
  48. {
  49. .compatible = "qcom,dsi-ctrl-hw-v1.4",
  50. .data = &dsi_ctrl_v1_4,
  51. },
  52. {
  53. .compatible = "qcom,dsi-ctrl-hw-v2.0",
  54. .data = &dsi_ctrl_v2_0,
  55. },
  56. {
  57. .compatible = "qcom,dsi-ctrl-hw-v2.2",
  58. .data = &dsi_ctrl_v2_2,
  59. },
  60. {
  61. .compatible = "qcom,dsi-ctrl-hw-v2.3",
  62. .data = &dsi_ctrl_v2_3,
  63. },
  64. {
  65. .compatible = "qcom,dsi-ctrl-hw-v2.4",
  66. .data = &dsi_ctrl_v2_4,
  67. },
  68. {
  69. .compatible = "qcom,dsi-ctrl-hw-v2.5",
  70. .data = &dsi_ctrl_v2_5,
  71. },
  72. {
  73. .compatible = "qcom,dsi-ctrl-hw-v2.6",
  74. .data = &dsi_ctrl_v2_6,
  75. },
  76. {}
  77. };
  78. #ifdef CONFIG_DEBUG_FS
  79. static ssize_t debugfs_state_info_read(struct file *file,
  80. char __user *buff,
  81. size_t count,
  82. loff_t *ppos)
  83. {
  84. struct dsi_ctrl *dsi_ctrl = file->private_data;
  85. char *buf;
  86. u32 len = 0;
  87. if (!dsi_ctrl)
  88. return -ENODEV;
  89. if (*ppos)
  90. return 0;
  91. buf = kzalloc(SZ_4K, GFP_KERNEL);
  92. if (!buf)
  93. return -ENOMEM;
  94. /* Dump current state */
  95. len += snprintf((buf + len), (SZ_4K - len), "Current State:\n");
  96. len += snprintf((buf + len), (SZ_4K - len),
  97. "\tCTRL_ENGINE = %s\n",
  98. TO_ON_OFF(dsi_ctrl->current_state.controller_state));
  99. len += snprintf((buf + len), (SZ_4K - len),
  100. "\tVIDEO_ENGINE = %s\n\tCOMMAND_ENGINE = %s\n",
  101. TO_ON_OFF(dsi_ctrl->current_state.vid_engine_state),
  102. TO_ON_OFF(dsi_ctrl->current_state.cmd_engine_state));
  103. /* Dump clock information */
  104. len += snprintf((buf + len), (SZ_4K - len), "\nClock Info:\n");
  105. len += snprintf((buf + len), (SZ_4K - len),
  106. "\tBYTE_CLK = %u, PIXEL_CLK = %u, ESC_CLK = %u\n",
  107. dsi_ctrl->clk_freq.byte_clk_rate,
  108. dsi_ctrl->clk_freq.pix_clk_rate,
  109. dsi_ctrl->clk_freq.esc_clk_rate);
  110. if (len > count)
  111. len = count;
  112. len = min_t(size_t, len, SZ_4K);
  113. if (copy_to_user(buff, buf, len)) {
  114. kfree(buf);
  115. return -EFAULT;
  116. }
  117. *ppos += len;
  118. kfree(buf);
  119. return len;
  120. }
  121. static ssize_t debugfs_reg_dump_read(struct file *file,
  122. char __user *buff,
  123. size_t count,
  124. loff_t *ppos)
  125. {
  126. struct dsi_ctrl *dsi_ctrl = file->private_data;
  127. char *buf;
  128. u32 len = 0;
  129. struct dsi_clk_ctrl_info clk_info;
  130. int rc = 0;
  131. if (!dsi_ctrl)
  132. return -ENODEV;
  133. if (*ppos)
  134. return 0;
  135. buf = kzalloc(SZ_4K, GFP_KERNEL);
  136. if (!buf)
  137. return -ENOMEM;
  138. clk_info.client = DSI_CLK_REQ_DSI_CLIENT;
  139. clk_info.clk_type = DSI_CORE_CLK;
  140. clk_info.clk_state = DSI_CLK_ON;
  141. rc = dsi_ctrl->clk_cb.dsi_clk_cb(dsi_ctrl->clk_cb.priv, clk_info);
  142. if (rc) {
  143. DSI_CTRL_ERR(dsi_ctrl, "failed to enable DSI core clocks\n");
  144. kfree(buf);
  145. return rc;
  146. }
  147. if (dsi_ctrl->hw.ops.reg_dump_to_buffer)
  148. len = dsi_ctrl->hw.ops.reg_dump_to_buffer(&dsi_ctrl->hw,
  149. buf, SZ_4K);
  150. clk_info.clk_state = DSI_CLK_OFF;
  151. rc = dsi_ctrl->clk_cb.dsi_clk_cb(dsi_ctrl->clk_cb.priv, clk_info);
  152. if (rc) {
  153. DSI_CTRL_ERR(dsi_ctrl, "failed to disable DSI core clocks\n");
  154. kfree(buf);
  155. return rc;
  156. }
  157. if (len > count)
  158. len = count;
  159. len = min_t(size_t, len, SZ_4K);
  160. if (copy_to_user(buff, buf, len)) {
  161. kfree(buf);
  162. return -EFAULT;
  163. }
  164. *ppos += len;
  165. kfree(buf);
  166. return len;
  167. }
  168. static ssize_t debugfs_line_count_read(struct file *file,
  169. char __user *user_buf,
  170. size_t user_len,
  171. loff_t *ppos)
  172. {
  173. struct dsi_ctrl *dsi_ctrl = file->private_data;
  174. char *buf;
  175. int rc = 0;
  176. u32 len = 0;
  177. size_t max_len = min_t(size_t, user_len, SZ_4K);
  178. if (!dsi_ctrl)
  179. return -ENODEV;
  180. if (*ppos)
  181. return 0;
  182. buf = kzalloc(max_len, GFP_KERNEL);
  183. if (ZERO_OR_NULL_PTR(buf))
  184. return -ENOMEM;
  185. mutex_lock(&dsi_ctrl->ctrl_lock);
  186. len += scnprintf(buf, max_len, "Command triggered at line: %04x\n",
  187. dsi_ctrl->cmd_trigger_line);
  188. len += scnprintf((buf + len), max_len - len,
  189. "Command triggered at frame: %04x\n",
  190. dsi_ctrl->cmd_trigger_frame);
  191. len += scnprintf((buf + len), max_len - len,
  192. "Command successful at line: %04x\n",
  193. dsi_ctrl->cmd_success_line);
  194. len += scnprintf((buf + len), max_len - len,
  195. "Command successful at frame: %04x\n",
  196. dsi_ctrl->cmd_success_frame);
  197. mutex_unlock(&dsi_ctrl->ctrl_lock);
  198. if (len > max_len)
  199. len = max_len;
  200. if (copy_to_user(user_buf, buf, len)) {
  201. rc = -EFAULT;
  202. goto error;
  203. }
  204. *ppos += len;
  205. error:
  206. kfree(buf);
  207. return len;
  208. }
  209. static const struct file_operations state_info_fops = {
  210. .open = simple_open,
  211. .read = debugfs_state_info_read,
  212. };
  213. static const struct file_operations reg_dump_fops = {
  214. .open = simple_open,
  215. .read = debugfs_reg_dump_read,
  216. };
  217. static const struct file_operations cmd_dma_stats_fops = {
  218. .open = simple_open,
  219. .read = debugfs_line_count_read,
  220. };
  221. static int dsi_ctrl_debugfs_init(struct dsi_ctrl *dsi_ctrl,
  222. struct dentry *parent)
  223. {
  224. int rc = 0;
  225. struct dentry *dir, *state_file, *reg_dump, *cmd_dma_logs;
  226. char dbg_name[DSI_DEBUG_NAME_LEN];
  227. if (!dsi_ctrl || !parent) {
  228. DSI_CTRL_ERR(dsi_ctrl, "Invalid params\n");
  229. return -EINVAL;
  230. }
  231. dir = debugfs_create_dir(dsi_ctrl->name, parent);
  232. if (IS_ERR_OR_NULL(dir)) {
  233. rc = PTR_ERR(dir);
  234. DSI_CTRL_ERR(dsi_ctrl, "debugfs create dir failed, rc=%d\n",
  235. rc);
  236. goto error;
  237. }
  238. state_file = debugfs_create_file("state_info",
  239. 0444,
  240. dir,
  241. dsi_ctrl,
  242. &state_info_fops);
  243. if (IS_ERR_OR_NULL(state_file)) {
  244. rc = PTR_ERR(state_file);
  245. DSI_CTRL_ERR(dsi_ctrl, "state file failed, rc=%d\n", rc);
  246. goto error_remove_dir;
  247. }
  248. reg_dump = debugfs_create_file("reg_dump",
  249. 0444,
  250. dir,
  251. dsi_ctrl,
  252. &reg_dump_fops);
  253. if (IS_ERR_OR_NULL(reg_dump)) {
  254. rc = PTR_ERR(reg_dump);
  255. DSI_CTRL_ERR(dsi_ctrl, "reg dump file failed, rc=%d\n", rc);
  256. goto error_remove_dir;
  257. }
  258. cmd_dma_logs = debugfs_create_bool("enable_cmd_dma_stats",
  259. 0600,
  260. dir,
  261. &dsi_ctrl->enable_cmd_dma_stats);
  262. if (IS_ERR_OR_NULL(cmd_dma_logs)) {
  263. rc = PTR_ERR(cmd_dma_logs);
  264. DSI_CTRL_ERR(dsi_ctrl,
  265. "enable cmd dma stats failed, rc=%d\n",
  266. rc);
  267. goto error_remove_dir;
  268. }
  269. cmd_dma_logs = debugfs_create_file("cmd_dma_stats",
  270. 0444,
  271. dir,
  272. dsi_ctrl,
  273. &cmd_dma_stats_fops);
  274. if (IS_ERR_OR_NULL(cmd_dma_logs)) {
  275. rc = PTR_ERR(cmd_dma_logs);
  276. DSI_CTRL_ERR(dsi_ctrl, "Line count file failed, rc=%d\n",
  277. rc);
  278. goto error_remove_dir;
  279. }
  280. dsi_ctrl->debugfs_root = dir;
  281. snprintf(dbg_name, DSI_DEBUG_NAME_LEN, "dsi%d_ctrl", dsi_ctrl->cell_index);
  282. sde_dbg_reg_register_base(dbg_name, dsi_ctrl->hw.base,
  283. msm_iomap_size(dsi_ctrl->pdev, "dsi_ctrl"),
  284. msm_get_phys_addr(dsi_ctrl->pdev, "dsi_ctrl"), SDE_DBG_DSI);
  285. error_remove_dir:
  286. debugfs_remove(dir);
  287. error:
  288. return rc;
  289. }
  290. static int dsi_ctrl_debugfs_deinit(struct dsi_ctrl *dsi_ctrl)
  291. {
  292. debugfs_remove(dsi_ctrl->debugfs_root);
  293. return 0;
  294. }
  295. #else
  296. static int dsi_ctrl_debugfs_init(struct dsi_ctrl *dsi_ctrl,
  297. struct dentry *parent)
  298. {
  299. char dbg_name[DSI_DEBUG_NAME_LEN];
  300. snprintf(dbg_name, DSI_DEBUG_NAME_LEN, "dsi%d_ctrl", dsi_ctrl->cell_index);
  301. sde_dbg_reg_register_base(dbg_name, dsi_ctrl->hw.base,
  302. msm_iomap_size(dsi_ctrl->pdev, "dsi_ctrl"),
  303. msm_get_phys_addr(dsi_ctrl->pdev, "dsi_ctrl"), SDE_DBG_DSI);
  304. return 0;
  305. }
  306. static int dsi_ctrl_debugfs_deinit(struct dsi_ctrl *dsi_ctrl)
  307. {
  308. return 0;
  309. }
  310. #endif /* CONFIG_DEBUG_FS */
  311. static inline struct msm_gem_address_space*
  312. dsi_ctrl_get_aspace(struct dsi_ctrl *dsi_ctrl,
  313. int domain)
  314. {
  315. if (!dsi_ctrl || !dsi_ctrl->drm_dev)
  316. return NULL;
  317. return msm_gem_smmu_address_space_get(dsi_ctrl->drm_dev, domain);
  318. }
  319. static void dsi_ctrl_flush_cmd_dma_queue(struct dsi_ctrl *dsi_ctrl)
  320. {
  321. /*
  322. * If a command is triggered right after another command,
  323. * check if the previous command transfer is completed. If
  324. * transfer is done, cancel any work that has been
  325. * queued. Otherwise wait till the work is scheduled and
  326. * completed before triggering the next command by
  327. * flushing the workqueue.
  328. */
  329. if (atomic_read(&dsi_ctrl->dma_irq_trig)) {
  330. cancel_work_sync(&dsi_ctrl->dma_cmd_wait);
  331. } else {
  332. flush_workqueue(dsi_ctrl->dma_cmd_workq);
  333. SDE_EVT32(SDE_EVTLOG_FUNC_CASE2);
  334. }
  335. }
  336. static void dsi_ctrl_dma_cmd_wait_for_done(struct work_struct *work)
  337. {
  338. int ret = 0;
  339. struct dsi_ctrl *dsi_ctrl = NULL;
  340. u32 status;
  341. u32 mask = DSI_CMD_MODE_DMA_DONE;
  342. struct dsi_ctrl_hw_ops dsi_hw_ops;
  343. dsi_ctrl = container_of(work, struct dsi_ctrl, dma_cmd_wait);
  344. dsi_hw_ops = dsi_ctrl->hw.ops;
  345. SDE_EVT32(dsi_ctrl->cell_index, SDE_EVTLOG_FUNC_ENTRY);
  346. /*
  347. * This atomic state will be set if ISR has been triggered,
  348. * so the wait is not needed.
  349. */
  350. if (atomic_read(&dsi_ctrl->dma_irq_trig))
  351. goto done;
  352. ret = wait_for_completion_timeout(
  353. &dsi_ctrl->irq_info.cmd_dma_done,
  354. msecs_to_jiffies(DSI_CTRL_TX_TO_MS));
  355. if (ret == 0 && !atomic_read(&dsi_ctrl->dma_irq_trig)) {
  356. status = dsi_hw_ops.get_interrupt_status(&dsi_ctrl->hw);
  357. if (status & mask) {
  358. status |= (DSI_CMD_MODE_DMA_DONE | DSI_BTA_DONE);
  359. dsi_hw_ops.clear_interrupt_status(&dsi_ctrl->hw,
  360. status);
  361. DSI_CTRL_WARN(dsi_ctrl,
  362. "dma_tx done but irq not triggered\n");
  363. } else {
  364. DSI_CTRL_ERR(dsi_ctrl,
  365. "Command transfer failed\n");
  366. }
  367. dsi_ctrl_disable_status_interrupt(dsi_ctrl,
  368. DSI_SINT_CMD_MODE_DMA_DONE);
  369. }
  370. done:
  371. dsi_ctrl->dma_wait_queued = false;
  372. }
  373. static int dsi_ctrl_check_state(struct dsi_ctrl *dsi_ctrl,
  374. enum dsi_ctrl_driver_ops op,
  375. u32 op_state)
  376. {
  377. int rc = 0;
  378. struct dsi_ctrl_state_info *state = &dsi_ctrl->current_state;
  379. SDE_EVT32(dsi_ctrl->cell_index, op, op_state);
  380. switch (op) {
  381. case DSI_CTRL_OP_POWER_STATE_CHANGE:
  382. if (state->power_state == op_state) {
  383. DSI_CTRL_ERR(dsi_ctrl, "No change in state, pwr_state=%d\n",
  384. op_state);
  385. rc = -EINVAL;
  386. } else if (state->power_state == DSI_CTRL_POWER_VREG_ON) {
  387. if (state->vid_engine_state == DSI_CTRL_ENGINE_ON) {
  388. DSI_CTRL_ERR(dsi_ctrl, "State error: op=%d: %d\n",
  389. op_state,
  390. state->vid_engine_state);
  391. rc = -EINVAL;
  392. }
  393. }
  394. break;
  395. case DSI_CTRL_OP_CMD_ENGINE:
  396. if (state->cmd_engine_state == op_state) {
  397. DSI_CTRL_ERR(dsi_ctrl, "No change in state, cmd_state=%d\n",
  398. op_state);
  399. rc = -EINVAL;
  400. } else if ((state->power_state != DSI_CTRL_POWER_VREG_ON) ||
  401. (state->controller_state != DSI_CTRL_ENGINE_ON)) {
  402. DSI_CTRL_ERR(dsi_ctrl, "State error: op=%d: %d, %d\n",
  403. op,
  404. state->power_state,
  405. state->controller_state);
  406. rc = -EINVAL;
  407. }
  408. break;
  409. case DSI_CTRL_OP_VID_ENGINE:
  410. if (state->vid_engine_state == op_state) {
  411. DSI_CTRL_ERR(dsi_ctrl, "No change in state, cmd_state=%d\n",
  412. op_state);
  413. rc = -EINVAL;
  414. } else if ((state->power_state != DSI_CTRL_POWER_VREG_ON) ||
  415. (state->controller_state != DSI_CTRL_ENGINE_ON)) {
  416. DSI_CTRL_ERR(dsi_ctrl, "State error: op=%d: %d, %d\n",
  417. op,
  418. state->power_state,
  419. state->controller_state);
  420. rc = -EINVAL;
  421. }
  422. break;
  423. case DSI_CTRL_OP_HOST_ENGINE:
  424. if (state->controller_state == op_state) {
  425. DSI_CTRL_ERR(dsi_ctrl, "No change in state, ctrl_state=%d\n",
  426. op_state);
  427. rc = -EINVAL;
  428. } else if (state->power_state != DSI_CTRL_POWER_VREG_ON) {
  429. DSI_CTRL_ERR(dsi_ctrl, "State error (link is off): op=%d:, %d\n",
  430. op_state,
  431. state->power_state);
  432. rc = -EINVAL;
  433. } else if ((op_state == DSI_CTRL_ENGINE_OFF) &&
  434. ((state->cmd_engine_state != DSI_CTRL_ENGINE_OFF) ||
  435. (state->vid_engine_state != DSI_CTRL_ENGINE_OFF))) {
  436. DSI_CTRL_ERR(dsi_ctrl, "State error (eng on): op=%d: %d, %d\n",
  437. op_state,
  438. state->cmd_engine_state,
  439. state->vid_engine_state);
  440. rc = -EINVAL;
  441. }
  442. break;
  443. case DSI_CTRL_OP_CMD_TX:
  444. if ((state->power_state != DSI_CTRL_POWER_VREG_ON) ||
  445. (!state->host_initialized) ||
  446. (state->cmd_engine_state != DSI_CTRL_ENGINE_ON)) {
  447. DSI_CTRL_ERR(dsi_ctrl, "State error: op=%d: %d, %d, %d\n",
  448. op,
  449. state->power_state,
  450. state->host_initialized,
  451. state->cmd_engine_state);
  452. rc = -EINVAL;
  453. }
  454. break;
  455. case DSI_CTRL_OP_HOST_INIT:
  456. if (state->host_initialized == op_state) {
  457. DSI_CTRL_ERR(dsi_ctrl, "No change in state, host_init=%d\n",
  458. op_state);
  459. rc = -EINVAL;
  460. } else if (state->power_state != DSI_CTRL_POWER_VREG_ON) {
  461. DSI_CTRL_ERR(dsi_ctrl, "State error: op=%d: %d\n",
  462. op, state->power_state);
  463. rc = -EINVAL;
  464. }
  465. break;
  466. case DSI_CTRL_OP_TPG:
  467. if (state->tpg_enabled == op_state) {
  468. DSI_CTRL_ERR(dsi_ctrl, "No change in state, tpg_enabled=%d\n",
  469. op_state);
  470. rc = -EINVAL;
  471. } else if ((state->power_state != DSI_CTRL_POWER_VREG_ON) ||
  472. (state->controller_state != DSI_CTRL_ENGINE_ON)) {
  473. DSI_CTRL_ERR(dsi_ctrl, "State error: op=%d: %d, %d\n",
  474. op,
  475. state->power_state,
  476. state->controller_state);
  477. rc = -EINVAL;
  478. }
  479. break;
  480. case DSI_CTRL_OP_PHY_SW_RESET:
  481. if (state->power_state != DSI_CTRL_POWER_VREG_ON) {
  482. DSI_CTRL_ERR(dsi_ctrl, "State error: op=%d: %d\n",
  483. op, state->power_state);
  484. rc = -EINVAL;
  485. }
  486. break;
  487. case DSI_CTRL_OP_ASYNC_TIMING:
  488. if (state->vid_engine_state != op_state) {
  489. DSI_CTRL_ERR(dsi_ctrl, "Unexpected engine state vid_state=%d\n",
  490. op_state);
  491. rc = -EINVAL;
  492. }
  493. break;
  494. default:
  495. rc = -ENOTSUPP;
  496. break;
  497. }
  498. return rc;
  499. }
  500. bool dsi_ctrl_validate_host_state(struct dsi_ctrl *dsi_ctrl)
  501. {
  502. struct dsi_ctrl_state_info *state = &dsi_ctrl->current_state;
  503. if (!state) {
  504. DSI_CTRL_ERR(dsi_ctrl, "Invalid host state for DSI controller\n");
  505. return -EINVAL;
  506. }
  507. if (!state->host_initialized)
  508. return false;
  509. return true;
  510. }
  511. static void dsi_ctrl_update_state(struct dsi_ctrl *dsi_ctrl,
  512. enum dsi_ctrl_driver_ops op,
  513. u32 op_state)
  514. {
  515. struct dsi_ctrl_state_info *state = &dsi_ctrl->current_state;
  516. switch (op) {
  517. case DSI_CTRL_OP_POWER_STATE_CHANGE:
  518. state->power_state = op_state;
  519. break;
  520. case DSI_CTRL_OP_CMD_ENGINE:
  521. state->cmd_engine_state = op_state;
  522. break;
  523. case DSI_CTRL_OP_VID_ENGINE:
  524. state->vid_engine_state = op_state;
  525. break;
  526. case DSI_CTRL_OP_HOST_ENGINE:
  527. state->controller_state = op_state;
  528. break;
  529. case DSI_CTRL_OP_HOST_INIT:
  530. state->host_initialized = (op_state == 1) ? true : false;
  531. break;
  532. case DSI_CTRL_OP_TPG:
  533. state->tpg_enabled = (op_state == 1) ? true : false;
  534. break;
  535. case DSI_CTRL_OP_CMD_TX:
  536. case DSI_CTRL_OP_PHY_SW_RESET:
  537. default:
  538. break;
  539. }
  540. }
  541. static int dsi_ctrl_init_regmap(struct platform_device *pdev,
  542. struct dsi_ctrl *ctrl)
  543. {
  544. int rc = 0;
  545. void __iomem *ptr;
  546. ptr = msm_ioremap(pdev, "dsi_ctrl", ctrl->name);
  547. if (IS_ERR(ptr)) {
  548. rc = PTR_ERR(ptr);
  549. return rc;
  550. }
  551. ctrl->hw.base = ptr;
  552. DSI_CTRL_DEBUG(ctrl, "map dsi_ctrl registers to %pK\n", ctrl->hw.base);
  553. switch (ctrl->version) {
  554. case DSI_CTRL_VERSION_1_4:
  555. case DSI_CTRL_VERSION_2_0:
  556. ptr = msm_ioremap(pdev, "mmss_misc", ctrl->name);
  557. if (IS_ERR(ptr)) {
  558. DSI_CTRL_ERR(ctrl, "mmss_misc base address not found\n");
  559. rc = PTR_ERR(ptr);
  560. return rc;
  561. }
  562. ctrl->hw.mmss_misc_base = ptr;
  563. ctrl->hw.disp_cc_base = NULL;
  564. ctrl->hw.mdp_intf_base = NULL;
  565. break;
  566. case DSI_CTRL_VERSION_2_2:
  567. case DSI_CTRL_VERSION_2_3:
  568. case DSI_CTRL_VERSION_2_4:
  569. case DSI_CTRL_VERSION_2_5:
  570. case DSI_CTRL_VERSION_2_6:
  571. ptr = msm_ioremap(pdev, "disp_cc_base", ctrl->name);
  572. if (IS_ERR(ptr)) {
  573. DSI_CTRL_ERR(ctrl, "disp_cc base address not found for\n");
  574. rc = PTR_ERR(ptr);
  575. return rc;
  576. }
  577. ctrl->hw.disp_cc_base = ptr;
  578. ctrl->hw.mmss_misc_base = NULL;
  579. ptr = msm_ioremap(pdev, "mdp_intf_base", ctrl->name);
  580. if (!IS_ERR(ptr))
  581. ctrl->hw.mdp_intf_base = ptr;
  582. break;
  583. default:
  584. break;
  585. }
  586. return rc;
  587. }
  588. static int dsi_ctrl_clocks_deinit(struct dsi_ctrl *ctrl)
  589. {
  590. struct dsi_core_clk_info *core = &ctrl->clk_info.core_clks;
  591. struct dsi_link_lp_clk_info *lp_link = &ctrl->clk_info.lp_link_clks;
  592. struct dsi_link_hs_clk_info *hs_link = &ctrl->clk_info.hs_link_clks;
  593. struct dsi_clk_link_set *rcg = &ctrl->clk_info.rcg_clks;
  594. if (core->mdp_core_clk)
  595. devm_clk_put(&ctrl->pdev->dev, core->mdp_core_clk);
  596. if (core->iface_clk)
  597. devm_clk_put(&ctrl->pdev->dev, core->iface_clk);
  598. if (core->core_mmss_clk)
  599. devm_clk_put(&ctrl->pdev->dev, core->core_mmss_clk);
  600. if (core->bus_clk)
  601. devm_clk_put(&ctrl->pdev->dev, core->bus_clk);
  602. if (core->mnoc_clk)
  603. devm_clk_put(&ctrl->pdev->dev, core->mnoc_clk);
  604. memset(core, 0x0, sizeof(*core));
  605. if (hs_link->byte_clk)
  606. devm_clk_put(&ctrl->pdev->dev, hs_link->byte_clk);
  607. if (hs_link->pixel_clk)
  608. devm_clk_put(&ctrl->pdev->dev, hs_link->pixel_clk);
  609. if (lp_link->esc_clk)
  610. devm_clk_put(&ctrl->pdev->dev, lp_link->esc_clk);
  611. if (hs_link->byte_intf_clk)
  612. devm_clk_put(&ctrl->pdev->dev, hs_link->byte_intf_clk);
  613. memset(hs_link, 0x0, sizeof(*hs_link));
  614. memset(lp_link, 0x0, sizeof(*lp_link));
  615. if (rcg->byte_clk)
  616. devm_clk_put(&ctrl->pdev->dev, rcg->byte_clk);
  617. if (rcg->pixel_clk)
  618. devm_clk_put(&ctrl->pdev->dev, rcg->pixel_clk);
  619. memset(rcg, 0x0, sizeof(*rcg));
  620. return 0;
  621. }
  622. static int dsi_ctrl_clocks_init(struct platform_device *pdev,
  623. struct dsi_ctrl *ctrl)
  624. {
  625. int rc = 0;
  626. struct dsi_core_clk_info *core = &ctrl->clk_info.core_clks;
  627. struct dsi_link_lp_clk_info *lp_link = &ctrl->clk_info.lp_link_clks;
  628. struct dsi_link_hs_clk_info *hs_link = &ctrl->clk_info.hs_link_clks;
  629. struct dsi_clk_link_set *rcg = &ctrl->clk_info.rcg_clks;
  630. struct dsi_clk_link_set *xo = &ctrl->clk_info.xo_clk;
  631. core->mdp_core_clk = devm_clk_get(&pdev->dev, "mdp_core_clk");
  632. if (IS_ERR(core->mdp_core_clk)) {
  633. core->mdp_core_clk = NULL;
  634. DSI_CTRL_DEBUG(ctrl, "failed to get mdp_core_clk, rc=%d\n", rc);
  635. }
  636. core->iface_clk = devm_clk_get(&pdev->dev, "iface_clk");
  637. if (IS_ERR(core->iface_clk)) {
  638. core->iface_clk = NULL;
  639. DSI_CTRL_DEBUG(ctrl, "failed to get iface_clk, rc=%d\n", rc);
  640. }
  641. core->core_mmss_clk = devm_clk_get(&pdev->dev, "core_mmss_clk");
  642. if (IS_ERR(core->core_mmss_clk)) {
  643. core->core_mmss_clk = NULL;
  644. DSI_CTRL_DEBUG(ctrl, "failed to get core_mmss_clk, rc=%d\n",
  645. rc);
  646. }
  647. core->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
  648. if (IS_ERR(core->bus_clk)) {
  649. core->bus_clk = NULL;
  650. DSI_CTRL_DEBUG(ctrl, "failed to get bus_clk, rc=%d\n", rc);
  651. }
  652. core->mnoc_clk = devm_clk_get(&pdev->dev, "mnoc_clk");
  653. if (IS_ERR(core->mnoc_clk)) {
  654. core->mnoc_clk = NULL;
  655. DSI_CTRL_DEBUG(ctrl, "can't get mnoc clock, rc=%d\n", rc);
  656. }
  657. hs_link->byte_clk = devm_clk_get(&pdev->dev, "byte_clk");
  658. if (IS_ERR(hs_link->byte_clk)) {
  659. rc = PTR_ERR(hs_link->byte_clk);
  660. DSI_CTRL_ERR(ctrl, "failed to get byte_clk, rc=%d\n", rc);
  661. goto fail;
  662. }
  663. hs_link->pixel_clk = devm_clk_get(&pdev->dev, "pixel_clk");
  664. if (IS_ERR(hs_link->pixel_clk)) {
  665. rc = PTR_ERR(hs_link->pixel_clk);
  666. DSI_CTRL_ERR(ctrl, "failed to get pixel_clk, rc=%d\n", rc);
  667. goto fail;
  668. }
  669. lp_link->esc_clk = devm_clk_get(&pdev->dev, "esc_clk");
  670. if (IS_ERR(lp_link->esc_clk)) {
  671. rc = PTR_ERR(lp_link->esc_clk);
  672. DSI_CTRL_ERR(ctrl, "failed to get esc_clk, rc=%d\n", rc);
  673. goto fail;
  674. }
  675. hs_link->byte_intf_clk = devm_clk_get(&pdev->dev, "byte_intf_clk");
  676. if (IS_ERR(hs_link->byte_intf_clk)) {
  677. hs_link->byte_intf_clk = NULL;
  678. DSI_CTRL_DEBUG(ctrl, "can't find byte intf clk, rc=%d\n", rc);
  679. }
  680. rcg->byte_clk = devm_clk_get(&pdev->dev, "byte_clk_rcg");
  681. if (IS_ERR(rcg->byte_clk)) {
  682. rc = PTR_ERR(rcg->byte_clk);
  683. DSI_CTRL_ERR(ctrl, "failed to get byte_clk_rcg, rc=%d\n", rc);
  684. goto fail;
  685. }
  686. rcg->pixel_clk = devm_clk_get(&pdev->dev, "pixel_clk_rcg");
  687. if (IS_ERR(rcg->pixel_clk)) {
  688. rc = PTR_ERR(rcg->pixel_clk);
  689. DSI_CTRL_ERR(ctrl, "failed to get pixel_clk_rcg, rc=%d\n", rc);
  690. goto fail;
  691. }
  692. xo->byte_clk = devm_clk_get(&pdev->dev, "xo");
  693. if (IS_ERR(xo->byte_clk)) {
  694. xo->byte_clk = NULL;
  695. DSI_CTRL_DEBUG(ctrl, "failed to get xo clk, rc=%d\n", rc);
  696. }
  697. xo->pixel_clk = xo->byte_clk;
  698. return 0;
  699. fail:
  700. dsi_ctrl_clocks_deinit(ctrl);
  701. return rc;
  702. }
  703. static int dsi_ctrl_supplies_deinit(struct dsi_ctrl *ctrl)
  704. {
  705. int i = 0;
  706. int rc = 0;
  707. struct dsi_regulator_info *regs;
  708. regs = &ctrl->pwr_info.digital;
  709. for (i = 0; i < regs->count; i++) {
  710. if (!regs->vregs[i].vreg)
  711. DSI_CTRL_ERR(ctrl,
  712. "vreg is NULL, should not reach here\n");
  713. else
  714. devm_regulator_put(regs->vregs[i].vreg);
  715. }
  716. regs = &ctrl->pwr_info.host_pwr;
  717. for (i = 0; i < regs->count; i++) {
  718. if (!regs->vregs[i].vreg)
  719. DSI_CTRL_ERR(ctrl,
  720. "vreg is NULL, should not reach here\n");
  721. else
  722. devm_regulator_put(regs->vregs[i].vreg);
  723. }
  724. if (!ctrl->pwr_info.host_pwr.vregs) {
  725. devm_kfree(&ctrl->pdev->dev, ctrl->pwr_info.host_pwr.vregs);
  726. ctrl->pwr_info.host_pwr.vregs = NULL;
  727. ctrl->pwr_info.host_pwr.count = 0;
  728. }
  729. if (!ctrl->pwr_info.digital.vregs) {
  730. devm_kfree(&ctrl->pdev->dev, ctrl->pwr_info.digital.vregs);
  731. ctrl->pwr_info.digital.vregs = NULL;
  732. ctrl->pwr_info.digital.count = 0;
  733. }
  734. return rc;
  735. }
  736. static int dsi_ctrl_supplies_init(struct platform_device *pdev,
  737. struct dsi_ctrl *ctrl)
  738. {
  739. int rc = 0;
  740. int i = 0;
  741. struct dsi_regulator_info *regs;
  742. struct regulator *vreg = NULL;
  743. rc = dsi_pwr_get_dt_vreg_data(&pdev->dev,
  744. &ctrl->pwr_info.digital,
  745. "qcom,core-supply-entries");
  746. if (rc)
  747. DSI_CTRL_DEBUG(ctrl,
  748. "failed to get digital supply, rc = %d\n", rc);
  749. rc = dsi_pwr_get_dt_vreg_data(&pdev->dev,
  750. &ctrl->pwr_info.host_pwr,
  751. "qcom,ctrl-supply-entries");
  752. if (rc) {
  753. DSI_CTRL_ERR(ctrl,
  754. "failed to get host power supplies, rc = %d\n", rc);
  755. goto error_digital;
  756. }
  757. regs = &ctrl->pwr_info.digital;
  758. for (i = 0; i < regs->count; i++) {
  759. vreg = devm_regulator_get(&pdev->dev, regs->vregs[i].vreg_name);
  760. if (IS_ERR(vreg)) {
  761. DSI_CTRL_ERR(ctrl, "failed to get %s regulator\n",
  762. regs->vregs[i].vreg_name);
  763. rc = PTR_ERR(vreg);
  764. goto error_host_pwr;
  765. }
  766. regs->vregs[i].vreg = vreg;
  767. }
  768. regs = &ctrl->pwr_info.host_pwr;
  769. for (i = 0; i < regs->count; i++) {
  770. vreg = devm_regulator_get(&pdev->dev, regs->vregs[i].vreg_name);
  771. if (IS_ERR(vreg)) {
  772. DSI_CTRL_ERR(ctrl, "failed to get %s regulator\n",
  773. regs->vregs[i].vreg_name);
  774. for (--i; i >= 0; i--)
  775. devm_regulator_put(regs->vregs[i].vreg);
  776. rc = PTR_ERR(vreg);
  777. goto error_digital_put;
  778. }
  779. regs->vregs[i].vreg = vreg;
  780. }
  781. return rc;
  782. error_digital_put:
  783. regs = &ctrl->pwr_info.digital;
  784. for (i = 0; i < regs->count; i++)
  785. devm_regulator_put(regs->vregs[i].vreg);
  786. error_host_pwr:
  787. devm_kfree(&pdev->dev, ctrl->pwr_info.host_pwr.vregs);
  788. ctrl->pwr_info.host_pwr.vregs = NULL;
  789. ctrl->pwr_info.host_pwr.count = 0;
  790. error_digital:
  791. if (ctrl->pwr_info.digital.vregs)
  792. devm_kfree(&pdev->dev, ctrl->pwr_info.digital.vregs);
  793. ctrl->pwr_info.digital.vregs = NULL;
  794. ctrl->pwr_info.digital.count = 0;
  795. return rc;
  796. }
  797. static int dsi_ctrl_validate_panel_info(struct dsi_ctrl *dsi_ctrl,
  798. struct dsi_host_config *config)
  799. {
  800. int rc = 0;
  801. struct dsi_host_common_cfg *host_cfg = &config->common_config;
  802. if (config->panel_mode >= DSI_OP_MODE_MAX) {
  803. DSI_CTRL_ERR(dsi_ctrl, "Invalid dsi operation mode (%d)\n",
  804. config->panel_mode);
  805. rc = -EINVAL;
  806. goto err;
  807. }
  808. if ((host_cfg->data_lanes & (DSI_CLOCK_LANE - 1)) == 0) {
  809. DSI_CTRL_ERR(dsi_ctrl, "No data lanes are enabled\n");
  810. rc = -EINVAL;
  811. goto err;
  812. }
  813. err:
  814. return rc;
  815. }
  816. /* Function returns number of bits per pxl */
  817. int dsi_ctrl_pixel_format_to_bpp(enum dsi_pixel_format dst_format)
  818. {
  819. u32 bpp = 0;
  820. switch (dst_format) {
  821. case DSI_PIXEL_FORMAT_RGB111:
  822. bpp = 3;
  823. break;
  824. case DSI_PIXEL_FORMAT_RGB332:
  825. bpp = 8;
  826. break;
  827. case DSI_PIXEL_FORMAT_RGB444:
  828. bpp = 12;
  829. break;
  830. case DSI_PIXEL_FORMAT_RGB565:
  831. bpp = 16;
  832. break;
  833. case DSI_PIXEL_FORMAT_RGB666:
  834. case DSI_PIXEL_FORMAT_RGB666_LOOSE:
  835. bpp = 18;
  836. break;
  837. case DSI_PIXEL_FORMAT_RGB888:
  838. bpp = 24;
  839. break;
  840. default:
  841. bpp = 24;
  842. break;
  843. }
  844. return bpp;
  845. }
  846. static int dsi_ctrl_update_link_freqs(struct dsi_ctrl *dsi_ctrl,
  847. struct dsi_host_config *config, void *clk_handle,
  848. struct dsi_display_mode *mode)
  849. {
  850. int rc = 0;
  851. u32 num_of_lanes = 0;
  852. u32 bits_per_symbol = 16, num_of_symbols = 7; /* For Cphy */
  853. u32 bpp, frame_time_us, byte_intf_clk_div;
  854. u64 h_period, v_period, bit_rate, pclk_rate, bit_rate_per_lane,
  855. byte_clk_rate, byte_intf_clk_rate;
  856. struct dsi_host_common_cfg *host_cfg = &config->common_config;
  857. struct dsi_split_link_config *split_link = &host_cfg->split_link;
  858. struct dsi_mode_info *timing = &config->video_timing;
  859. u64 dsi_transfer_time_us = mode->priv_info->dsi_transfer_time_us;
  860. u64 min_dsi_clk_hz = mode->priv_info->min_dsi_clk_hz;
  861. /* Get bits per pxl in destination format */
  862. bpp = dsi_ctrl_pixel_format_to_bpp(host_cfg->dst_format);
  863. frame_time_us = mult_frac(1000, 1000, (timing->refresh_rate));
  864. if (host_cfg->data_lanes & DSI_DATA_LANE_0)
  865. num_of_lanes++;
  866. if (host_cfg->data_lanes & DSI_DATA_LANE_1)
  867. num_of_lanes++;
  868. if (host_cfg->data_lanes & DSI_DATA_LANE_2)
  869. num_of_lanes++;
  870. if (host_cfg->data_lanes & DSI_DATA_LANE_3)
  871. num_of_lanes++;
  872. if (split_link->enabled)
  873. num_of_lanes = split_link->lanes_per_sublink;
  874. config->common_config.num_data_lanes = num_of_lanes;
  875. config->common_config.bpp = bpp;
  876. if (config->bit_clk_rate_hz_override != 0) {
  877. bit_rate = config->bit_clk_rate_hz_override * num_of_lanes;
  878. if (host_cfg->phy_type == DSI_PHY_TYPE_CPHY) {
  879. bit_rate *= bits_per_symbol;
  880. do_div(bit_rate, num_of_symbols);
  881. }
  882. } else if (config->panel_mode == DSI_OP_CMD_MODE) {
  883. /* Calculate the bit rate needed to match dsi transfer time */
  884. bit_rate = min_dsi_clk_hz * frame_time_us;
  885. do_div(bit_rate, dsi_transfer_time_us);
  886. bit_rate = bit_rate * num_of_lanes;
  887. } else {
  888. h_period = dsi_h_total_dce(timing);
  889. v_period = DSI_V_TOTAL(timing);
  890. bit_rate = h_period * v_period * timing->refresh_rate * bpp;
  891. }
  892. pclk_rate = bit_rate;
  893. do_div(pclk_rate, bpp);
  894. if (host_cfg->phy_type == DSI_PHY_TYPE_DPHY) {
  895. bit_rate_per_lane = bit_rate;
  896. do_div(bit_rate_per_lane, num_of_lanes);
  897. byte_clk_rate = bit_rate_per_lane;
  898. /**
  899. * Ensure that the byte clock rate is even to avoid failures
  900. * during set rate for byte intf clock. Round up to the nearest
  901. * even number for byte clk.
  902. */
  903. byte_clk_rate = DIV_ROUND_CLOSEST(byte_clk_rate, 8);
  904. byte_clk_rate = ((byte_clk_rate + 1) & ~BIT(0));
  905. byte_intf_clk_rate = byte_clk_rate;
  906. byte_intf_clk_div = host_cfg->byte_intf_clk_div;
  907. do_div(byte_intf_clk_rate, byte_intf_clk_div);
  908. config->bit_clk_rate_hz = byte_clk_rate * 8;
  909. } else {
  910. do_div(bit_rate, bits_per_symbol);
  911. bit_rate *= num_of_symbols;
  912. bit_rate_per_lane = bit_rate;
  913. do_div(bit_rate_per_lane, num_of_lanes);
  914. byte_clk_rate = bit_rate_per_lane;
  915. do_div(byte_clk_rate, 7);
  916. /* For CPHY, byte_intf_clk is same as byte_clk */
  917. byte_intf_clk_rate = byte_clk_rate;
  918. config->bit_clk_rate_hz = byte_clk_rate * 7;
  919. }
  920. DSI_CTRL_DEBUG(dsi_ctrl, "bit_clk_rate = %llu, bit_clk_rate_per_lane = %llu\n",
  921. bit_rate, bit_rate_per_lane);
  922. DSI_CTRL_DEBUG(dsi_ctrl, "byte_clk_rate = %llu, byte_intf_clk = %llu\n",
  923. byte_clk_rate, byte_intf_clk_rate);
  924. DSI_CTRL_DEBUG(dsi_ctrl, "pclk_rate = %llu\n", pclk_rate);
  925. SDE_EVT32(dsi_ctrl->cell_index, bit_rate, byte_clk_rate, pclk_rate);
  926. dsi_ctrl->clk_freq.byte_clk_rate = byte_clk_rate;
  927. dsi_ctrl->clk_freq.byte_intf_clk_rate = byte_intf_clk_rate;
  928. dsi_ctrl->clk_freq.pix_clk_rate = pclk_rate;
  929. dsi_ctrl->clk_freq.esc_clk_rate = config->esc_clk_rate_hz;
  930. rc = dsi_clk_set_link_frequencies(clk_handle, dsi_ctrl->clk_freq,
  931. dsi_ctrl->cell_index);
  932. if (rc)
  933. DSI_CTRL_ERR(dsi_ctrl, "Failed to update link frequencies\n");
  934. return rc;
  935. }
  936. static int dsi_ctrl_enable_supplies(struct dsi_ctrl *dsi_ctrl, bool enable)
  937. {
  938. int rc = 0;
  939. if (enable) {
  940. rc = pm_runtime_get_sync(dsi_ctrl->drm_dev->dev);
  941. if (rc < 0) {
  942. DSI_CTRL_ERR(dsi_ctrl,
  943. "Power resource enable failed, rc=%d\n", rc);
  944. goto error;
  945. }
  946. if (!dsi_ctrl->current_state.host_initialized) {
  947. rc = dsi_pwr_enable_regulator(
  948. &dsi_ctrl->pwr_info.host_pwr, true);
  949. if (rc) {
  950. DSI_CTRL_ERR(dsi_ctrl, "failed to enable host power regs\n");
  951. goto error_get_sync;
  952. }
  953. }
  954. rc = dsi_pwr_enable_regulator(&dsi_ctrl->pwr_info.digital,
  955. true);
  956. if (rc) {
  957. DSI_CTRL_ERR(dsi_ctrl, "failed to enable gdsc, rc=%d\n",
  958. rc);
  959. (void)dsi_pwr_enable_regulator(
  960. &dsi_ctrl->pwr_info.host_pwr,
  961. false
  962. );
  963. goto error_get_sync;
  964. }
  965. return rc;
  966. } else {
  967. rc = dsi_pwr_enable_regulator(&dsi_ctrl->pwr_info.digital,
  968. false);
  969. if (rc) {
  970. DSI_CTRL_ERR(dsi_ctrl, "failed to disable gdsc, rc=%d\n",
  971. rc);
  972. goto error;
  973. }
  974. if (!dsi_ctrl->current_state.host_initialized) {
  975. rc = dsi_pwr_enable_regulator(
  976. &dsi_ctrl->pwr_info.host_pwr, false);
  977. if (rc) {
  978. DSI_CTRL_ERR(dsi_ctrl, "failed to disable host power regs\n");
  979. goto error;
  980. }
  981. }
  982. pm_runtime_put_sync(dsi_ctrl->drm_dev->dev);
  983. return rc;
  984. }
  985. error_get_sync:
  986. pm_runtime_put_sync(dsi_ctrl->drm_dev->dev);
  987. error:
  988. return rc;
  989. }
  990. static int dsi_ctrl_copy_and_pad_cmd(struct dsi_ctrl *dsi_ctrl,
  991. const struct mipi_dsi_packet *packet,
  992. u8 **buffer,
  993. u32 *size)
  994. {
  995. int rc = 0;
  996. u8 *buf = NULL;
  997. u32 len, i;
  998. u8 cmd_type = 0;
  999. len = packet->size;
  1000. len += 0x3; len &= ~0x03; /* Align to 32 bits */
  1001. buf = devm_kzalloc(&dsi_ctrl->pdev->dev, len * sizeof(u8), GFP_KERNEL);
  1002. if (!buf)
  1003. return -ENOMEM;
  1004. for (i = 0; i < len; i++) {
  1005. if (i >= packet->size)
  1006. buf[i] = 0xFF;
  1007. else if (i < sizeof(packet->header))
  1008. buf[i] = packet->header[i];
  1009. else
  1010. buf[i] = packet->payload[i - sizeof(packet->header)];
  1011. }
  1012. if (packet->payload_length > 0)
  1013. buf[3] |= BIT(6);
  1014. /* Swap BYTE order in the command buffer for MSM */
  1015. buf[0] = packet->header[1];
  1016. buf[1] = packet->header[2];
  1017. buf[2] = packet->header[0];
  1018. /* send embedded BTA for read commands */
  1019. cmd_type = buf[2] & 0x3f;
  1020. if ((cmd_type == MIPI_DSI_DCS_READ) ||
  1021. (cmd_type == MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM) ||
  1022. (cmd_type == MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM) ||
  1023. (cmd_type == MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM))
  1024. buf[3] |= BIT(5);
  1025. *buffer = buf;
  1026. *size = len;
  1027. return rc;
  1028. }
  1029. int dsi_ctrl_wait_for_cmd_mode_mdp_idle(struct dsi_ctrl *dsi_ctrl)
  1030. {
  1031. int rc = 0;
  1032. if (!dsi_ctrl) {
  1033. DSI_CTRL_ERR(dsi_ctrl, "Invalid params\n");
  1034. return -EINVAL;
  1035. }
  1036. if (dsi_ctrl->host_config.panel_mode != DSI_OP_CMD_MODE)
  1037. return -EINVAL;
  1038. mutex_lock(&dsi_ctrl->ctrl_lock);
  1039. rc = dsi_ctrl->hw.ops.wait_for_cmd_mode_mdp_idle(&dsi_ctrl->hw);
  1040. mutex_unlock(&dsi_ctrl->ctrl_lock);
  1041. return rc;
  1042. }
  1043. static void dsi_ctrl_wait_for_video_done(struct dsi_ctrl *dsi_ctrl)
  1044. {
  1045. u32 v_total = 0, v_blank = 0, sleep_ms = 0, fps = 0, ret;
  1046. struct dsi_mode_info *timing;
  1047. /**
  1048. * No need to wait if the panel is not video mode or
  1049. * if DSI controller supports command DMA scheduling or
  1050. * if we are sending init commands.
  1051. */
  1052. if ((dsi_ctrl->host_config.panel_mode != DSI_OP_VIDEO_MODE) ||
  1053. (dsi_ctrl->version >= DSI_CTRL_VERSION_2_2) ||
  1054. (dsi_ctrl->current_state.vid_engine_state !=
  1055. DSI_CTRL_ENGINE_ON))
  1056. return;
  1057. dsi_ctrl->hw.ops.clear_interrupt_status(&dsi_ctrl->hw,
  1058. DSI_VIDEO_MODE_FRAME_DONE);
  1059. dsi_ctrl_enable_status_interrupt(dsi_ctrl,
  1060. DSI_SINT_VIDEO_MODE_FRAME_DONE, NULL);
  1061. reinit_completion(&dsi_ctrl->irq_info.vid_frame_done);
  1062. ret = wait_for_completion_timeout(
  1063. &dsi_ctrl->irq_info.vid_frame_done,
  1064. msecs_to_jiffies(DSI_CTRL_TX_TO_MS));
  1065. if (ret <= 0)
  1066. DSI_CTRL_DEBUG(dsi_ctrl, "wait for video done failed\n");
  1067. dsi_ctrl_disable_status_interrupt(dsi_ctrl,
  1068. DSI_SINT_VIDEO_MODE_FRAME_DONE);
  1069. timing = &(dsi_ctrl->host_config.video_timing);
  1070. v_total = timing->v_sync_width + timing->v_back_porch +
  1071. timing->v_front_porch + timing->v_active;
  1072. v_blank = timing->v_sync_width + timing->v_back_porch;
  1073. fps = timing->refresh_rate;
  1074. sleep_ms = CEIL((v_blank * 1000), (v_total * fps)) + 1;
  1075. udelay(sleep_ms * 1000);
  1076. }
  1077. int dsi_message_validate_tx_mode(struct dsi_ctrl *dsi_ctrl,
  1078. u32 cmd_len,
  1079. u32 *flags)
  1080. {
  1081. int rc = 0;
  1082. if (*flags & DSI_CTRL_CMD_FIFO_STORE) {
  1083. /* if command size plus header is greater than fifo size */
  1084. if ((cmd_len + 4) > DSI_CTRL_MAX_CMD_FIFO_STORE_SIZE) {
  1085. DSI_CTRL_ERR(dsi_ctrl, "Cannot transfer Cmd in FIFO config\n");
  1086. return -ENOTSUPP;
  1087. }
  1088. if (!dsi_ctrl->hw.ops.kickoff_fifo_command) {
  1089. DSI_CTRL_ERR(dsi_ctrl, "Cannot transfer command,ops not defined\n");
  1090. return -ENOTSUPP;
  1091. }
  1092. }
  1093. if (*flags & DSI_CTRL_CMD_NON_EMBEDDED_MODE) {
  1094. if (*flags & DSI_CTRL_CMD_BROADCAST) {
  1095. DSI_CTRL_ERR(dsi_ctrl, "Non embedded not supported with broadcast\n");
  1096. return -ENOTSUPP;
  1097. }
  1098. if (!dsi_ctrl->hw.ops.kickoff_command_non_embedded_mode) {
  1099. DSI_CTRL_ERR(dsi_ctrl, " Cannot transfer command,ops not defined\n");
  1100. return -ENOTSUPP;
  1101. }
  1102. if ((cmd_len + 4) > SZ_4K) {
  1103. DSI_CTRL_ERR(dsi_ctrl, "Cannot transfer,size is greater than 4096\n");
  1104. return -ENOTSUPP;
  1105. }
  1106. }
  1107. if (*flags & DSI_CTRL_CMD_FETCH_MEMORY) {
  1108. if ((dsi_ctrl->cmd_len + cmd_len + 4) > SZ_4K) {
  1109. DSI_CTRL_ERR(dsi_ctrl, "Cannot transfer,size is greater than 4096\n");
  1110. return -ENOTSUPP;
  1111. }
  1112. }
  1113. return rc;
  1114. }
  1115. static void dsi_configure_command_scheduling(struct dsi_ctrl *dsi_ctrl,
  1116. struct dsi_ctrl_cmd_dma_info *cmd_mem)
  1117. {
  1118. u32 line_no = 0, window = 0, sched_line_no = 0;
  1119. struct dsi_ctrl_hw_ops dsi_hw_ops = dsi_ctrl->hw.ops;
  1120. struct dsi_mode_info *timing = &(dsi_ctrl->host_config.video_timing);
  1121. line_no = dsi_ctrl->host_config.common_config.dma_sched_line;
  1122. window = dsi_ctrl->host_config.common_config.dma_sched_window;
  1123. SDE_EVT32(dsi_ctrl->cell_index, SDE_EVTLOG_FUNC_ENTRY, line_no, window);
  1124. /*
  1125. * In case of command scheduling in video mode, the line at which
  1126. * the command is scheduled can revert to the default value i.e. 1
  1127. * for the following cases:
  1128. * 1) No schedule line defined by the panel.
  1129. * 2) schedule line defined is greater than VFP.
  1130. */
  1131. if ((dsi_ctrl->host_config.panel_mode == DSI_OP_VIDEO_MODE) &&
  1132. dsi_hw_ops.schedule_dma_cmd &&
  1133. (dsi_ctrl->current_state.vid_engine_state ==
  1134. DSI_CTRL_ENGINE_ON)) {
  1135. sched_line_no = (line_no == 0) ? 1 : line_no;
  1136. if (timing) {
  1137. if (sched_line_no >= timing->v_front_porch)
  1138. sched_line_no = 1;
  1139. sched_line_no += timing->v_back_porch +
  1140. timing->v_sync_width + timing->v_active;
  1141. }
  1142. dsi_hw_ops.schedule_dma_cmd(&dsi_ctrl->hw, sched_line_no);
  1143. }
  1144. /*
  1145. * In case of command scheduling in command mode, set the maximum
  1146. * possible size of the DMA start window in case no schedule line and
  1147. * window size properties are defined by the panel.
  1148. */
  1149. if ((dsi_ctrl->host_config.panel_mode == DSI_OP_CMD_MODE) &&
  1150. dsi_hw_ops.configure_cmddma_window) {
  1151. sched_line_no = (line_no == 0) ? TEARCHECK_WINDOW_SIZE :
  1152. line_no;
  1153. window = (window == 0) ? timing->v_active : window;
  1154. sched_line_no += timing->v_active;
  1155. dsi_hw_ops.configure_cmddma_window(&dsi_ctrl->hw, cmd_mem,
  1156. sched_line_no, window);
  1157. }
  1158. SDE_EVT32(dsi_ctrl->cell_index, SDE_EVTLOG_FUNC_EXIT,
  1159. sched_line_no, window);
  1160. }
  1161. static u32 calculate_schedule_line(struct dsi_ctrl *dsi_ctrl, u32 flags)
  1162. {
  1163. u32 line_no = 0x1;
  1164. struct dsi_mode_info *timing;
  1165. /* check if custom dma scheduling line needed */
  1166. if ((dsi_ctrl->host_config.panel_mode == DSI_OP_VIDEO_MODE) &&
  1167. (flags & DSI_CTRL_CMD_CUSTOM_DMA_SCHED))
  1168. line_no = dsi_ctrl->host_config.common_config.dma_sched_line;
  1169. timing = &(dsi_ctrl->host_config.video_timing);
  1170. if (timing)
  1171. line_no += timing->v_back_porch + timing->v_sync_width +
  1172. timing->v_active;
  1173. return line_no;
  1174. }
  1175. static void dsi_kickoff_msg_tx(struct dsi_ctrl *dsi_ctrl,
  1176. const struct mipi_dsi_msg *msg,
  1177. struct dsi_ctrl_cmd_dma_fifo_info *cmd,
  1178. struct dsi_ctrl_cmd_dma_info *cmd_mem,
  1179. u32 flags)
  1180. {
  1181. u32 hw_flags = 0;
  1182. struct dsi_ctrl_hw_ops dsi_hw_ops = dsi_ctrl->hw.ops;
  1183. struct dsi_split_link_config *split_link;
  1184. split_link = &(dsi_ctrl->host_config.common_config.split_link);
  1185. SDE_EVT32(dsi_ctrl->cell_index, SDE_EVTLOG_FUNC_ENTRY, flags,
  1186. msg->flags);
  1187. if (dsi_ctrl->hw.reset_trig_ctrl)
  1188. dsi_hw_ops.reset_trig_ctrl(&dsi_ctrl->hw,
  1189. &dsi_ctrl->host_config.common_config);
  1190. if (dsi_hw_ops.splitlink_cmd_setup && split_link->enabled)
  1191. dsi_hw_ops.splitlink_cmd_setup(&dsi_ctrl->hw,
  1192. &dsi_ctrl->host_config.common_config, flags);
  1193. /*
  1194. * Always enable DMA scheduling for video mode panel.
  1195. *
  1196. * In video mode panel, if the DMA is triggered very close to
  1197. * the beginning of the active window and the DMA transfer
  1198. * happens in the last line of VBP, then the HW state will
  1199. * stay in ‘wait’ and return to ‘idle’ in the first line of VFP.
  1200. * But somewhere in the middle of the active window, if SW
  1201. * disables DSI command mode engine while the HW is still
  1202. * waiting and re-enable after timing engine is OFF. So the
  1203. * HW never ‘sees’ another vblank line and hence it gets
  1204. * stuck in the ‘wait’ state.
  1205. */
  1206. if ((flags & DSI_CTRL_CMD_CUSTOM_DMA_SCHED) ||
  1207. (dsi_ctrl->host_config.panel_mode == DSI_OP_VIDEO_MODE))
  1208. dsi_configure_command_scheduling(dsi_ctrl, cmd_mem);
  1209. dsi_ctrl->cmd_mode = (dsi_ctrl->host_config.panel_mode ==
  1210. DSI_OP_CMD_MODE);
  1211. hw_flags |= (flags & DSI_CTRL_CMD_DEFER_TRIGGER) ?
  1212. DSI_CTRL_HW_CMD_WAIT_FOR_TRIGGER : 0;
  1213. if (flags & DSI_CTRL_CMD_LAST_COMMAND)
  1214. hw_flags |= DSI_CTRL_CMD_LAST_COMMAND;
  1215. if (flags & DSI_CTRL_CMD_DEFER_TRIGGER) {
  1216. if (flags & DSI_CTRL_CMD_FETCH_MEMORY) {
  1217. if (flags & DSI_CTRL_CMD_NON_EMBEDDED_MODE) {
  1218. dsi_hw_ops.kickoff_command_non_embedded_mode(
  1219. &dsi_ctrl->hw,
  1220. cmd_mem,
  1221. hw_flags);
  1222. } else {
  1223. dsi_hw_ops.kickoff_command(
  1224. &dsi_ctrl->hw,
  1225. cmd_mem,
  1226. hw_flags);
  1227. }
  1228. } else if (flags & DSI_CTRL_CMD_FIFO_STORE) {
  1229. dsi_hw_ops.kickoff_fifo_command(&dsi_ctrl->hw,
  1230. cmd,
  1231. hw_flags);
  1232. }
  1233. }
  1234. if (!(flags & DSI_CTRL_CMD_DEFER_TRIGGER)) {
  1235. dsi_ctrl_wait_for_video_done(dsi_ctrl);
  1236. dsi_ctrl_mask_overflow(dsi_ctrl, true);
  1237. atomic_set(&dsi_ctrl->dma_irq_trig, 0);
  1238. dsi_ctrl_enable_status_interrupt(dsi_ctrl,
  1239. DSI_SINT_CMD_MODE_DMA_DONE, NULL);
  1240. reinit_completion(&dsi_ctrl->irq_info.cmd_dma_done);
  1241. if (flags & DSI_CTRL_CMD_FETCH_MEMORY) {
  1242. if (flags & DSI_CTRL_CMD_NON_EMBEDDED_MODE) {
  1243. dsi_hw_ops.kickoff_command_non_embedded_mode(
  1244. &dsi_ctrl->hw,
  1245. cmd_mem,
  1246. hw_flags);
  1247. } else {
  1248. dsi_hw_ops.kickoff_command(
  1249. &dsi_ctrl->hw,
  1250. cmd_mem,
  1251. hw_flags);
  1252. }
  1253. } else if (flags & DSI_CTRL_CMD_FIFO_STORE) {
  1254. dsi_hw_ops.kickoff_fifo_command(&dsi_ctrl->hw,
  1255. cmd,
  1256. hw_flags);
  1257. }
  1258. if (dsi_ctrl->enable_cmd_dma_stats) {
  1259. u32 reg = dsi_hw_ops.log_line_count(&dsi_ctrl->hw,
  1260. dsi_ctrl->cmd_mode);
  1261. dsi_ctrl->cmd_trigger_line = (reg & 0xFFFF);
  1262. dsi_ctrl->cmd_trigger_frame = ((reg >> 16) & 0xFFFF);
  1263. SDE_EVT32(dsi_ctrl->cell_index, SDE_EVTLOG_FUNC_CASE1,
  1264. dsi_ctrl->cmd_trigger_line,
  1265. dsi_ctrl->cmd_trigger_frame);
  1266. }
  1267. if (flags & DSI_CTRL_CMD_ASYNC_WAIT) {
  1268. dsi_ctrl->dma_wait_queued = true;
  1269. queue_work(dsi_ctrl->dma_cmd_workq,
  1270. &dsi_ctrl->dma_cmd_wait);
  1271. } else {
  1272. dsi_ctrl->dma_wait_queued = false;
  1273. dsi_ctrl_dma_cmd_wait_for_done(&dsi_ctrl->dma_cmd_wait);
  1274. }
  1275. dsi_ctrl_mask_overflow(dsi_ctrl, false);
  1276. dsi_hw_ops.reset_cmd_fifo(&dsi_ctrl->hw);
  1277. /*
  1278. * DSI 2.2 needs a soft reset whenever we send non-embedded
  1279. * mode command followed by embedded mode. Otherwise it will
  1280. * result in smmu write faults with DSI as client.
  1281. */
  1282. if (flags & DSI_CTRL_CMD_NON_EMBEDDED_MODE) {
  1283. if (dsi_ctrl->version < DSI_CTRL_VERSION_2_4)
  1284. dsi_hw_ops.soft_reset(&dsi_ctrl->hw);
  1285. dsi_ctrl->cmd_len = 0;
  1286. }
  1287. }
  1288. }
  1289. static int dsi_message_tx(struct dsi_ctrl *dsi_ctrl, struct dsi_cmd_desc *cmd_desc)
  1290. {
  1291. int rc = 0;
  1292. struct mipi_dsi_packet packet;
  1293. struct dsi_ctrl_cmd_dma_fifo_info cmd;
  1294. struct dsi_ctrl_cmd_dma_info cmd_mem;
  1295. const struct mipi_dsi_msg *msg;
  1296. u32 length = 0;
  1297. u8 *buffer = NULL;
  1298. u32 cnt = 0;
  1299. u8 *cmdbuf;
  1300. u32 *flags;
  1301. msg = &cmd_desc->msg;
  1302. flags = &cmd_desc->ctrl_flags;
  1303. /* Validate the mode before sending the command */
  1304. rc = dsi_message_validate_tx_mode(dsi_ctrl, msg->tx_len, flags);
  1305. if (rc) {
  1306. DSI_CTRL_ERR(dsi_ctrl,
  1307. "Cmd tx validation failed, cannot transfer cmd\n");
  1308. rc = -ENOTSUPP;
  1309. goto error;
  1310. }
  1311. SDE_EVT32(dsi_ctrl->cell_index, SDE_EVTLOG_FUNC_ENTRY, flags);
  1312. if (dsi_ctrl->dma_wait_queued)
  1313. dsi_ctrl_flush_cmd_dma_queue(dsi_ctrl);
  1314. if (*flags & DSI_CTRL_CMD_NON_EMBEDDED_MODE) {
  1315. cmd_mem.offset = dsi_ctrl->cmd_buffer_iova;
  1316. cmd_mem.en_broadcast = (*flags & DSI_CTRL_CMD_BROADCAST) ?
  1317. true : false;
  1318. cmd_mem.is_master = (*flags & DSI_CTRL_CMD_BROADCAST_MASTER) ?
  1319. true : false;
  1320. cmd_mem.use_lpm = (msg->flags & MIPI_DSI_MSG_USE_LPM) ?
  1321. true : false;
  1322. cmd_mem.datatype = msg->type;
  1323. cmd_mem.length = msg->tx_len;
  1324. dsi_ctrl->cmd_len = msg->tx_len;
  1325. memcpy(dsi_ctrl->vaddr, msg->tx_buf, msg->tx_len);
  1326. DSI_CTRL_DEBUG(dsi_ctrl,
  1327. "non-embedded mode , size of command =%zd\n",
  1328. msg->tx_len);
  1329. goto kickoff;
  1330. }
  1331. rc = mipi_dsi_create_packet(&packet, msg);
  1332. if (rc) {
  1333. DSI_CTRL_ERR(dsi_ctrl, "Failed to create message packet, rc=%d\n",
  1334. rc);
  1335. goto error;
  1336. }
  1337. rc = dsi_ctrl_copy_and_pad_cmd(dsi_ctrl,
  1338. &packet,
  1339. &buffer,
  1340. &length);
  1341. if (rc) {
  1342. DSI_CTRL_ERR(dsi_ctrl, "failed to copy message, rc=%d\n", rc);
  1343. goto error;
  1344. }
  1345. /*
  1346. * In case of broadcast CMD length cannot be greater than 512 bytes
  1347. * as specified by HW limitations. Need to overwrite the flags to
  1348. * set the LAST_COMMAND flag to ensure no command transfer failures.
  1349. */
  1350. if ((*flags & DSI_CTRL_CMD_FETCH_MEMORY) &&
  1351. (*flags & DSI_CTRL_CMD_BROADCAST)) {
  1352. if ((dsi_ctrl->cmd_len + length) > 240) {
  1353. dsi_ctrl_mask_overflow(dsi_ctrl, true);
  1354. *flags |= DSI_CTRL_CMD_LAST_COMMAND;
  1355. SDE_EVT32(dsi_ctrl->cell_index, SDE_EVTLOG_FUNC_CASE1,
  1356. flags);
  1357. }
  1358. }
  1359. if (*flags & DSI_CTRL_CMD_LAST_COMMAND)
  1360. buffer[3] |= BIT(7);//set the last cmd bit in header.
  1361. if (*flags & DSI_CTRL_CMD_FETCH_MEMORY) {
  1362. /* Embedded mode config is selected */
  1363. cmd_mem.offset = dsi_ctrl->cmd_buffer_iova;
  1364. cmd_mem.en_broadcast = (*flags & DSI_CTRL_CMD_BROADCAST) ?
  1365. true : false;
  1366. cmd_mem.is_master = (*flags & DSI_CTRL_CMD_BROADCAST_MASTER) ?
  1367. true : false;
  1368. cmd_mem.use_lpm = (msg->flags & MIPI_DSI_MSG_USE_LPM) ?
  1369. true : false;
  1370. cmdbuf = (u8 *)(dsi_ctrl->vaddr);
  1371. msm_gem_sync(dsi_ctrl->tx_cmd_buf);
  1372. for (cnt = 0; cnt < length; cnt++)
  1373. cmdbuf[dsi_ctrl->cmd_len + cnt] = buffer[cnt];
  1374. dsi_ctrl->cmd_len += length;
  1375. if (*flags & DSI_CTRL_CMD_LAST_COMMAND) {
  1376. cmd_mem.length = dsi_ctrl->cmd_len;
  1377. dsi_ctrl->cmd_len = 0;
  1378. } else {
  1379. goto error;
  1380. }
  1381. } else if (*flags & DSI_CTRL_CMD_FIFO_STORE) {
  1382. cmd.command = (u32 *)buffer;
  1383. cmd.size = length;
  1384. cmd.en_broadcast = (*flags & DSI_CTRL_CMD_BROADCAST) ?
  1385. true : false;
  1386. cmd.is_master = (*flags & DSI_CTRL_CMD_BROADCAST_MASTER) ?
  1387. true : false;
  1388. cmd.use_lpm = (msg->flags & MIPI_DSI_MSG_USE_LPM) ?
  1389. true : false;
  1390. }
  1391. kickoff:
  1392. dsi_kickoff_msg_tx(dsi_ctrl, msg, &cmd, &cmd_mem, *flags);
  1393. error:
  1394. if (buffer)
  1395. devm_kfree(&dsi_ctrl->pdev->dev, buffer);
  1396. return rc;
  1397. }
  1398. static int dsi_set_max_return_size(struct dsi_ctrl *dsi_ctrl, struct dsi_cmd_desc *rx_cmd, u32 size)
  1399. {
  1400. int rc = 0;
  1401. const struct mipi_dsi_msg *rx_msg = &rx_cmd->msg;
  1402. u8 tx[2] = { (u8)(size & 0xFF), (u8)(size >> 8) };
  1403. u16 dflags = rx_msg->flags;
  1404. struct dsi_cmd_desc cmd= {
  1405. .msg.channel = rx_msg->channel,
  1406. .msg.type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
  1407. .msg.tx_len = 2,
  1408. .msg.tx_buf = tx,
  1409. .msg.flags = rx_msg->flags,
  1410. };
  1411. /* remove last message flag to batch max packet cmd to read command */
  1412. dflags &= ~BIT(3);
  1413. cmd.msg.flags = dflags;
  1414. cmd.ctrl_flags = DSI_CTRL_CMD_FETCH_MEMORY;
  1415. rc = dsi_message_tx(dsi_ctrl, &cmd);
  1416. if (rc)
  1417. DSI_CTRL_ERR(dsi_ctrl, "failed to send max return size packet, rc=%d\n",
  1418. rc);
  1419. return rc;
  1420. }
  1421. /* Helper functions to support DCS read operation */
  1422. static int dsi_parse_short_read1_resp(const struct mipi_dsi_msg *msg,
  1423. unsigned char *buff)
  1424. {
  1425. u8 *data = msg->rx_buf;
  1426. int read_len = 1;
  1427. if (!data)
  1428. return 0;
  1429. /* remove dcs type */
  1430. if (msg->rx_len >= 1)
  1431. data[0] = buff[1];
  1432. else
  1433. read_len = 0;
  1434. return read_len;
  1435. }
  1436. static int dsi_parse_short_read2_resp(const struct mipi_dsi_msg *msg,
  1437. unsigned char *buff)
  1438. {
  1439. u8 *data = msg->rx_buf;
  1440. int read_len = 2;
  1441. if (!data)
  1442. return 0;
  1443. /* remove dcs type */
  1444. if (msg->rx_len >= 2) {
  1445. data[0] = buff[1];
  1446. data[1] = buff[2];
  1447. } else {
  1448. read_len = 0;
  1449. }
  1450. return read_len;
  1451. }
  1452. static int dsi_parse_long_read_resp(const struct mipi_dsi_msg *msg,
  1453. unsigned char *buff)
  1454. {
  1455. if (!msg->rx_buf)
  1456. return 0;
  1457. /* remove dcs type */
  1458. if (msg->rx_buf && msg->rx_len)
  1459. memcpy(msg->rx_buf, buff + 4, msg->rx_len);
  1460. return msg->rx_len;
  1461. }
  1462. static int dsi_message_rx(struct dsi_ctrl *dsi_ctrl, struct dsi_cmd_desc *cmd_desc)
  1463. {
  1464. int rc = 0;
  1465. u32 rd_pkt_size, total_read_len, hw_read_cnt;
  1466. u32 current_read_len = 0, total_bytes_read = 0;
  1467. bool short_resp = false;
  1468. bool read_done = false;
  1469. u32 dlen, diff, rlen;
  1470. unsigned char *buff;
  1471. char cmd;
  1472. const struct mipi_dsi_msg *msg;
  1473. if (!cmd_desc) {
  1474. DSI_CTRL_ERR(dsi_ctrl, "Invalid command\n");
  1475. rc = -EINVAL;
  1476. goto error;
  1477. }
  1478. msg = &cmd_desc->msg;
  1479. rlen = msg->rx_len;
  1480. if (msg->rx_len <= 2) {
  1481. short_resp = true;
  1482. rd_pkt_size = msg->rx_len;
  1483. total_read_len = 4;
  1484. } else {
  1485. short_resp = false;
  1486. current_read_len = 10;
  1487. if (msg->rx_len < current_read_len)
  1488. rd_pkt_size = msg->rx_len;
  1489. else
  1490. rd_pkt_size = current_read_len;
  1491. total_read_len = current_read_len + 6;
  1492. }
  1493. buff = msg->rx_buf;
  1494. while (!read_done) {
  1495. rc = dsi_set_max_return_size(dsi_ctrl, cmd_desc, rd_pkt_size);
  1496. if (rc) {
  1497. DSI_CTRL_ERR(dsi_ctrl, "Failed to set max return packet size, rc=%d\n",
  1498. rc);
  1499. goto error;
  1500. }
  1501. /* clear RDBK_DATA registers before proceeding */
  1502. dsi_ctrl->hw.ops.clear_rdbk_register(&dsi_ctrl->hw);
  1503. rc = dsi_message_tx(dsi_ctrl, cmd_desc);
  1504. if (rc) {
  1505. DSI_CTRL_ERR(dsi_ctrl, "Message transmission failed, rc=%d\n",
  1506. rc);
  1507. goto error;
  1508. }
  1509. /*
  1510. * wait before reading rdbk_data register, if any delay is
  1511. * required after sending the read command.
  1512. */
  1513. if (cmd_desc->post_wait_ms)
  1514. usleep_range(cmd_desc->post_wait_ms * 1000,
  1515. ((cmd_desc->post_wait_ms * 1000) + 10));
  1516. dlen = dsi_ctrl->hw.ops.get_cmd_read_data(&dsi_ctrl->hw,
  1517. buff, total_bytes_read,
  1518. total_read_len, rd_pkt_size,
  1519. &hw_read_cnt);
  1520. if (!dlen)
  1521. goto error;
  1522. if (short_resp)
  1523. break;
  1524. if (rlen <= current_read_len) {
  1525. diff = current_read_len - rlen;
  1526. read_done = true;
  1527. } else {
  1528. diff = 0;
  1529. rlen -= current_read_len;
  1530. }
  1531. dlen -= 2; /* 2 bytes of CRC */
  1532. dlen -= diff;
  1533. buff += dlen;
  1534. total_bytes_read += dlen;
  1535. if (!read_done) {
  1536. current_read_len = 14; /* Not first read */
  1537. if (rlen < current_read_len)
  1538. rd_pkt_size += rlen;
  1539. else
  1540. rd_pkt_size += current_read_len;
  1541. }
  1542. }
  1543. if (hw_read_cnt < 16 && !short_resp)
  1544. buff = msg->rx_buf + (16 - hw_read_cnt);
  1545. else
  1546. buff = msg->rx_buf;
  1547. /* parse the data read from panel */
  1548. cmd = buff[0];
  1549. switch (cmd) {
  1550. case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
  1551. DSI_CTRL_ERR(dsi_ctrl, "Rx ACK_ERROR 0x%x\n", cmd);
  1552. rc = 0;
  1553. break;
  1554. case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE:
  1555. case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
  1556. rc = dsi_parse_short_read1_resp(msg, buff);
  1557. break;
  1558. case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE:
  1559. case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
  1560. rc = dsi_parse_short_read2_resp(msg, buff);
  1561. break;
  1562. case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
  1563. case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
  1564. rc = dsi_parse_long_read_resp(msg, buff);
  1565. break;
  1566. default:
  1567. DSI_CTRL_WARN(dsi_ctrl, "Invalid response: 0x%x\n", cmd);
  1568. rc = 0;
  1569. }
  1570. error:
  1571. return rc;
  1572. }
  1573. static int dsi_enable_ulps(struct dsi_ctrl *dsi_ctrl)
  1574. {
  1575. int rc = 0;
  1576. u32 lanes = 0;
  1577. u32 ulps_lanes;
  1578. lanes = dsi_ctrl->host_config.common_config.data_lanes;
  1579. rc = dsi_ctrl->hw.ops.wait_for_lane_idle(&dsi_ctrl->hw, lanes);
  1580. if (rc) {
  1581. DSI_CTRL_ERR(dsi_ctrl, "lanes not entering idle, skip ULPS\n");
  1582. return rc;
  1583. }
  1584. if (!dsi_ctrl->hw.ops.ulps_ops.ulps_request ||
  1585. !dsi_ctrl->hw.ops.ulps_ops.ulps_exit) {
  1586. DSI_CTRL_DEBUG(dsi_ctrl, "DSI controller ULPS ops not present\n");
  1587. return 0;
  1588. }
  1589. lanes |= DSI_CLOCK_LANE;
  1590. dsi_ctrl->hw.ops.ulps_ops.ulps_request(&dsi_ctrl->hw, lanes);
  1591. ulps_lanes = dsi_ctrl->hw.ops.ulps_ops.get_lanes_in_ulps(&dsi_ctrl->hw);
  1592. if ((lanes & ulps_lanes) != lanes) {
  1593. DSI_CTRL_ERR(dsi_ctrl, "Failed to enter ULPS, request=0x%x, actual=0x%x\n",
  1594. lanes, ulps_lanes);
  1595. rc = -EIO;
  1596. }
  1597. return rc;
  1598. }
  1599. static int dsi_disable_ulps(struct dsi_ctrl *dsi_ctrl)
  1600. {
  1601. int rc = 0;
  1602. u32 ulps_lanes, lanes = 0;
  1603. dsi_ctrl->hw.ops.clear_phy0_ln_err(&dsi_ctrl->hw);
  1604. if (!dsi_ctrl->hw.ops.ulps_ops.ulps_request ||
  1605. !dsi_ctrl->hw.ops.ulps_ops.ulps_exit) {
  1606. DSI_CTRL_DEBUG(dsi_ctrl, "DSI controller ULPS ops not present\n");
  1607. return 0;
  1608. }
  1609. lanes = dsi_ctrl->host_config.common_config.data_lanes;
  1610. lanes |= DSI_CLOCK_LANE;
  1611. ulps_lanes = dsi_ctrl->hw.ops.ulps_ops.get_lanes_in_ulps(&dsi_ctrl->hw);
  1612. if ((lanes & ulps_lanes) != lanes)
  1613. DSI_CTRL_ERR(dsi_ctrl, "Mismatch between lanes in ULPS\n");
  1614. lanes &= ulps_lanes;
  1615. dsi_ctrl->hw.ops.ulps_ops.ulps_exit(&dsi_ctrl->hw, lanes);
  1616. ulps_lanes = dsi_ctrl->hw.ops.ulps_ops.get_lanes_in_ulps(&dsi_ctrl->hw);
  1617. if (ulps_lanes & lanes) {
  1618. DSI_CTRL_ERR(dsi_ctrl, "Lanes (0x%x) stuck in ULPS\n",
  1619. ulps_lanes);
  1620. rc = -EIO;
  1621. }
  1622. return rc;
  1623. }
  1624. static void dsi_ctrl_enable_error_interrupts(struct dsi_ctrl *dsi_ctrl)
  1625. {
  1626. if (dsi_ctrl->host_config.panel_mode == DSI_OP_VIDEO_MODE &&
  1627. !dsi_ctrl->host_config.u.video_engine.bllp_lp11_en &&
  1628. !dsi_ctrl->host_config.u.video_engine.eof_bllp_lp11_en)
  1629. dsi_ctrl->hw.ops.enable_error_interrupts(&dsi_ctrl->hw,
  1630. 0xFF00A0);
  1631. else
  1632. dsi_ctrl->hw.ops.enable_error_interrupts(&dsi_ctrl->hw,
  1633. 0xFF00E0);
  1634. }
  1635. static int dsi_ctrl_drv_state_init(struct dsi_ctrl *dsi_ctrl)
  1636. {
  1637. int rc = 0;
  1638. bool splash_enabled = false;
  1639. struct dsi_ctrl_state_info *state = &dsi_ctrl->current_state;
  1640. if (!splash_enabled) {
  1641. state->power_state = DSI_CTRL_POWER_VREG_OFF;
  1642. state->cmd_engine_state = DSI_CTRL_ENGINE_OFF;
  1643. state->vid_engine_state = DSI_CTRL_ENGINE_OFF;
  1644. }
  1645. return rc;
  1646. }
  1647. static int dsi_ctrl_buffer_deinit(struct dsi_ctrl *dsi_ctrl)
  1648. {
  1649. struct msm_gem_address_space *aspace = NULL;
  1650. if (dsi_ctrl->tx_cmd_buf) {
  1651. aspace = dsi_ctrl_get_aspace(dsi_ctrl,
  1652. MSM_SMMU_DOMAIN_UNSECURE);
  1653. if (!aspace) {
  1654. DSI_CTRL_ERR(dsi_ctrl, "failed to get address space\n");
  1655. return -ENOMEM;
  1656. }
  1657. msm_gem_put_iova(dsi_ctrl->tx_cmd_buf, aspace);
  1658. mutex_lock(&dsi_ctrl->drm_dev->struct_mutex);
  1659. msm_gem_free_object(dsi_ctrl->tx_cmd_buf);
  1660. mutex_unlock(&dsi_ctrl->drm_dev->struct_mutex);
  1661. dsi_ctrl->tx_cmd_buf = NULL;
  1662. }
  1663. return 0;
  1664. }
  1665. int dsi_ctrl_buffer_init(struct dsi_ctrl *dsi_ctrl)
  1666. {
  1667. int rc = 0;
  1668. u64 iova = 0;
  1669. struct msm_gem_address_space *aspace = NULL;
  1670. aspace = dsi_ctrl_get_aspace(dsi_ctrl, MSM_SMMU_DOMAIN_UNSECURE);
  1671. if (!aspace) {
  1672. DSI_CTRL_ERR(dsi_ctrl, "failed to get address space\n");
  1673. return -ENOMEM;
  1674. }
  1675. dsi_ctrl->tx_cmd_buf = msm_gem_new(dsi_ctrl->drm_dev,
  1676. SZ_4K,
  1677. MSM_BO_UNCACHED);
  1678. if (IS_ERR(dsi_ctrl->tx_cmd_buf)) {
  1679. rc = PTR_ERR(dsi_ctrl->tx_cmd_buf);
  1680. DSI_CTRL_ERR(dsi_ctrl, "failed to allocate gem, rc=%d\n", rc);
  1681. dsi_ctrl->tx_cmd_buf = NULL;
  1682. goto error;
  1683. }
  1684. dsi_ctrl->cmd_buffer_size = SZ_4K;
  1685. rc = msm_gem_get_iova(dsi_ctrl->tx_cmd_buf, aspace, &iova);
  1686. if (rc) {
  1687. DSI_CTRL_ERR(dsi_ctrl, "failed to get iova, rc=%d\n", rc);
  1688. (void)dsi_ctrl_buffer_deinit(dsi_ctrl);
  1689. goto error;
  1690. }
  1691. if (iova & 0x07) {
  1692. DSI_CTRL_ERR(dsi_ctrl, "Tx command buffer is not 8 byte aligned\n");
  1693. rc = -ENOTSUPP;
  1694. (void)dsi_ctrl_buffer_deinit(dsi_ctrl);
  1695. goto error;
  1696. }
  1697. error:
  1698. return rc;
  1699. }
  1700. static int dsi_enable_io_clamp(struct dsi_ctrl *dsi_ctrl,
  1701. bool enable, bool ulps_enabled)
  1702. {
  1703. u32 lanes = 0;
  1704. if (dsi_ctrl->host_config.panel_mode == DSI_OP_CMD_MODE)
  1705. lanes = dsi_ctrl->host_config.common_config.data_lanes;
  1706. lanes |= DSI_CLOCK_LANE;
  1707. if (enable)
  1708. dsi_ctrl->hw.ops.clamp_enable(&dsi_ctrl->hw,
  1709. lanes, ulps_enabled);
  1710. else
  1711. dsi_ctrl->hw.ops.clamp_disable(&dsi_ctrl->hw,
  1712. lanes, ulps_enabled);
  1713. return 0;
  1714. }
  1715. static int dsi_ctrl_dts_parse(struct dsi_ctrl *dsi_ctrl,
  1716. struct device_node *of_node)
  1717. {
  1718. u32 index = 0, frame_threshold_time_us = 0;
  1719. int rc = 0;
  1720. if (!dsi_ctrl || !of_node) {
  1721. DSI_CTRL_ERR(dsi_ctrl, "invalid dsi_ctrl:%d or of_node:%d\n",
  1722. dsi_ctrl != NULL, of_node != NULL);
  1723. return -EINVAL;
  1724. }
  1725. rc = of_property_read_u32(of_node, "cell-index", &index);
  1726. if (rc) {
  1727. DSI_CTRL_DEBUG(dsi_ctrl, "cell index not set, default to 0\n");
  1728. index = 0;
  1729. }
  1730. dsi_ctrl->cell_index = index;
  1731. dsi_ctrl->name = of_get_property(of_node, "label", NULL);
  1732. if (!dsi_ctrl->name)
  1733. dsi_ctrl->name = DSI_CTRL_DEFAULT_LABEL;
  1734. dsi_ctrl->phy_isolation_enabled = of_property_read_bool(of_node,
  1735. "qcom,dsi-phy-isolation-enabled");
  1736. dsi_ctrl->null_insertion_enabled = of_property_read_bool(of_node,
  1737. "qcom,null-insertion-enabled");
  1738. dsi_ctrl->split_link_supported = of_property_read_bool(of_node,
  1739. "qcom,split-link-supported");
  1740. rc = of_property_read_u32(of_node, "frame-threshold-time-us",
  1741. &frame_threshold_time_us);
  1742. if (rc) {
  1743. DSI_CTRL_DEBUG(dsi_ctrl,
  1744. "frame-threshold-time not specified, defaulting\n");
  1745. frame_threshold_time_us = 2666;
  1746. }
  1747. dsi_ctrl->frame_threshold_time_us = frame_threshold_time_us;
  1748. return 0;
  1749. }
  1750. static int dsi_ctrl_dev_probe(struct platform_device *pdev)
  1751. {
  1752. struct dsi_ctrl *dsi_ctrl;
  1753. struct dsi_ctrl_list_item *item;
  1754. const struct of_device_id *id;
  1755. enum dsi_ctrl_version version;
  1756. int rc = 0;
  1757. id = of_match_node(msm_dsi_of_match, pdev->dev.of_node);
  1758. if (!id)
  1759. return -ENODEV;
  1760. version = *(enum dsi_ctrl_version *)id->data;
  1761. item = devm_kzalloc(&pdev->dev, sizeof(*item), GFP_KERNEL);
  1762. if (!item)
  1763. return -ENOMEM;
  1764. dsi_ctrl = devm_kzalloc(&pdev->dev, sizeof(*dsi_ctrl), GFP_KERNEL);
  1765. if (!dsi_ctrl)
  1766. return -ENOMEM;
  1767. dsi_ctrl->version = version;
  1768. dsi_ctrl->irq_info.irq_num = -1;
  1769. dsi_ctrl->irq_info.irq_stat_mask = 0x0;
  1770. INIT_WORK(&dsi_ctrl->dma_cmd_wait, dsi_ctrl_dma_cmd_wait_for_done);
  1771. atomic_set(&dsi_ctrl->dma_irq_trig, 0);
  1772. spin_lock_init(&dsi_ctrl->irq_info.irq_lock);
  1773. rc = dsi_ctrl_dts_parse(dsi_ctrl, pdev->dev.of_node);
  1774. if (rc) {
  1775. DSI_CTRL_ERR(dsi_ctrl, "dts parse failed, rc = %d\n", rc);
  1776. goto fail;
  1777. }
  1778. rc = dsi_ctrl_init_regmap(pdev, dsi_ctrl);
  1779. if (rc) {
  1780. DSI_CTRL_ERR(dsi_ctrl, "Failed to parse register information, rc = %d\n",
  1781. rc);
  1782. goto fail;
  1783. }
  1784. rc = dsi_ctrl_supplies_init(pdev, dsi_ctrl);
  1785. if (rc) {
  1786. DSI_CTRL_ERR(dsi_ctrl, "Failed to parse voltage supplies, rc = %d\n",
  1787. rc);
  1788. goto fail;
  1789. }
  1790. rc = dsi_ctrl_clocks_init(pdev, dsi_ctrl);
  1791. if (rc) {
  1792. DSI_CTRL_ERR(dsi_ctrl, "Failed to parse clock information, rc = %d\n",
  1793. rc);
  1794. goto fail_supplies;
  1795. }
  1796. rc = dsi_catalog_ctrl_setup(&dsi_ctrl->hw, dsi_ctrl->version,
  1797. dsi_ctrl->cell_index, dsi_ctrl->phy_isolation_enabled,
  1798. dsi_ctrl->null_insertion_enabled);
  1799. if (rc) {
  1800. DSI_CTRL_ERR(dsi_ctrl, "Catalog does not support version (%d)\n",
  1801. dsi_ctrl->version);
  1802. goto fail_clks;
  1803. }
  1804. item->ctrl = dsi_ctrl;
  1805. sde_dbg_dsi_ctrl_register(dsi_ctrl->hw.base, dsi_ctrl->name);
  1806. mutex_lock(&dsi_ctrl_list_lock);
  1807. list_add(&item->list, &dsi_ctrl_list);
  1808. mutex_unlock(&dsi_ctrl_list_lock);
  1809. mutex_init(&dsi_ctrl->ctrl_lock);
  1810. dsi_ctrl->secure_mode = false;
  1811. dsi_ctrl->pdev = pdev;
  1812. platform_set_drvdata(pdev, dsi_ctrl);
  1813. DSI_CTRL_INFO(dsi_ctrl, "Probe successful\n");
  1814. return 0;
  1815. fail_clks:
  1816. (void)dsi_ctrl_clocks_deinit(dsi_ctrl);
  1817. fail_supplies:
  1818. (void)dsi_ctrl_supplies_deinit(dsi_ctrl);
  1819. fail:
  1820. return rc;
  1821. }
  1822. static int dsi_ctrl_dev_remove(struct platform_device *pdev)
  1823. {
  1824. int rc = 0;
  1825. struct dsi_ctrl *dsi_ctrl;
  1826. struct list_head *pos, *tmp;
  1827. dsi_ctrl = platform_get_drvdata(pdev);
  1828. mutex_lock(&dsi_ctrl_list_lock);
  1829. list_for_each_safe(pos, tmp, &dsi_ctrl_list) {
  1830. struct dsi_ctrl_list_item *n = list_entry(pos,
  1831. struct dsi_ctrl_list_item,
  1832. list);
  1833. if (n->ctrl == dsi_ctrl) {
  1834. list_del(&n->list);
  1835. break;
  1836. }
  1837. }
  1838. mutex_unlock(&dsi_ctrl_list_lock);
  1839. mutex_lock(&dsi_ctrl->ctrl_lock);
  1840. dsi_ctrl_isr_configure(dsi_ctrl, false);
  1841. rc = dsi_ctrl_supplies_deinit(dsi_ctrl);
  1842. if (rc)
  1843. DSI_CTRL_ERR(dsi_ctrl,
  1844. "failed to deinitialize voltage supplies, rc=%d\n",
  1845. rc);
  1846. rc = dsi_ctrl_clocks_deinit(dsi_ctrl);
  1847. if (rc)
  1848. DSI_CTRL_ERR(dsi_ctrl,
  1849. "failed to deinitialize clocks, rc=%d\n", rc);
  1850. atomic_set(&dsi_ctrl->dma_irq_trig, 0);
  1851. mutex_unlock(&dsi_ctrl->ctrl_lock);
  1852. mutex_destroy(&dsi_ctrl->ctrl_lock);
  1853. devm_kfree(&pdev->dev, dsi_ctrl);
  1854. platform_set_drvdata(pdev, NULL);
  1855. return 0;
  1856. }
  1857. static struct platform_driver dsi_ctrl_driver = {
  1858. .probe = dsi_ctrl_dev_probe,
  1859. .remove = dsi_ctrl_dev_remove,
  1860. .driver = {
  1861. .name = "drm_dsi_ctrl",
  1862. .of_match_table = msm_dsi_of_match,
  1863. .suppress_bind_attrs = true,
  1864. },
  1865. };
  1866. int dsi_ctrl_get_io_resources(struct msm_io_res *io_res)
  1867. {
  1868. int rc = 0;
  1869. struct dsi_ctrl_list_item *dsi_ctrl;
  1870. mutex_lock(&dsi_ctrl_list_lock);
  1871. list_for_each_entry(dsi_ctrl, &dsi_ctrl_list, list) {
  1872. rc = msm_dss_get_io_mem(dsi_ctrl->ctrl->pdev, &io_res->mem);
  1873. if (rc) {
  1874. DSI_CTRL_ERR(dsi_ctrl->ctrl,
  1875. "failed to get io mem, rc = %d\n", rc);
  1876. return rc;
  1877. }
  1878. }
  1879. mutex_unlock(&dsi_ctrl_list_lock);
  1880. return rc;
  1881. }
  1882. /**
  1883. * dsi_ctrl_check_resource() - check if DSI controller is probed
  1884. * @of_node: of_node of the DSI controller.
  1885. *
  1886. * Checks if the DSI controller has been probed and is available.
  1887. *
  1888. * Return: status of DSI controller
  1889. */
  1890. bool dsi_ctrl_check_resource(struct device_node *of_node)
  1891. {
  1892. struct list_head *pos, *tmp;
  1893. struct dsi_ctrl *ctrl = NULL;
  1894. mutex_lock(&dsi_ctrl_list_lock);
  1895. list_for_each_safe(pos, tmp, &dsi_ctrl_list) {
  1896. struct dsi_ctrl_list_item *n;
  1897. n = list_entry(pos, struct dsi_ctrl_list_item, list);
  1898. if (!n->ctrl || !n->ctrl->pdev)
  1899. break;
  1900. if (n->ctrl->pdev->dev.of_node == of_node) {
  1901. ctrl = n->ctrl;
  1902. break;
  1903. }
  1904. }
  1905. mutex_unlock(&dsi_ctrl_list_lock);
  1906. return ctrl ? true : false;
  1907. }
  1908. /**
  1909. * dsi_ctrl_get() - get a dsi_ctrl handle from an of_node
  1910. * @of_node: of_node of the DSI controller.
  1911. *
  1912. * Gets the DSI controller handle for the corresponding of_node. The ref count
  1913. * is incremented to one and all subsequent gets will fail until the original
  1914. * clients calls a put.
  1915. *
  1916. * Return: DSI Controller handle.
  1917. */
  1918. struct dsi_ctrl *dsi_ctrl_get(struct device_node *of_node)
  1919. {
  1920. struct list_head *pos, *tmp;
  1921. struct dsi_ctrl *ctrl = NULL;
  1922. mutex_lock(&dsi_ctrl_list_lock);
  1923. list_for_each_safe(pos, tmp, &dsi_ctrl_list) {
  1924. struct dsi_ctrl_list_item *n;
  1925. n = list_entry(pos, struct dsi_ctrl_list_item, list);
  1926. if (n->ctrl->pdev->dev.of_node == of_node) {
  1927. ctrl = n->ctrl;
  1928. break;
  1929. }
  1930. }
  1931. mutex_unlock(&dsi_ctrl_list_lock);
  1932. if (!ctrl) {
  1933. DSI_CTRL_ERR(ctrl, "Device with of node not found rc=%d\n",
  1934. -EPROBE_DEFER);
  1935. ctrl = ERR_PTR(-EPROBE_DEFER);
  1936. return ctrl;
  1937. }
  1938. mutex_lock(&ctrl->ctrl_lock);
  1939. if (ctrl->refcount == 1) {
  1940. DSI_CTRL_ERR(ctrl, "Device in use\n");
  1941. mutex_unlock(&ctrl->ctrl_lock);
  1942. ctrl = ERR_PTR(-EBUSY);
  1943. return ctrl;
  1944. }
  1945. ctrl->refcount++;
  1946. mutex_unlock(&ctrl->ctrl_lock);
  1947. return ctrl;
  1948. }
  1949. /**
  1950. * dsi_ctrl_put() - releases a dsi controller handle.
  1951. * @dsi_ctrl: DSI controller handle.
  1952. *
  1953. * Releases the DSI controller. Driver will clean up all resources and puts back
  1954. * the DSI controller into reset state.
  1955. */
  1956. void dsi_ctrl_put(struct dsi_ctrl *dsi_ctrl)
  1957. {
  1958. mutex_lock(&dsi_ctrl->ctrl_lock);
  1959. if (dsi_ctrl->refcount == 0)
  1960. DSI_CTRL_ERR(dsi_ctrl, "Unbalanced %s call\n", __func__);
  1961. else
  1962. dsi_ctrl->refcount--;
  1963. mutex_unlock(&dsi_ctrl->ctrl_lock);
  1964. }
  1965. /**
  1966. * dsi_ctrl_drv_init() - initialize dsi controller driver.
  1967. * @dsi_ctrl: DSI controller handle.
  1968. * @parent: Parent directory for debug fs.
  1969. *
  1970. * Initializes DSI controller driver. Driver should be initialized after
  1971. * dsi_ctrl_get() succeeds.
  1972. *
  1973. * Return: error code.
  1974. */
  1975. int dsi_ctrl_drv_init(struct dsi_ctrl *dsi_ctrl, struct dentry *parent)
  1976. {
  1977. int rc = 0;
  1978. if (!dsi_ctrl) {
  1979. DSI_CTRL_ERR(dsi_ctrl, "Invalid params\n");
  1980. return -EINVAL;
  1981. }
  1982. mutex_lock(&dsi_ctrl->ctrl_lock);
  1983. rc = dsi_ctrl_drv_state_init(dsi_ctrl);
  1984. if (rc) {
  1985. DSI_CTRL_ERR(dsi_ctrl, "Failed to initialize driver state, rc=%d\n",
  1986. rc);
  1987. goto error;
  1988. }
  1989. rc = dsi_ctrl_debugfs_init(dsi_ctrl, parent);
  1990. if (rc) {
  1991. DSI_CTRL_ERR(dsi_ctrl, "failed to init debug fs, rc=%d\n", rc);
  1992. goto error;
  1993. }
  1994. error:
  1995. mutex_unlock(&dsi_ctrl->ctrl_lock);
  1996. return rc;
  1997. }
  1998. /**
  1999. * dsi_ctrl_drv_deinit() - de-initializes dsi controller driver
  2000. * @dsi_ctrl: DSI controller handle.
  2001. *
  2002. * Releases all resources acquired by dsi_ctrl_drv_init().
  2003. *
  2004. * Return: error code.
  2005. */
  2006. int dsi_ctrl_drv_deinit(struct dsi_ctrl *dsi_ctrl)
  2007. {
  2008. int rc = 0;
  2009. if (!dsi_ctrl) {
  2010. DSI_CTRL_ERR(dsi_ctrl, "Invalid params\n");
  2011. return -EINVAL;
  2012. }
  2013. mutex_lock(&dsi_ctrl->ctrl_lock);
  2014. rc = dsi_ctrl_debugfs_deinit(dsi_ctrl);
  2015. if (rc)
  2016. DSI_CTRL_ERR(dsi_ctrl, "failed to release debugfs root, rc=%d\n",
  2017. rc);
  2018. rc = dsi_ctrl_buffer_deinit(dsi_ctrl);
  2019. if (rc)
  2020. DSI_CTRL_ERR(dsi_ctrl, "Failed to free cmd buffers, rc=%d\n",
  2021. rc);
  2022. mutex_unlock(&dsi_ctrl->ctrl_lock);
  2023. return rc;
  2024. }
  2025. int dsi_ctrl_clk_cb_register(struct dsi_ctrl *dsi_ctrl,
  2026. struct clk_ctrl_cb *clk_cb)
  2027. {
  2028. if (!dsi_ctrl || !clk_cb) {
  2029. DSI_CTRL_ERR(dsi_ctrl, "Invalid params\n");
  2030. return -EINVAL;
  2031. }
  2032. dsi_ctrl->clk_cb.priv = clk_cb->priv;
  2033. dsi_ctrl->clk_cb.dsi_clk_cb = clk_cb->dsi_clk_cb;
  2034. return 0;
  2035. }
  2036. /**
  2037. * dsi_ctrl_phy_sw_reset() - perform a PHY software reset
  2038. * @dsi_ctrl: DSI controller handle.
  2039. *
  2040. * Performs a PHY software reset on the DSI controller. Reset should be done
  2041. * when the controller power state is DSI_CTRL_POWER_CORE_CLK_ON and the PHY is
  2042. * not enabled.
  2043. *
  2044. * This function will fail if driver is in any other state.
  2045. *
  2046. * Return: error code.
  2047. */
  2048. int dsi_ctrl_phy_sw_reset(struct dsi_ctrl *dsi_ctrl)
  2049. {
  2050. int rc = 0;
  2051. if (!dsi_ctrl) {
  2052. DSI_CTRL_ERR(dsi_ctrl, "Invalid params\n");
  2053. return -EINVAL;
  2054. }
  2055. mutex_lock(&dsi_ctrl->ctrl_lock);
  2056. rc = dsi_ctrl_check_state(dsi_ctrl, DSI_CTRL_OP_PHY_SW_RESET, 0x0);
  2057. if (rc) {
  2058. DSI_CTRL_ERR(dsi_ctrl, "Controller state check failed, rc=%d\n",
  2059. rc);
  2060. goto error;
  2061. }
  2062. dsi_ctrl->hw.ops.phy_sw_reset(&dsi_ctrl->hw);
  2063. DSI_CTRL_DEBUG(dsi_ctrl, "PHY soft reset done\n");
  2064. dsi_ctrl_update_state(dsi_ctrl, DSI_CTRL_OP_PHY_SW_RESET, 0x0);
  2065. error:
  2066. mutex_unlock(&dsi_ctrl->ctrl_lock);
  2067. return rc;
  2068. }
  2069. /**
  2070. * dsi_ctrl_seamless_timing_update() - update only controller timing
  2071. * @dsi_ctrl: DSI controller handle.
  2072. * @timing: New DSI timing info
  2073. *
  2074. * Updates host timing values to conduct a seamless transition to new timing
  2075. * For example, to update the porch values in a dynamic fps switch.
  2076. *
  2077. * Return: error code.
  2078. */
  2079. int dsi_ctrl_async_timing_update(struct dsi_ctrl *dsi_ctrl,
  2080. struct dsi_mode_info *timing)
  2081. {
  2082. struct dsi_mode_info *host_mode;
  2083. int rc = 0;
  2084. if (!dsi_ctrl || !timing) {
  2085. DSI_CTRL_ERR(dsi_ctrl, "Invalid params\n");
  2086. return -EINVAL;
  2087. }
  2088. mutex_lock(&dsi_ctrl->ctrl_lock);
  2089. rc = dsi_ctrl_check_state(dsi_ctrl, DSI_CTRL_OP_ASYNC_TIMING,
  2090. DSI_CTRL_ENGINE_ON);
  2091. if (rc) {
  2092. DSI_CTRL_ERR(dsi_ctrl, "Controller state check failed, rc=%d\n",
  2093. rc);
  2094. goto exit;
  2095. }
  2096. host_mode = &dsi_ctrl->host_config.video_timing;
  2097. memcpy(host_mode, timing, sizeof(*host_mode));
  2098. dsi_ctrl->hw.ops.set_timing_db(&dsi_ctrl->hw, true);
  2099. dsi_ctrl->hw.ops.set_video_timing(&dsi_ctrl->hw, host_mode);
  2100. exit:
  2101. mutex_unlock(&dsi_ctrl->ctrl_lock);
  2102. return rc;
  2103. }
  2104. /**
  2105. * dsi_ctrl_timing_db_update() - update only controller Timing DB
  2106. * @dsi_ctrl: DSI controller handle.
  2107. * @enable: Enable/disable Timing DB register
  2108. *
  2109. * Update timing db register value during dfps usecases
  2110. *
  2111. * Return: error code.
  2112. */
  2113. int dsi_ctrl_timing_db_update(struct dsi_ctrl *dsi_ctrl,
  2114. bool enable)
  2115. {
  2116. int rc = 0;
  2117. if (!dsi_ctrl) {
  2118. DSI_CTRL_ERR(dsi_ctrl, "Invalid dsi_ctrl\n");
  2119. return -EINVAL;
  2120. }
  2121. mutex_lock(&dsi_ctrl->ctrl_lock);
  2122. rc = dsi_ctrl_check_state(dsi_ctrl, DSI_CTRL_OP_ASYNC_TIMING,
  2123. DSI_CTRL_ENGINE_ON);
  2124. if (rc) {
  2125. DSI_CTRL_ERR(dsi_ctrl, "Controller state check failed, rc=%d\n",
  2126. rc);
  2127. goto exit;
  2128. }
  2129. /*
  2130. * Add HW recommended delay for dfps feature.
  2131. * When prefetch is enabled, MDSS HW works on 2 vsync
  2132. * boundaries i.e. mdp_vsync and panel_vsync.
  2133. * In the current implementation we are only waiting
  2134. * for mdp_vsync. We need to make sure that interface
  2135. * flush is after panel_vsync. So, added the recommended
  2136. * delays after dfps update.
  2137. */
  2138. usleep_range(2000, 2010);
  2139. dsi_ctrl->hw.ops.set_timing_db(&dsi_ctrl->hw, enable);
  2140. exit:
  2141. mutex_unlock(&dsi_ctrl->ctrl_lock);
  2142. return rc;
  2143. }
  2144. int dsi_ctrl_timing_setup(struct dsi_ctrl *dsi_ctrl)
  2145. {
  2146. int rc = 0;
  2147. if (!dsi_ctrl) {
  2148. DSI_CTRL_ERR(dsi_ctrl, "Invalid params\n");
  2149. return -EINVAL;
  2150. }
  2151. mutex_lock(&dsi_ctrl->ctrl_lock);
  2152. if (dsi_ctrl->host_config.panel_mode == DSI_OP_CMD_MODE) {
  2153. dsi_ctrl->hw.ops.cmd_engine_setup(&dsi_ctrl->hw,
  2154. &dsi_ctrl->host_config.common_config,
  2155. &dsi_ctrl->host_config.u.cmd_engine);
  2156. dsi_ctrl->hw.ops.setup_cmd_stream(&dsi_ctrl->hw,
  2157. &dsi_ctrl->host_config.video_timing,
  2158. &dsi_ctrl->host_config.common_config,
  2159. 0x0,
  2160. &dsi_ctrl->roi);
  2161. dsi_ctrl->hw.ops.cmd_engine_en(&dsi_ctrl->hw, true);
  2162. } else {
  2163. dsi_ctrl->hw.ops.video_engine_setup(&dsi_ctrl->hw,
  2164. &dsi_ctrl->host_config.common_config,
  2165. &dsi_ctrl->host_config.u.video_engine);
  2166. dsi_ctrl->hw.ops.set_video_timing(&dsi_ctrl->hw,
  2167. &dsi_ctrl->host_config.video_timing);
  2168. dsi_ctrl->hw.ops.video_engine_en(&dsi_ctrl->hw, true);
  2169. }
  2170. mutex_unlock(&dsi_ctrl->ctrl_lock);
  2171. return rc;
  2172. }
  2173. int dsi_ctrl_setup(struct dsi_ctrl *dsi_ctrl)
  2174. {
  2175. int rc = 0;
  2176. rc = dsi_ctrl_timing_setup(dsi_ctrl);
  2177. if (rc)
  2178. return -EINVAL;
  2179. mutex_lock(&dsi_ctrl->ctrl_lock);
  2180. dsi_ctrl->hw.ops.setup_lane_map(&dsi_ctrl->hw,
  2181. &dsi_ctrl->host_config.lane_map);
  2182. dsi_ctrl->hw.ops.host_setup(&dsi_ctrl->hw,
  2183. &dsi_ctrl->host_config.common_config);
  2184. dsi_ctrl->hw.ops.enable_status_interrupts(&dsi_ctrl->hw, 0x0);
  2185. dsi_ctrl_enable_error_interrupts(dsi_ctrl);
  2186. dsi_ctrl->hw.ops.ctrl_en(&dsi_ctrl->hw, true);
  2187. mutex_unlock(&dsi_ctrl->ctrl_lock);
  2188. return rc;
  2189. }
  2190. int dsi_ctrl_set_roi(struct dsi_ctrl *dsi_ctrl, struct dsi_rect *roi,
  2191. bool *changed)
  2192. {
  2193. int rc = 0;
  2194. if (!dsi_ctrl || !roi || !changed) {
  2195. DSI_CTRL_ERR(dsi_ctrl, "Invalid params\n");
  2196. return -EINVAL;
  2197. }
  2198. mutex_lock(&dsi_ctrl->ctrl_lock);
  2199. if ((!dsi_rect_is_equal(&dsi_ctrl->roi, roi)) ||
  2200. dsi_ctrl->modeupdated) {
  2201. *changed = true;
  2202. memcpy(&dsi_ctrl->roi, roi, sizeof(dsi_ctrl->roi));
  2203. dsi_ctrl->modeupdated = false;
  2204. } else
  2205. *changed = false;
  2206. mutex_unlock(&dsi_ctrl->ctrl_lock);
  2207. return rc;
  2208. }
  2209. /**
  2210. * dsi_ctrl_config_clk_gating() - Enable/disable DSI PHY clk gating.
  2211. * @dsi_ctrl: DSI controller handle.
  2212. * @enable: Enable/disable DSI PHY clk gating
  2213. * @clk_selection: clock to enable/disable clock gating
  2214. *
  2215. * Return: error code.
  2216. */
  2217. int dsi_ctrl_config_clk_gating(struct dsi_ctrl *dsi_ctrl, bool enable,
  2218. enum dsi_clk_gate_type clk_selection)
  2219. {
  2220. if (!dsi_ctrl) {
  2221. DSI_CTRL_ERR(dsi_ctrl, "Invalid params\n");
  2222. return -EINVAL;
  2223. }
  2224. if (dsi_ctrl->hw.ops.config_clk_gating)
  2225. dsi_ctrl->hw.ops.config_clk_gating(&dsi_ctrl->hw, enable,
  2226. clk_selection);
  2227. return 0;
  2228. }
  2229. /**
  2230. * dsi_ctrl_phy_reset_config() - Mask/unmask propagation of ahb reset signal
  2231. * to DSI PHY hardware.
  2232. * @dsi_ctrl: DSI controller handle.
  2233. * @enable: Mask/unmask the PHY reset signal.
  2234. *
  2235. * Return: error code.
  2236. */
  2237. int dsi_ctrl_phy_reset_config(struct dsi_ctrl *dsi_ctrl, bool enable)
  2238. {
  2239. if (!dsi_ctrl) {
  2240. DSI_CTRL_ERR(dsi_ctrl, "Invalid params\n");
  2241. return -EINVAL;
  2242. }
  2243. if (dsi_ctrl->hw.ops.phy_reset_config)
  2244. dsi_ctrl->hw.ops.phy_reset_config(&dsi_ctrl->hw, enable);
  2245. return 0;
  2246. }
  2247. static bool dsi_ctrl_check_for_spurious_error_interrupts(
  2248. struct dsi_ctrl *dsi_ctrl)
  2249. {
  2250. const unsigned long intr_check_interval = msecs_to_jiffies(1000);
  2251. const unsigned int interrupt_threshold = 15;
  2252. unsigned long jiffies_now = jiffies;
  2253. if (!dsi_ctrl) {
  2254. DSI_CTRL_ERR(dsi_ctrl, "Invalid DSI controller structure\n");
  2255. return false;
  2256. }
  2257. if (dsi_ctrl->jiffies_start == 0)
  2258. dsi_ctrl->jiffies_start = jiffies;
  2259. dsi_ctrl->error_interrupt_count++;
  2260. if ((jiffies_now - dsi_ctrl->jiffies_start) < intr_check_interval) {
  2261. if (dsi_ctrl->error_interrupt_count > interrupt_threshold) {
  2262. SDE_EVT32_IRQ(dsi_ctrl->cell_index,
  2263. dsi_ctrl->error_interrupt_count,
  2264. interrupt_threshold);
  2265. return true;
  2266. }
  2267. } else {
  2268. dsi_ctrl->jiffies_start = jiffies;
  2269. dsi_ctrl->error_interrupt_count = 1;
  2270. }
  2271. return false;
  2272. }
  2273. static void dsi_ctrl_handle_error_status(struct dsi_ctrl *dsi_ctrl,
  2274. unsigned long error)
  2275. {
  2276. struct dsi_event_cb_info cb_info;
  2277. cb_info = dsi_ctrl->irq_info.irq_err_cb;
  2278. /* disable error interrupts */
  2279. if (dsi_ctrl->hw.ops.error_intr_ctrl)
  2280. dsi_ctrl->hw.ops.error_intr_ctrl(&dsi_ctrl->hw, false);
  2281. /* clear error interrupts first */
  2282. if (dsi_ctrl->hw.ops.clear_error_status)
  2283. dsi_ctrl->hw.ops.clear_error_status(&dsi_ctrl->hw,
  2284. error);
  2285. /* DTLN PHY error */
  2286. if (error & 0x3000E00)
  2287. DSI_CTRL_ERR(dsi_ctrl, "dsi PHY contention error: 0x%lx\n",
  2288. error);
  2289. /* ignore TX timeout if blpp_lp11 is disabled */
  2290. if (dsi_ctrl->host_config.panel_mode == DSI_OP_VIDEO_MODE &&
  2291. !dsi_ctrl->host_config.u.video_engine.bllp_lp11_en &&
  2292. !dsi_ctrl->host_config.u.video_engine.eof_bllp_lp11_en)
  2293. error &= ~DSI_HS_TX_TIMEOUT;
  2294. /* TX timeout error */
  2295. if (error & 0xE0) {
  2296. if (error & 0xA0) {
  2297. if (cb_info.event_cb) {
  2298. cb_info.event_idx = DSI_LP_Rx_TIMEOUT;
  2299. (void)cb_info.event_cb(cb_info.event_usr_ptr,
  2300. cb_info.event_idx,
  2301. dsi_ctrl->cell_index,
  2302. 0, 0, 0, 0);
  2303. }
  2304. }
  2305. }
  2306. /* DSI FIFO OVERFLOW error */
  2307. if (error & 0xF0000) {
  2308. u32 mask = 0;
  2309. if (dsi_ctrl->hw.ops.get_error_mask)
  2310. mask = dsi_ctrl->hw.ops.get_error_mask(&dsi_ctrl->hw);
  2311. /* no need to report FIFO overflow if already masked */
  2312. if (cb_info.event_cb && !(mask & 0xf0000)) {
  2313. cb_info.event_idx = DSI_FIFO_OVERFLOW;
  2314. (void)cb_info.event_cb(cb_info.event_usr_ptr,
  2315. cb_info.event_idx,
  2316. dsi_ctrl->cell_index,
  2317. 0, 0, 0, 0);
  2318. }
  2319. }
  2320. /* DSI FIFO UNDERFLOW error */
  2321. if (error & 0xF00000) {
  2322. if (cb_info.event_cb) {
  2323. cb_info.event_idx = DSI_FIFO_UNDERFLOW;
  2324. (void)cb_info.event_cb(cb_info.event_usr_ptr,
  2325. cb_info.event_idx,
  2326. dsi_ctrl->cell_index,
  2327. 0, 0, 0, 0);
  2328. }
  2329. }
  2330. /* DSI PLL UNLOCK error */
  2331. if (error & BIT(8))
  2332. DSI_CTRL_ERR(dsi_ctrl, "dsi PLL unlock error: 0x%lx\n", error);
  2333. /* ACK error */
  2334. if (error & 0xF)
  2335. DSI_CTRL_ERR(dsi_ctrl, "ack error: 0x%lx\n", error);
  2336. /*
  2337. * DSI Phy can go into bad state during ESD influence. This can
  2338. * manifest as various types of spurious error interrupts on
  2339. * DSI controller. This check will allow us to handle afore mentioned
  2340. * case and prevent us from re enabling interrupts until a full ESD
  2341. * recovery is completed.
  2342. */
  2343. if (dsi_ctrl_check_for_spurious_error_interrupts(dsi_ctrl) &&
  2344. dsi_ctrl->esd_check_underway) {
  2345. dsi_ctrl->hw.ops.soft_reset(&dsi_ctrl->hw);
  2346. return;
  2347. }
  2348. /* enable back DSI interrupts */
  2349. if (dsi_ctrl->hw.ops.error_intr_ctrl)
  2350. dsi_ctrl->hw.ops.error_intr_ctrl(&dsi_ctrl->hw, true);
  2351. }
  2352. /**
  2353. * dsi_ctrl_isr - interrupt service routine for DSI CTRL component
  2354. * @irq: Incoming IRQ number
  2355. * @ptr: Pointer to user data structure (struct dsi_ctrl)
  2356. * Returns: IRQ_HANDLED if no further action required
  2357. */
  2358. static irqreturn_t dsi_ctrl_isr(int irq, void *ptr)
  2359. {
  2360. struct dsi_ctrl *dsi_ctrl;
  2361. struct dsi_event_cb_info cb_info;
  2362. unsigned long flags;
  2363. uint32_t status = 0x0, i;
  2364. uint64_t errors = 0x0;
  2365. if (!ptr)
  2366. return IRQ_NONE;
  2367. dsi_ctrl = ptr;
  2368. /* check status interrupts */
  2369. if (dsi_ctrl->hw.ops.get_interrupt_status)
  2370. status = dsi_ctrl->hw.ops.get_interrupt_status(&dsi_ctrl->hw);
  2371. /* check error interrupts */
  2372. if (dsi_ctrl->hw.ops.get_error_status)
  2373. errors = dsi_ctrl->hw.ops.get_error_status(&dsi_ctrl->hw);
  2374. /* clear interrupts */
  2375. if (dsi_ctrl->hw.ops.clear_interrupt_status)
  2376. dsi_ctrl->hw.ops.clear_interrupt_status(&dsi_ctrl->hw, 0x0);
  2377. SDE_EVT32_IRQ(dsi_ctrl->cell_index, status, errors);
  2378. /* handle DSI error recovery */
  2379. if (status & DSI_ERROR)
  2380. dsi_ctrl_handle_error_status(dsi_ctrl, errors);
  2381. if (status & DSI_CMD_MODE_DMA_DONE) {
  2382. if (dsi_ctrl->enable_cmd_dma_stats) {
  2383. u32 reg = dsi_ctrl->hw.ops.log_line_count(&dsi_ctrl->hw,
  2384. dsi_ctrl->cmd_mode);
  2385. dsi_ctrl->cmd_success_line = (reg & 0xFFFF);
  2386. dsi_ctrl->cmd_success_frame = ((reg >> 16) & 0xFFFF);
  2387. SDE_EVT32(dsi_ctrl->cell_index, SDE_EVTLOG_FUNC_CASE1,
  2388. dsi_ctrl->cmd_success_line,
  2389. dsi_ctrl->cmd_success_frame);
  2390. }
  2391. atomic_set(&dsi_ctrl->dma_irq_trig, 1);
  2392. dsi_ctrl_disable_status_interrupt(dsi_ctrl,
  2393. DSI_SINT_CMD_MODE_DMA_DONE);
  2394. complete_all(&dsi_ctrl->irq_info.cmd_dma_done);
  2395. }
  2396. if (status & DSI_CMD_FRAME_DONE) {
  2397. dsi_ctrl_disable_status_interrupt(dsi_ctrl,
  2398. DSI_SINT_CMD_FRAME_DONE);
  2399. complete_all(&dsi_ctrl->irq_info.cmd_frame_done);
  2400. }
  2401. if (status & DSI_VIDEO_MODE_FRAME_DONE) {
  2402. dsi_ctrl_disable_status_interrupt(dsi_ctrl,
  2403. DSI_SINT_VIDEO_MODE_FRAME_DONE);
  2404. complete_all(&dsi_ctrl->irq_info.vid_frame_done);
  2405. }
  2406. if (status & DSI_BTA_DONE) {
  2407. u32 fifo_overflow_mask = (DSI_DLN0_HS_FIFO_OVERFLOW |
  2408. DSI_DLN1_HS_FIFO_OVERFLOW |
  2409. DSI_DLN2_HS_FIFO_OVERFLOW |
  2410. DSI_DLN3_HS_FIFO_OVERFLOW);
  2411. dsi_ctrl_disable_status_interrupt(dsi_ctrl,
  2412. DSI_SINT_BTA_DONE);
  2413. complete_all(&dsi_ctrl->irq_info.bta_done);
  2414. if (dsi_ctrl->hw.ops.clear_error_status)
  2415. dsi_ctrl->hw.ops.clear_error_status(&dsi_ctrl->hw,
  2416. fifo_overflow_mask);
  2417. }
  2418. for (i = 0; status && i < DSI_STATUS_INTERRUPT_COUNT; ++i) {
  2419. if (status & 0x1) {
  2420. spin_lock_irqsave(&dsi_ctrl->irq_info.irq_lock, flags);
  2421. cb_info = dsi_ctrl->irq_info.irq_stat_cb[i];
  2422. spin_unlock_irqrestore(
  2423. &dsi_ctrl->irq_info.irq_lock, flags);
  2424. if (cb_info.event_cb)
  2425. (void)cb_info.event_cb(cb_info.event_usr_ptr,
  2426. cb_info.event_idx,
  2427. dsi_ctrl->cell_index,
  2428. irq, 0, 0, 0);
  2429. }
  2430. status >>= 1;
  2431. }
  2432. return IRQ_HANDLED;
  2433. }
  2434. /**
  2435. * _dsi_ctrl_setup_isr - register ISR handler
  2436. * @dsi_ctrl: Pointer to associated dsi_ctrl structure
  2437. * Returns: Zero on success
  2438. */
  2439. static int _dsi_ctrl_setup_isr(struct dsi_ctrl *dsi_ctrl)
  2440. {
  2441. int irq_num, rc;
  2442. if (!dsi_ctrl)
  2443. return -EINVAL;
  2444. if (dsi_ctrl->irq_info.irq_num != -1)
  2445. return 0;
  2446. init_completion(&dsi_ctrl->irq_info.cmd_dma_done);
  2447. init_completion(&dsi_ctrl->irq_info.vid_frame_done);
  2448. init_completion(&dsi_ctrl->irq_info.cmd_frame_done);
  2449. init_completion(&dsi_ctrl->irq_info.bta_done);
  2450. irq_num = platform_get_irq(dsi_ctrl->pdev, 0);
  2451. if (irq_num < 0) {
  2452. DSI_CTRL_ERR(dsi_ctrl, "Failed to get IRQ number, %d\n",
  2453. irq_num);
  2454. rc = irq_num;
  2455. } else {
  2456. rc = devm_request_threaded_irq(&dsi_ctrl->pdev->dev, irq_num,
  2457. dsi_ctrl_isr, NULL, 0, "dsi_ctrl", dsi_ctrl);
  2458. if (rc) {
  2459. DSI_CTRL_ERR(dsi_ctrl, "Failed to request IRQ, %d\n",
  2460. rc);
  2461. } else {
  2462. dsi_ctrl->irq_info.irq_num = irq_num;
  2463. disable_irq_nosync(irq_num);
  2464. DSI_CTRL_INFO(dsi_ctrl, "IRQ %d registered\n", irq_num);
  2465. }
  2466. }
  2467. return rc;
  2468. }
  2469. /**
  2470. * _dsi_ctrl_destroy_isr - unregister ISR handler
  2471. * @dsi_ctrl: Pointer to associated dsi_ctrl structure
  2472. */
  2473. static void _dsi_ctrl_destroy_isr(struct dsi_ctrl *dsi_ctrl)
  2474. {
  2475. if (!dsi_ctrl || !dsi_ctrl->pdev || dsi_ctrl->irq_info.irq_num < 0)
  2476. return;
  2477. if (dsi_ctrl->irq_info.irq_num != -1) {
  2478. devm_free_irq(&dsi_ctrl->pdev->dev,
  2479. dsi_ctrl->irq_info.irq_num, dsi_ctrl);
  2480. dsi_ctrl->irq_info.irq_num = -1;
  2481. }
  2482. }
  2483. void dsi_ctrl_enable_status_interrupt(struct dsi_ctrl *dsi_ctrl,
  2484. uint32_t intr_idx, struct dsi_event_cb_info *event_info)
  2485. {
  2486. unsigned long flags;
  2487. if (!dsi_ctrl || dsi_ctrl->irq_info.irq_num == -1 ||
  2488. intr_idx >= DSI_STATUS_INTERRUPT_COUNT)
  2489. return;
  2490. SDE_EVT32(dsi_ctrl->cell_index, SDE_EVTLOG_FUNC_ENTRY, intr_idx);
  2491. spin_lock_irqsave(&dsi_ctrl->irq_info.irq_lock, flags);
  2492. if (dsi_ctrl->irq_info.irq_stat_refcount[intr_idx] == 0) {
  2493. /* enable irq on first request */
  2494. if (dsi_ctrl->irq_info.irq_stat_mask == 0)
  2495. enable_irq(dsi_ctrl->irq_info.irq_num);
  2496. /* update hardware mask */
  2497. dsi_ctrl->irq_info.irq_stat_mask |= BIT(intr_idx);
  2498. dsi_ctrl->hw.ops.enable_status_interrupts(&dsi_ctrl->hw,
  2499. dsi_ctrl->irq_info.irq_stat_mask);
  2500. }
  2501. if (intr_idx == DSI_SINT_CMD_MODE_DMA_DONE)
  2502. dsi_ctrl->hw.ops.enable_status_interrupts(&dsi_ctrl->hw,
  2503. dsi_ctrl->irq_info.irq_stat_mask);
  2504. ++(dsi_ctrl->irq_info.irq_stat_refcount[intr_idx]);
  2505. if (event_info)
  2506. dsi_ctrl->irq_info.irq_stat_cb[intr_idx] = *event_info;
  2507. spin_unlock_irqrestore(&dsi_ctrl->irq_info.irq_lock, flags);
  2508. }
  2509. void dsi_ctrl_disable_status_interrupt(struct dsi_ctrl *dsi_ctrl,
  2510. uint32_t intr_idx)
  2511. {
  2512. unsigned long flags;
  2513. if (!dsi_ctrl || intr_idx >= DSI_STATUS_INTERRUPT_COUNT)
  2514. return;
  2515. SDE_EVT32_IRQ(dsi_ctrl->cell_index, SDE_EVTLOG_FUNC_ENTRY, intr_idx);
  2516. spin_lock_irqsave(&dsi_ctrl->irq_info.irq_lock, flags);
  2517. if (dsi_ctrl->irq_info.irq_stat_refcount[intr_idx])
  2518. if (--(dsi_ctrl->irq_info.irq_stat_refcount[intr_idx]) == 0) {
  2519. dsi_ctrl->irq_info.irq_stat_mask &= ~BIT(intr_idx);
  2520. dsi_ctrl->hw.ops.enable_status_interrupts(&dsi_ctrl->hw,
  2521. dsi_ctrl->irq_info.irq_stat_mask);
  2522. /* don't need irq if no lines are enabled */
  2523. if (dsi_ctrl->irq_info.irq_stat_mask == 0 &&
  2524. dsi_ctrl->irq_info.irq_num != -1)
  2525. disable_irq_nosync(dsi_ctrl->irq_info.irq_num);
  2526. }
  2527. spin_unlock_irqrestore(&dsi_ctrl->irq_info.irq_lock, flags);
  2528. }
  2529. int dsi_ctrl_host_timing_update(struct dsi_ctrl *dsi_ctrl)
  2530. {
  2531. if (!dsi_ctrl) {
  2532. DSI_CTRL_ERR(dsi_ctrl, "Invalid params\n");
  2533. return -EINVAL;
  2534. }
  2535. if (dsi_ctrl->hw.ops.host_setup)
  2536. dsi_ctrl->hw.ops.host_setup(&dsi_ctrl->hw,
  2537. &dsi_ctrl->host_config.common_config);
  2538. if (dsi_ctrl->host_config.panel_mode == DSI_OP_CMD_MODE) {
  2539. if (dsi_ctrl->hw.ops.cmd_engine_setup)
  2540. dsi_ctrl->hw.ops.cmd_engine_setup(&dsi_ctrl->hw,
  2541. &dsi_ctrl->host_config.common_config,
  2542. &dsi_ctrl->host_config.u.cmd_engine);
  2543. if (dsi_ctrl->hw.ops.setup_cmd_stream)
  2544. dsi_ctrl->hw.ops.setup_cmd_stream(&dsi_ctrl->hw,
  2545. &dsi_ctrl->host_config.video_timing,
  2546. &dsi_ctrl->host_config.common_config,
  2547. 0x0, NULL);
  2548. } else {
  2549. DSI_CTRL_ERR(dsi_ctrl, "invalid panel mode for resolution switch\n");
  2550. return -EINVAL;
  2551. }
  2552. return 0;
  2553. }
  2554. /**
  2555. * dsi_ctrl_update_host_state() - Update the host initialization state.
  2556. * @dsi_ctrl: DSI controller handle.
  2557. * @op: ctrl driver ops
  2558. * @enable: boolean signifying host state.
  2559. *
  2560. * Update the host status only while exiting from ulps during suspend state.
  2561. *
  2562. * Return: error code.
  2563. */
  2564. int dsi_ctrl_update_host_state(struct dsi_ctrl *dsi_ctrl,
  2565. enum dsi_ctrl_driver_ops op, bool enable)
  2566. {
  2567. int rc = 0;
  2568. u32 state = enable ? 0x1 : 0x0;
  2569. if (!dsi_ctrl)
  2570. return rc;
  2571. mutex_lock(&dsi_ctrl->ctrl_lock);
  2572. rc = dsi_ctrl_check_state(dsi_ctrl, op, state);
  2573. if (rc) {
  2574. DSI_CTRL_ERR(dsi_ctrl, "Controller state check failed, rc=%d\n",
  2575. rc);
  2576. mutex_unlock(&dsi_ctrl->ctrl_lock);
  2577. return rc;
  2578. }
  2579. dsi_ctrl_update_state(dsi_ctrl, op, state);
  2580. mutex_unlock(&dsi_ctrl->ctrl_lock);
  2581. return rc;
  2582. }
  2583. /**
  2584. * dsi_ctrl_host_init() - Initialize DSI host hardware.
  2585. * @dsi_ctrl: DSI controller handle.
  2586. * @skip_op: Boolean to indicate few operations can be skipped.
  2587. * Set during the cont-splash or trusted-vm enable case.
  2588. *
  2589. * Initializes DSI controller hardware with host configuration provided by
  2590. * dsi_ctrl_update_host_config(). Initialization can be performed only during
  2591. * DSI_CTRL_POWER_CORE_CLK_ON state and after the PHY SW reset has been
  2592. * performed.
  2593. *
  2594. * Return: error code.
  2595. */
  2596. int dsi_ctrl_host_init(struct dsi_ctrl *dsi_ctrl, bool skip_op)
  2597. {
  2598. int rc = 0;
  2599. if (!dsi_ctrl) {
  2600. DSI_CTRL_ERR(dsi_ctrl, "Invalid params\n");
  2601. return -EINVAL;
  2602. }
  2603. mutex_lock(&dsi_ctrl->ctrl_lock);
  2604. rc = dsi_ctrl_check_state(dsi_ctrl, DSI_CTRL_OP_HOST_INIT, 0x1);
  2605. if (rc) {
  2606. DSI_CTRL_ERR(dsi_ctrl, "Controller state check failed, rc=%d\n",
  2607. rc);
  2608. goto error;
  2609. }
  2610. /*
  2611. * For continuous splash/trusted vm usecases we omit hw operations
  2612. * as bootloader/primary vm takes care of them respectively
  2613. */
  2614. if (!skip_op) {
  2615. dsi_ctrl->hw.ops.setup_lane_map(&dsi_ctrl->hw,
  2616. &dsi_ctrl->host_config.lane_map);
  2617. dsi_ctrl->hw.ops.host_setup(&dsi_ctrl->hw,
  2618. &dsi_ctrl->host_config.common_config);
  2619. if (dsi_ctrl->host_config.panel_mode == DSI_OP_CMD_MODE) {
  2620. dsi_ctrl->hw.ops.cmd_engine_setup(&dsi_ctrl->hw,
  2621. &dsi_ctrl->host_config.common_config,
  2622. &dsi_ctrl->host_config.u.cmd_engine);
  2623. dsi_ctrl->hw.ops.setup_cmd_stream(&dsi_ctrl->hw,
  2624. &dsi_ctrl->host_config.video_timing,
  2625. &dsi_ctrl->host_config.common_config,
  2626. 0x0,
  2627. NULL);
  2628. } else {
  2629. dsi_ctrl->hw.ops.video_engine_setup(&dsi_ctrl->hw,
  2630. &dsi_ctrl->host_config.common_config,
  2631. &dsi_ctrl->host_config.u.video_engine);
  2632. dsi_ctrl->hw.ops.set_video_timing(&dsi_ctrl->hw,
  2633. &dsi_ctrl->host_config.video_timing);
  2634. }
  2635. }
  2636. dsi_ctrl->hw.ops.enable_status_interrupts(&dsi_ctrl->hw, 0x0);
  2637. dsi_ctrl_enable_error_interrupts(dsi_ctrl);
  2638. DSI_CTRL_DEBUG(dsi_ctrl, "Host initialization complete, skip op: %d\n",
  2639. skip_op);
  2640. dsi_ctrl_update_state(dsi_ctrl, DSI_CTRL_OP_HOST_INIT, 0x1);
  2641. error:
  2642. mutex_unlock(&dsi_ctrl->ctrl_lock);
  2643. return rc;
  2644. }
  2645. /**
  2646. * dsi_ctrl_isr_configure() - API to register/deregister dsi isr
  2647. * @dsi_ctrl: DSI controller handle.
  2648. * @enable: variable to control register/deregister isr
  2649. */
  2650. void dsi_ctrl_isr_configure(struct dsi_ctrl *dsi_ctrl, bool enable)
  2651. {
  2652. if (!dsi_ctrl)
  2653. return;
  2654. mutex_lock(&dsi_ctrl->ctrl_lock);
  2655. if (enable)
  2656. _dsi_ctrl_setup_isr(dsi_ctrl);
  2657. else
  2658. _dsi_ctrl_destroy_isr(dsi_ctrl);
  2659. mutex_unlock(&dsi_ctrl->ctrl_lock);
  2660. }
  2661. void dsi_ctrl_hs_req_sel(struct dsi_ctrl *dsi_ctrl, bool sel_phy)
  2662. {
  2663. if (!dsi_ctrl)
  2664. return;
  2665. mutex_lock(&dsi_ctrl->ctrl_lock);
  2666. dsi_ctrl->hw.ops.hs_req_sel(&dsi_ctrl->hw, sel_phy);
  2667. mutex_unlock(&dsi_ctrl->ctrl_lock);
  2668. }
  2669. void dsi_ctrl_set_continuous_clk(struct dsi_ctrl *dsi_ctrl, bool enable)
  2670. {
  2671. if (!dsi_ctrl)
  2672. return;
  2673. mutex_lock(&dsi_ctrl->ctrl_lock);
  2674. dsi_ctrl->hw.ops.set_continuous_clk(&dsi_ctrl->hw, enable);
  2675. mutex_unlock(&dsi_ctrl->ctrl_lock);
  2676. }
  2677. int dsi_ctrl_soft_reset(struct dsi_ctrl *dsi_ctrl)
  2678. {
  2679. if (!dsi_ctrl)
  2680. return -EINVAL;
  2681. mutex_lock(&dsi_ctrl->ctrl_lock);
  2682. dsi_ctrl->hw.ops.soft_reset(&dsi_ctrl->hw);
  2683. mutex_unlock(&dsi_ctrl->ctrl_lock);
  2684. DSI_CTRL_DEBUG(dsi_ctrl, "Soft reset complete\n");
  2685. return 0;
  2686. }
  2687. int dsi_ctrl_reset(struct dsi_ctrl *dsi_ctrl, int mask)
  2688. {
  2689. int rc = 0;
  2690. if (!dsi_ctrl)
  2691. return -EINVAL;
  2692. mutex_lock(&dsi_ctrl->ctrl_lock);
  2693. rc = dsi_ctrl->hw.ops.ctrl_reset(&dsi_ctrl->hw, mask);
  2694. mutex_unlock(&dsi_ctrl->ctrl_lock);
  2695. return rc;
  2696. }
  2697. int dsi_ctrl_get_hw_version(struct dsi_ctrl *dsi_ctrl)
  2698. {
  2699. int rc = 0;
  2700. if (!dsi_ctrl)
  2701. return -EINVAL;
  2702. mutex_lock(&dsi_ctrl->ctrl_lock);
  2703. rc = dsi_ctrl->hw.ops.get_hw_version(&dsi_ctrl->hw);
  2704. mutex_unlock(&dsi_ctrl->ctrl_lock);
  2705. return rc;
  2706. }
  2707. int dsi_ctrl_vid_engine_en(struct dsi_ctrl *dsi_ctrl, bool on)
  2708. {
  2709. int rc = 0;
  2710. if (!dsi_ctrl)
  2711. return -EINVAL;
  2712. mutex_lock(&dsi_ctrl->ctrl_lock);
  2713. dsi_ctrl->hw.ops.video_engine_en(&dsi_ctrl->hw, on);
  2714. mutex_unlock(&dsi_ctrl->ctrl_lock);
  2715. return rc;
  2716. }
  2717. int dsi_ctrl_setup_avr(struct dsi_ctrl *dsi_ctrl, bool enable)
  2718. {
  2719. if (!dsi_ctrl)
  2720. return -EINVAL;
  2721. if (dsi_ctrl->host_config.panel_mode == DSI_OP_VIDEO_MODE) {
  2722. mutex_lock(&dsi_ctrl->ctrl_lock);
  2723. dsi_ctrl->hw.ops.setup_avr(&dsi_ctrl->hw, enable);
  2724. mutex_unlock(&dsi_ctrl->ctrl_lock);
  2725. }
  2726. return 0;
  2727. }
  2728. /**
  2729. * dsi_ctrl_host_deinit() - De-Initialize DSI host hardware.
  2730. * @dsi_ctrl: DSI controller handle.
  2731. *
  2732. * De-initializes DSI controller hardware. It can be performed only during
  2733. * DSI_CTRL_POWER_CORE_CLK_ON state after LINK clocks have been turned off.
  2734. *
  2735. * Return: error code.
  2736. */
  2737. int dsi_ctrl_host_deinit(struct dsi_ctrl *dsi_ctrl)
  2738. {
  2739. int rc = 0;
  2740. if (!dsi_ctrl) {
  2741. DSI_CTRL_ERR(dsi_ctrl, "Invalid params\n");
  2742. return -EINVAL;
  2743. }
  2744. mutex_lock(&dsi_ctrl->ctrl_lock);
  2745. rc = dsi_ctrl_check_state(dsi_ctrl, DSI_CTRL_OP_HOST_INIT, 0x0);
  2746. if (rc) {
  2747. DSI_CTRL_ERR(dsi_ctrl, "Controller state check failed, rc=%d\n",
  2748. rc);
  2749. DSI_CTRL_ERR(dsi_ctrl, "driver state check failed, rc=%d\n",
  2750. rc);
  2751. goto error;
  2752. }
  2753. DSI_CTRL_DEBUG(dsi_ctrl, "Host deinitization complete\n");
  2754. dsi_ctrl_update_state(dsi_ctrl, DSI_CTRL_OP_HOST_INIT, 0x0);
  2755. error:
  2756. mutex_unlock(&dsi_ctrl->ctrl_lock);
  2757. return rc;
  2758. }
  2759. /**
  2760. * dsi_ctrl_update_host_config() - update dsi host configuration
  2761. * @dsi_ctrl: DSI controller handle.
  2762. * @config: DSI host configuration.
  2763. * @flags: dsi_mode_flags modifying the behavior
  2764. *
  2765. * Updates driver with new Host configuration to use for host initialization.
  2766. * This function call will only update the software context. The stored
  2767. * configuration information will be used when the host is initialized.
  2768. *
  2769. * Return: error code.
  2770. */
  2771. int dsi_ctrl_update_host_config(struct dsi_ctrl *ctrl,
  2772. struct dsi_host_config *config,
  2773. struct dsi_display_mode *mode, int flags,
  2774. void *clk_handle)
  2775. {
  2776. int rc = 0;
  2777. if (!ctrl || !config) {
  2778. DSI_CTRL_ERR(ctrl, "Invalid params\n");
  2779. return -EINVAL;
  2780. }
  2781. mutex_lock(&ctrl->ctrl_lock);
  2782. rc = dsi_ctrl_validate_panel_info(ctrl, config);
  2783. if (rc) {
  2784. DSI_CTRL_ERR(ctrl, "panel validation failed, rc=%d\n", rc);
  2785. goto error;
  2786. }
  2787. if (!(flags & (DSI_MODE_FLAG_SEAMLESS | DSI_MODE_FLAG_VRR |
  2788. DSI_MODE_FLAG_DYN_CLK))) {
  2789. /*
  2790. * for dynamic clk switch case link frequence would
  2791. * be updated dsi_display_dynamic_clk_switch().
  2792. */
  2793. rc = dsi_ctrl_update_link_freqs(ctrl, config, clk_handle,
  2794. mode);
  2795. if (rc) {
  2796. DSI_CTRL_ERR(ctrl, "failed to update link frequency, rc=%d\n",
  2797. rc);
  2798. goto error;
  2799. }
  2800. }
  2801. DSI_CTRL_DEBUG(ctrl, "Host config updated\n");
  2802. memcpy(&ctrl->host_config, config, sizeof(ctrl->host_config));
  2803. ctrl->mode_bounds.x = ctrl->host_config.video_timing.h_active *
  2804. ctrl->horiz_index;
  2805. ctrl->mode_bounds.y = 0;
  2806. ctrl->mode_bounds.w = ctrl->host_config.video_timing.h_active;
  2807. ctrl->mode_bounds.h = ctrl->host_config.video_timing.v_active;
  2808. memcpy(&ctrl->roi, &ctrl->mode_bounds, sizeof(ctrl->mode_bounds));
  2809. ctrl->modeupdated = true;
  2810. ctrl->roi.x = 0;
  2811. error:
  2812. mutex_unlock(&ctrl->ctrl_lock);
  2813. return rc;
  2814. }
  2815. /**
  2816. * dsi_ctrl_validate_timing() - validate a video timing configuration
  2817. * @dsi_ctrl: DSI controller handle.
  2818. * @timing: Pointer to timing data.
  2819. *
  2820. * Driver will validate if the timing configuration is supported on the
  2821. * controller hardware.
  2822. *
  2823. * Return: error code if timing is not supported.
  2824. */
  2825. int dsi_ctrl_validate_timing(struct dsi_ctrl *dsi_ctrl,
  2826. struct dsi_mode_info *mode)
  2827. {
  2828. int rc = 0;
  2829. if (!dsi_ctrl || !mode) {
  2830. DSI_CTRL_ERR(dsi_ctrl, "Invalid params\n");
  2831. return -EINVAL;
  2832. }
  2833. return rc;
  2834. }
  2835. /**
  2836. * dsi_ctrl_cmd_transfer() - Transfer commands on DSI link
  2837. * @dsi_ctrl: DSI controller handle.
  2838. * @cmd: Command description to transfer on DSI link.
  2839. *
  2840. * Command transfer can be done only when command engine is enabled. The
  2841. * transfer API will block until either the command transfer finishes or
  2842. * the timeout value is reached. If the trigger is deferred, it will return
  2843. * without triggering the transfer. Command parameters are programmed to
  2844. * hardware.
  2845. *
  2846. * Return: error code.
  2847. */
  2848. int dsi_ctrl_cmd_transfer(struct dsi_ctrl *dsi_ctrl, struct dsi_cmd_desc *cmd)
  2849. {
  2850. int rc = 0;
  2851. if (!dsi_ctrl || !cmd) {
  2852. DSI_CTRL_ERR(dsi_ctrl, "Invalid params\n");
  2853. return -EINVAL;
  2854. }
  2855. mutex_lock(&dsi_ctrl->ctrl_lock);
  2856. rc = dsi_ctrl_check_state(dsi_ctrl, DSI_CTRL_OP_CMD_TX, 0x0);
  2857. if (rc) {
  2858. DSI_CTRL_ERR(dsi_ctrl, "Controller state check failed, rc=%d\n",
  2859. rc);
  2860. goto error;
  2861. }
  2862. if (cmd->ctrl_flags & DSI_CTRL_CMD_READ) {
  2863. rc = dsi_message_rx(dsi_ctrl, cmd);
  2864. if (rc <= 0)
  2865. DSI_CTRL_ERR(dsi_ctrl, "read message failed read length, rc=%d\n",
  2866. rc);
  2867. } else {
  2868. rc = dsi_message_tx(dsi_ctrl, cmd);
  2869. if (rc)
  2870. DSI_CTRL_ERR(dsi_ctrl, "command msg transfer failed, rc = %d\n",
  2871. rc);
  2872. }
  2873. dsi_ctrl_update_state(dsi_ctrl, DSI_CTRL_OP_CMD_TX, 0x0);
  2874. error:
  2875. mutex_unlock(&dsi_ctrl->ctrl_lock);
  2876. return rc;
  2877. }
  2878. /**
  2879. * dsi_ctrl_mask_overflow() - API to mask/unmask overflow error.
  2880. * @dsi_ctrl: DSI controller handle.
  2881. * @enable: variable to control masking/unmasking.
  2882. */
  2883. void dsi_ctrl_mask_overflow(struct dsi_ctrl *dsi_ctrl, bool enable)
  2884. {
  2885. struct dsi_ctrl_hw_ops dsi_hw_ops;
  2886. dsi_hw_ops = dsi_ctrl->hw.ops;
  2887. if (enable) {
  2888. if (dsi_hw_ops.mask_error_intr)
  2889. dsi_hw_ops.mask_error_intr(&dsi_ctrl->hw,
  2890. BIT(DSI_FIFO_OVERFLOW), true);
  2891. } else {
  2892. if (dsi_hw_ops.mask_error_intr && !dsi_ctrl->esd_check_underway)
  2893. dsi_hw_ops.mask_error_intr(&dsi_ctrl->hw,
  2894. BIT(DSI_FIFO_OVERFLOW), false);
  2895. }
  2896. }
  2897. /**
  2898. * dsi_ctrl_cmd_tx_trigger() - Trigger a deferred command.
  2899. * @dsi_ctrl: DSI controller handle.
  2900. * @flags: Modifiers.
  2901. *
  2902. * Return: error code.
  2903. */
  2904. int dsi_ctrl_cmd_tx_trigger(struct dsi_ctrl *dsi_ctrl, u32 flags)
  2905. {
  2906. int rc = 0;
  2907. struct dsi_ctrl_hw_ops dsi_hw_ops;
  2908. u32 v_total = 0, fps = 0, cur_line = 0, mem_latency_us = 100;
  2909. u32 line_time = 0, schedule_line = 0x1, latency_by_line = 0;
  2910. struct dsi_mode_info *timing;
  2911. unsigned long flag;
  2912. if (!dsi_ctrl) {
  2913. DSI_CTRL_ERR(dsi_ctrl, "Invalid params\n");
  2914. return -EINVAL;
  2915. }
  2916. dsi_hw_ops = dsi_ctrl->hw.ops;
  2917. SDE_EVT32(dsi_ctrl->cell_index, SDE_EVTLOG_FUNC_ENTRY, flags);
  2918. /* Dont trigger the command if this is not the last ocmmand */
  2919. if (!(flags & DSI_CTRL_CMD_LAST_COMMAND))
  2920. return rc;
  2921. mutex_lock(&dsi_ctrl->ctrl_lock);
  2922. timing = &(dsi_ctrl->host_config.video_timing);
  2923. if (timing &&
  2924. (dsi_ctrl->host_config.panel_mode == DSI_OP_VIDEO_MODE)) {
  2925. v_total = timing->v_sync_width + timing->v_back_porch +
  2926. timing->v_front_porch + timing->v_active;
  2927. fps = timing->refresh_rate;
  2928. schedule_line = calculate_schedule_line(dsi_ctrl, flags);
  2929. line_time = (1000000 / fps) / v_total;
  2930. latency_by_line = CEIL(mem_latency_us, line_time);
  2931. }
  2932. if (!(flags & DSI_CTRL_CMD_BROADCAST_MASTER)) {
  2933. dsi_hw_ops.trigger_command_dma(&dsi_ctrl->hw);
  2934. if (dsi_ctrl->enable_cmd_dma_stats) {
  2935. u32 reg = dsi_hw_ops.log_line_count(&dsi_ctrl->hw,
  2936. dsi_ctrl->cmd_mode);
  2937. dsi_ctrl->cmd_trigger_line = (reg & 0xFFFF);
  2938. dsi_ctrl->cmd_trigger_frame = ((reg >> 16) & 0xFFFF);
  2939. SDE_EVT32(dsi_ctrl->cell_index, SDE_EVTLOG_FUNC_CASE1,
  2940. dsi_ctrl->cmd_trigger_line,
  2941. dsi_ctrl->cmd_trigger_frame);
  2942. }
  2943. }
  2944. if ((flags & DSI_CTRL_CMD_BROADCAST) &&
  2945. (flags & DSI_CTRL_CMD_BROADCAST_MASTER)) {
  2946. dsi_ctrl_wait_for_video_done(dsi_ctrl);
  2947. atomic_set(&dsi_ctrl->dma_irq_trig, 0);
  2948. dsi_ctrl_enable_status_interrupt(dsi_ctrl,
  2949. DSI_SINT_CMD_MODE_DMA_DONE, NULL);
  2950. reinit_completion(&dsi_ctrl->irq_info.cmd_dma_done);
  2951. /* trigger command */
  2952. if ((dsi_ctrl->host_config.panel_mode == DSI_OP_VIDEO_MODE) &&
  2953. dsi_hw_ops.schedule_dma_cmd &&
  2954. (dsi_ctrl->current_state.vid_engine_state ==
  2955. DSI_CTRL_ENGINE_ON)) {
  2956. /*
  2957. * This change reads the video line count from
  2958. * MDP_INTF_LINE_COUNT register and checks whether
  2959. * DMA trigger happens close to the schedule line.
  2960. * If it is not close to the schedule line, then DMA
  2961. * command transfer is triggered.
  2962. */
  2963. while (1) {
  2964. local_irq_save(flag);
  2965. cur_line =
  2966. dsi_hw_ops.log_line_count(&dsi_ctrl->hw,
  2967. dsi_ctrl->cmd_mode);
  2968. if (cur_line <
  2969. (schedule_line - latency_by_line) ||
  2970. cur_line > (schedule_line + 1)) {
  2971. dsi_hw_ops.trigger_command_dma(
  2972. &dsi_ctrl->hw);
  2973. local_irq_restore(flag);
  2974. break;
  2975. }
  2976. local_irq_restore(flag);
  2977. udelay(1000);
  2978. }
  2979. } else
  2980. dsi_hw_ops.trigger_command_dma(&dsi_ctrl->hw);
  2981. if (dsi_ctrl->enable_cmd_dma_stats) {
  2982. u32 reg = dsi_hw_ops.log_line_count(&dsi_ctrl->hw,
  2983. dsi_ctrl->cmd_mode);
  2984. dsi_ctrl->cmd_trigger_line = (reg & 0xFFFF);
  2985. dsi_ctrl->cmd_trigger_frame = ((reg >> 16) & 0xFFFF);
  2986. SDE_EVT32(dsi_ctrl->cell_index, SDE_EVTLOG_FUNC_CASE1,
  2987. dsi_ctrl->cmd_trigger_line,
  2988. dsi_ctrl->cmd_trigger_frame);
  2989. }
  2990. if (flags & DSI_CTRL_CMD_ASYNC_WAIT) {
  2991. dsi_ctrl->dma_wait_queued = true;
  2992. queue_work(dsi_ctrl->dma_cmd_workq,
  2993. &dsi_ctrl->dma_cmd_wait);
  2994. } else {
  2995. dsi_ctrl->dma_wait_queued = false;
  2996. dsi_ctrl_dma_cmd_wait_for_done(&dsi_ctrl->dma_cmd_wait);
  2997. }
  2998. if (flags & DSI_CTRL_CMD_NON_EMBEDDED_MODE) {
  2999. if (dsi_ctrl->version < DSI_CTRL_VERSION_2_4)
  3000. dsi_hw_ops.soft_reset(&dsi_ctrl->hw);
  3001. dsi_ctrl->cmd_len = 0;
  3002. }
  3003. }
  3004. mutex_unlock(&dsi_ctrl->ctrl_lock);
  3005. return rc;
  3006. }
  3007. /**
  3008. * dsi_ctrl_cache_misr - Cache frame MISR value
  3009. * @dsi_ctrl: Pointer to associated dsi_ctrl structure
  3010. */
  3011. void dsi_ctrl_cache_misr(struct dsi_ctrl *dsi_ctrl)
  3012. {
  3013. u32 misr;
  3014. if (!dsi_ctrl || !dsi_ctrl->hw.ops.collect_misr)
  3015. return;
  3016. misr = dsi_ctrl->hw.ops.collect_misr(&dsi_ctrl->hw,
  3017. dsi_ctrl->host_config.panel_mode);
  3018. if (misr)
  3019. dsi_ctrl->misr_cache = misr;
  3020. DSI_CTRL_DEBUG(dsi_ctrl, "misr_cache = %x\n", dsi_ctrl->misr_cache);
  3021. }
  3022. /**
  3023. * dsi_ctrl_get_host_engine_init_state() - Return host init state
  3024. * @dsi_ctrl: DSI controller handle.
  3025. * @state: Controller initialization state
  3026. *
  3027. * Return: error code.
  3028. */
  3029. int dsi_ctrl_get_host_engine_init_state(struct dsi_ctrl *dsi_ctrl,
  3030. bool *state)
  3031. {
  3032. if (!dsi_ctrl || !state) {
  3033. DSI_CTRL_ERR(dsi_ctrl, "Invalid Params\n");
  3034. return -EINVAL;
  3035. }
  3036. mutex_lock(&dsi_ctrl->ctrl_lock);
  3037. *state = dsi_ctrl->current_state.host_initialized;
  3038. mutex_unlock(&dsi_ctrl->ctrl_lock);
  3039. return 0;
  3040. }
  3041. /**
  3042. * dsi_ctrl_set_power_state() - set power state for dsi controller
  3043. * @dsi_ctrl: DSI controller handle.
  3044. * @state: Power state.
  3045. *
  3046. * Set power state for DSI controller. Power state can be changed only when
  3047. * Controller, Video and Command engines are turned off.
  3048. *
  3049. * Return: error code.
  3050. */
  3051. int dsi_ctrl_set_power_state(struct dsi_ctrl *dsi_ctrl,
  3052. enum dsi_power_state state)
  3053. {
  3054. int rc = 0;
  3055. if (!dsi_ctrl || (state >= DSI_CTRL_POWER_MAX)) {
  3056. DSI_CTRL_ERR(dsi_ctrl, "Invalid Params\n");
  3057. return -EINVAL;
  3058. }
  3059. mutex_lock(&dsi_ctrl->ctrl_lock);
  3060. rc = dsi_ctrl_check_state(dsi_ctrl, DSI_CTRL_OP_POWER_STATE_CHANGE,
  3061. state);
  3062. if (rc) {
  3063. DSI_CTRL_ERR(dsi_ctrl, "Controller state check failed, rc=%d\n",
  3064. rc);
  3065. goto error;
  3066. }
  3067. if (state == DSI_CTRL_POWER_VREG_ON) {
  3068. rc = dsi_ctrl_enable_supplies(dsi_ctrl, true);
  3069. if (rc) {
  3070. DSI_CTRL_ERR(dsi_ctrl, "failed to enable voltage supplies, rc=%d\n",
  3071. rc);
  3072. goto error;
  3073. }
  3074. } else if (state == DSI_CTRL_POWER_VREG_OFF) {
  3075. rc = dsi_ctrl_enable_supplies(dsi_ctrl, false);
  3076. if (rc) {
  3077. DSI_CTRL_ERR(dsi_ctrl, "failed to disable vreg supplies, rc=%d\n",
  3078. rc);
  3079. goto error;
  3080. }
  3081. }
  3082. DSI_CTRL_DEBUG(dsi_ctrl, "Power state updated to %d\n", state);
  3083. dsi_ctrl_update_state(dsi_ctrl, DSI_CTRL_OP_POWER_STATE_CHANGE, state);
  3084. error:
  3085. mutex_unlock(&dsi_ctrl->ctrl_lock);
  3086. return rc;
  3087. }
  3088. /**
  3089. * dsi_ctrl_set_tpg_state() - enable/disable test pattern on the controller
  3090. * @dsi_ctrl: DSI controller handle.
  3091. * @on: enable/disable test pattern.
  3092. *
  3093. * Test pattern can be enabled only after Video engine (for video mode panels)
  3094. * or command engine (for cmd mode panels) is enabled.
  3095. *
  3096. * Return: error code.
  3097. */
  3098. int dsi_ctrl_set_tpg_state(struct dsi_ctrl *dsi_ctrl, bool on)
  3099. {
  3100. int rc = 0;
  3101. if (!dsi_ctrl) {
  3102. DSI_CTRL_ERR(dsi_ctrl, "Invalid params\n");
  3103. return -EINVAL;
  3104. }
  3105. mutex_lock(&dsi_ctrl->ctrl_lock);
  3106. rc = dsi_ctrl_check_state(dsi_ctrl, DSI_CTRL_OP_TPG, on);
  3107. if (rc) {
  3108. DSI_CTRL_ERR(dsi_ctrl, "Controller state check failed, rc=%d\n",
  3109. rc);
  3110. goto error;
  3111. }
  3112. if (on) {
  3113. if (dsi_ctrl->host_config.panel_mode == DSI_OP_VIDEO_MODE) {
  3114. dsi_ctrl->hw.ops.video_test_pattern_setup(&dsi_ctrl->hw,
  3115. DSI_TEST_PATTERN_INC,
  3116. 0xFFFF);
  3117. } else {
  3118. dsi_ctrl->hw.ops.cmd_test_pattern_setup(
  3119. &dsi_ctrl->hw,
  3120. DSI_TEST_PATTERN_INC,
  3121. 0xFFFF,
  3122. 0x0);
  3123. }
  3124. }
  3125. dsi_ctrl->hw.ops.test_pattern_enable(&dsi_ctrl->hw, on);
  3126. DSI_CTRL_DEBUG(dsi_ctrl, "Set test pattern state=%d\n", on);
  3127. dsi_ctrl_update_state(dsi_ctrl, DSI_CTRL_OP_TPG, on);
  3128. error:
  3129. mutex_unlock(&dsi_ctrl->ctrl_lock);
  3130. return rc;
  3131. }
  3132. /**
  3133. * dsi_ctrl_set_host_engine_state() - set host engine state
  3134. * @dsi_ctrl: DSI Controller handle.
  3135. * @state: Engine state.
  3136. * @skip_op: Boolean to indicate few operations can be skipped.
  3137. * Set during the cont-splash or trusted-vm enable case.
  3138. *
  3139. * Host engine state can be modified only when DSI controller power state is
  3140. * set to DSI_CTRL_POWER_LINK_CLK_ON and cmd, video engines are disabled.
  3141. *
  3142. * Return: error code.
  3143. */
  3144. int dsi_ctrl_set_host_engine_state(struct dsi_ctrl *dsi_ctrl,
  3145. enum dsi_engine_state state, bool skip_op)
  3146. {
  3147. int rc = 0;
  3148. if (!dsi_ctrl || (state >= DSI_CTRL_ENGINE_MAX)) {
  3149. DSI_CTRL_ERR(dsi_ctrl, "Invalid params\n");
  3150. return -EINVAL;
  3151. }
  3152. mutex_lock(&dsi_ctrl->ctrl_lock);
  3153. rc = dsi_ctrl_check_state(dsi_ctrl, DSI_CTRL_OP_HOST_ENGINE, state);
  3154. if (rc) {
  3155. DSI_CTRL_ERR(dsi_ctrl, "Controller state check failed, rc=%d\n",
  3156. rc);
  3157. goto error;
  3158. }
  3159. if (!skip_op) {
  3160. if (state == DSI_CTRL_ENGINE_ON)
  3161. dsi_ctrl->hw.ops.ctrl_en(&dsi_ctrl->hw, true);
  3162. else
  3163. dsi_ctrl->hw.ops.ctrl_en(&dsi_ctrl->hw, false);
  3164. }
  3165. SDE_EVT32(dsi_ctrl->cell_index, state, skip_op);
  3166. DSI_CTRL_DEBUG(dsi_ctrl, "Set host engine state = %d\n", state);
  3167. dsi_ctrl_update_state(dsi_ctrl, DSI_CTRL_OP_HOST_ENGINE, state);
  3168. error:
  3169. mutex_unlock(&dsi_ctrl->ctrl_lock);
  3170. return rc;
  3171. }
  3172. /**
  3173. * dsi_ctrl_set_cmd_engine_state() - set command engine state
  3174. * @dsi_ctrl: DSI Controller handle.
  3175. * @state: Engine state.
  3176. * @skip_op: Boolean to indicate few operations can be skipped.
  3177. * Set during the cont-splash or trusted-vm enable case.
  3178. *
  3179. * Command engine state can be modified only when DSI controller power state is
  3180. * set to DSI_CTRL_POWER_LINK_CLK_ON.
  3181. *
  3182. * Return: error code.
  3183. */
  3184. int dsi_ctrl_set_cmd_engine_state(struct dsi_ctrl *dsi_ctrl,
  3185. enum dsi_engine_state state, bool skip_op)
  3186. {
  3187. int rc = 0;
  3188. if (!dsi_ctrl || (state >= DSI_CTRL_ENGINE_MAX)) {
  3189. DSI_CTRL_ERR(dsi_ctrl, "Invalid params\n");
  3190. return -EINVAL;
  3191. }
  3192. rc = dsi_ctrl_check_state(dsi_ctrl, DSI_CTRL_OP_CMD_ENGINE, state);
  3193. if (rc) {
  3194. DSI_CTRL_ERR(dsi_ctrl, "Controller state check failed, rc=%d\n",
  3195. rc);
  3196. goto error;
  3197. }
  3198. if (!skip_op) {
  3199. if (state == DSI_CTRL_ENGINE_ON)
  3200. dsi_ctrl->hw.ops.cmd_engine_en(&dsi_ctrl->hw, true);
  3201. else
  3202. dsi_ctrl->hw.ops.cmd_engine_en(&dsi_ctrl->hw, false);
  3203. }
  3204. SDE_EVT32(dsi_ctrl->cell_index, state, skip_op);
  3205. DSI_CTRL_DEBUG(dsi_ctrl, "Set cmd engine state:%d, skip_op:%d\n",
  3206. state, skip_op);
  3207. dsi_ctrl_update_state(dsi_ctrl, DSI_CTRL_OP_CMD_ENGINE, state);
  3208. error:
  3209. return rc;
  3210. }
  3211. /**
  3212. * dsi_ctrl_set_vid_engine_state() - set video engine state
  3213. * @dsi_ctrl: DSI Controller handle.
  3214. * @state: Engine state.
  3215. * @skip_op: Boolean to indicate few operations can be skipped.
  3216. * Set during the cont-splash or trusted-vm enable case.
  3217. *
  3218. * Video engine state can be modified only when DSI controller power state is
  3219. * set to DSI_CTRL_POWER_LINK_CLK_ON.
  3220. *
  3221. * Return: error code.
  3222. */
  3223. int dsi_ctrl_set_vid_engine_state(struct dsi_ctrl *dsi_ctrl,
  3224. enum dsi_engine_state state, bool skip_op)
  3225. {
  3226. int rc = 0;
  3227. bool on;
  3228. bool vid_eng_busy;
  3229. if (!dsi_ctrl || (state >= DSI_CTRL_ENGINE_MAX)) {
  3230. DSI_CTRL_ERR(dsi_ctrl, "Invalid params\n");
  3231. return -EINVAL;
  3232. }
  3233. mutex_lock(&dsi_ctrl->ctrl_lock);
  3234. rc = dsi_ctrl_check_state(dsi_ctrl, DSI_CTRL_OP_VID_ENGINE, state);
  3235. if (rc) {
  3236. DSI_CTRL_ERR(dsi_ctrl, "Controller state check failed, rc=%d\n",
  3237. rc);
  3238. goto error;
  3239. }
  3240. if (!skip_op) {
  3241. on = (state == DSI_CTRL_ENGINE_ON) ? true : false;
  3242. dsi_ctrl->hw.ops.video_engine_en(&dsi_ctrl->hw, on);
  3243. vid_eng_busy = dsi_ctrl->hw.ops.vid_engine_busy(&dsi_ctrl->hw);
  3244. /*
  3245. * During ESD check failure, DSI video engine can get stuck
  3246. * sending data from display engine. In use cases where GDSC
  3247. * toggle does not happen like DP MST connected or secure video
  3248. * playback, display does not recover back after ESD failure.
  3249. * Perform a reset if video engine is stuck.
  3250. */
  3251. if (!on && (dsi_ctrl->version < DSI_CTRL_VERSION_1_3 ||
  3252. vid_eng_busy))
  3253. dsi_ctrl->hw.ops.soft_reset(&dsi_ctrl->hw);
  3254. }
  3255. SDE_EVT32(dsi_ctrl->cell_index, state, skip_op);
  3256. DSI_CTRL_DEBUG(dsi_ctrl, "Set video engine state:%d, skip_op:%d\n",
  3257. state, skip_op);
  3258. dsi_ctrl_update_state(dsi_ctrl, DSI_CTRL_OP_VID_ENGINE, state);
  3259. error:
  3260. mutex_unlock(&dsi_ctrl->ctrl_lock);
  3261. return rc;
  3262. }
  3263. /**
  3264. * dsi_ctrl_set_ulps() - set ULPS state for DSI lanes.
  3265. * @dsi_ctrl: DSI controller handle.
  3266. * @enable: enable/disable ULPS.
  3267. *
  3268. * ULPS can be enabled/disabled after DSI host engine is turned on.
  3269. *
  3270. * Return: error code.
  3271. */
  3272. int dsi_ctrl_set_ulps(struct dsi_ctrl *dsi_ctrl, bool enable)
  3273. {
  3274. int rc = 0;
  3275. if (!dsi_ctrl) {
  3276. DSI_CTRL_ERR(dsi_ctrl, "Invalid params\n");
  3277. return -EINVAL;
  3278. }
  3279. mutex_lock(&dsi_ctrl->ctrl_lock);
  3280. if (enable)
  3281. rc = dsi_enable_ulps(dsi_ctrl);
  3282. else
  3283. rc = dsi_disable_ulps(dsi_ctrl);
  3284. if (rc) {
  3285. DSI_CTRL_ERR(dsi_ctrl, "Ulps state change(%d) failed, rc=%d\n",
  3286. enable, rc);
  3287. goto error;
  3288. }
  3289. DSI_CTRL_DEBUG(dsi_ctrl, "ULPS state = %d\n", enable);
  3290. error:
  3291. mutex_unlock(&dsi_ctrl->ctrl_lock);
  3292. return rc;
  3293. }
  3294. /**
  3295. * dsi_ctrl_set_clamp_state() - set clamp state for DSI phy
  3296. * @dsi_ctrl: DSI controller handle.
  3297. * @enable: enable/disable clamping.
  3298. *
  3299. * Clamps can be enabled/disabled while DSI controller is still turned on.
  3300. *
  3301. * Return: error code.
  3302. */
  3303. int dsi_ctrl_set_clamp_state(struct dsi_ctrl *dsi_ctrl,
  3304. bool enable, bool ulps_enabled)
  3305. {
  3306. int rc = 0;
  3307. if (!dsi_ctrl) {
  3308. DSI_CTRL_ERR(dsi_ctrl, "Invalid params\n");
  3309. return -EINVAL;
  3310. }
  3311. if (!dsi_ctrl->hw.ops.clamp_enable ||
  3312. !dsi_ctrl->hw.ops.clamp_disable) {
  3313. DSI_CTRL_DEBUG(dsi_ctrl, "No clamp control for DSI controller\n");
  3314. return 0;
  3315. }
  3316. mutex_lock(&dsi_ctrl->ctrl_lock);
  3317. rc = dsi_enable_io_clamp(dsi_ctrl, enable, ulps_enabled);
  3318. if (rc) {
  3319. DSI_CTRL_ERR(dsi_ctrl, "Failed to enable IO clamp\n");
  3320. goto error;
  3321. }
  3322. DSI_CTRL_DEBUG(dsi_ctrl, "Clamp state = %d\n", enable);
  3323. error:
  3324. mutex_unlock(&dsi_ctrl->ctrl_lock);
  3325. return rc;
  3326. }
  3327. /**
  3328. * dsi_ctrl_set_clock_source() - set clock source fpr dsi link clocks
  3329. * @dsi_ctrl: DSI controller handle.
  3330. * @source_clks: Source clocks for DSI link clocks.
  3331. *
  3332. * Clock source should be changed while link clocks are disabled.
  3333. *
  3334. * Return: error code.
  3335. */
  3336. int dsi_ctrl_set_clock_source(struct dsi_ctrl *dsi_ctrl,
  3337. struct dsi_clk_link_set *source_clks)
  3338. {
  3339. int rc = 0;
  3340. if (!dsi_ctrl || !source_clks) {
  3341. DSI_CTRL_ERR(dsi_ctrl, "Invalid params\n");
  3342. return -EINVAL;
  3343. }
  3344. mutex_lock(&dsi_ctrl->ctrl_lock);
  3345. rc = dsi_clk_update_parent(source_clks, &dsi_ctrl->clk_info.rcg_clks);
  3346. if (rc) {
  3347. DSI_CTRL_ERR(dsi_ctrl, "Failed to update link clk parent, rc=%d\n",
  3348. rc);
  3349. (void)dsi_clk_update_parent(&dsi_ctrl->clk_info.pll_op_clks,
  3350. &dsi_ctrl->clk_info.rcg_clks);
  3351. goto error;
  3352. }
  3353. dsi_ctrl->clk_info.pll_op_clks.byte_clk = source_clks->byte_clk;
  3354. dsi_ctrl->clk_info.pll_op_clks.pixel_clk = source_clks->pixel_clk;
  3355. DSI_CTRL_DEBUG(dsi_ctrl, "Source clocks are updated\n");
  3356. error:
  3357. mutex_unlock(&dsi_ctrl->ctrl_lock);
  3358. return rc;
  3359. }
  3360. /**
  3361. * dsi_ctrl_setup_misr() - Setup frame MISR
  3362. * @dsi_ctrl: DSI controller handle.
  3363. * @enable: enable/disable MISR.
  3364. * @frame_count: Number of frames to accumulate MISR.
  3365. *
  3366. * Return: error code.
  3367. */
  3368. int dsi_ctrl_setup_misr(struct dsi_ctrl *dsi_ctrl,
  3369. bool enable,
  3370. u32 frame_count)
  3371. {
  3372. if (!dsi_ctrl) {
  3373. DSI_CTRL_ERR(dsi_ctrl, "Invalid params\n");
  3374. return -EINVAL;
  3375. }
  3376. if (!dsi_ctrl->hw.ops.setup_misr)
  3377. return 0;
  3378. mutex_lock(&dsi_ctrl->ctrl_lock);
  3379. dsi_ctrl->misr_enable = enable;
  3380. dsi_ctrl->hw.ops.setup_misr(&dsi_ctrl->hw,
  3381. dsi_ctrl->host_config.panel_mode,
  3382. enable, frame_count);
  3383. mutex_unlock(&dsi_ctrl->ctrl_lock);
  3384. return 0;
  3385. }
  3386. /**
  3387. * dsi_ctrl_collect_misr() - Read frame MISR
  3388. * @dsi_ctrl: DSI controller handle.
  3389. *
  3390. * Return: MISR value.
  3391. */
  3392. u32 dsi_ctrl_collect_misr(struct dsi_ctrl *dsi_ctrl)
  3393. {
  3394. u32 misr;
  3395. if (!dsi_ctrl || !dsi_ctrl->hw.ops.collect_misr)
  3396. return 0;
  3397. misr = dsi_ctrl->hw.ops.collect_misr(&dsi_ctrl->hw,
  3398. dsi_ctrl->host_config.panel_mode);
  3399. if (!misr)
  3400. misr = dsi_ctrl->misr_cache;
  3401. DSI_CTRL_DEBUG(dsi_ctrl, "cached misr = %x, final = %x\n",
  3402. dsi_ctrl->misr_cache, misr);
  3403. return misr;
  3404. }
  3405. void dsi_ctrl_mask_error_status_interrupts(struct dsi_ctrl *dsi_ctrl, u32 idx,
  3406. bool mask_enable)
  3407. {
  3408. if (!dsi_ctrl || !dsi_ctrl->hw.ops.error_intr_ctrl
  3409. || !dsi_ctrl->hw.ops.clear_error_status) {
  3410. DSI_CTRL_ERR(dsi_ctrl, "Invalid params\n");
  3411. return;
  3412. }
  3413. /*
  3414. * Mask DSI error status interrupts and clear error status
  3415. * register
  3416. */
  3417. mutex_lock(&dsi_ctrl->ctrl_lock);
  3418. if (idx & BIT(DSI_ERR_INTR_ALL)) {
  3419. /*
  3420. * The behavior of mask_enable is different in ctrl register
  3421. * and mask register and hence mask_enable is manipulated for
  3422. * selective error interrupt masking vs total error interrupt
  3423. * masking.
  3424. */
  3425. dsi_ctrl->hw.ops.error_intr_ctrl(&dsi_ctrl->hw, !mask_enable);
  3426. dsi_ctrl->hw.ops.clear_error_status(&dsi_ctrl->hw,
  3427. DSI_ERROR_INTERRUPT_COUNT);
  3428. } else {
  3429. dsi_ctrl->hw.ops.mask_error_intr(&dsi_ctrl->hw, idx,
  3430. mask_enable);
  3431. dsi_ctrl->hw.ops.clear_error_status(&dsi_ctrl->hw,
  3432. DSI_ERROR_INTERRUPT_COUNT);
  3433. }
  3434. mutex_unlock(&dsi_ctrl->ctrl_lock);
  3435. }
  3436. /**
  3437. * dsi_ctrl_irq_update() - Put a irq vote to process DSI error
  3438. * interrupts at any time.
  3439. * @dsi_ctrl: DSI controller handle.
  3440. * @enable: variable to enable/disable irq
  3441. */
  3442. void dsi_ctrl_irq_update(struct dsi_ctrl *dsi_ctrl, bool enable)
  3443. {
  3444. if (!dsi_ctrl)
  3445. return;
  3446. mutex_lock(&dsi_ctrl->ctrl_lock);
  3447. if (enable)
  3448. dsi_ctrl_enable_status_interrupt(dsi_ctrl,
  3449. DSI_SINT_ERROR, NULL);
  3450. else
  3451. dsi_ctrl_disable_status_interrupt(dsi_ctrl,
  3452. DSI_SINT_ERROR);
  3453. mutex_unlock(&dsi_ctrl->ctrl_lock);
  3454. }
  3455. /**
  3456. * dsi_ctrl_wait4dynamic_refresh_done() - Poll for dynamci refresh
  3457. * done interrupt.
  3458. * @dsi_ctrl: DSI controller handle.
  3459. */
  3460. int dsi_ctrl_wait4dynamic_refresh_done(struct dsi_ctrl *ctrl)
  3461. {
  3462. int rc = 0;
  3463. if (!ctrl)
  3464. return 0;
  3465. mutex_lock(&ctrl->ctrl_lock);
  3466. if (ctrl->hw.ops.wait4dynamic_refresh_done)
  3467. rc = ctrl->hw.ops.wait4dynamic_refresh_done(&ctrl->hw);
  3468. mutex_unlock(&ctrl->ctrl_lock);
  3469. return rc;
  3470. }
  3471. /**
  3472. * dsi_ctrl_drv_register() - register platform driver for dsi controller
  3473. */
  3474. void dsi_ctrl_drv_register(void)
  3475. {
  3476. platform_driver_register(&dsi_ctrl_driver);
  3477. }
  3478. /**
  3479. * dsi_ctrl_drv_unregister() - unregister platform driver
  3480. */
  3481. void dsi_ctrl_drv_unregister(void)
  3482. {
  3483. platform_driver_unregister(&dsi_ctrl_driver);
  3484. }