dsi_ctrl.c 90 KB

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