dsi_ctrl.c 90 KB

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