dsi_ctrl.c 94 KB

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