dsi_host.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/delay.h>
  7. #include <linux/dma-mapping.h>
  8. #include <linux/err.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <linux/of.h>
  12. #include <linux/of_graph.h>
  13. #include <linux/of_irq.h>
  14. #include <linux/pinctrl/consumer.h>
  15. #include <linux/pm_opp.h>
  16. #include <linux/regmap.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <linux/spinlock.h>
  19. #include <video/mipi_display.h>
  20. #include <drm/display/drm_dsc_helper.h>
  21. #include <drm/drm_of.h>
  22. #include "dsi.h"
  23. #include "dsi.xml.h"
  24. #include "sfpb.xml.h"
  25. #include "dsi_cfg.h"
  26. #include "msm_dsc_helper.h"
  27. #include "msm_kms.h"
  28. #include "msm_gem.h"
  29. #include "phy/dsi_phy.h"
  30. #define DSI_RESET_TOGGLE_DELAY_MS 20
  31. static int dsi_populate_dsc_params(struct msm_dsi_host *msm_host, struct drm_dsc_config *dsc);
  32. static int dsi_get_version(const void __iomem *base, u32 *major, u32 *minor)
  33. {
  34. u32 ver;
  35. if (!major || !minor)
  36. return -EINVAL;
  37. /*
  38. * From DSI6G(v3), addition of a 6G_HW_VERSION register at offset 0
  39. * makes all other registers 4-byte shifted down.
  40. *
  41. * In order to identify between DSI6G(v3) and beyond, and DSIv2 and
  42. * older, we read the DSI_VERSION register without any shift(offset
  43. * 0x1f0). In the case of DSIv2, this hast to be a non-zero value. In
  44. * the case of DSI6G, this has to be zero (the offset points to a
  45. * scratch register which we never touch)
  46. */
  47. ver = readl(base + REG_DSI_VERSION);
  48. if (ver) {
  49. /* older dsi host, there is no register shift */
  50. ver = FIELD(ver, DSI_VERSION_MAJOR);
  51. if (ver <= MSM_DSI_VER_MAJOR_V2) {
  52. /* old versions */
  53. *major = ver;
  54. *minor = 0;
  55. return 0;
  56. } else {
  57. return -EINVAL;
  58. }
  59. } else {
  60. /*
  61. * newer host, offset 0 has 6G_HW_VERSION, the rest of the
  62. * registers are shifted down, read DSI_VERSION again with
  63. * the shifted offset
  64. */
  65. ver = readl(base + DSI_6G_REG_SHIFT + REG_DSI_VERSION);
  66. ver = FIELD(ver, DSI_VERSION_MAJOR);
  67. if (ver == MSM_DSI_VER_MAJOR_6G) {
  68. /* 6G version */
  69. *major = ver;
  70. *minor = readl(base + REG_DSI_6G_HW_VERSION);
  71. return 0;
  72. } else {
  73. return -EINVAL;
  74. }
  75. }
  76. }
  77. #define DSI_ERR_STATE_ACK 0x0000
  78. #define DSI_ERR_STATE_TIMEOUT 0x0001
  79. #define DSI_ERR_STATE_DLN0_PHY 0x0002
  80. #define DSI_ERR_STATE_FIFO 0x0004
  81. #define DSI_ERR_STATE_MDP_FIFO_UNDERFLOW 0x0008
  82. #define DSI_ERR_STATE_INTERLEAVE_OP_CONTENTION 0x0010
  83. #define DSI_ERR_STATE_PLL_UNLOCKED 0x0020
  84. #define DSI_CLK_CTRL_ENABLE_CLKS \
  85. (DSI_CLK_CTRL_AHBS_HCLK_ON | DSI_CLK_CTRL_AHBM_SCLK_ON | \
  86. DSI_CLK_CTRL_PCLK_ON | DSI_CLK_CTRL_DSICLK_ON | \
  87. DSI_CLK_CTRL_BYTECLK_ON | DSI_CLK_CTRL_ESCCLK_ON | \
  88. DSI_CLK_CTRL_FORCE_ON_DYN_AHBM_HCLK)
  89. struct msm_dsi_host {
  90. struct mipi_dsi_host base;
  91. struct platform_device *pdev;
  92. struct drm_device *dev;
  93. int id;
  94. void __iomem *ctrl_base;
  95. phys_addr_t ctrl_size;
  96. struct regulator_bulk_data *supplies;
  97. int num_bus_clks;
  98. struct clk_bulk_data bus_clks[DSI_BUS_CLK_MAX];
  99. struct clk *byte_clk;
  100. struct clk *esc_clk;
  101. struct clk *pixel_clk;
  102. struct clk *byte_intf_clk;
  103. unsigned long byte_clk_rate;
  104. unsigned long byte_intf_clk_rate;
  105. unsigned long pixel_clk_rate;
  106. unsigned long esc_clk_rate;
  107. /* DSI v2 specific clocks */
  108. struct clk *src_clk;
  109. unsigned long src_clk_rate;
  110. const struct msm_dsi_cfg_handler *cfg_hnd;
  111. struct completion dma_comp;
  112. struct completion video_comp;
  113. struct mutex dev_mutex;
  114. struct mutex cmd_mutex;
  115. spinlock_t intr_lock; /* Protect interrupt ctrl register */
  116. u32 err_work_state;
  117. struct work_struct err_work;
  118. struct workqueue_struct *workqueue;
  119. /* DSI 6G TX buffer*/
  120. struct drm_gem_object *tx_gem_obj;
  121. struct msm_gem_address_space *aspace;
  122. /* DSI v2 TX buffer */
  123. void *tx_buf;
  124. dma_addr_t tx_buf_paddr;
  125. int tx_size;
  126. u8 *rx_buf;
  127. struct regmap *sfpb;
  128. struct drm_display_mode *mode;
  129. struct drm_dsc_config *dsc;
  130. /* connected device info */
  131. unsigned int channel;
  132. unsigned int lanes;
  133. enum mipi_dsi_pixel_format format;
  134. unsigned long mode_flags;
  135. /* lane data parsed via DT */
  136. int dlane_swap;
  137. int num_data_lanes;
  138. /* from phy DT */
  139. bool cphy_mode;
  140. u32 dma_cmd_ctrl_restore;
  141. bool registered;
  142. bool power_on;
  143. bool enabled;
  144. int irq;
  145. };
  146. static inline u32 dsi_read(struct msm_dsi_host *msm_host, u32 reg)
  147. {
  148. return readl(msm_host->ctrl_base + reg);
  149. }
  150. static inline void dsi_write(struct msm_dsi_host *msm_host, u32 reg, u32 data)
  151. {
  152. writel(data, msm_host->ctrl_base + reg);
  153. }
  154. static const struct msm_dsi_cfg_handler *dsi_get_config(
  155. struct msm_dsi_host *msm_host)
  156. {
  157. const struct msm_dsi_cfg_handler *cfg_hnd = NULL;
  158. struct device *dev = &msm_host->pdev->dev;
  159. struct clk *ahb_clk;
  160. int ret;
  161. u32 major = 0, minor = 0;
  162. ahb_clk = msm_clk_get(msm_host->pdev, "iface");
  163. if (IS_ERR(ahb_clk)) {
  164. pr_err("%s: cannot get interface clock\n", __func__);
  165. goto exit;
  166. }
  167. pm_runtime_get_sync(dev);
  168. ret = clk_prepare_enable(ahb_clk);
  169. if (ret) {
  170. pr_err("%s: unable to enable ahb_clk\n", __func__);
  171. goto runtime_put;
  172. }
  173. ret = dsi_get_version(msm_host->ctrl_base, &major, &minor);
  174. if (ret) {
  175. pr_err("%s: Invalid version\n", __func__);
  176. goto disable_clks;
  177. }
  178. cfg_hnd = msm_dsi_cfg_get(major, minor);
  179. DBG("%s: Version %x:%x\n", __func__, major, minor);
  180. disable_clks:
  181. clk_disable_unprepare(ahb_clk);
  182. runtime_put:
  183. pm_runtime_put_sync(dev);
  184. exit:
  185. return cfg_hnd;
  186. }
  187. static inline struct msm_dsi_host *to_msm_dsi_host(struct mipi_dsi_host *host)
  188. {
  189. return container_of(host, struct msm_dsi_host, base);
  190. }
  191. int dsi_clk_init_v2(struct msm_dsi_host *msm_host)
  192. {
  193. struct platform_device *pdev = msm_host->pdev;
  194. int ret = 0;
  195. msm_host->src_clk = msm_clk_get(pdev, "src");
  196. if (IS_ERR(msm_host->src_clk)) {
  197. ret = PTR_ERR(msm_host->src_clk);
  198. pr_err("%s: can't find src clock. ret=%d\n",
  199. __func__, ret);
  200. msm_host->src_clk = NULL;
  201. return ret;
  202. }
  203. return ret;
  204. }
  205. int dsi_clk_init_6g_v2(struct msm_dsi_host *msm_host)
  206. {
  207. struct platform_device *pdev = msm_host->pdev;
  208. int ret = 0;
  209. msm_host->byte_intf_clk = msm_clk_get(pdev, "byte_intf");
  210. if (IS_ERR(msm_host->byte_intf_clk)) {
  211. ret = PTR_ERR(msm_host->byte_intf_clk);
  212. pr_err("%s: can't find byte_intf clock. ret=%d\n",
  213. __func__, ret);
  214. }
  215. return ret;
  216. }
  217. static int dsi_clk_init(struct msm_dsi_host *msm_host)
  218. {
  219. struct platform_device *pdev = msm_host->pdev;
  220. const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
  221. const struct msm_dsi_config *cfg = cfg_hnd->cfg;
  222. int i, ret = 0;
  223. /* get bus clocks */
  224. for (i = 0; i < cfg->num_bus_clks; i++)
  225. msm_host->bus_clks[i].id = cfg->bus_clk_names[i];
  226. msm_host->num_bus_clks = cfg->num_bus_clks;
  227. ret = devm_clk_bulk_get(&pdev->dev, msm_host->num_bus_clks, msm_host->bus_clks);
  228. if (ret < 0) {
  229. dev_err(&pdev->dev, "Unable to get clocks, ret = %d\n", ret);
  230. goto exit;
  231. }
  232. /* get link and source clocks */
  233. msm_host->byte_clk = msm_clk_get(pdev, "byte");
  234. if (IS_ERR(msm_host->byte_clk)) {
  235. ret = PTR_ERR(msm_host->byte_clk);
  236. pr_err("%s: can't find dsi_byte clock. ret=%d\n",
  237. __func__, ret);
  238. msm_host->byte_clk = NULL;
  239. goto exit;
  240. }
  241. msm_host->pixel_clk = msm_clk_get(pdev, "pixel");
  242. if (IS_ERR(msm_host->pixel_clk)) {
  243. ret = PTR_ERR(msm_host->pixel_clk);
  244. pr_err("%s: can't find dsi_pixel clock. ret=%d\n",
  245. __func__, ret);
  246. msm_host->pixel_clk = NULL;
  247. goto exit;
  248. }
  249. msm_host->esc_clk = msm_clk_get(pdev, "core");
  250. if (IS_ERR(msm_host->esc_clk)) {
  251. ret = PTR_ERR(msm_host->esc_clk);
  252. pr_err("%s: can't find dsi_esc clock. ret=%d\n",
  253. __func__, ret);
  254. msm_host->esc_clk = NULL;
  255. goto exit;
  256. }
  257. if (cfg_hnd->ops->clk_init_ver)
  258. ret = cfg_hnd->ops->clk_init_ver(msm_host);
  259. exit:
  260. return ret;
  261. }
  262. int msm_dsi_runtime_suspend(struct device *dev)
  263. {
  264. struct platform_device *pdev = to_platform_device(dev);
  265. struct msm_dsi *msm_dsi = platform_get_drvdata(pdev);
  266. struct mipi_dsi_host *host = msm_dsi->host;
  267. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  268. if (!msm_host->cfg_hnd)
  269. return 0;
  270. clk_bulk_disable_unprepare(msm_host->num_bus_clks, msm_host->bus_clks);
  271. return 0;
  272. }
  273. int msm_dsi_runtime_resume(struct device *dev)
  274. {
  275. struct platform_device *pdev = to_platform_device(dev);
  276. struct msm_dsi *msm_dsi = platform_get_drvdata(pdev);
  277. struct mipi_dsi_host *host = msm_dsi->host;
  278. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  279. if (!msm_host->cfg_hnd)
  280. return 0;
  281. return clk_bulk_prepare_enable(msm_host->num_bus_clks, msm_host->bus_clks);
  282. }
  283. int dsi_link_clk_set_rate_6g(struct msm_dsi_host *msm_host)
  284. {
  285. int ret;
  286. DBG("Set clk rates: pclk=%lu, byteclk=%lu",
  287. msm_host->pixel_clk_rate, msm_host->byte_clk_rate);
  288. ret = dev_pm_opp_set_rate(&msm_host->pdev->dev,
  289. msm_host->byte_clk_rate);
  290. if (ret) {
  291. pr_err("%s: dev_pm_opp_set_rate failed %d\n", __func__, ret);
  292. return ret;
  293. }
  294. ret = clk_set_rate(msm_host->pixel_clk, msm_host->pixel_clk_rate);
  295. if (ret) {
  296. pr_err("%s: Failed to set rate pixel clk, %d\n", __func__, ret);
  297. return ret;
  298. }
  299. if (msm_host->byte_intf_clk) {
  300. ret = clk_set_rate(msm_host->byte_intf_clk, msm_host->byte_intf_clk_rate);
  301. if (ret) {
  302. pr_err("%s: Failed to set rate byte intf clk, %d\n",
  303. __func__, ret);
  304. return ret;
  305. }
  306. }
  307. return 0;
  308. }
  309. int dsi_link_clk_enable_6g(struct msm_dsi_host *msm_host)
  310. {
  311. int ret;
  312. ret = clk_prepare_enable(msm_host->esc_clk);
  313. if (ret) {
  314. pr_err("%s: Failed to enable dsi esc clk\n", __func__);
  315. goto error;
  316. }
  317. ret = clk_prepare_enable(msm_host->byte_clk);
  318. if (ret) {
  319. pr_err("%s: Failed to enable dsi byte clk\n", __func__);
  320. goto byte_clk_err;
  321. }
  322. ret = clk_prepare_enable(msm_host->pixel_clk);
  323. if (ret) {
  324. pr_err("%s: Failed to enable dsi pixel clk\n", __func__);
  325. goto pixel_clk_err;
  326. }
  327. ret = clk_prepare_enable(msm_host->byte_intf_clk);
  328. if (ret) {
  329. pr_err("%s: Failed to enable byte intf clk\n",
  330. __func__);
  331. goto byte_intf_clk_err;
  332. }
  333. return 0;
  334. byte_intf_clk_err:
  335. clk_disable_unprepare(msm_host->pixel_clk);
  336. pixel_clk_err:
  337. clk_disable_unprepare(msm_host->byte_clk);
  338. byte_clk_err:
  339. clk_disable_unprepare(msm_host->esc_clk);
  340. error:
  341. return ret;
  342. }
  343. int dsi_link_clk_set_rate_v2(struct msm_dsi_host *msm_host)
  344. {
  345. int ret;
  346. DBG("Set clk rates: pclk=%lu, byteclk=%lu, esc_clk=%lu, dsi_src_clk=%lu",
  347. msm_host->pixel_clk_rate, msm_host->byte_clk_rate,
  348. msm_host->esc_clk_rate, msm_host->src_clk_rate);
  349. ret = clk_set_rate(msm_host->byte_clk, msm_host->byte_clk_rate);
  350. if (ret) {
  351. pr_err("%s: Failed to set rate byte clk, %d\n", __func__, ret);
  352. return ret;
  353. }
  354. ret = clk_set_rate(msm_host->esc_clk, msm_host->esc_clk_rate);
  355. if (ret) {
  356. pr_err("%s: Failed to set rate esc clk, %d\n", __func__, ret);
  357. return ret;
  358. }
  359. ret = clk_set_rate(msm_host->src_clk, msm_host->src_clk_rate);
  360. if (ret) {
  361. pr_err("%s: Failed to set rate src clk, %d\n", __func__, ret);
  362. return ret;
  363. }
  364. ret = clk_set_rate(msm_host->pixel_clk, msm_host->pixel_clk_rate);
  365. if (ret) {
  366. pr_err("%s: Failed to set rate pixel clk, %d\n", __func__, ret);
  367. return ret;
  368. }
  369. return 0;
  370. }
  371. int dsi_link_clk_enable_v2(struct msm_dsi_host *msm_host)
  372. {
  373. int ret;
  374. ret = clk_prepare_enable(msm_host->byte_clk);
  375. if (ret) {
  376. pr_err("%s: Failed to enable dsi byte clk\n", __func__);
  377. goto error;
  378. }
  379. ret = clk_prepare_enable(msm_host->esc_clk);
  380. if (ret) {
  381. pr_err("%s: Failed to enable dsi esc clk\n", __func__);
  382. goto esc_clk_err;
  383. }
  384. ret = clk_prepare_enable(msm_host->src_clk);
  385. if (ret) {
  386. pr_err("%s: Failed to enable dsi src clk\n", __func__);
  387. goto src_clk_err;
  388. }
  389. ret = clk_prepare_enable(msm_host->pixel_clk);
  390. if (ret) {
  391. pr_err("%s: Failed to enable dsi pixel clk\n", __func__);
  392. goto pixel_clk_err;
  393. }
  394. return 0;
  395. pixel_clk_err:
  396. clk_disable_unprepare(msm_host->src_clk);
  397. src_clk_err:
  398. clk_disable_unprepare(msm_host->esc_clk);
  399. esc_clk_err:
  400. clk_disable_unprepare(msm_host->byte_clk);
  401. error:
  402. return ret;
  403. }
  404. void dsi_link_clk_disable_6g(struct msm_dsi_host *msm_host)
  405. {
  406. /* Drop the performance state vote */
  407. dev_pm_opp_set_rate(&msm_host->pdev->dev, 0);
  408. clk_disable_unprepare(msm_host->esc_clk);
  409. clk_disable_unprepare(msm_host->pixel_clk);
  410. clk_disable_unprepare(msm_host->byte_intf_clk);
  411. clk_disable_unprepare(msm_host->byte_clk);
  412. }
  413. void dsi_link_clk_disable_v2(struct msm_dsi_host *msm_host)
  414. {
  415. clk_disable_unprepare(msm_host->pixel_clk);
  416. clk_disable_unprepare(msm_host->src_clk);
  417. clk_disable_unprepare(msm_host->esc_clk);
  418. clk_disable_unprepare(msm_host->byte_clk);
  419. }
  420. /**
  421. * dsi_adjust_pclk_for_compression() - Adjust the pclk rate for compression case
  422. * @mode: The selected mode for the DSI output
  423. * @dsc: DRM DSC configuration for this DSI output
  424. *
  425. * Adjust the pclk rate by calculating a new hdisplay proportional to
  426. * the compression ratio such that:
  427. * new_hdisplay = old_hdisplay * compressed_bpp / uncompressed_bpp
  428. *
  429. * Porches do not need to be adjusted:
  430. * - For VIDEO mode they are not compressed by DSC and are passed as is.
  431. * - For CMD mode there are no actual porches. Instead these fields
  432. * currently represent the overhead to the image data transfer. As such, they
  433. * are calculated for the final mode parameters (after the compression) and
  434. * are not to be adjusted too.
  435. *
  436. * FIXME: Reconsider this if/when CMD mode handling is rewritten to use
  437. * transfer time and data overhead as a starting point of the calculations.
  438. */
  439. static unsigned long dsi_adjust_pclk_for_compression(const struct drm_display_mode *mode,
  440. const struct drm_dsc_config *dsc)
  441. {
  442. int new_hdisplay = DIV_ROUND_UP(mode->hdisplay * drm_dsc_get_bpp_int(dsc),
  443. dsc->bits_per_component * 3);
  444. int new_htotal = mode->htotal - mode->hdisplay + new_hdisplay;
  445. return mult_frac(mode->clock * 1000u, new_htotal, mode->htotal);
  446. }
  447. static unsigned long dsi_get_pclk_rate(const struct drm_display_mode *mode,
  448. const struct drm_dsc_config *dsc, bool is_bonded_dsi)
  449. {
  450. unsigned long pclk_rate;
  451. pclk_rate = mode->clock * 1000u;
  452. if (dsc)
  453. pclk_rate = dsi_adjust_pclk_for_compression(mode, dsc);
  454. /*
  455. * For bonded DSI mode, the current DRM mode has the complete width of the
  456. * panel. Since, the complete panel is driven by two DSI controllers,
  457. * the clock rates have to be split between the two dsi controllers.
  458. * Adjust the byte and pixel clock rates for each dsi host accordingly.
  459. */
  460. if (is_bonded_dsi)
  461. pclk_rate /= 2;
  462. return pclk_rate;
  463. }
  464. unsigned long dsi_byte_clk_get_rate(struct mipi_dsi_host *host, bool is_bonded_dsi,
  465. const struct drm_display_mode *mode)
  466. {
  467. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  468. u8 lanes = msm_host->lanes;
  469. u32 bpp = mipi_dsi_pixel_format_to_bpp(msm_host->format);
  470. unsigned long pclk_rate = dsi_get_pclk_rate(mode, msm_host->dsc, is_bonded_dsi);
  471. unsigned long pclk_bpp;
  472. if (lanes == 0) {
  473. pr_err("%s: forcing mdss_dsi lanes to 1\n", __func__);
  474. lanes = 1;
  475. }
  476. /* CPHY "byte_clk" is in units of 16 bits */
  477. if (msm_host->cphy_mode)
  478. pclk_bpp = mult_frac(pclk_rate, bpp, 16 * lanes);
  479. else
  480. pclk_bpp = mult_frac(pclk_rate, bpp, 8 * lanes);
  481. return pclk_bpp;
  482. }
  483. static void dsi_calc_pclk(struct msm_dsi_host *msm_host, bool is_bonded_dsi)
  484. {
  485. msm_host->pixel_clk_rate = dsi_get_pclk_rate(msm_host->mode, msm_host->dsc, is_bonded_dsi);
  486. msm_host->byte_clk_rate = dsi_byte_clk_get_rate(&msm_host->base, is_bonded_dsi,
  487. msm_host->mode);
  488. DBG("pclk=%lu, bclk=%lu", msm_host->pixel_clk_rate,
  489. msm_host->byte_clk_rate);
  490. }
  491. int dsi_calc_clk_rate_6g(struct msm_dsi_host *msm_host, bool is_bonded_dsi)
  492. {
  493. if (!msm_host->mode) {
  494. pr_err("%s: mode not set\n", __func__);
  495. return -EINVAL;
  496. }
  497. dsi_calc_pclk(msm_host, is_bonded_dsi);
  498. msm_host->esc_clk_rate = clk_get_rate(msm_host->esc_clk);
  499. return 0;
  500. }
  501. int dsi_calc_clk_rate_v2(struct msm_dsi_host *msm_host, bool is_bonded_dsi)
  502. {
  503. u32 bpp = mipi_dsi_pixel_format_to_bpp(msm_host->format);
  504. unsigned int esc_mhz, esc_div;
  505. unsigned long byte_mhz;
  506. dsi_calc_pclk(msm_host, is_bonded_dsi);
  507. msm_host->src_clk_rate = mult_frac(msm_host->pixel_clk_rate, bpp, 8);
  508. /*
  509. * esc clock is byte clock followed by a 4 bit divider,
  510. * we need to find an escape clock frequency within the
  511. * mipi DSI spec range within the maximum divider limit
  512. * We iterate here between an escape clock frequencey
  513. * between 20 Mhz to 5 Mhz and pick up the first one
  514. * that can be supported by our divider
  515. */
  516. byte_mhz = msm_host->byte_clk_rate / 1000000;
  517. for (esc_mhz = 20; esc_mhz >= 5; esc_mhz--) {
  518. esc_div = DIV_ROUND_UP(byte_mhz, esc_mhz);
  519. /*
  520. * TODO: Ideally, we shouldn't know what sort of divider
  521. * is available in mmss_cc, we're just assuming that
  522. * it'll always be a 4 bit divider. Need to come up with
  523. * a better way here.
  524. */
  525. if (esc_div >= 1 && esc_div <= 16)
  526. break;
  527. }
  528. if (esc_mhz < 5)
  529. return -EINVAL;
  530. msm_host->esc_clk_rate = msm_host->byte_clk_rate / esc_div;
  531. DBG("esc=%lu, src=%lu", msm_host->esc_clk_rate,
  532. msm_host->src_clk_rate);
  533. return 0;
  534. }
  535. static void dsi_intr_ctrl(struct msm_dsi_host *msm_host, u32 mask, int enable)
  536. {
  537. u32 intr;
  538. unsigned long flags;
  539. spin_lock_irqsave(&msm_host->intr_lock, flags);
  540. intr = dsi_read(msm_host, REG_DSI_INTR_CTRL);
  541. if (enable)
  542. intr |= mask;
  543. else
  544. intr &= ~mask;
  545. DBG("intr=%x enable=%d", intr, enable);
  546. dsi_write(msm_host, REG_DSI_INTR_CTRL, intr);
  547. spin_unlock_irqrestore(&msm_host->intr_lock, flags);
  548. }
  549. static inline enum dsi_traffic_mode dsi_get_traffic_mode(const u32 mode_flags)
  550. {
  551. if (mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
  552. return BURST_MODE;
  553. else if (mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
  554. return NON_BURST_SYNCH_PULSE;
  555. return NON_BURST_SYNCH_EVENT;
  556. }
  557. static inline enum dsi_vid_dst_format dsi_get_vid_fmt(
  558. const enum mipi_dsi_pixel_format mipi_fmt)
  559. {
  560. switch (mipi_fmt) {
  561. case MIPI_DSI_FMT_RGB888: return VID_DST_FORMAT_RGB888;
  562. case MIPI_DSI_FMT_RGB666: return VID_DST_FORMAT_RGB666_LOOSE;
  563. case MIPI_DSI_FMT_RGB666_PACKED: return VID_DST_FORMAT_RGB666;
  564. case MIPI_DSI_FMT_RGB565: return VID_DST_FORMAT_RGB565;
  565. default: return VID_DST_FORMAT_RGB888;
  566. }
  567. }
  568. static inline enum dsi_cmd_dst_format dsi_get_cmd_fmt(
  569. const enum mipi_dsi_pixel_format mipi_fmt)
  570. {
  571. switch (mipi_fmt) {
  572. case MIPI_DSI_FMT_RGB888: return CMD_DST_FORMAT_RGB888;
  573. case MIPI_DSI_FMT_RGB666_PACKED:
  574. case MIPI_DSI_FMT_RGB666: return CMD_DST_FORMAT_RGB666;
  575. case MIPI_DSI_FMT_RGB565: return CMD_DST_FORMAT_RGB565;
  576. default: return CMD_DST_FORMAT_RGB888;
  577. }
  578. }
  579. static void dsi_ctrl_disable(struct msm_dsi_host *msm_host)
  580. {
  581. dsi_write(msm_host, REG_DSI_CTRL, 0);
  582. }
  583. bool msm_dsi_host_is_wide_bus_enabled(struct mipi_dsi_host *host)
  584. {
  585. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  586. return msm_host->dsc &&
  587. (msm_host->cfg_hnd->major == MSM_DSI_VER_MAJOR_6G &&
  588. msm_host->cfg_hnd->minor >= MSM_DSI_6G_VER_MINOR_V2_5_0);
  589. }
  590. static void dsi_ctrl_enable(struct msm_dsi_host *msm_host,
  591. struct msm_dsi_phy_shared_timings *phy_shared_timings, struct msm_dsi_phy *phy)
  592. {
  593. u32 flags = msm_host->mode_flags;
  594. enum mipi_dsi_pixel_format mipi_fmt = msm_host->format;
  595. const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
  596. u32 data = 0, lane_ctrl = 0;
  597. if (flags & MIPI_DSI_MODE_VIDEO) {
  598. if (flags & MIPI_DSI_MODE_VIDEO_HSE)
  599. data |= DSI_VID_CFG0_PULSE_MODE_HSA_HE;
  600. if (flags & MIPI_DSI_MODE_VIDEO_NO_HFP)
  601. data |= DSI_VID_CFG0_HFP_POWER_STOP;
  602. if (flags & MIPI_DSI_MODE_VIDEO_NO_HBP)
  603. data |= DSI_VID_CFG0_HBP_POWER_STOP;
  604. if (flags & MIPI_DSI_MODE_VIDEO_NO_HSA)
  605. data |= DSI_VID_CFG0_HSA_POWER_STOP;
  606. /* Always set low power stop mode for BLLP
  607. * to let command engine send packets
  608. */
  609. data |= DSI_VID_CFG0_EOF_BLLP_POWER_STOP |
  610. DSI_VID_CFG0_BLLP_POWER_STOP;
  611. data |= DSI_VID_CFG0_TRAFFIC_MODE(dsi_get_traffic_mode(flags));
  612. data |= DSI_VID_CFG0_DST_FORMAT(dsi_get_vid_fmt(mipi_fmt));
  613. data |= DSI_VID_CFG0_VIRT_CHANNEL(msm_host->channel);
  614. if (msm_dsi_host_is_wide_bus_enabled(&msm_host->base))
  615. data |= DSI_VID_CFG0_DATABUS_WIDEN;
  616. dsi_write(msm_host, REG_DSI_VID_CFG0, data);
  617. /* Do not swap RGB colors */
  618. data = DSI_VID_CFG1_RGB_SWAP(SWAP_RGB);
  619. dsi_write(msm_host, REG_DSI_VID_CFG1, 0);
  620. } else {
  621. /* Do not swap RGB colors */
  622. data = DSI_CMD_CFG0_RGB_SWAP(SWAP_RGB);
  623. data |= DSI_CMD_CFG0_DST_FORMAT(dsi_get_cmd_fmt(mipi_fmt));
  624. dsi_write(msm_host, REG_DSI_CMD_CFG0, data);
  625. data = DSI_CMD_CFG1_WR_MEM_START(MIPI_DCS_WRITE_MEMORY_START) |
  626. DSI_CMD_CFG1_WR_MEM_CONTINUE(
  627. MIPI_DCS_WRITE_MEMORY_CONTINUE);
  628. /* Always insert DCS command */
  629. data |= DSI_CMD_CFG1_INSERT_DCS_COMMAND;
  630. dsi_write(msm_host, REG_DSI_CMD_CFG1, data);
  631. if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) {
  632. data = dsi_read(msm_host, REG_DSI_CMD_MODE_MDP_CTRL2);
  633. if (cfg_hnd->minor >= MSM_DSI_6G_VER_MINOR_V1_3)
  634. data |= DSI_CMD_MODE_MDP_CTRL2_BURST_MODE;
  635. if (msm_dsi_host_is_wide_bus_enabled(&msm_host->base))
  636. data |= DSI_CMD_MODE_MDP_CTRL2_DATABUS_WIDEN;
  637. dsi_write(msm_host, REG_DSI_CMD_MODE_MDP_CTRL2, data);
  638. }
  639. }
  640. dsi_write(msm_host, REG_DSI_CMD_DMA_CTRL,
  641. DSI_CMD_DMA_CTRL_FROM_FRAME_BUFFER |
  642. DSI_CMD_DMA_CTRL_LOW_POWER);
  643. data = 0;
  644. /* Always assume dedicated TE pin */
  645. data |= DSI_TRIG_CTRL_TE;
  646. data |= DSI_TRIG_CTRL_MDP_TRIGGER(TRIGGER_NONE);
  647. data |= DSI_TRIG_CTRL_DMA_TRIGGER(TRIGGER_SW);
  648. data |= DSI_TRIG_CTRL_STREAM(msm_host->channel);
  649. if ((cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) &&
  650. (cfg_hnd->minor >= MSM_DSI_6G_VER_MINOR_V1_2))
  651. data |= DSI_TRIG_CTRL_BLOCK_DMA_WITHIN_FRAME;
  652. dsi_write(msm_host, REG_DSI_TRIG_CTRL, data);
  653. data = DSI_CLKOUT_TIMING_CTRL_T_CLK_POST(phy_shared_timings->clk_post) |
  654. DSI_CLKOUT_TIMING_CTRL_T_CLK_PRE(phy_shared_timings->clk_pre);
  655. dsi_write(msm_host, REG_DSI_CLKOUT_TIMING_CTRL, data);
  656. if ((cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) &&
  657. (cfg_hnd->minor > MSM_DSI_6G_VER_MINOR_V1_0) &&
  658. phy_shared_timings->clk_pre_inc_by_2)
  659. dsi_write(msm_host, REG_DSI_T_CLK_PRE_EXTEND,
  660. DSI_T_CLK_PRE_EXTEND_INC_BY_2_BYTECLK);
  661. data = 0;
  662. if (!(flags & MIPI_DSI_MODE_NO_EOT_PACKET))
  663. data |= DSI_EOT_PACKET_CTRL_TX_EOT_APPEND;
  664. dsi_write(msm_host, REG_DSI_EOT_PACKET_CTRL, data);
  665. /* allow only ack-err-status to generate interrupt */
  666. dsi_write(msm_host, REG_DSI_ERR_INT_MASK0, 0x13ff3fe0);
  667. dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 1);
  668. dsi_write(msm_host, REG_DSI_CLK_CTRL, DSI_CLK_CTRL_ENABLE_CLKS);
  669. data = DSI_CTRL_CLK_EN;
  670. DBG("lane number=%d", msm_host->lanes);
  671. data |= ((DSI_CTRL_LANE0 << msm_host->lanes) - DSI_CTRL_LANE0);
  672. dsi_write(msm_host, REG_DSI_LANE_SWAP_CTRL,
  673. DSI_LANE_SWAP_CTRL_DLN_SWAP_SEL(msm_host->dlane_swap));
  674. if (!(flags & MIPI_DSI_CLOCK_NON_CONTINUOUS)) {
  675. lane_ctrl = dsi_read(msm_host, REG_DSI_LANE_CTRL);
  676. if (msm_dsi_phy_set_continuous_clock(phy, true))
  677. lane_ctrl &= ~DSI_LANE_CTRL_HS_REQ_SEL_PHY;
  678. dsi_write(msm_host, REG_DSI_LANE_CTRL,
  679. lane_ctrl | DSI_LANE_CTRL_CLKLN_HS_FORCE_REQUEST);
  680. }
  681. data |= DSI_CTRL_ENABLE;
  682. dsi_write(msm_host, REG_DSI_CTRL, data);
  683. if (msm_host->cphy_mode)
  684. dsi_write(msm_host, REG_DSI_CPHY_MODE_CTRL, BIT(0));
  685. }
  686. static void dsi_update_dsc_timing(struct msm_dsi_host *msm_host, bool is_cmd_mode)
  687. {
  688. struct drm_dsc_config *dsc = msm_host->dsc;
  689. u32 reg, reg_ctrl, reg_ctrl2;
  690. u32 slice_per_intf, total_bytes_per_intf;
  691. u32 pkt_per_line;
  692. u32 eol_byte_num;
  693. u32 bytes_per_pkt;
  694. /* first calculate dsc parameters and then program
  695. * compress mode registers
  696. */
  697. slice_per_intf = dsc->slice_count;
  698. total_bytes_per_intf = dsc->slice_chunk_size * slice_per_intf;
  699. bytes_per_pkt = dsc->slice_chunk_size; /* * slice_per_pkt; */
  700. eol_byte_num = total_bytes_per_intf % 3;
  701. /*
  702. * Typically, pkt_per_line = slice_per_intf * slice_per_pkt.
  703. *
  704. * Since the current driver only supports slice_per_pkt = 1,
  705. * pkt_per_line will be equal to slice per intf for now.
  706. */
  707. pkt_per_line = slice_per_intf;
  708. if (is_cmd_mode) /* packet data type */
  709. reg = DSI_COMMAND_COMPRESSION_MODE_CTRL_STREAM0_DATATYPE(MIPI_DSI_DCS_LONG_WRITE);
  710. else
  711. reg = DSI_VIDEO_COMPRESSION_MODE_CTRL_DATATYPE(MIPI_DSI_COMPRESSED_PIXEL_STREAM);
  712. /* DSI_VIDEO_COMPRESSION_MODE & DSI_COMMAND_COMPRESSION_MODE
  713. * registers have similar offsets, so for below common code use
  714. * DSI_VIDEO_COMPRESSION_MODE_XXXX for setting bits
  715. *
  716. * pkt_per_line is log2 encoded, >>1 works for supported values (1,2,4)
  717. */
  718. if (pkt_per_line > 4)
  719. drm_warn_once(msm_host->dev, "pkt_per_line too big");
  720. reg |= DSI_VIDEO_COMPRESSION_MODE_CTRL_PKT_PER_LINE(pkt_per_line >> 1);
  721. reg |= DSI_VIDEO_COMPRESSION_MODE_CTRL_EOL_BYTE_NUM(eol_byte_num);
  722. reg |= DSI_VIDEO_COMPRESSION_MODE_CTRL_EN;
  723. if (is_cmd_mode) {
  724. reg_ctrl = dsi_read(msm_host, REG_DSI_COMMAND_COMPRESSION_MODE_CTRL);
  725. reg_ctrl2 = dsi_read(msm_host, REG_DSI_COMMAND_COMPRESSION_MODE_CTRL2);
  726. reg_ctrl &= ~0xffff;
  727. reg_ctrl |= reg;
  728. reg_ctrl2 &= ~DSI_COMMAND_COMPRESSION_MODE_CTRL2_STREAM0_SLICE_WIDTH__MASK;
  729. reg_ctrl2 |= DSI_COMMAND_COMPRESSION_MODE_CTRL2_STREAM0_SLICE_WIDTH(dsc->slice_chunk_size);
  730. dsi_write(msm_host, REG_DSI_COMMAND_COMPRESSION_MODE_CTRL, reg_ctrl);
  731. dsi_write(msm_host, REG_DSI_COMMAND_COMPRESSION_MODE_CTRL2, reg_ctrl2);
  732. } else {
  733. reg |= DSI_VIDEO_COMPRESSION_MODE_CTRL_WC(bytes_per_pkt);
  734. dsi_write(msm_host, REG_DSI_VIDEO_COMPRESSION_MODE_CTRL, reg);
  735. }
  736. }
  737. static void dsi_timing_setup(struct msm_dsi_host *msm_host, bool is_bonded_dsi)
  738. {
  739. struct drm_display_mode *mode = msm_host->mode;
  740. u32 hs_start = 0, vs_start = 0; /* take sync start as 0 */
  741. u32 h_total = mode->htotal;
  742. u32 v_total = mode->vtotal;
  743. u32 hs_end = mode->hsync_end - mode->hsync_start;
  744. u32 vs_end = mode->vsync_end - mode->vsync_start;
  745. u32 ha_start = h_total - mode->hsync_start;
  746. u32 ha_end = ha_start + mode->hdisplay;
  747. u32 va_start = v_total - mode->vsync_start;
  748. u32 va_end = va_start + mode->vdisplay;
  749. u32 hdisplay = mode->hdisplay;
  750. u32 wc;
  751. int ret;
  752. bool wide_bus_enabled = msm_dsi_host_is_wide_bus_enabled(&msm_host->base);
  753. DBG("");
  754. /*
  755. * For bonded DSI mode, the current DRM mode has
  756. * the complete width of the panel. Since, the complete
  757. * panel is driven by two DSI controllers, the horizontal
  758. * timings have to be split between the two dsi controllers.
  759. * Adjust the DSI host timing values accordingly.
  760. */
  761. if (is_bonded_dsi) {
  762. h_total /= 2;
  763. hs_end /= 2;
  764. ha_start /= 2;
  765. ha_end /= 2;
  766. hdisplay /= 2;
  767. }
  768. if (msm_host->dsc) {
  769. struct drm_dsc_config *dsc = msm_host->dsc;
  770. u32 bytes_per_pclk;
  771. /* update dsc params with timing params */
  772. if (!dsc || !mode->hdisplay || !mode->vdisplay) {
  773. pr_err("DSI: invalid input: pic_width: %d pic_height: %d\n",
  774. mode->hdisplay, mode->vdisplay);
  775. return;
  776. }
  777. dsc->pic_width = mode->hdisplay;
  778. dsc->pic_height = mode->vdisplay;
  779. DBG("Mode %dx%d\n", dsc->pic_width, dsc->pic_height);
  780. /* we do the calculations for dsc parameters here so that
  781. * panel can use these parameters
  782. */
  783. ret = dsi_populate_dsc_params(msm_host, dsc);
  784. if (ret)
  785. return;
  786. /*
  787. * DPU sends 3 bytes per pclk cycle to DSI. If widebus is
  788. * enabled, bus width is extended to 6 bytes.
  789. *
  790. * Calculate the number of pclks needed to transmit one line of
  791. * the compressed data.
  792. * The back/font porch and pulse width are kept intact. For
  793. * VIDEO mode they represent timing parameters rather than
  794. * actual data transfer, see the documentation for
  795. * dsi_adjust_pclk_for_compression(). For CMD mode they are
  796. * unused anyway.
  797. */
  798. h_total -= hdisplay;
  799. if (wide_bus_enabled && !(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO))
  800. bytes_per_pclk = 6;
  801. else
  802. bytes_per_pclk = 3;
  803. hdisplay = DIV_ROUND_UP(msm_dsc_get_bytes_per_line(msm_host->dsc), bytes_per_pclk);
  804. h_total += hdisplay;
  805. ha_end = ha_start + hdisplay;
  806. }
  807. if (msm_host->mode_flags & MIPI_DSI_MODE_VIDEO) {
  808. if (msm_host->dsc)
  809. dsi_update_dsc_timing(msm_host, false);
  810. dsi_write(msm_host, REG_DSI_ACTIVE_H,
  811. DSI_ACTIVE_H_START(ha_start) |
  812. DSI_ACTIVE_H_END(ha_end));
  813. dsi_write(msm_host, REG_DSI_ACTIVE_V,
  814. DSI_ACTIVE_V_START(va_start) |
  815. DSI_ACTIVE_V_END(va_end));
  816. dsi_write(msm_host, REG_DSI_TOTAL,
  817. DSI_TOTAL_H_TOTAL(h_total - 1) |
  818. DSI_TOTAL_V_TOTAL(v_total - 1));
  819. dsi_write(msm_host, REG_DSI_ACTIVE_HSYNC,
  820. DSI_ACTIVE_HSYNC_START(hs_start) |
  821. DSI_ACTIVE_HSYNC_END(hs_end));
  822. dsi_write(msm_host, REG_DSI_ACTIVE_VSYNC_HPOS, 0);
  823. dsi_write(msm_host, REG_DSI_ACTIVE_VSYNC_VPOS,
  824. DSI_ACTIVE_VSYNC_VPOS_START(vs_start) |
  825. DSI_ACTIVE_VSYNC_VPOS_END(vs_end));
  826. } else { /* command mode */
  827. if (msm_host->dsc)
  828. dsi_update_dsc_timing(msm_host, true);
  829. /* image data and 1 byte write_memory_start cmd */
  830. if (!msm_host->dsc)
  831. wc = hdisplay * mipi_dsi_pixel_format_to_bpp(msm_host->format) / 8 + 1;
  832. else
  833. /*
  834. * When DSC is enabled, WC = slice_chunk_size * slice_per_pkt + 1.
  835. * Currently, the driver only supports default value of slice_per_pkt = 1
  836. *
  837. * TODO: Expand mipi_dsi_device struct to hold slice_per_pkt info
  838. * and adjust DSC math to account for slice_per_pkt.
  839. */
  840. wc = msm_host->dsc->slice_chunk_size + 1;
  841. dsi_write(msm_host, REG_DSI_CMD_MDP_STREAM0_CTRL,
  842. DSI_CMD_MDP_STREAM0_CTRL_WORD_COUNT(wc) |
  843. DSI_CMD_MDP_STREAM0_CTRL_VIRTUAL_CHANNEL(
  844. msm_host->channel) |
  845. DSI_CMD_MDP_STREAM0_CTRL_DATA_TYPE(
  846. MIPI_DSI_DCS_LONG_WRITE));
  847. dsi_write(msm_host, REG_DSI_CMD_MDP_STREAM0_TOTAL,
  848. DSI_CMD_MDP_STREAM0_TOTAL_H_TOTAL(hdisplay) |
  849. DSI_CMD_MDP_STREAM0_TOTAL_V_TOTAL(mode->vdisplay));
  850. }
  851. }
  852. static void dsi_sw_reset(struct msm_dsi_host *msm_host)
  853. {
  854. u32 ctrl;
  855. ctrl = dsi_read(msm_host, REG_DSI_CTRL);
  856. if (ctrl & DSI_CTRL_ENABLE) {
  857. dsi_write(msm_host, REG_DSI_CTRL, ctrl & ~DSI_CTRL_ENABLE);
  858. /*
  859. * dsi controller need to be disabled before
  860. * clocks turned on
  861. */
  862. wmb();
  863. }
  864. dsi_write(msm_host, REG_DSI_CLK_CTRL, DSI_CLK_CTRL_ENABLE_CLKS);
  865. wmb(); /* clocks need to be enabled before reset */
  866. /* dsi controller can only be reset while clocks are running */
  867. dsi_write(msm_host, REG_DSI_RESET, 1);
  868. msleep(DSI_RESET_TOGGLE_DELAY_MS); /* make sure reset happen */
  869. dsi_write(msm_host, REG_DSI_RESET, 0);
  870. wmb(); /* controller out of reset */
  871. if (ctrl & DSI_CTRL_ENABLE) {
  872. dsi_write(msm_host, REG_DSI_CTRL, ctrl);
  873. wmb(); /* make sure dsi controller enabled again */
  874. }
  875. }
  876. static void dsi_op_mode_config(struct msm_dsi_host *msm_host,
  877. bool video_mode, bool enable)
  878. {
  879. u32 dsi_ctrl;
  880. dsi_ctrl = dsi_read(msm_host, REG_DSI_CTRL);
  881. if (!enable) {
  882. dsi_ctrl &= ~(DSI_CTRL_ENABLE | DSI_CTRL_VID_MODE_EN |
  883. DSI_CTRL_CMD_MODE_EN);
  884. dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_MDP_DONE |
  885. DSI_IRQ_MASK_VIDEO_DONE, 0);
  886. } else {
  887. if (video_mode) {
  888. dsi_ctrl |= DSI_CTRL_VID_MODE_EN;
  889. } else { /* command mode */
  890. dsi_ctrl |= DSI_CTRL_CMD_MODE_EN;
  891. dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_MDP_DONE, 1);
  892. }
  893. dsi_ctrl |= DSI_CTRL_ENABLE;
  894. }
  895. dsi_write(msm_host, REG_DSI_CTRL, dsi_ctrl);
  896. }
  897. static void dsi_set_tx_power_mode(int mode, struct msm_dsi_host *msm_host)
  898. {
  899. u32 data;
  900. data = dsi_read(msm_host, REG_DSI_CMD_DMA_CTRL);
  901. if (mode == 0)
  902. data &= ~DSI_CMD_DMA_CTRL_LOW_POWER;
  903. else
  904. data |= DSI_CMD_DMA_CTRL_LOW_POWER;
  905. dsi_write(msm_host, REG_DSI_CMD_DMA_CTRL, data);
  906. }
  907. static void dsi_wait4video_done(struct msm_dsi_host *msm_host)
  908. {
  909. u32 ret = 0;
  910. struct device *dev = &msm_host->pdev->dev;
  911. dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_VIDEO_DONE, 1);
  912. reinit_completion(&msm_host->video_comp);
  913. ret = wait_for_completion_timeout(&msm_host->video_comp,
  914. msecs_to_jiffies(70));
  915. if (ret == 0)
  916. DRM_DEV_ERROR(dev, "wait for video done timed out\n");
  917. dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_VIDEO_DONE, 0);
  918. }
  919. static void dsi_wait4video_eng_busy(struct msm_dsi_host *msm_host)
  920. {
  921. u32 data;
  922. if (!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO))
  923. return;
  924. data = dsi_read(msm_host, REG_DSI_STATUS0);
  925. /* if video mode engine is not busy, its because
  926. * either timing engine was not turned on or the
  927. * DSI controller has finished transmitting the video
  928. * data already, so no need to wait in those cases
  929. */
  930. if (!(data & DSI_STATUS0_VIDEO_MODE_ENGINE_BUSY))
  931. return;
  932. if (msm_host->power_on && msm_host->enabled) {
  933. dsi_wait4video_done(msm_host);
  934. /* delay 4 ms to skip BLLP */
  935. usleep_range(2000, 4000);
  936. }
  937. }
  938. int dsi_tx_buf_alloc_6g(struct msm_dsi_host *msm_host, int size)
  939. {
  940. struct drm_device *dev = msm_host->dev;
  941. struct msm_drm_private *priv = dev->dev_private;
  942. uint64_t iova;
  943. u8 *data;
  944. msm_host->aspace = msm_gem_address_space_get(priv->kms->aspace);
  945. data = msm_gem_kernel_new(dev, size, MSM_BO_WC,
  946. msm_host->aspace,
  947. &msm_host->tx_gem_obj, &iova);
  948. if (IS_ERR(data)) {
  949. msm_host->tx_gem_obj = NULL;
  950. return PTR_ERR(data);
  951. }
  952. msm_gem_object_set_name(msm_host->tx_gem_obj, "tx_gem");
  953. msm_host->tx_size = msm_host->tx_gem_obj->size;
  954. return 0;
  955. }
  956. int dsi_tx_buf_alloc_v2(struct msm_dsi_host *msm_host, int size)
  957. {
  958. struct drm_device *dev = msm_host->dev;
  959. msm_host->tx_buf = dma_alloc_coherent(dev->dev, size,
  960. &msm_host->tx_buf_paddr, GFP_KERNEL);
  961. if (!msm_host->tx_buf)
  962. return -ENOMEM;
  963. msm_host->tx_size = size;
  964. return 0;
  965. }
  966. void msm_dsi_tx_buf_free(struct mipi_dsi_host *host)
  967. {
  968. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  969. struct drm_device *dev = msm_host->dev;
  970. /*
  971. * This is possible if we're tearing down before we've had a chance to
  972. * fully initialize. A very real possibility if our probe is deferred,
  973. * in which case we'll hit msm_dsi_host_destroy() without having run
  974. * through the dsi_tx_buf_alloc().
  975. */
  976. if (!dev)
  977. return;
  978. if (msm_host->tx_gem_obj) {
  979. msm_gem_kernel_put(msm_host->tx_gem_obj, msm_host->aspace);
  980. msm_gem_address_space_put(msm_host->aspace);
  981. msm_host->tx_gem_obj = NULL;
  982. msm_host->aspace = NULL;
  983. }
  984. if (msm_host->tx_buf)
  985. dma_free_coherent(dev->dev, msm_host->tx_size, msm_host->tx_buf,
  986. msm_host->tx_buf_paddr);
  987. }
  988. void *dsi_tx_buf_get_6g(struct msm_dsi_host *msm_host)
  989. {
  990. return msm_gem_get_vaddr(msm_host->tx_gem_obj);
  991. }
  992. void *dsi_tx_buf_get_v2(struct msm_dsi_host *msm_host)
  993. {
  994. return msm_host->tx_buf;
  995. }
  996. void dsi_tx_buf_put_6g(struct msm_dsi_host *msm_host)
  997. {
  998. msm_gem_put_vaddr(msm_host->tx_gem_obj);
  999. }
  1000. /*
  1001. * prepare cmd buffer to be txed
  1002. */
  1003. static int dsi_cmd_dma_add(struct msm_dsi_host *msm_host,
  1004. const struct mipi_dsi_msg *msg)
  1005. {
  1006. const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
  1007. struct mipi_dsi_packet packet;
  1008. int len;
  1009. int ret;
  1010. u8 *data;
  1011. ret = mipi_dsi_create_packet(&packet, msg);
  1012. if (ret) {
  1013. pr_err("%s: create packet failed, %d\n", __func__, ret);
  1014. return ret;
  1015. }
  1016. len = (packet.size + 3) & (~0x3);
  1017. if (len > msm_host->tx_size) {
  1018. pr_err("%s: packet size is too big\n", __func__);
  1019. return -EINVAL;
  1020. }
  1021. data = cfg_hnd->ops->tx_buf_get(msm_host);
  1022. if (IS_ERR(data)) {
  1023. ret = PTR_ERR(data);
  1024. pr_err("%s: get vaddr failed, %d\n", __func__, ret);
  1025. return ret;
  1026. }
  1027. /* MSM specific command format in memory */
  1028. data[0] = packet.header[1];
  1029. data[1] = packet.header[2];
  1030. data[2] = packet.header[0];
  1031. data[3] = BIT(7); /* Last packet */
  1032. if (mipi_dsi_packet_format_is_long(msg->type))
  1033. data[3] |= BIT(6);
  1034. if (msg->rx_buf && msg->rx_len)
  1035. data[3] |= BIT(5);
  1036. /* Long packet */
  1037. if (packet.payload && packet.payload_length)
  1038. memcpy(data + 4, packet.payload, packet.payload_length);
  1039. /* Append 0xff to the end */
  1040. if (packet.size < len)
  1041. memset(data + packet.size, 0xff, len - packet.size);
  1042. if (cfg_hnd->ops->tx_buf_put)
  1043. cfg_hnd->ops->tx_buf_put(msm_host);
  1044. return len;
  1045. }
  1046. /*
  1047. * dsi_short_read1_resp: 1 parameter
  1048. */
  1049. static int dsi_short_read1_resp(u8 *buf, const struct mipi_dsi_msg *msg)
  1050. {
  1051. u8 *data = msg->rx_buf;
  1052. if (data && (msg->rx_len >= 1)) {
  1053. *data = buf[1]; /* strip out dcs type */
  1054. return 1;
  1055. } else {
  1056. pr_err("%s: read data does not match with rx_buf len %zu\n",
  1057. __func__, msg->rx_len);
  1058. return -EINVAL;
  1059. }
  1060. }
  1061. /*
  1062. * dsi_short_read2_resp: 2 parameter
  1063. */
  1064. static int dsi_short_read2_resp(u8 *buf, const struct mipi_dsi_msg *msg)
  1065. {
  1066. u8 *data = msg->rx_buf;
  1067. if (data && (msg->rx_len >= 2)) {
  1068. data[0] = buf[1]; /* strip out dcs type */
  1069. data[1] = buf[2];
  1070. return 2;
  1071. } else {
  1072. pr_err("%s: read data does not match with rx_buf len %zu\n",
  1073. __func__, msg->rx_len);
  1074. return -EINVAL;
  1075. }
  1076. }
  1077. static int dsi_long_read_resp(u8 *buf, const struct mipi_dsi_msg *msg)
  1078. {
  1079. /* strip out 4 byte dcs header */
  1080. if (msg->rx_buf && msg->rx_len)
  1081. memcpy(msg->rx_buf, buf + 4, msg->rx_len);
  1082. return msg->rx_len;
  1083. }
  1084. int dsi_dma_base_get_6g(struct msm_dsi_host *msm_host, uint64_t *dma_base)
  1085. {
  1086. struct drm_device *dev = msm_host->dev;
  1087. struct msm_drm_private *priv = dev->dev_private;
  1088. if (!dma_base)
  1089. return -EINVAL;
  1090. return msm_gem_get_and_pin_iova(msm_host->tx_gem_obj,
  1091. priv->kms->aspace, dma_base);
  1092. }
  1093. int dsi_dma_base_get_v2(struct msm_dsi_host *msm_host, uint64_t *dma_base)
  1094. {
  1095. if (!dma_base)
  1096. return -EINVAL;
  1097. *dma_base = msm_host->tx_buf_paddr;
  1098. return 0;
  1099. }
  1100. static int dsi_cmd_dma_tx(struct msm_dsi_host *msm_host, int len)
  1101. {
  1102. const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
  1103. int ret;
  1104. uint64_t dma_base;
  1105. bool triggered;
  1106. ret = cfg_hnd->ops->dma_base_get(msm_host, &dma_base);
  1107. if (ret) {
  1108. pr_err("%s: failed to get iova: %d\n", __func__, ret);
  1109. return ret;
  1110. }
  1111. reinit_completion(&msm_host->dma_comp);
  1112. dsi_wait4video_eng_busy(msm_host);
  1113. triggered = msm_dsi_manager_cmd_xfer_trigger(
  1114. msm_host->id, dma_base, len);
  1115. if (triggered) {
  1116. ret = wait_for_completion_timeout(&msm_host->dma_comp,
  1117. msecs_to_jiffies(200));
  1118. DBG("ret=%d", ret);
  1119. if (ret == 0)
  1120. ret = -ETIMEDOUT;
  1121. else
  1122. ret = len;
  1123. } else
  1124. ret = len;
  1125. return ret;
  1126. }
  1127. static int dsi_cmd_dma_rx(struct msm_dsi_host *msm_host,
  1128. u8 *buf, int rx_byte, int pkt_size)
  1129. {
  1130. u32 *temp, data;
  1131. int i, j = 0, cnt;
  1132. u32 read_cnt;
  1133. u8 reg[16];
  1134. int repeated_bytes = 0;
  1135. int buf_offset = buf - msm_host->rx_buf;
  1136. temp = (u32 *)reg;
  1137. cnt = (rx_byte + 3) >> 2;
  1138. if (cnt > 4)
  1139. cnt = 4; /* 4 x 32 bits registers only */
  1140. if (rx_byte == 4)
  1141. read_cnt = 4;
  1142. else
  1143. read_cnt = pkt_size + 6;
  1144. /*
  1145. * In case of multiple reads from the panel, after the first read, there
  1146. * is possibility that there are some bytes in the payload repeating in
  1147. * the RDBK_DATA registers. Since we read all the parameters from the
  1148. * panel right from the first byte for every pass. We need to skip the
  1149. * repeating bytes and then append the new parameters to the rx buffer.
  1150. */
  1151. if (read_cnt > 16) {
  1152. int bytes_shifted;
  1153. /* Any data more than 16 bytes will be shifted out.
  1154. * The temp read buffer should already contain these bytes.
  1155. * The remaining bytes in read buffer are the repeated bytes.
  1156. */
  1157. bytes_shifted = read_cnt - 16;
  1158. repeated_bytes = buf_offset - bytes_shifted;
  1159. }
  1160. for (i = cnt - 1; i >= 0; i--) {
  1161. data = dsi_read(msm_host, REG_DSI_RDBK_DATA(i));
  1162. *temp++ = ntohl(data); /* to host byte order */
  1163. DBG("data = 0x%x and ntohl(data) = 0x%x", data, ntohl(data));
  1164. }
  1165. for (i = repeated_bytes; i < 16; i++)
  1166. buf[j++] = reg[i];
  1167. return j;
  1168. }
  1169. static int dsi_cmds2buf_tx(struct msm_dsi_host *msm_host,
  1170. const struct mipi_dsi_msg *msg)
  1171. {
  1172. int len, ret;
  1173. int bllp_len = msm_host->mode->hdisplay *
  1174. mipi_dsi_pixel_format_to_bpp(msm_host->format) / 8;
  1175. len = dsi_cmd_dma_add(msm_host, msg);
  1176. if (len < 0) {
  1177. pr_err("%s: failed to add cmd type = 0x%x\n",
  1178. __func__, msg->type);
  1179. return len;
  1180. }
  1181. /* for video mode, do not send cmds more than
  1182. * one pixel line, since it only transmit it
  1183. * during BLLP.
  1184. */
  1185. /* TODO: if the command is sent in LP mode, the bit rate is only
  1186. * half of esc clk rate. In this case, if the video is already
  1187. * actively streaming, we need to check more carefully if the
  1188. * command can be fit into one BLLP.
  1189. */
  1190. if ((msm_host->mode_flags & MIPI_DSI_MODE_VIDEO) && (len > bllp_len)) {
  1191. pr_err("%s: cmd cannot fit into BLLP period, len=%d\n",
  1192. __func__, len);
  1193. return -EINVAL;
  1194. }
  1195. ret = dsi_cmd_dma_tx(msm_host, len);
  1196. if (ret < 0) {
  1197. pr_err("%s: cmd dma tx failed, type=0x%x, data0=0x%x, len=%d, ret=%d\n",
  1198. __func__, msg->type, (*(u8 *)(msg->tx_buf)), len, ret);
  1199. return ret;
  1200. } else if (ret < len) {
  1201. pr_err("%s: cmd dma tx failed, type=0x%x, data0=0x%x, ret=%d len=%d\n",
  1202. __func__, msg->type, (*(u8 *)(msg->tx_buf)), ret, len);
  1203. return -EIO;
  1204. }
  1205. return len;
  1206. }
  1207. static void dsi_err_worker(struct work_struct *work)
  1208. {
  1209. struct msm_dsi_host *msm_host =
  1210. container_of(work, struct msm_dsi_host, err_work);
  1211. u32 status = msm_host->err_work_state;
  1212. pr_err_ratelimited("%s: status=%x\n", __func__, status);
  1213. if (status & DSI_ERR_STATE_MDP_FIFO_UNDERFLOW)
  1214. dsi_sw_reset(msm_host);
  1215. /* It is safe to clear here because error irq is disabled. */
  1216. msm_host->err_work_state = 0;
  1217. /* enable dsi error interrupt */
  1218. dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 1);
  1219. }
  1220. static void dsi_ack_err_status(struct msm_dsi_host *msm_host)
  1221. {
  1222. u32 status;
  1223. status = dsi_read(msm_host, REG_DSI_ACK_ERR_STATUS);
  1224. if (status) {
  1225. dsi_write(msm_host, REG_DSI_ACK_ERR_STATUS, status);
  1226. /* Writing of an extra 0 needed to clear error bits */
  1227. dsi_write(msm_host, REG_DSI_ACK_ERR_STATUS, 0);
  1228. msm_host->err_work_state |= DSI_ERR_STATE_ACK;
  1229. }
  1230. }
  1231. static void dsi_timeout_status(struct msm_dsi_host *msm_host)
  1232. {
  1233. u32 status;
  1234. status = dsi_read(msm_host, REG_DSI_TIMEOUT_STATUS);
  1235. if (status) {
  1236. dsi_write(msm_host, REG_DSI_TIMEOUT_STATUS, status);
  1237. msm_host->err_work_state |= DSI_ERR_STATE_TIMEOUT;
  1238. }
  1239. }
  1240. static void dsi_dln0_phy_err(struct msm_dsi_host *msm_host)
  1241. {
  1242. u32 status;
  1243. status = dsi_read(msm_host, REG_DSI_DLN0_PHY_ERR);
  1244. if (status & (DSI_DLN0_PHY_ERR_DLN0_ERR_ESC |
  1245. DSI_DLN0_PHY_ERR_DLN0_ERR_SYNC_ESC |
  1246. DSI_DLN0_PHY_ERR_DLN0_ERR_CONTROL |
  1247. DSI_DLN0_PHY_ERR_DLN0_ERR_CONTENTION_LP0 |
  1248. DSI_DLN0_PHY_ERR_DLN0_ERR_CONTENTION_LP1)) {
  1249. dsi_write(msm_host, REG_DSI_DLN0_PHY_ERR, status);
  1250. msm_host->err_work_state |= DSI_ERR_STATE_DLN0_PHY;
  1251. }
  1252. }
  1253. static void dsi_fifo_status(struct msm_dsi_host *msm_host)
  1254. {
  1255. u32 status;
  1256. status = dsi_read(msm_host, REG_DSI_FIFO_STATUS);
  1257. /* fifo underflow, overflow */
  1258. if (status) {
  1259. dsi_write(msm_host, REG_DSI_FIFO_STATUS, status);
  1260. msm_host->err_work_state |= DSI_ERR_STATE_FIFO;
  1261. if (status & DSI_FIFO_STATUS_CMD_MDP_FIFO_UNDERFLOW)
  1262. msm_host->err_work_state |=
  1263. DSI_ERR_STATE_MDP_FIFO_UNDERFLOW;
  1264. }
  1265. }
  1266. static void dsi_status(struct msm_dsi_host *msm_host)
  1267. {
  1268. u32 status;
  1269. status = dsi_read(msm_host, REG_DSI_STATUS0);
  1270. if (status & DSI_STATUS0_INTERLEAVE_OP_CONTENTION) {
  1271. dsi_write(msm_host, REG_DSI_STATUS0, status);
  1272. msm_host->err_work_state |=
  1273. DSI_ERR_STATE_INTERLEAVE_OP_CONTENTION;
  1274. }
  1275. }
  1276. static void dsi_clk_status(struct msm_dsi_host *msm_host)
  1277. {
  1278. u32 status;
  1279. status = dsi_read(msm_host, REG_DSI_CLK_STATUS);
  1280. if (status & DSI_CLK_STATUS_PLL_UNLOCKED) {
  1281. dsi_write(msm_host, REG_DSI_CLK_STATUS, status);
  1282. msm_host->err_work_state |= DSI_ERR_STATE_PLL_UNLOCKED;
  1283. }
  1284. }
  1285. static void dsi_error(struct msm_dsi_host *msm_host)
  1286. {
  1287. /* disable dsi error interrupt */
  1288. dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 0);
  1289. dsi_clk_status(msm_host);
  1290. dsi_fifo_status(msm_host);
  1291. dsi_ack_err_status(msm_host);
  1292. dsi_timeout_status(msm_host);
  1293. dsi_status(msm_host);
  1294. dsi_dln0_phy_err(msm_host);
  1295. queue_work(msm_host->workqueue, &msm_host->err_work);
  1296. }
  1297. static irqreturn_t dsi_host_irq(int irq, void *ptr)
  1298. {
  1299. struct msm_dsi_host *msm_host = ptr;
  1300. u32 isr;
  1301. unsigned long flags;
  1302. if (!msm_host->ctrl_base)
  1303. return IRQ_HANDLED;
  1304. spin_lock_irqsave(&msm_host->intr_lock, flags);
  1305. isr = dsi_read(msm_host, REG_DSI_INTR_CTRL);
  1306. dsi_write(msm_host, REG_DSI_INTR_CTRL, isr);
  1307. spin_unlock_irqrestore(&msm_host->intr_lock, flags);
  1308. DBG("isr=0x%x, id=%d", isr, msm_host->id);
  1309. if (isr & DSI_IRQ_ERROR)
  1310. dsi_error(msm_host);
  1311. if (isr & DSI_IRQ_VIDEO_DONE)
  1312. complete(&msm_host->video_comp);
  1313. if (isr & DSI_IRQ_CMD_DMA_DONE)
  1314. complete(&msm_host->dma_comp);
  1315. return IRQ_HANDLED;
  1316. }
  1317. static int dsi_host_attach(struct mipi_dsi_host *host,
  1318. struct mipi_dsi_device *dsi)
  1319. {
  1320. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1321. int ret;
  1322. if (dsi->lanes > msm_host->num_data_lanes)
  1323. return -EINVAL;
  1324. msm_host->channel = dsi->channel;
  1325. msm_host->lanes = dsi->lanes;
  1326. msm_host->format = dsi->format;
  1327. msm_host->mode_flags = dsi->mode_flags;
  1328. if (dsi->dsc)
  1329. msm_host->dsc = dsi->dsc;
  1330. ret = dsi_dev_attach(msm_host->pdev);
  1331. if (ret)
  1332. return ret;
  1333. DBG("id=%d", msm_host->id);
  1334. return 0;
  1335. }
  1336. static int dsi_host_detach(struct mipi_dsi_host *host,
  1337. struct mipi_dsi_device *dsi)
  1338. {
  1339. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1340. dsi_dev_detach(msm_host->pdev);
  1341. DBG("id=%d", msm_host->id);
  1342. return 0;
  1343. }
  1344. static ssize_t dsi_host_transfer(struct mipi_dsi_host *host,
  1345. const struct mipi_dsi_msg *msg)
  1346. {
  1347. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1348. int ret;
  1349. if (!msg || !msm_host->power_on)
  1350. return -EINVAL;
  1351. mutex_lock(&msm_host->cmd_mutex);
  1352. ret = msm_dsi_manager_cmd_xfer(msm_host->id, msg);
  1353. mutex_unlock(&msm_host->cmd_mutex);
  1354. return ret;
  1355. }
  1356. static const struct mipi_dsi_host_ops dsi_host_ops = {
  1357. .attach = dsi_host_attach,
  1358. .detach = dsi_host_detach,
  1359. .transfer = dsi_host_transfer,
  1360. };
  1361. /*
  1362. * List of supported physical to logical lane mappings.
  1363. * For example, the 2nd entry represents the following mapping:
  1364. *
  1365. * "3012": Logic 3->Phys 0; Logic 0->Phys 1; Logic 1->Phys 2; Logic 2->Phys 3;
  1366. */
  1367. static const int supported_data_lane_swaps[][4] = {
  1368. { 0, 1, 2, 3 },
  1369. { 3, 0, 1, 2 },
  1370. { 2, 3, 0, 1 },
  1371. { 1, 2, 3, 0 },
  1372. { 0, 3, 2, 1 },
  1373. { 1, 0, 3, 2 },
  1374. { 2, 1, 0, 3 },
  1375. { 3, 2, 1, 0 },
  1376. };
  1377. static int dsi_host_parse_lane_data(struct msm_dsi_host *msm_host,
  1378. struct device_node *ep)
  1379. {
  1380. struct device *dev = &msm_host->pdev->dev;
  1381. struct property *prop;
  1382. u32 lane_map[4];
  1383. int ret, i, len, num_lanes;
  1384. prop = of_find_property(ep, "data-lanes", &len);
  1385. if (!prop) {
  1386. DRM_DEV_DEBUG(dev,
  1387. "failed to find data lane mapping, using default\n");
  1388. /* Set the number of date lanes to 4 by default. */
  1389. msm_host->num_data_lanes = 4;
  1390. return 0;
  1391. }
  1392. num_lanes = drm_of_get_data_lanes_count(ep, 1, 4);
  1393. if (num_lanes < 0) {
  1394. DRM_DEV_ERROR(dev, "bad number of data lanes\n");
  1395. return num_lanes;
  1396. }
  1397. msm_host->num_data_lanes = num_lanes;
  1398. ret = of_property_read_u32_array(ep, "data-lanes", lane_map,
  1399. num_lanes);
  1400. if (ret) {
  1401. DRM_DEV_ERROR(dev, "failed to read lane data\n");
  1402. return ret;
  1403. }
  1404. /*
  1405. * compare DT specified physical-logical lane mappings with the ones
  1406. * supported by hardware
  1407. */
  1408. for (i = 0; i < ARRAY_SIZE(supported_data_lane_swaps); i++) {
  1409. const int *swap = supported_data_lane_swaps[i];
  1410. int j;
  1411. /*
  1412. * the data-lanes array we get from DT has a logical->physical
  1413. * mapping. The "data lane swap" register field represents
  1414. * supported configurations in a physical->logical mapping.
  1415. * Translate the DT mapping to what we understand and find a
  1416. * configuration that works.
  1417. */
  1418. for (j = 0; j < num_lanes; j++) {
  1419. if (lane_map[j] < 0 || lane_map[j] > 3)
  1420. DRM_DEV_ERROR(dev, "bad physical lane entry %u\n",
  1421. lane_map[j]);
  1422. if (swap[lane_map[j]] != j)
  1423. break;
  1424. }
  1425. if (j == num_lanes) {
  1426. msm_host->dlane_swap = i;
  1427. return 0;
  1428. }
  1429. }
  1430. return -EINVAL;
  1431. }
  1432. static int dsi_populate_dsc_params(struct msm_dsi_host *msm_host, struct drm_dsc_config *dsc)
  1433. {
  1434. int ret;
  1435. if (dsc->bits_per_pixel & 0xf) {
  1436. DRM_DEV_ERROR(&msm_host->pdev->dev, "DSI does not support fractional bits_per_pixel\n");
  1437. return -EINVAL;
  1438. }
  1439. if (dsc->bits_per_component != 8) {
  1440. DRM_DEV_ERROR(&msm_host->pdev->dev, "DSI does not support bits_per_component != 8 yet\n");
  1441. return -EOPNOTSUPP;
  1442. }
  1443. dsc->simple_422 = 0;
  1444. dsc->convert_rgb = 1;
  1445. dsc->vbr_enable = 0;
  1446. drm_dsc_set_const_params(dsc);
  1447. drm_dsc_set_rc_buf_thresh(dsc);
  1448. /* handle only bpp = bpc = 8, pre-SCR panels */
  1449. ret = drm_dsc_setup_rc_params(dsc, DRM_DSC_1_1_PRE_SCR);
  1450. if (ret) {
  1451. DRM_DEV_ERROR(&msm_host->pdev->dev, "could not find DSC RC parameters\n");
  1452. return ret;
  1453. }
  1454. dsc->initial_scale_value = drm_dsc_initial_scale_value(dsc);
  1455. dsc->line_buf_depth = dsc->bits_per_component + 1;
  1456. return drm_dsc_compute_rc_parameters(dsc);
  1457. }
  1458. static int dsi_host_parse_dt(struct msm_dsi_host *msm_host)
  1459. {
  1460. struct msm_dsi *msm_dsi = platform_get_drvdata(msm_host->pdev);
  1461. struct device *dev = &msm_host->pdev->dev;
  1462. struct device_node *np = dev->of_node;
  1463. struct device_node *endpoint;
  1464. const char *te_source;
  1465. int ret = 0;
  1466. /*
  1467. * Get the endpoint of the output port of the DSI host. In our case,
  1468. * this is mapped to port number with reg = 1. Don't return an error if
  1469. * the remote endpoint isn't defined. It's possible that there is
  1470. * nothing connected to the dsi output.
  1471. */
  1472. endpoint = of_graph_get_endpoint_by_regs(np, 1, -1);
  1473. if (!endpoint) {
  1474. DRM_DEV_DEBUG(dev, "%s: no endpoint\n", __func__);
  1475. return 0;
  1476. }
  1477. ret = dsi_host_parse_lane_data(msm_host, endpoint);
  1478. if (ret) {
  1479. DRM_DEV_ERROR(dev, "%s: invalid lane configuration %d\n",
  1480. __func__, ret);
  1481. ret = -EINVAL;
  1482. goto err;
  1483. }
  1484. ret = of_property_read_string(endpoint, "qcom,te-source", &te_source);
  1485. if (ret && ret != -EINVAL) {
  1486. DRM_DEV_ERROR(dev, "%s: invalid TE source configuration %d\n",
  1487. __func__, ret);
  1488. goto err;
  1489. }
  1490. if (!ret) {
  1491. msm_dsi->te_source = devm_kstrdup(dev, te_source, GFP_KERNEL);
  1492. if (!msm_dsi->te_source) {
  1493. DRM_DEV_ERROR(dev, "%s: failed to allocate te_source\n",
  1494. __func__);
  1495. ret = -ENOMEM;
  1496. goto err;
  1497. }
  1498. }
  1499. ret = 0;
  1500. if (of_property_read_bool(np, "syscon-sfpb")) {
  1501. msm_host->sfpb = syscon_regmap_lookup_by_phandle(np,
  1502. "syscon-sfpb");
  1503. if (IS_ERR(msm_host->sfpb)) {
  1504. DRM_DEV_ERROR(dev, "%s: failed to get sfpb regmap\n",
  1505. __func__);
  1506. ret = PTR_ERR(msm_host->sfpb);
  1507. }
  1508. }
  1509. err:
  1510. of_node_put(endpoint);
  1511. return ret;
  1512. }
  1513. static int dsi_host_get_id(struct msm_dsi_host *msm_host)
  1514. {
  1515. struct platform_device *pdev = msm_host->pdev;
  1516. const struct msm_dsi_config *cfg = msm_host->cfg_hnd->cfg;
  1517. struct resource *res;
  1518. int i, j;
  1519. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dsi_ctrl");
  1520. if (!res)
  1521. return -EINVAL;
  1522. for (i = 0; i < VARIANTS_MAX; i++)
  1523. for (j = 0; j < DSI_MAX; j++)
  1524. if (cfg->io_start[i][j] == res->start)
  1525. return j;
  1526. return -EINVAL;
  1527. }
  1528. int msm_dsi_host_init(struct msm_dsi *msm_dsi)
  1529. {
  1530. struct msm_dsi_host *msm_host = NULL;
  1531. struct platform_device *pdev = msm_dsi->pdev;
  1532. const struct msm_dsi_config *cfg;
  1533. int ret;
  1534. msm_host = devm_kzalloc(&pdev->dev, sizeof(*msm_host), GFP_KERNEL);
  1535. if (!msm_host) {
  1536. return -ENOMEM;
  1537. }
  1538. msm_host->pdev = pdev;
  1539. msm_dsi->host = &msm_host->base;
  1540. ret = dsi_host_parse_dt(msm_host);
  1541. if (ret) {
  1542. pr_err("%s: failed to parse dt\n", __func__);
  1543. return ret;
  1544. }
  1545. msm_host->ctrl_base = msm_ioremap_size(pdev, "dsi_ctrl", &msm_host->ctrl_size);
  1546. if (IS_ERR(msm_host->ctrl_base)) {
  1547. pr_err("%s: unable to map Dsi ctrl base\n", __func__);
  1548. return PTR_ERR(msm_host->ctrl_base);
  1549. }
  1550. pm_runtime_enable(&pdev->dev);
  1551. msm_host->cfg_hnd = dsi_get_config(msm_host);
  1552. if (!msm_host->cfg_hnd) {
  1553. pr_err("%s: get config failed\n", __func__);
  1554. return -EINVAL;
  1555. }
  1556. cfg = msm_host->cfg_hnd->cfg;
  1557. msm_host->id = dsi_host_get_id(msm_host);
  1558. if (msm_host->id < 0) {
  1559. pr_err("%s: unable to identify DSI host index\n", __func__);
  1560. return msm_host->id;
  1561. }
  1562. /* fixup base address by io offset */
  1563. msm_host->ctrl_base += cfg->io_offset;
  1564. ret = devm_regulator_bulk_get_const(&pdev->dev, cfg->num_regulators,
  1565. cfg->regulator_data,
  1566. &msm_host->supplies);
  1567. if (ret)
  1568. return ret;
  1569. ret = dsi_clk_init(msm_host);
  1570. if (ret) {
  1571. pr_err("%s: unable to initialize dsi clks\n", __func__);
  1572. return ret;
  1573. }
  1574. msm_host->rx_buf = devm_kzalloc(&pdev->dev, SZ_4K, GFP_KERNEL);
  1575. if (!msm_host->rx_buf) {
  1576. pr_err("%s: alloc rx temp buf failed\n", __func__);
  1577. return -ENOMEM;
  1578. }
  1579. ret = devm_pm_opp_set_clkname(&pdev->dev, "byte");
  1580. if (ret)
  1581. return ret;
  1582. /* OPP table is optional */
  1583. ret = devm_pm_opp_of_add_table(&pdev->dev);
  1584. if (ret && ret != -ENODEV) {
  1585. dev_err(&pdev->dev, "invalid OPP table in device tree\n");
  1586. return ret;
  1587. }
  1588. msm_host->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
  1589. if (!msm_host->irq) {
  1590. dev_err(&pdev->dev, "failed to get irq\n");
  1591. return -EINVAL;
  1592. }
  1593. /* do not autoenable, will be enabled later */
  1594. ret = devm_request_irq(&pdev->dev, msm_host->irq, dsi_host_irq,
  1595. IRQF_TRIGGER_HIGH | IRQF_NO_AUTOEN,
  1596. "dsi_isr", msm_host);
  1597. if (ret < 0) {
  1598. dev_err(&pdev->dev, "failed to request IRQ%u: %d\n",
  1599. msm_host->irq, ret);
  1600. return ret;
  1601. }
  1602. init_completion(&msm_host->dma_comp);
  1603. init_completion(&msm_host->video_comp);
  1604. mutex_init(&msm_host->dev_mutex);
  1605. mutex_init(&msm_host->cmd_mutex);
  1606. spin_lock_init(&msm_host->intr_lock);
  1607. /* setup workqueue */
  1608. msm_host->workqueue = alloc_ordered_workqueue("dsi_drm_work", 0);
  1609. if (!msm_host->workqueue)
  1610. return -ENOMEM;
  1611. INIT_WORK(&msm_host->err_work, dsi_err_worker);
  1612. msm_dsi->id = msm_host->id;
  1613. DBG("Dsi Host %d initialized", msm_host->id);
  1614. return 0;
  1615. }
  1616. void msm_dsi_host_destroy(struct mipi_dsi_host *host)
  1617. {
  1618. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1619. DBG("");
  1620. if (msm_host->workqueue) {
  1621. destroy_workqueue(msm_host->workqueue);
  1622. msm_host->workqueue = NULL;
  1623. }
  1624. mutex_destroy(&msm_host->cmd_mutex);
  1625. mutex_destroy(&msm_host->dev_mutex);
  1626. pm_runtime_disable(&msm_host->pdev->dev);
  1627. }
  1628. int msm_dsi_host_modeset_init(struct mipi_dsi_host *host,
  1629. struct drm_device *dev)
  1630. {
  1631. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1632. const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
  1633. int ret;
  1634. msm_host->dev = dev;
  1635. ret = cfg_hnd->ops->tx_buf_alloc(msm_host, SZ_4K);
  1636. if (ret) {
  1637. pr_err("%s: alloc tx gem obj failed, %d\n", __func__, ret);
  1638. return ret;
  1639. }
  1640. return 0;
  1641. }
  1642. int msm_dsi_host_register(struct mipi_dsi_host *host)
  1643. {
  1644. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1645. int ret;
  1646. /* Register mipi dsi host */
  1647. if (!msm_host->registered) {
  1648. host->dev = &msm_host->pdev->dev;
  1649. host->ops = &dsi_host_ops;
  1650. ret = mipi_dsi_host_register(host);
  1651. if (ret)
  1652. return ret;
  1653. msm_host->registered = true;
  1654. }
  1655. return 0;
  1656. }
  1657. void msm_dsi_host_unregister(struct mipi_dsi_host *host)
  1658. {
  1659. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1660. if (msm_host->registered) {
  1661. mipi_dsi_host_unregister(host);
  1662. host->dev = NULL;
  1663. host->ops = NULL;
  1664. msm_host->registered = false;
  1665. }
  1666. }
  1667. int msm_dsi_host_xfer_prepare(struct mipi_dsi_host *host,
  1668. const struct mipi_dsi_msg *msg)
  1669. {
  1670. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1671. const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
  1672. /* TODO: make sure dsi_cmd_mdp is idle.
  1673. * Since DSI6G v1.2.0, we can set DSI_TRIG_CTRL.BLOCK_DMA_WITHIN_FRAME
  1674. * to ask H/W to wait until cmd mdp is idle. S/W wait is not needed.
  1675. * How to handle the old versions? Wait for mdp cmd done?
  1676. */
  1677. /*
  1678. * mdss interrupt is generated in mdp core clock domain
  1679. * mdp clock need to be enabled to receive dsi interrupt
  1680. */
  1681. pm_runtime_get_sync(&msm_host->pdev->dev);
  1682. cfg_hnd->ops->link_clk_set_rate(msm_host);
  1683. cfg_hnd->ops->link_clk_enable(msm_host);
  1684. /* TODO: vote for bus bandwidth */
  1685. if (!(msg->flags & MIPI_DSI_MSG_USE_LPM))
  1686. dsi_set_tx_power_mode(0, msm_host);
  1687. msm_host->dma_cmd_ctrl_restore = dsi_read(msm_host, REG_DSI_CTRL);
  1688. dsi_write(msm_host, REG_DSI_CTRL,
  1689. msm_host->dma_cmd_ctrl_restore |
  1690. DSI_CTRL_CMD_MODE_EN |
  1691. DSI_CTRL_ENABLE);
  1692. dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_DMA_DONE, 1);
  1693. return 0;
  1694. }
  1695. void msm_dsi_host_xfer_restore(struct mipi_dsi_host *host,
  1696. const struct mipi_dsi_msg *msg)
  1697. {
  1698. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1699. const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
  1700. dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_DMA_DONE, 0);
  1701. dsi_write(msm_host, REG_DSI_CTRL, msm_host->dma_cmd_ctrl_restore);
  1702. if (!(msg->flags & MIPI_DSI_MSG_USE_LPM))
  1703. dsi_set_tx_power_mode(1, msm_host);
  1704. /* TODO: unvote for bus bandwidth */
  1705. cfg_hnd->ops->link_clk_disable(msm_host);
  1706. pm_runtime_put(&msm_host->pdev->dev);
  1707. }
  1708. int msm_dsi_host_cmd_tx(struct mipi_dsi_host *host,
  1709. const struct mipi_dsi_msg *msg)
  1710. {
  1711. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1712. return dsi_cmds2buf_tx(msm_host, msg);
  1713. }
  1714. int msm_dsi_host_cmd_rx(struct mipi_dsi_host *host,
  1715. const struct mipi_dsi_msg *msg)
  1716. {
  1717. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1718. const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
  1719. int data_byte, rx_byte, dlen, end;
  1720. int short_response, diff, pkt_size, ret = 0;
  1721. char cmd;
  1722. int rlen = msg->rx_len;
  1723. u8 *buf;
  1724. if (rlen <= 2) {
  1725. short_response = 1;
  1726. pkt_size = rlen;
  1727. rx_byte = 4;
  1728. } else {
  1729. short_response = 0;
  1730. data_byte = 10; /* first read */
  1731. if (rlen < data_byte)
  1732. pkt_size = rlen;
  1733. else
  1734. pkt_size = data_byte;
  1735. rx_byte = data_byte + 6; /* 4 header + 2 crc */
  1736. }
  1737. buf = msm_host->rx_buf;
  1738. end = 0;
  1739. while (!end) {
  1740. u8 tx[2] = {pkt_size & 0xff, pkt_size >> 8};
  1741. struct mipi_dsi_msg max_pkt_size_msg = {
  1742. .channel = msg->channel,
  1743. .type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
  1744. .tx_len = 2,
  1745. .tx_buf = tx,
  1746. };
  1747. DBG("rlen=%d pkt_size=%d rx_byte=%d",
  1748. rlen, pkt_size, rx_byte);
  1749. ret = dsi_cmds2buf_tx(msm_host, &max_pkt_size_msg);
  1750. if (ret < 2) {
  1751. pr_err("%s: Set max pkt size failed, %d\n",
  1752. __func__, ret);
  1753. return -EINVAL;
  1754. }
  1755. if ((cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) &&
  1756. (cfg_hnd->minor >= MSM_DSI_6G_VER_MINOR_V1_1)) {
  1757. /* Clear the RDBK_DATA registers */
  1758. dsi_write(msm_host, REG_DSI_RDBK_DATA_CTRL,
  1759. DSI_RDBK_DATA_CTRL_CLR);
  1760. wmb(); /* make sure the RDBK registers are cleared */
  1761. dsi_write(msm_host, REG_DSI_RDBK_DATA_CTRL, 0);
  1762. wmb(); /* release cleared status before transfer */
  1763. }
  1764. ret = dsi_cmds2buf_tx(msm_host, msg);
  1765. if (ret < 0) {
  1766. pr_err("%s: Read cmd Tx failed, %d\n", __func__, ret);
  1767. return ret;
  1768. } else if (ret < msg->tx_len) {
  1769. pr_err("%s: Read cmd Tx failed, too short: %d\n", __func__, ret);
  1770. return -ECOMM;
  1771. }
  1772. /*
  1773. * once cmd_dma_done interrupt received,
  1774. * return data from client is ready and stored
  1775. * at RDBK_DATA register already
  1776. * since rx fifo is 16 bytes, dcs header is kept at first loop,
  1777. * after that dcs header lost during shift into registers
  1778. */
  1779. dlen = dsi_cmd_dma_rx(msm_host, buf, rx_byte, pkt_size);
  1780. if (dlen <= 0)
  1781. return 0;
  1782. if (short_response)
  1783. break;
  1784. if (rlen <= data_byte) {
  1785. diff = data_byte - rlen;
  1786. end = 1;
  1787. } else {
  1788. diff = 0;
  1789. rlen -= data_byte;
  1790. }
  1791. if (!end) {
  1792. dlen -= 2; /* 2 crc */
  1793. dlen -= diff;
  1794. buf += dlen; /* next start position */
  1795. data_byte = 14; /* NOT first read */
  1796. if (rlen < data_byte)
  1797. pkt_size += rlen;
  1798. else
  1799. pkt_size += data_byte;
  1800. DBG("buf=%p dlen=%d diff=%d", buf, dlen, diff);
  1801. }
  1802. }
  1803. /*
  1804. * For single Long read, if the requested rlen < 10,
  1805. * we need to shift the start position of rx
  1806. * data buffer to skip the bytes which are not
  1807. * updated.
  1808. */
  1809. if (pkt_size < 10 && !short_response)
  1810. buf = msm_host->rx_buf + (10 - rlen);
  1811. else
  1812. buf = msm_host->rx_buf;
  1813. cmd = buf[0];
  1814. switch (cmd) {
  1815. case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
  1816. pr_err("%s: rx ACK_ERR_PACLAGE\n", __func__);
  1817. ret = 0;
  1818. break;
  1819. case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE:
  1820. case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
  1821. ret = dsi_short_read1_resp(buf, msg);
  1822. break;
  1823. case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE:
  1824. case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
  1825. ret = dsi_short_read2_resp(buf, msg);
  1826. break;
  1827. case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
  1828. case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
  1829. ret = dsi_long_read_resp(buf, msg);
  1830. break;
  1831. default:
  1832. pr_warn("%s:Invalid response cmd\n", __func__);
  1833. ret = 0;
  1834. }
  1835. return ret;
  1836. }
  1837. void msm_dsi_host_cmd_xfer_commit(struct mipi_dsi_host *host, u32 dma_base,
  1838. u32 len)
  1839. {
  1840. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1841. dsi_write(msm_host, REG_DSI_DMA_BASE, dma_base);
  1842. dsi_write(msm_host, REG_DSI_DMA_LEN, len);
  1843. dsi_write(msm_host, REG_DSI_TRIG_DMA, 1);
  1844. /* Make sure trigger happens */
  1845. wmb();
  1846. }
  1847. void msm_dsi_host_set_phy_mode(struct mipi_dsi_host *host,
  1848. struct msm_dsi_phy *src_phy)
  1849. {
  1850. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1851. msm_host->cphy_mode = src_phy->cphy_mode;
  1852. }
  1853. void msm_dsi_host_reset_phy(struct mipi_dsi_host *host)
  1854. {
  1855. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1856. DBG("");
  1857. dsi_write(msm_host, REG_DSI_PHY_RESET, DSI_PHY_RESET_RESET);
  1858. /* Make sure fully reset */
  1859. wmb();
  1860. udelay(1000);
  1861. dsi_write(msm_host, REG_DSI_PHY_RESET, 0);
  1862. udelay(100);
  1863. }
  1864. void msm_dsi_host_get_phy_clk_req(struct mipi_dsi_host *host,
  1865. struct msm_dsi_phy_clk_request *clk_req,
  1866. bool is_bonded_dsi)
  1867. {
  1868. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1869. const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
  1870. int ret;
  1871. ret = cfg_hnd->ops->calc_clk_rate(msm_host, is_bonded_dsi);
  1872. if (ret) {
  1873. pr_err("%s: unable to calc clk rate, %d\n", __func__, ret);
  1874. return;
  1875. }
  1876. /* CPHY transmits 16 bits over 7 clock cycles
  1877. * "byte_clk" is in units of 16-bits (see dsi_calc_pclk),
  1878. * so multiply by 7 to get the "bitclk rate"
  1879. */
  1880. if (msm_host->cphy_mode)
  1881. clk_req->bitclk_rate = msm_host->byte_clk_rate * 7;
  1882. else
  1883. clk_req->bitclk_rate = msm_host->byte_clk_rate * 8;
  1884. clk_req->escclk_rate = msm_host->esc_clk_rate;
  1885. }
  1886. void msm_dsi_host_enable_irq(struct mipi_dsi_host *host)
  1887. {
  1888. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1889. enable_irq(msm_host->irq);
  1890. }
  1891. void msm_dsi_host_disable_irq(struct mipi_dsi_host *host)
  1892. {
  1893. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1894. disable_irq(msm_host->irq);
  1895. }
  1896. int msm_dsi_host_enable(struct mipi_dsi_host *host)
  1897. {
  1898. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1899. dsi_op_mode_config(msm_host,
  1900. !!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO), true);
  1901. /* TODO: clock should be turned off for command mode,
  1902. * and only turned on before MDP START.
  1903. * This part of code should be enabled once mdp driver support it.
  1904. */
  1905. /* if (msm_panel->mode == MSM_DSI_CMD_MODE) {
  1906. * dsi_link_clk_disable(msm_host);
  1907. * pm_runtime_put(&msm_host->pdev->dev);
  1908. * }
  1909. */
  1910. msm_host->enabled = true;
  1911. return 0;
  1912. }
  1913. int msm_dsi_host_disable(struct mipi_dsi_host *host)
  1914. {
  1915. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1916. msm_host->enabled = false;
  1917. dsi_op_mode_config(msm_host,
  1918. !!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO), false);
  1919. /* Since we have disabled INTF, the video engine won't stop so that
  1920. * the cmd engine will be blocked.
  1921. * Reset to disable video engine so that we can send off cmd.
  1922. */
  1923. dsi_sw_reset(msm_host);
  1924. return 0;
  1925. }
  1926. static void msm_dsi_sfpb_config(struct msm_dsi_host *msm_host, bool enable)
  1927. {
  1928. enum sfpb_ahb_arb_master_port_en en;
  1929. if (!msm_host->sfpb)
  1930. return;
  1931. en = enable ? SFPB_MASTER_PORT_ENABLE : SFPB_MASTER_PORT_DISABLE;
  1932. regmap_update_bits(msm_host->sfpb, REG_SFPB_GPREG,
  1933. SFPB_GPREG_MASTER_PORT_EN__MASK,
  1934. SFPB_GPREG_MASTER_PORT_EN(en));
  1935. }
  1936. int msm_dsi_host_power_on(struct mipi_dsi_host *host,
  1937. struct msm_dsi_phy_shared_timings *phy_shared_timings,
  1938. bool is_bonded_dsi, struct msm_dsi_phy *phy)
  1939. {
  1940. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1941. const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
  1942. int ret = 0;
  1943. mutex_lock(&msm_host->dev_mutex);
  1944. if (msm_host->power_on) {
  1945. DBG("dsi host already on");
  1946. goto unlock_ret;
  1947. }
  1948. msm_host->byte_intf_clk_rate = msm_host->byte_clk_rate;
  1949. if (phy_shared_timings->byte_intf_clk_div_2)
  1950. msm_host->byte_intf_clk_rate /= 2;
  1951. msm_dsi_sfpb_config(msm_host, true);
  1952. ret = regulator_bulk_enable(msm_host->cfg_hnd->cfg->num_regulators,
  1953. msm_host->supplies);
  1954. if (ret) {
  1955. pr_err("%s:Failed to enable vregs.ret=%d\n",
  1956. __func__, ret);
  1957. goto unlock_ret;
  1958. }
  1959. pm_runtime_get_sync(&msm_host->pdev->dev);
  1960. ret = cfg_hnd->ops->link_clk_set_rate(msm_host);
  1961. if (!ret)
  1962. ret = cfg_hnd->ops->link_clk_enable(msm_host);
  1963. if (ret) {
  1964. pr_err("%s: failed to enable link clocks. ret=%d\n",
  1965. __func__, ret);
  1966. goto fail_disable_reg;
  1967. }
  1968. ret = pinctrl_pm_select_default_state(&msm_host->pdev->dev);
  1969. if (ret) {
  1970. pr_err("%s: failed to set pinctrl default state, %d\n",
  1971. __func__, ret);
  1972. goto fail_disable_clk;
  1973. }
  1974. dsi_timing_setup(msm_host, is_bonded_dsi);
  1975. dsi_sw_reset(msm_host);
  1976. dsi_ctrl_enable(msm_host, phy_shared_timings, phy);
  1977. msm_host->power_on = true;
  1978. mutex_unlock(&msm_host->dev_mutex);
  1979. return 0;
  1980. fail_disable_clk:
  1981. cfg_hnd->ops->link_clk_disable(msm_host);
  1982. pm_runtime_put(&msm_host->pdev->dev);
  1983. fail_disable_reg:
  1984. regulator_bulk_disable(msm_host->cfg_hnd->cfg->num_regulators,
  1985. msm_host->supplies);
  1986. unlock_ret:
  1987. mutex_unlock(&msm_host->dev_mutex);
  1988. return ret;
  1989. }
  1990. int msm_dsi_host_power_off(struct mipi_dsi_host *host)
  1991. {
  1992. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1993. const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
  1994. mutex_lock(&msm_host->dev_mutex);
  1995. if (!msm_host->power_on) {
  1996. DBG("dsi host already off");
  1997. goto unlock_ret;
  1998. }
  1999. dsi_ctrl_disable(msm_host);
  2000. pinctrl_pm_select_sleep_state(&msm_host->pdev->dev);
  2001. cfg_hnd->ops->link_clk_disable(msm_host);
  2002. pm_runtime_put(&msm_host->pdev->dev);
  2003. regulator_bulk_disable(msm_host->cfg_hnd->cfg->num_regulators,
  2004. msm_host->supplies);
  2005. msm_dsi_sfpb_config(msm_host, false);
  2006. DBG("-");
  2007. msm_host->power_on = false;
  2008. unlock_ret:
  2009. mutex_unlock(&msm_host->dev_mutex);
  2010. return 0;
  2011. }
  2012. int msm_dsi_host_set_display_mode(struct mipi_dsi_host *host,
  2013. const struct drm_display_mode *mode)
  2014. {
  2015. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  2016. if (msm_host->mode) {
  2017. drm_mode_destroy(msm_host->dev, msm_host->mode);
  2018. msm_host->mode = NULL;
  2019. }
  2020. msm_host->mode = drm_mode_duplicate(msm_host->dev, mode);
  2021. if (!msm_host->mode) {
  2022. pr_err("%s: cannot duplicate mode\n", __func__);
  2023. return -ENOMEM;
  2024. }
  2025. return 0;
  2026. }
  2027. enum drm_mode_status msm_dsi_host_check_dsc(struct mipi_dsi_host *host,
  2028. const struct drm_display_mode *mode)
  2029. {
  2030. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  2031. struct drm_dsc_config *dsc = msm_host->dsc;
  2032. int pic_width = mode->hdisplay;
  2033. int pic_height = mode->vdisplay;
  2034. if (!msm_host->dsc)
  2035. return MODE_OK;
  2036. if (pic_width % dsc->slice_width) {
  2037. pr_err("DSI: pic_width %d has to be multiple of slice %d\n",
  2038. pic_width, dsc->slice_width);
  2039. return MODE_H_ILLEGAL;
  2040. }
  2041. if (pic_height % dsc->slice_height) {
  2042. pr_err("DSI: pic_height %d has to be multiple of slice %d\n",
  2043. pic_height, dsc->slice_height);
  2044. return MODE_V_ILLEGAL;
  2045. }
  2046. return MODE_OK;
  2047. }
  2048. unsigned long msm_dsi_host_get_mode_flags(struct mipi_dsi_host *host)
  2049. {
  2050. return to_msm_dsi_host(host)->mode_flags;
  2051. }
  2052. void msm_dsi_host_snapshot(struct msm_disp_state *disp_state, struct mipi_dsi_host *host)
  2053. {
  2054. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  2055. pm_runtime_get_sync(&msm_host->pdev->dev);
  2056. msm_disp_snapshot_add_block(disp_state, msm_host->ctrl_size,
  2057. msm_host->ctrl_base, "dsi%d_ctrl", msm_host->id);
  2058. pm_runtime_put_sync(&msm_host->pdev->dev);
  2059. }
  2060. static void msm_dsi_host_video_test_pattern_setup(struct msm_dsi_host *msm_host)
  2061. {
  2062. u32 reg;
  2063. reg = dsi_read(msm_host, REG_DSI_TEST_PATTERN_GEN_CTRL);
  2064. dsi_write(msm_host, REG_DSI_TEST_PATTERN_GEN_VIDEO_INIT_VAL, 0xff);
  2065. /* draw checkered rectangle pattern */
  2066. dsi_write(msm_host, REG_DSI_TPG_MAIN_CONTROL,
  2067. DSI_TPG_MAIN_CONTROL_CHECKERED_RECTANGLE_PATTERN);
  2068. /* use 24-bit RGB test pttern */
  2069. dsi_write(msm_host, REG_DSI_TPG_VIDEO_CONFIG,
  2070. DSI_TPG_VIDEO_CONFIG_BPP(VIDEO_CONFIG_24BPP) |
  2071. DSI_TPG_VIDEO_CONFIG_RGB);
  2072. reg |= DSI_TEST_PATTERN_GEN_CTRL_VIDEO_PATTERN_SEL(VID_MDSS_GENERAL_PATTERN);
  2073. dsi_write(msm_host, REG_DSI_TEST_PATTERN_GEN_CTRL, reg);
  2074. DBG("Video test pattern setup done\n");
  2075. }
  2076. static void msm_dsi_host_cmd_test_pattern_setup(struct msm_dsi_host *msm_host)
  2077. {
  2078. u32 reg;
  2079. reg = dsi_read(msm_host, REG_DSI_TEST_PATTERN_GEN_CTRL);
  2080. /* initial value for test pattern */
  2081. dsi_write(msm_host, REG_DSI_TEST_PATTERN_GEN_CMD_MDP_INIT_VAL0, 0xff);
  2082. reg |= DSI_TEST_PATTERN_GEN_CTRL_CMD_MDP_STREAM0_PATTERN_SEL(CMD_MDP_MDSS_GENERAL_PATTERN);
  2083. dsi_write(msm_host, REG_DSI_TEST_PATTERN_GEN_CTRL, reg);
  2084. /* draw checkered rectangle pattern */
  2085. dsi_write(msm_host, REG_DSI_TPG_MAIN_CONTROL2,
  2086. DSI_TPG_MAIN_CONTROL2_CMD_MDP0_CHECKERED_RECTANGLE_PATTERN);
  2087. DBG("Cmd test pattern setup done\n");
  2088. }
  2089. void msm_dsi_host_test_pattern_en(struct mipi_dsi_host *host)
  2090. {
  2091. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  2092. bool is_video_mode = !!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO);
  2093. u32 reg;
  2094. if (is_video_mode)
  2095. msm_dsi_host_video_test_pattern_setup(msm_host);
  2096. else
  2097. msm_dsi_host_cmd_test_pattern_setup(msm_host);
  2098. reg = dsi_read(msm_host, REG_DSI_TEST_PATTERN_GEN_CTRL);
  2099. /* enable the test pattern generator */
  2100. dsi_write(msm_host, REG_DSI_TEST_PATTERN_GEN_CTRL, (reg | DSI_TEST_PATTERN_GEN_CTRL_EN));
  2101. /* for command mode need to trigger one frame from tpg */
  2102. if (!is_video_mode)
  2103. dsi_write(msm_host, REG_DSI_TEST_PATTERN_GEN_CMD_STREAM0_TRIGGER,
  2104. DSI_TEST_PATTERN_GEN_CMD_STREAM0_TRIGGER_SW_TRIGGER);
  2105. }
  2106. struct drm_dsc_config *msm_dsi_host_get_dsc_config(struct mipi_dsi_host *host)
  2107. {
  2108. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  2109. return msm_host->dsc;
  2110. }