dsi_ctrl.c 90 KB

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