dsi_host.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976
  1. /*
  2. * Copyright (c) 2015, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/gpio.h>
  17. #include <linux/gpio/consumer.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/of_device.h>
  20. #include <linux/of_gpio.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/pinctrl/consumer.h>
  23. #include <linux/of_graph.h>
  24. #include <linux/regulator/consumer.h>
  25. #include <linux/spinlock.h>
  26. #include <video/mipi_display.h>
  27. #include "dsi.h"
  28. #include "dsi.xml.h"
  29. #include "dsi_cfg.h"
  30. static int dsi_get_version(const void __iomem *base, u32 *major, u32 *minor)
  31. {
  32. u32 ver;
  33. u32 ver_6g;
  34. if (!major || !minor)
  35. return -EINVAL;
  36. /* From DSI6G(v3), addition of a 6G_HW_VERSION register at offset 0
  37. * makes all other registers 4-byte shifted down.
  38. */
  39. ver_6g = msm_readl(base + REG_DSI_6G_HW_VERSION);
  40. if (ver_6g == 0) {
  41. ver = msm_readl(base + REG_DSI_VERSION);
  42. ver = FIELD(ver, DSI_VERSION_MAJOR);
  43. if (ver <= MSM_DSI_VER_MAJOR_V2) {
  44. /* old versions */
  45. *major = ver;
  46. *minor = 0;
  47. return 0;
  48. } else {
  49. return -EINVAL;
  50. }
  51. } else {
  52. ver = msm_readl(base + DSI_6G_REG_SHIFT + REG_DSI_VERSION);
  53. ver = FIELD(ver, DSI_VERSION_MAJOR);
  54. if (ver == MSM_DSI_VER_MAJOR_6G) {
  55. /* 6G version */
  56. *major = ver;
  57. *minor = ver_6g;
  58. return 0;
  59. } else {
  60. return -EINVAL;
  61. }
  62. }
  63. }
  64. #define DSI_ERR_STATE_ACK 0x0000
  65. #define DSI_ERR_STATE_TIMEOUT 0x0001
  66. #define DSI_ERR_STATE_DLN0_PHY 0x0002
  67. #define DSI_ERR_STATE_FIFO 0x0004
  68. #define DSI_ERR_STATE_MDP_FIFO_UNDERFLOW 0x0008
  69. #define DSI_ERR_STATE_INTERLEAVE_OP_CONTENTION 0x0010
  70. #define DSI_ERR_STATE_PLL_UNLOCKED 0x0020
  71. #define DSI_CLK_CTRL_ENABLE_CLKS \
  72. (DSI_CLK_CTRL_AHBS_HCLK_ON | DSI_CLK_CTRL_AHBM_SCLK_ON | \
  73. DSI_CLK_CTRL_PCLK_ON | DSI_CLK_CTRL_DSICLK_ON | \
  74. DSI_CLK_CTRL_BYTECLK_ON | DSI_CLK_CTRL_ESCCLK_ON | \
  75. DSI_CLK_CTRL_FORCE_ON_DYN_AHBM_HCLK)
  76. struct msm_dsi_host {
  77. struct mipi_dsi_host base;
  78. struct platform_device *pdev;
  79. struct drm_device *dev;
  80. int id;
  81. void __iomem *ctrl_base;
  82. struct regulator_bulk_data supplies[DSI_DEV_REGULATOR_MAX];
  83. struct clk *mdp_core_clk;
  84. struct clk *ahb_clk;
  85. struct clk *axi_clk;
  86. struct clk *mmss_misc_ahb_clk;
  87. struct clk *byte_clk;
  88. struct clk *esc_clk;
  89. struct clk *pixel_clk;
  90. struct clk *byte_clk_src;
  91. struct clk *pixel_clk_src;
  92. u32 byte_clk_rate;
  93. struct gpio_desc *disp_en_gpio;
  94. struct gpio_desc *te_gpio;
  95. const struct msm_dsi_cfg_handler *cfg_hnd;
  96. struct completion dma_comp;
  97. struct completion video_comp;
  98. struct mutex dev_mutex;
  99. struct mutex cmd_mutex;
  100. struct mutex clk_mutex;
  101. spinlock_t intr_lock; /* Protect interrupt ctrl register */
  102. u32 err_work_state;
  103. struct work_struct err_work;
  104. struct workqueue_struct *workqueue;
  105. struct drm_gem_object *tx_gem_obj;
  106. u8 *rx_buf;
  107. struct drm_display_mode *mode;
  108. /* connected device info */
  109. struct device_node *device_node;
  110. unsigned int channel;
  111. unsigned int lanes;
  112. enum mipi_dsi_pixel_format format;
  113. unsigned long mode_flags;
  114. u32 dma_cmd_ctrl_restore;
  115. bool registered;
  116. bool power_on;
  117. int irq;
  118. };
  119. static u32 dsi_get_bpp(const enum mipi_dsi_pixel_format fmt)
  120. {
  121. switch (fmt) {
  122. case MIPI_DSI_FMT_RGB565: return 16;
  123. case MIPI_DSI_FMT_RGB666_PACKED: return 18;
  124. case MIPI_DSI_FMT_RGB666:
  125. case MIPI_DSI_FMT_RGB888:
  126. default: return 24;
  127. }
  128. }
  129. static inline u32 dsi_read(struct msm_dsi_host *msm_host, u32 reg)
  130. {
  131. return msm_readl(msm_host->ctrl_base + reg);
  132. }
  133. static inline void dsi_write(struct msm_dsi_host *msm_host, u32 reg, u32 data)
  134. {
  135. msm_writel(data, msm_host->ctrl_base + reg);
  136. }
  137. static int dsi_host_regulator_enable(struct msm_dsi_host *msm_host);
  138. static void dsi_host_regulator_disable(struct msm_dsi_host *msm_host);
  139. static const struct msm_dsi_cfg_handler *dsi_get_config(
  140. struct msm_dsi_host *msm_host)
  141. {
  142. const struct msm_dsi_cfg_handler *cfg_hnd = NULL;
  143. struct regulator *gdsc_reg;
  144. int ret;
  145. u32 major = 0, minor = 0;
  146. gdsc_reg = regulator_get(&msm_host->pdev->dev, "gdsc");
  147. if (IS_ERR(gdsc_reg)) {
  148. pr_err("%s: cannot get gdsc\n", __func__);
  149. goto exit;
  150. }
  151. ret = regulator_enable(gdsc_reg);
  152. if (ret) {
  153. pr_err("%s: unable to enable gdsc\n", __func__);
  154. goto put_gdsc;
  155. }
  156. ret = clk_prepare_enable(msm_host->ahb_clk);
  157. if (ret) {
  158. pr_err("%s: unable to enable ahb_clk\n", __func__);
  159. goto disable_gdsc;
  160. }
  161. ret = dsi_get_version(msm_host->ctrl_base, &major, &minor);
  162. if (ret) {
  163. pr_err("%s: Invalid version\n", __func__);
  164. goto disable_clks;
  165. }
  166. cfg_hnd = msm_dsi_cfg_get(major, minor);
  167. DBG("%s: Version %x:%x\n", __func__, major, minor);
  168. disable_clks:
  169. clk_disable_unprepare(msm_host->ahb_clk);
  170. disable_gdsc:
  171. regulator_disable(gdsc_reg);
  172. put_gdsc:
  173. regulator_put(gdsc_reg);
  174. exit:
  175. return cfg_hnd;
  176. }
  177. static inline struct msm_dsi_host *to_msm_dsi_host(struct mipi_dsi_host *host)
  178. {
  179. return container_of(host, struct msm_dsi_host, base);
  180. }
  181. static void dsi_host_regulator_disable(struct msm_dsi_host *msm_host)
  182. {
  183. struct regulator_bulk_data *s = msm_host->supplies;
  184. const struct dsi_reg_entry *regs = msm_host->cfg_hnd->cfg->reg_cfg.regs;
  185. int num = msm_host->cfg_hnd->cfg->reg_cfg.num;
  186. int i;
  187. DBG("");
  188. for (i = num - 1; i >= 0; i--)
  189. if (regs[i].disable_load >= 0)
  190. regulator_set_load(s[i].consumer,
  191. regs[i].disable_load);
  192. regulator_bulk_disable(num, s);
  193. }
  194. static int dsi_host_regulator_enable(struct msm_dsi_host *msm_host)
  195. {
  196. struct regulator_bulk_data *s = msm_host->supplies;
  197. const struct dsi_reg_entry *regs = msm_host->cfg_hnd->cfg->reg_cfg.regs;
  198. int num = msm_host->cfg_hnd->cfg->reg_cfg.num;
  199. int ret, i;
  200. DBG("");
  201. for (i = 0; i < num; i++) {
  202. if (regs[i].enable_load >= 0) {
  203. ret = regulator_set_load(s[i].consumer,
  204. regs[i].enable_load);
  205. if (ret < 0) {
  206. pr_err("regulator %d set op mode failed, %d\n",
  207. i, ret);
  208. goto fail;
  209. }
  210. }
  211. }
  212. ret = regulator_bulk_enable(num, s);
  213. if (ret < 0) {
  214. pr_err("regulator enable failed, %d\n", ret);
  215. goto fail;
  216. }
  217. return 0;
  218. fail:
  219. for (i--; i >= 0; i--)
  220. regulator_set_load(s[i].consumer, regs[i].disable_load);
  221. return ret;
  222. }
  223. static int dsi_regulator_init(struct msm_dsi_host *msm_host)
  224. {
  225. struct regulator_bulk_data *s = msm_host->supplies;
  226. const struct dsi_reg_entry *regs = msm_host->cfg_hnd->cfg->reg_cfg.regs;
  227. int num = msm_host->cfg_hnd->cfg->reg_cfg.num;
  228. int i, ret;
  229. for (i = 0; i < num; i++)
  230. s[i].supply = regs[i].name;
  231. ret = devm_regulator_bulk_get(&msm_host->pdev->dev, num, s);
  232. if (ret < 0) {
  233. pr_err("%s: failed to init regulator, ret=%d\n",
  234. __func__, ret);
  235. return ret;
  236. }
  237. for (i = 0; i < num; i++) {
  238. if (regulator_can_change_voltage(s[i].consumer)) {
  239. ret = regulator_set_voltage(s[i].consumer,
  240. regs[i].min_voltage, regs[i].max_voltage);
  241. if (ret < 0) {
  242. pr_err("regulator %d set voltage failed, %d\n",
  243. i, ret);
  244. return ret;
  245. }
  246. }
  247. }
  248. return 0;
  249. }
  250. static int dsi_clk_init(struct msm_dsi_host *msm_host)
  251. {
  252. struct device *dev = &msm_host->pdev->dev;
  253. int ret = 0;
  254. msm_host->mdp_core_clk = devm_clk_get(dev, "mdp_core_clk");
  255. if (IS_ERR(msm_host->mdp_core_clk)) {
  256. ret = PTR_ERR(msm_host->mdp_core_clk);
  257. pr_err("%s: Unable to get mdp core clk. ret=%d\n",
  258. __func__, ret);
  259. goto exit;
  260. }
  261. msm_host->ahb_clk = devm_clk_get(dev, "iface_clk");
  262. if (IS_ERR(msm_host->ahb_clk)) {
  263. ret = PTR_ERR(msm_host->ahb_clk);
  264. pr_err("%s: Unable to get mdss ahb clk. ret=%d\n",
  265. __func__, ret);
  266. goto exit;
  267. }
  268. msm_host->axi_clk = devm_clk_get(dev, "bus_clk");
  269. if (IS_ERR(msm_host->axi_clk)) {
  270. ret = PTR_ERR(msm_host->axi_clk);
  271. pr_err("%s: Unable to get axi bus clk. ret=%d\n",
  272. __func__, ret);
  273. goto exit;
  274. }
  275. msm_host->mmss_misc_ahb_clk = devm_clk_get(dev, "core_mmss_clk");
  276. if (IS_ERR(msm_host->mmss_misc_ahb_clk)) {
  277. ret = PTR_ERR(msm_host->mmss_misc_ahb_clk);
  278. pr_err("%s: Unable to get mmss misc ahb clk. ret=%d\n",
  279. __func__, ret);
  280. goto exit;
  281. }
  282. msm_host->byte_clk = devm_clk_get(dev, "byte_clk");
  283. if (IS_ERR(msm_host->byte_clk)) {
  284. ret = PTR_ERR(msm_host->byte_clk);
  285. pr_err("%s: can't find dsi_byte_clk. ret=%d\n",
  286. __func__, ret);
  287. msm_host->byte_clk = NULL;
  288. goto exit;
  289. }
  290. msm_host->pixel_clk = devm_clk_get(dev, "pixel_clk");
  291. if (IS_ERR(msm_host->pixel_clk)) {
  292. ret = PTR_ERR(msm_host->pixel_clk);
  293. pr_err("%s: can't find dsi_pixel_clk. ret=%d\n",
  294. __func__, ret);
  295. msm_host->pixel_clk = NULL;
  296. goto exit;
  297. }
  298. msm_host->esc_clk = devm_clk_get(dev, "core_clk");
  299. if (IS_ERR(msm_host->esc_clk)) {
  300. ret = PTR_ERR(msm_host->esc_clk);
  301. pr_err("%s: can't find dsi_esc_clk. ret=%d\n",
  302. __func__, ret);
  303. msm_host->esc_clk = NULL;
  304. goto exit;
  305. }
  306. msm_host->byte_clk_src = devm_clk_get(dev, "byte_clk_src");
  307. if (IS_ERR(msm_host->byte_clk_src)) {
  308. ret = PTR_ERR(msm_host->byte_clk_src);
  309. pr_err("%s: can't find byte_clk_src. ret=%d\n", __func__, ret);
  310. msm_host->byte_clk_src = NULL;
  311. goto exit;
  312. }
  313. msm_host->pixel_clk_src = devm_clk_get(dev, "pixel_clk_src");
  314. if (IS_ERR(msm_host->pixel_clk_src)) {
  315. ret = PTR_ERR(msm_host->pixel_clk_src);
  316. pr_err("%s: can't find pixel_clk_src. ret=%d\n", __func__, ret);
  317. msm_host->pixel_clk_src = NULL;
  318. goto exit;
  319. }
  320. exit:
  321. return ret;
  322. }
  323. static int dsi_bus_clk_enable(struct msm_dsi_host *msm_host)
  324. {
  325. int ret;
  326. DBG("id=%d", msm_host->id);
  327. ret = clk_prepare_enable(msm_host->mdp_core_clk);
  328. if (ret) {
  329. pr_err("%s: failed to enable mdp_core_clock, %d\n",
  330. __func__, ret);
  331. goto core_clk_err;
  332. }
  333. ret = clk_prepare_enable(msm_host->ahb_clk);
  334. if (ret) {
  335. pr_err("%s: failed to enable ahb clock, %d\n", __func__, ret);
  336. goto ahb_clk_err;
  337. }
  338. ret = clk_prepare_enable(msm_host->axi_clk);
  339. if (ret) {
  340. pr_err("%s: failed to enable ahb clock, %d\n", __func__, ret);
  341. goto axi_clk_err;
  342. }
  343. ret = clk_prepare_enable(msm_host->mmss_misc_ahb_clk);
  344. if (ret) {
  345. pr_err("%s: failed to enable mmss misc ahb clk, %d\n",
  346. __func__, ret);
  347. goto misc_ahb_clk_err;
  348. }
  349. return 0;
  350. misc_ahb_clk_err:
  351. clk_disable_unprepare(msm_host->axi_clk);
  352. axi_clk_err:
  353. clk_disable_unprepare(msm_host->ahb_clk);
  354. ahb_clk_err:
  355. clk_disable_unprepare(msm_host->mdp_core_clk);
  356. core_clk_err:
  357. return ret;
  358. }
  359. static void dsi_bus_clk_disable(struct msm_dsi_host *msm_host)
  360. {
  361. DBG("");
  362. clk_disable_unprepare(msm_host->mmss_misc_ahb_clk);
  363. clk_disable_unprepare(msm_host->axi_clk);
  364. clk_disable_unprepare(msm_host->ahb_clk);
  365. clk_disable_unprepare(msm_host->mdp_core_clk);
  366. }
  367. static int dsi_link_clk_enable(struct msm_dsi_host *msm_host)
  368. {
  369. int ret;
  370. DBG("Set clk rates: pclk=%d, byteclk=%d",
  371. msm_host->mode->clock, msm_host->byte_clk_rate);
  372. ret = clk_set_rate(msm_host->byte_clk, msm_host->byte_clk_rate);
  373. if (ret) {
  374. pr_err("%s: Failed to set rate byte clk, %d\n", __func__, ret);
  375. goto error;
  376. }
  377. ret = clk_set_rate(msm_host->pixel_clk, msm_host->mode->clock * 1000);
  378. if (ret) {
  379. pr_err("%s: Failed to set rate pixel clk, %d\n", __func__, ret);
  380. goto error;
  381. }
  382. ret = clk_prepare_enable(msm_host->esc_clk);
  383. if (ret) {
  384. pr_err("%s: Failed to enable dsi esc clk\n", __func__);
  385. goto error;
  386. }
  387. ret = clk_prepare_enable(msm_host->byte_clk);
  388. if (ret) {
  389. pr_err("%s: Failed to enable dsi byte clk\n", __func__);
  390. goto byte_clk_err;
  391. }
  392. ret = clk_prepare_enable(msm_host->pixel_clk);
  393. if (ret) {
  394. pr_err("%s: Failed to enable dsi pixel clk\n", __func__);
  395. goto pixel_clk_err;
  396. }
  397. return 0;
  398. pixel_clk_err:
  399. clk_disable_unprepare(msm_host->byte_clk);
  400. byte_clk_err:
  401. clk_disable_unprepare(msm_host->esc_clk);
  402. error:
  403. return ret;
  404. }
  405. static void dsi_link_clk_disable(struct msm_dsi_host *msm_host)
  406. {
  407. clk_disable_unprepare(msm_host->esc_clk);
  408. clk_disable_unprepare(msm_host->pixel_clk);
  409. clk_disable_unprepare(msm_host->byte_clk);
  410. }
  411. static int dsi_clk_ctrl(struct msm_dsi_host *msm_host, bool enable)
  412. {
  413. int ret = 0;
  414. mutex_lock(&msm_host->clk_mutex);
  415. if (enable) {
  416. ret = dsi_bus_clk_enable(msm_host);
  417. if (ret) {
  418. pr_err("%s: Can not enable bus clk, %d\n",
  419. __func__, ret);
  420. goto unlock_ret;
  421. }
  422. ret = dsi_link_clk_enable(msm_host);
  423. if (ret) {
  424. pr_err("%s: Can not enable link clk, %d\n",
  425. __func__, ret);
  426. dsi_bus_clk_disable(msm_host);
  427. goto unlock_ret;
  428. }
  429. } else {
  430. dsi_link_clk_disable(msm_host);
  431. dsi_bus_clk_disable(msm_host);
  432. }
  433. unlock_ret:
  434. mutex_unlock(&msm_host->clk_mutex);
  435. return ret;
  436. }
  437. static int dsi_calc_clk_rate(struct msm_dsi_host *msm_host)
  438. {
  439. struct drm_display_mode *mode = msm_host->mode;
  440. u8 lanes = msm_host->lanes;
  441. u32 bpp = dsi_get_bpp(msm_host->format);
  442. u32 pclk_rate;
  443. if (!mode) {
  444. pr_err("%s: mode not set\n", __func__);
  445. return -EINVAL;
  446. }
  447. pclk_rate = mode->clock * 1000;
  448. if (lanes > 0) {
  449. msm_host->byte_clk_rate = (pclk_rate * bpp) / (8 * lanes);
  450. } else {
  451. pr_err("%s: forcing mdss_dsi lanes to 1\n", __func__);
  452. msm_host->byte_clk_rate = (pclk_rate * bpp) / 8;
  453. }
  454. DBG("pclk=%d, bclk=%d", pclk_rate, msm_host->byte_clk_rate);
  455. return 0;
  456. }
  457. static void dsi_phy_sw_reset(struct msm_dsi_host *msm_host)
  458. {
  459. DBG("");
  460. dsi_write(msm_host, REG_DSI_PHY_RESET, DSI_PHY_RESET_RESET);
  461. /* Make sure fully reset */
  462. wmb();
  463. udelay(1000);
  464. dsi_write(msm_host, REG_DSI_PHY_RESET, 0);
  465. udelay(100);
  466. }
  467. static void dsi_intr_ctrl(struct msm_dsi_host *msm_host, u32 mask, int enable)
  468. {
  469. u32 intr;
  470. unsigned long flags;
  471. spin_lock_irqsave(&msm_host->intr_lock, flags);
  472. intr = dsi_read(msm_host, REG_DSI_INTR_CTRL);
  473. if (enable)
  474. intr |= mask;
  475. else
  476. intr &= ~mask;
  477. DBG("intr=%x enable=%d", intr, enable);
  478. dsi_write(msm_host, REG_DSI_INTR_CTRL, intr);
  479. spin_unlock_irqrestore(&msm_host->intr_lock, flags);
  480. }
  481. static inline enum dsi_traffic_mode dsi_get_traffic_mode(const u32 mode_flags)
  482. {
  483. if (mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
  484. return BURST_MODE;
  485. else if (mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
  486. return NON_BURST_SYNCH_PULSE;
  487. return NON_BURST_SYNCH_EVENT;
  488. }
  489. static inline enum dsi_vid_dst_format dsi_get_vid_fmt(
  490. const enum mipi_dsi_pixel_format mipi_fmt)
  491. {
  492. switch (mipi_fmt) {
  493. case MIPI_DSI_FMT_RGB888: return VID_DST_FORMAT_RGB888;
  494. case MIPI_DSI_FMT_RGB666: return VID_DST_FORMAT_RGB666_LOOSE;
  495. case MIPI_DSI_FMT_RGB666_PACKED: return VID_DST_FORMAT_RGB666;
  496. case MIPI_DSI_FMT_RGB565: return VID_DST_FORMAT_RGB565;
  497. default: return VID_DST_FORMAT_RGB888;
  498. }
  499. }
  500. static inline enum dsi_cmd_dst_format dsi_get_cmd_fmt(
  501. const enum mipi_dsi_pixel_format mipi_fmt)
  502. {
  503. switch (mipi_fmt) {
  504. case MIPI_DSI_FMT_RGB888: return CMD_DST_FORMAT_RGB888;
  505. case MIPI_DSI_FMT_RGB666_PACKED:
  506. case MIPI_DSI_FMT_RGB666: return VID_DST_FORMAT_RGB666;
  507. case MIPI_DSI_FMT_RGB565: return CMD_DST_FORMAT_RGB565;
  508. default: return CMD_DST_FORMAT_RGB888;
  509. }
  510. }
  511. static void dsi_ctrl_config(struct msm_dsi_host *msm_host, bool enable,
  512. u32 clk_pre, u32 clk_post)
  513. {
  514. u32 flags = msm_host->mode_flags;
  515. enum mipi_dsi_pixel_format mipi_fmt = msm_host->format;
  516. const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
  517. u32 data = 0;
  518. if (!enable) {
  519. dsi_write(msm_host, REG_DSI_CTRL, 0);
  520. return;
  521. }
  522. if (flags & MIPI_DSI_MODE_VIDEO) {
  523. if (flags & MIPI_DSI_MODE_VIDEO_HSE)
  524. data |= DSI_VID_CFG0_PULSE_MODE_HSA_HE;
  525. if (flags & MIPI_DSI_MODE_VIDEO_HFP)
  526. data |= DSI_VID_CFG0_HFP_POWER_STOP;
  527. if (flags & MIPI_DSI_MODE_VIDEO_HBP)
  528. data |= DSI_VID_CFG0_HBP_POWER_STOP;
  529. if (flags & MIPI_DSI_MODE_VIDEO_HSA)
  530. data |= DSI_VID_CFG0_HSA_POWER_STOP;
  531. /* Always set low power stop mode for BLLP
  532. * to let command engine send packets
  533. */
  534. data |= DSI_VID_CFG0_EOF_BLLP_POWER_STOP |
  535. DSI_VID_CFG0_BLLP_POWER_STOP;
  536. data |= DSI_VID_CFG0_TRAFFIC_MODE(dsi_get_traffic_mode(flags));
  537. data |= DSI_VID_CFG0_DST_FORMAT(dsi_get_vid_fmt(mipi_fmt));
  538. data |= DSI_VID_CFG0_VIRT_CHANNEL(msm_host->channel);
  539. dsi_write(msm_host, REG_DSI_VID_CFG0, data);
  540. /* Do not swap RGB colors */
  541. data = DSI_VID_CFG1_RGB_SWAP(SWAP_RGB);
  542. dsi_write(msm_host, REG_DSI_VID_CFG1, 0);
  543. } else {
  544. /* Do not swap RGB colors */
  545. data = DSI_CMD_CFG0_RGB_SWAP(SWAP_RGB);
  546. data |= DSI_CMD_CFG0_DST_FORMAT(dsi_get_cmd_fmt(mipi_fmt));
  547. dsi_write(msm_host, REG_DSI_CMD_CFG0, data);
  548. data = DSI_CMD_CFG1_WR_MEM_START(MIPI_DCS_WRITE_MEMORY_START) |
  549. DSI_CMD_CFG1_WR_MEM_CONTINUE(
  550. MIPI_DCS_WRITE_MEMORY_CONTINUE);
  551. /* Always insert DCS command */
  552. data |= DSI_CMD_CFG1_INSERT_DCS_COMMAND;
  553. dsi_write(msm_host, REG_DSI_CMD_CFG1, data);
  554. }
  555. dsi_write(msm_host, REG_DSI_CMD_DMA_CTRL,
  556. DSI_CMD_DMA_CTRL_FROM_FRAME_BUFFER |
  557. DSI_CMD_DMA_CTRL_LOW_POWER);
  558. data = 0;
  559. /* Always assume dedicated TE pin */
  560. data |= DSI_TRIG_CTRL_TE;
  561. data |= DSI_TRIG_CTRL_MDP_TRIGGER(TRIGGER_NONE);
  562. data |= DSI_TRIG_CTRL_DMA_TRIGGER(TRIGGER_SW);
  563. data |= DSI_TRIG_CTRL_STREAM(msm_host->channel);
  564. if ((cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) &&
  565. (cfg_hnd->minor >= MSM_DSI_6G_VER_MINOR_V1_2))
  566. data |= DSI_TRIG_CTRL_BLOCK_DMA_WITHIN_FRAME;
  567. dsi_write(msm_host, REG_DSI_TRIG_CTRL, data);
  568. data = DSI_CLKOUT_TIMING_CTRL_T_CLK_POST(clk_post) |
  569. DSI_CLKOUT_TIMING_CTRL_T_CLK_PRE(clk_pre);
  570. dsi_write(msm_host, REG_DSI_CLKOUT_TIMING_CTRL, data);
  571. data = 0;
  572. if (!(flags & MIPI_DSI_MODE_EOT_PACKET))
  573. data |= DSI_EOT_PACKET_CTRL_TX_EOT_APPEND;
  574. dsi_write(msm_host, REG_DSI_EOT_PACKET_CTRL, data);
  575. /* allow only ack-err-status to generate interrupt */
  576. dsi_write(msm_host, REG_DSI_ERR_INT_MASK0, 0x13ff3fe0);
  577. dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 1);
  578. dsi_write(msm_host, REG_DSI_CLK_CTRL, DSI_CLK_CTRL_ENABLE_CLKS);
  579. data = DSI_CTRL_CLK_EN;
  580. DBG("lane number=%d", msm_host->lanes);
  581. if (msm_host->lanes == 2) {
  582. data |= DSI_CTRL_LANE1 | DSI_CTRL_LANE2;
  583. /* swap lanes for 2-lane panel for better performance */
  584. dsi_write(msm_host, REG_DSI_LANE_SWAP_CTRL,
  585. DSI_LANE_SWAP_CTRL_DLN_SWAP_SEL(LANE_SWAP_1230));
  586. } else {
  587. /* Take 4 lanes as default */
  588. data |= DSI_CTRL_LANE0 | DSI_CTRL_LANE1 | DSI_CTRL_LANE2 |
  589. DSI_CTRL_LANE3;
  590. /* Do not swap lanes for 4-lane panel */
  591. dsi_write(msm_host, REG_DSI_LANE_SWAP_CTRL,
  592. DSI_LANE_SWAP_CTRL_DLN_SWAP_SEL(LANE_SWAP_0123));
  593. }
  594. if (!(flags & MIPI_DSI_CLOCK_NON_CONTINUOUS))
  595. dsi_write(msm_host, REG_DSI_LANE_CTRL,
  596. DSI_LANE_CTRL_CLKLN_HS_FORCE_REQUEST);
  597. data |= DSI_CTRL_ENABLE;
  598. dsi_write(msm_host, REG_DSI_CTRL, data);
  599. }
  600. static void dsi_timing_setup(struct msm_dsi_host *msm_host)
  601. {
  602. struct drm_display_mode *mode = msm_host->mode;
  603. u32 hs_start = 0, vs_start = 0; /* take sync start as 0 */
  604. u32 h_total = mode->htotal;
  605. u32 v_total = mode->vtotal;
  606. u32 hs_end = mode->hsync_end - mode->hsync_start;
  607. u32 vs_end = mode->vsync_end - mode->vsync_start;
  608. u32 ha_start = h_total - mode->hsync_start;
  609. u32 ha_end = ha_start + mode->hdisplay;
  610. u32 va_start = v_total - mode->vsync_start;
  611. u32 va_end = va_start + mode->vdisplay;
  612. u32 wc;
  613. DBG("");
  614. if (msm_host->mode_flags & MIPI_DSI_MODE_VIDEO) {
  615. dsi_write(msm_host, REG_DSI_ACTIVE_H,
  616. DSI_ACTIVE_H_START(ha_start) |
  617. DSI_ACTIVE_H_END(ha_end));
  618. dsi_write(msm_host, REG_DSI_ACTIVE_V,
  619. DSI_ACTIVE_V_START(va_start) |
  620. DSI_ACTIVE_V_END(va_end));
  621. dsi_write(msm_host, REG_DSI_TOTAL,
  622. DSI_TOTAL_H_TOTAL(h_total - 1) |
  623. DSI_TOTAL_V_TOTAL(v_total - 1));
  624. dsi_write(msm_host, REG_DSI_ACTIVE_HSYNC,
  625. DSI_ACTIVE_HSYNC_START(hs_start) |
  626. DSI_ACTIVE_HSYNC_END(hs_end));
  627. dsi_write(msm_host, REG_DSI_ACTIVE_VSYNC_HPOS, 0);
  628. dsi_write(msm_host, REG_DSI_ACTIVE_VSYNC_VPOS,
  629. DSI_ACTIVE_VSYNC_VPOS_START(vs_start) |
  630. DSI_ACTIVE_VSYNC_VPOS_END(vs_end));
  631. } else { /* command mode */
  632. /* image data and 1 byte write_memory_start cmd */
  633. wc = mode->hdisplay * dsi_get_bpp(msm_host->format) / 8 + 1;
  634. dsi_write(msm_host, REG_DSI_CMD_MDP_STREAM_CTRL,
  635. DSI_CMD_MDP_STREAM_CTRL_WORD_COUNT(wc) |
  636. DSI_CMD_MDP_STREAM_CTRL_VIRTUAL_CHANNEL(
  637. msm_host->channel) |
  638. DSI_CMD_MDP_STREAM_CTRL_DATA_TYPE(
  639. MIPI_DSI_DCS_LONG_WRITE));
  640. dsi_write(msm_host, REG_DSI_CMD_MDP_STREAM_TOTAL,
  641. DSI_CMD_MDP_STREAM_TOTAL_H_TOTAL(mode->hdisplay) |
  642. DSI_CMD_MDP_STREAM_TOTAL_V_TOTAL(mode->vdisplay));
  643. }
  644. }
  645. static void dsi_sw_reset(struct msm_dsi_host *msm_host)
  646. {
  647. dsi_write(msm_host, REG_DSI_CLK_CTRL, DSI_CLK_CTRL_ENABLE_CLKS);
  648. wmb(); /* clocks need to be enabled before reset */
  649. dsi_write(msm_host, REG_DSI_RESET, 1);
  650. wmb(); /* make sure reset happen */
  651. dsi_write(msm_host, REG_DSI_RESET, 0);
  652. }
  653. static void dsi_op_mode_config(struct msm_dsi_host *msm_host,
  654. bool video_mode, bool enable)
  655. {
  656. u32 dsi_ctrl;
  657. dsi_ctrl = dsi_read(msm_host, REG_DSI_CTRL);
  658. if (!enable) {
  659. dsi_ctrl &= ~(DSI_CTRL_ENABLE | DSI_CTRL_VID_MODE_EN |
  660. DSI_CTRL_CMD_MODE_EN);
  661. dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_MDP_DONE |
  662. DSI_IRQ_MASK_VIDEO_DONE, 0);
  663. } else {
  664. if (video_mode) {
  665. dsi_ctrl |= DSI_CTRL_VID_MODE_EN;
  666. } else { /* command mode */
  667. dsi_ctrl |= DSI_CTRL_CMD_MODE_EN;
  668. dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_MDP_DONE, 1);
  669. }
  670. dsi_ctrl |= DSI_CTRL_ENABLE;
  671. }
  672. dsi_write(msm_host, REG_DSI_CTRL, dsi_ctrl);
  673. }
  674. static void dsi_set_tx_power_mode(int mode, struct msm_dsi_host *msm_host)
  675. {
  676. u32 data;
  677. data = dsi_read(msm_host, REG_DSI_CMD_DMA_CTRL);
  678. if (mode == 0)
  679. data &= ~DSI_CMD_DMA_CTRL_LOW_POWER;
  680. else
  681. data |= DSI_CMD_DMA_CTRL_LOW_POWER;
  682. dsi_write(msm_host, REG_DSI_CMD_DMA_CTRL, data);
  683. }
  684. static void dsi_wait4video_done(struct msm_dsi_host *msm_host)
  685. {
  686. dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_VIDEO_DONE, 1);
  687. reinit_completion(&msm_host->video_comp);
  688. wait_for_completion_timeout(&msm_host->video_comp,
  689. msecs_to_jiffies(70));
  690. dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_VIDEO_DONE, 0);
  691. }
  692. static void dsi_wait4video_eng_busy(struct msm_dsi_host *msm_host)
  693. {
  694. if (!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO))
  695. return;
  696. if (msm_host->power_on) {
  697. dsi_wait4video_done(msm_host);
  698. /* delay 4 ms to skip BLLP */
  699. usleep_range(2000, 4000);
  700. }
  701. }
  702. /* dsi_cmd */
  703. static int dsi_tx_buf_alloc(struct msm_dsi_host *msm_host, int size)
  704. {
  705. struct drm_device *dev = msm_host->dev;
  706. int ret;
  707. u32 iova;
  708. mutex_lock(&dev->struct_mutex);
  709. msm_host->tx_gem_obj = msm_gem_new(dev, size, MSM_BO_UNCACHED);
  710. if (IS_ERR(msm_host->tx_gem_obj)) {
  711. ret = PTR_ERR(msm_host->tx_gem_obj);
  712. pr_err("%s: failed to allocate gem, %d\n", __func__, ret);
  713. msm_host->tx_gem_obj = NULL;
  714. mutex_unlock(&dev->struct_mutex);
  715. return ret;
  716. }
  717. ret = msm_gem_get_iova_locked(msm_host->tx_gem_obj, 0, &iova);
  718. if (ret) {
  719. pr_err("%s: failed to get iova, %d\n", __func__, ret);
  720. return ret;
  721. }
  722. mutex_unlock(&dev->struct_mutex);
  723. if (iova & 0x07) {
  724. pr_err("%s: buf NOT 8 bytes aligned\n", __func__);
  725. return -EINVAL;
  726. }
  727. return 0;
  728. }
  729. static void dsi_tx_buf_free(struct msm_dsi_host *msm_host)
  730. {
  731. struct drm_device *dev = msm_host->dev;
  732. if (msm_host->tx_gem_obj) {
  733. msm_gem_put_iova(msm_host->tx_gem_obj, 0);
  734. mutex_lock(&dev->struct_mutex);
  735. msm_gem_free_object(msm_host->tx_gem_obj);
  736. msm_host->tx_gem_obj = NULL;
  737. mutex_unlock(&dev->struct_mutex);
  738. }
  739. }
  740. /*
  741. * prepare cmd buffer to be txed
  742. */
  743. static int dsi_cmd_dma_add(struct drm_gem_object *tx_gem,
  744. const struct mipi_dsi_msg *msg)
  745. {
  746. struct mipi_dsi_packet packet;
  747. int len;
  748. int ret;
  749. u8 *data;
  750. ret = mipi_dsi_create_packet(&packet, msg);
  751. if (ret) {
  752. pr_err("%s: create packet failed, %d\n", __func__, ret);
  753. return ret;
  754. }
  755. len = (packet.size + 3) & (~0x3);
  756. if (len > tx_gem->size) {
  757. pr_err("%s: packet size is too big\n", __func__);
  758. return -EINVAL;
  759. }
  760. data = msm_gem_vaddr(tx_gem);
  761. if (IS_ERR(data)) {
  762. ret = PTR_ERR(data);
  763. pr_err("%s: get vaddr failed, %d\n", __func__, ret);
  764. return ret;
  765. }
  766. /* MSM specific command format in memory */
  767. data[0] = packet.header[1];
  768. data[1] = packet.header[2];
  769. data[2] = packet.header[0];
  770. data[3] = BIT(7); /* Last packet */
  771. if (mipi_dsi_packet_format_is_long(msg->type))
  772. data[3] |= BIT(6);
  773. if (msg->rx_buf && msg->rx_len)
  774. data[3] |= BIT(5);
  775. /* Long packet */
  776. if (packet.payload && packet.payload_length)
  777. memcpy(data + 4, packet.payload, packet.payload_length);
  778. /* Append 0xff to the end */
  779. if (packet.size < len)
  780. memset(data + packet.size, 0xff, len - packet.size);
  781. return len;
  782. }
  783. /*
  784. * dsi_short_read1_resp: 1 parameter
  785. */
  786. static int dsi_short_read1_resp(u8 *buf, const struct mipi_dsi_msg *msg)
  787. {
  788. u8 *data = msg->rx_buf;
  789. if (data && (msg->rx_len >= 1)) {
  790. *data = buf[1]; /* strip out dcs type */
  791. return 1;
  792. } else {
  793. pr_err("%s: read data does not match with rx_buf len %zu\n",
  794. __func__, msg->rx_len);
  795. return -EINVAL;
  796. }
  797. }
  798. /*
  799. * dsi_short_read2_resp: 2 parameter
  800. */
  801. static int dsi_short_read2_resp(u8 *buf, const struct mipi_dsi_msg *msg)
  802. {
  803. u8 *data = msg->rx_buf;
  804. if (data && (msg->rx_len >= 2)) {
  805. data[0] = buf[1]; /* strip out dcs type */
  806. data[1] = buf[2];
  807. return 2;
  808. } else {
  809. pr_err("%s: read data does not match with rx_buf len %zu\n",
  810. __func__, msg->rx_len);
  811. return -EINVAL;
  812. }
  813. }
  814. static int dsi_long_read_resp(u8 *buf, const struct mipi_dsi_msg *msg)
  815. {
  816. /* strip out 4 byte dcs header */
  817. if (msg->rx_buf && msg->rx_len)
  818. memcpy(msg->rx_buf, buf + 4, msg->rx_len);
  819. return msg->rx_len;
  820. }
  821. static int dsi_cmd_dma_tx(struct msm_dsi_host *msm_host, int len)
  822. {
  823. int ret;
  824. u32 iova;
  825. bool triggered;
  826. ret = msm_gem_get_iova(msm_host->tx_gem_obj, 0, &iova);
  827. if (ret) {
  828. pr_err("%s: failed to get iova: %d\n", __func__, ret);
  829. return ret;
  830. }
  831. reinit_completion(&msm_host->dma_comp);
  832. dsi_wait4video_eng_busy(msm_host);
  833. triggered = msm_dsi_manager_cmd_xfer_trigger(
  834. msm_host->id, iova, len);
  835. if (triggered) {
  836. ret = wait_for_completion_timeout(&msm_host->dma_comp,
  837. msecs_to_jiffies(200));
  838. DBG("ret=%d", ret);
  839. if (ret == 0)
  840. ret = -ETIMEDOUT;
  841. else
  842. ret = len;
  843. } else
  844. ret = len;
  845. return ret;
  846. }
  847. static int dsi_cmd_dma_rx(struct msm_dsi_host *msm_host,
  848. u8 *buf, int rx_byte, int pkt_size)
  849. {
  850. u32 *lp, *temp, data;
  851. int i, j = 0, cnt;
  852. u32 read_cnt;
  853. u8 reg[16];
  854. int repeated_bytes = 0;
  855. int buf_offset = buf - msm_host->rx_buf;
  856. lp = (u32 *)buf;
  857. temp = (u32 *)reg;
  858. cnt = (rx_byte + 3) >> 2;
  859. if (cnt > 4)
  860. cnt = 4; /* 4 x 32 bits registers only */
  861. if (rx_byte == 4)
  862. read_cnt = 4;
  863. else
  864. read_cnt = pkt_size + 6;
  865. /*
  866. * In case of multiple reads from the panel, after the first read, there
  867. * is possibility that there are some bytes in the payload repeating in
  868. * the RDBK_DATA registers. Since we read all the parameters from the
  869. * panel right from the first byte for every pass. We need to skip the
  870. * repeating bytes and then append the new parameters to the rx buffer.
  871. */
  872. if (read_cnt > 16) {
  873. int bytes_shifted;
  874. /* Any data more than 16 bytes will be shifted out.
  875. * The temp read buffer should already contain these bytes.
  876. * The remaining bytes in read buffer are the repeated bytes.
  877. */
  878. bytes_shifted = read_cnt - 16;
  879. repeated_bytes = buf_offset - bytes_shifted;
  880. }
  881. for (i = cnt - 1; i >= 0; i--) {
  882. data = dsi_read(msm_host, REG_DSI_RDBK_DATA(i));
  883. *temp++ = ntohl(data); /* to host byte order */
  884. DBG("data = 0x%x and ntohl(data) = 0x%x", data, ntohl(data));
  885. }
  886. for (i = repeated_bytes; i < 16; i++)
  887. buf[j++] = reg[i];
  888. return j;
  889. }
  890. static int dsi_cmds2buf_tx(struct msm_dsi_host *msm_host,
  891. const struct mipi_dsi_msg *msg)
  892. {
  893. int len, ret;
  894. int bllp_len = msm_host->mode->hdisplay *
  895. dsi_get_bpp(msm_host->format) / 8;
  896. len = dsi_cmd_dma_add(msm_host->tx_gem_obj, msg);
  897. if (!len) {
  898. pr_err("%s: failed to add cmd type = 0x%x\n",
  899. __func__, msg->type);
  900. return -EINVAL;
  901. }
  902. /* for video mode, do not send cmds more than
  903. * one pixel line, since it only transmit it
  904. * during BLLP.
  905. */
  906. /* TODO: if the command is sent in LP mode, the bit rate is only
  907. * half of esc clk rate. In this case, if the video is already
  908. * actively streaming, we need to check more carefully if the
  909. * command can be fit into one BLLP.
  910. */
  911. if ((msm_host->mode_flags & MIPI_DSI_MODE_VIDEO) && (len > bllp_len)) {
  912. pr_err("%s: cmd cannot fit into BLLP period, len=%d\n",
  913. __func__, len);
  914. return -EINVAL;
  915. }
  916. ret = dsi_cmd_dma_tx(msm_host, len);
  917. if (ret < len) {
  918. pr_err("%s: cmd dma tx failed, type=0x%x, data0=0x%x, len=%d\n",
  919. __func__, msg->type, (*(u8 *)(msg->tx_buf)), len);
  920. return -ECOMM;
  921. }
  922. return len;
  923. }
  924. static void dsi_sw_reset_restore(struct msm_dsi_host *msm_host)
  925. {
  926. u32 data0, data1;
  927. data0 = dsi_read(msm_host, REG_DSI_CTRL);
  928. data1 = data0;
  929. data1 &= ~DSI_CTRL_ENABLE;
  930. dsi_write(msm_host, REG_DSI_CTRL, data1);
  931. /*
  932. * dsi controller need to be disabled before
  933. * clocks turned on
  934. */
  935. wmb();
  936. dsi_write(msm_host, REG_DSI_CLK_CTRL, DSI_CLK_CTRL_ENABLE_CLKS);
  937. wmb(); /* make sure clocks enabled */
  938. /* dsi controller can only be reset while clocks are running */
  939. dsi_write(msm_host, REG_DSI_RESET, 1);
  940. wmb(); /* make sure reset happen */
  941. dsi_write(msm_host, REG_DSI_RESET, 0);
  942. wmb(); /* controller out of reset */
  943. dsi_write(msm_host, REG_DSI_CTRL, data0);
  944. wmb(); /* make sure dsi controller enabled again */
  945. }
  946. static void dsi_err_worker(struct work_struct *work)
  947. {
  948. struct msm_dsi_host *msm_host =
  949. container_of(work, struct msm_dsi_host, err_work);
  950. u32 status = msm_host->err_work_state;
  951. pr_err_ratelimited("%s: status=%x\n", __func__, status);
  952. if (status & DSI_ERR_STATE_MDP_FIFO_UNDERFLOW)
  953. dsi_sw_reset_restore(msm_host);
  954. /* It is safe to clear here because error irq is disabled. */
  955. msm_host->err_work_state = 0;
  956. /* enable dsi error interrupt */
  957. dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 1);
  958. }
  959. static void dsi_ack_err_status(struct msm_dsi_host *msm_host)
  960. {
  961. u32 status;
  962. status = dsi_read(msm_host, REG_DSI_ACK_ERR_STATUS);
  963. if (status) {
  964. dsi_write(msm_host, REG_DSI_ACK_ERR_STATUS, status);
  965. /* Writing of an extra 0 needed to clear error bits */
  966. dsi_write(msm_host, REG_DSI_ACK_ERR_STATUS, 0);
  967. msm_host->err_work_state |= DSI_ERR_STATE_ACK;
  968. }
  969. }
  970. static void dsi_timeout_status(struct msm_dsi_host *msm_host)
  971. {
  972. u32 status;
  973. status = dsi_read(msm_host, REG_DSI_TIMEOUT_STATUS);
  974. if (status) {
  975. dsi_write(msm_host, REG_DSI_TIMEOUT_STATUS, status);
  976. msm_host->err_work_state |= DSI_ERR_STATE_TIMEOUT;
  977. }
  978. }
  979. static void dsi_dln0_phy_err(struct msm_dsi_host *msm_host)
  980. {
  981. u32 status;
  982. status = dsi_read(msm_host, REG_DSI_DLN0_PHY_ERR);
  983. if (status & (DSI_DLN0_PHY_ERR_DLN0_ERR_ESC |
  984. DSI_DLN0_PHY_ERR_DLN0_ERR_SYNC_ESC |
  985. DSI_DLN0_PHY_ERR_DLN0_ERR_CONTROL |
  986. DSI_DLN0_PHY_ERR_DLN0_ERR_CONTENTION_LP0 |
  987. DSI_DLN0_PHY_ERR_DLN0_ERR_CONTENTION_LP1)) {
  988. dsi_write(msm_host, REG_DSI_DLN0_PHY_ERR, status);
  989. msm_host->err_work_state |= DSI_ERR_STATE_DLN0_PHY;
  990. }
  991. }
  992. static void dsi_fifo_status(struct msm_dsi_host *msm_host)
  993. {
  994. u32 status;
  995. status = dsi_read(msm_host, REG_DSI_FIFO_STATUS);
  996. /* fifo underflow, overflow */
  997. if (status) {
  998. dsi_write(msm_host, REG_DSI_FIFO_STATUS, status);
  999. msm_host->err_work_state |= DSI_ERR_STATE_FIFO;
  1000. if (status & DSI_FIFO_STATUS_CMD_MDP_FIFO_UNDERFLOW)
  1001. msm_host->err_work_state |=
  1002. DSI_ERR_STATE_MDP_FIFO_UNDERFLOW;
  1003. }
  1004. }
  1005. static void dsi_status(struct msm_dsi_host *msm_host)
  1006. {
  1007. u32 status;
  1008. status = dsi_read(msm_host, REG_DSI_STATUS0);
  1009. if (status & DSI_STATUS0_INTERLEAVE_OP_CONTENTION) {
  1010. dsi_write(msm_host, REG_DSI_STATUS0, status);
  1011. msm_host->err_work_state |=
  1012. DSI_ERR_STATE_INTERLEAVE_OP_CONTENTION;
  1013. }
  1014. }
  1015. static void dsi_clk_status(struct msm_dsi_host *msm_host)
  1016. {
  1017. u32 status;
  1018. status = dsi_read(msm_host, REG_DSI_CLK_STATUS);
  1019. if (status & DSI_CLK_STATUS_PLL_UNLOCKED) {
  1020. dsi_write(msm_host, REG_DSI_CLK_STATUS, status);
  1021. msm_host->err_work_state |= DSI_ERR_STATE_PLL_UNLOCKED;
  1022. }
  1023. }
  1024. static void dsi_error(struct msm_dsi_host *msm_host)
  1025. {
  1026. /* disable dsi error interrupt */
  1027. dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 0);
  1028. dsi_clk_status(msm_host);
  1029. dsi_fifo_status(msm_host);
  1030. dsi_ack_err_status(msm_host);
  1031. dsi_timeout_status(msm_host);
  1032. dsi_status(msm_host);
  1033. dsi_dln0_phy_err(msm_host);
  1034. queue_work(msm_host->workqueue, &msm_host->err_work);
  1035. }
  1036. static irqreturn_t dsi_host_irq(int irq, void *ptr)
  1037. {
  1038. struct msm_dsi_host *msm_host = ptr;
  1039. u32 isr;
  1040. unsigned long flags;
  1041. if (!msm_host->ctrl_base)
  1042. return IRQ_HANDLED;
  1043. spin_lock_irqsave(&msm_host->intr_lock, flags);
  1044. isr = dsi_read(msm_host, REG_DSI_INTR_CTRL);
  1045. dsi_write(msm_host, REG_DSI_INTR_CTRL, isr);
  1046. spin_unlock_irqrestore(&msm_host->intr_lock, flags);
  1047. DBG("isr=0x%x, id=%d", isr, msm_host->id);
  1048. if (isr & DSI_IRQ_ERROR)
  1049. dsi_error(msm_host);
  1050. if (isr & DSI_IRQ_VIDEO_DONE)
  1051. complete(&msm_host->video_comp);
  1052. if (isr & DSI_IRQ_CMD_DMA_DONE)
  1053. complete(&msm_host->dma_comp);
  1054. return IRQ_HANDLED;
  1055. }
  1056. static int dsi_host_init_panel_gpios(struct msm_dsi_host *msm_host,
  1057. struct device *panel_device)
  1058. {
  1059. msm_host->disp_en_gpio = devm_gpiod_get_optional(panel_device,
  1060. "disp-enable",
  1061. GPIOD_OUT_LOW);
  1062. if (IS_ERR(msm_host->disp_en_gpio)) {
  1063. DBG("cannot get disp-enable-gpios %ld",
  1064. PTR_ERR(msm_host->disp_en_gpio));
  1065. return PTR_ERR(msm_host->disp_en_gpio);
  1066. }
  1067. msm_host->te_gpio = devm_gpiod_get_optional(panel_device, "disp-te",
  1068. GPIOD_IN);
  1069. if (IS_ERR(msm_host->te_gpio)) {
  1070. DBG("cannot get disp-te-gpios %ld", PTR_ERR(msm_host->te_gpio));
  1071. return PTR_ERR(msm_host->te_gpio);
  1072. }
  1073. return 0;
  1074. }
  1075. static int dsi_host_attach(struct mipi_dsi_host *host,
  1076. struct mipi_dsi_device *dsi)
  1077. {
  1078. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1079. int ret;
  1080. msm_host->channel = dsi->channel;
  1081. msm_host->lanes = dsi->lanes;
  1082. msm_host->format = dsi->format;
  1083. msm_host->mode_flags = dsi->mode_flags;
  1084. WARN_ON(dsi->dev.of_node != msm_host->device_node);
  1085. /* Some gpios defined in panel DT need to be controlled by host */
  1086. ret = dsi_host_init_panel_gpios(msm_host, &dsi->dev);
  1087. if (ret)
  1088. return ret;
  1089. DBG("id=%d", msm_host->id);
  1090. if (msm_host->dev)
  1091. drm_helper_hpd_irq_event(msm_host->dev);
  1092. return 0;
  1093. }
  1094. static int dsi_host_detach(struct mipi_dsi_host *host,
  1095. struct mipi_dsi_device *dsi)
  1096. {
  1097. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1098. msm_host->device_node = NULL;
  1099. DBG("id=%d", msm_host->id);
  1100. if (msm_host->dev)
  1101. drm_helper_hpd_irq_event(msm_host->dev);
  1102. return 0;
  1103. }
  1104. static ssize_t dsi_host_transfer(struct mipi_dsi_host *host,
  1105. const struct mipi_dsi_msg *msg)
  1106. {
  1107. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1108. int ret;
  1109. if (!msg || !msm_host->power_on)
  1110. return -EINVAL;
  1111. mutex_lock(&msm_host->cmd_mutex);
  1112. ret = msm_dsi_manager_cmd_xfer(msm_host->id, msg);
  1113. mutex_unlock(&msm_host->cmd_mutex);
  1114. return ret;
  1115. }
  1116. static struct mipi_dsi_host_ops dsi_host_ops = {
  1117. .attach = dsi_host_attach,
  1118. .detach = dsi_host_detach,
  1119. .transfer = dsi_host_transfer,
  1120. };
  1121. static int dsi_host_parse_dt(struct msm_dsi_host *msm_host)
  1122. {
  1123. struct device *dev = &msm_host->pdev->dev;
  1124. struct device_node *np = dev->of_node;
  1125. struct device_node *endpoint, *device_node;
  1126. int ret;
  1127. ret = of_property_read_u32(np, "qcom,dsi-host-index", &msm_host->id);
  1128. if (ret) {
  1129. dev_err(dev, "%s: host index not specified, ret=%d\n",
  1130. __func__, ret);
  1131. return ret;
  1132. }
  1133. /*
  1134. * Get the first endpoint node. In our case, dsi has one output port
  1135. * to which the panel is connected. Don't return an error if a port
  1136. * isn't defined. It's possible that there is nothing connected to
  1137. * the dsi output.
  1138. */
  1139. endpoint = of_graph_get_next_endpoint(np, NULL);
  1140. if (!endpoint) {
  1141. dev_dbg(dev, "%s: no endpoint\n", __func__);
  1142. return 0;
  1143. }
  1144. /* Get panel node from the output port's endpoint data */
  1145. device_node = of_graph_get_remote_port_parent(endpoint);
  1146. if (!device_node) {
  1147. dev_err(dev, "%s: no valid device\n", __func__);
  1148. of_node_put(endpoint);
  1149. return -ENODEV;
  1150. }
  1151. of_node_put(endpoint);
  1152. of_node_put(device_node);
  1153. msm_host->device_node = device_node;
  1154. return 0;
  1155. }
  1156. int msm_dsi_host_init(struct msm_dsi *msm_dsi)
  1157. {
  1158. struct msm_dsi_host *msm_host = NULL;
  1159. struct platform_device *pdev = msm_dsi->pdev;
  1160. int ret;
  1161. msm_host = devm_kzalloc(&pdev->dev, sizeof(*msm_host), GFP_KERNEL);
  1162. if (!msm_host) {
  1163. pr_err("%s: FAILED: cannot alloc dsi host\n",
  1164. __func__);
  1165. ret = -ENOMEM;
  1166. goto fail;
  1167. }
  1168. msm_host->pdev = pdev;
  1169. ret = dsi_host_parse_dt(msm_host);
  1170. if (ret) {
  1171. pr_err("%s: failed to parse dt\n", __func__);
  1172. goto fail;
  1173. }
  1174. ret = dsi_clk_init(msm_host);
  1175. if (ret) {
  1176. pr_err("%s: unable to initialize dsi clks\n", __func__);
  1177. goto fail;
  1178. }
  1179. msm_host->ctrl_base = msm_ioremap(pdev, "dsi_ctrl", "DSI CTRL");
  1180. if (IS_ERR(msm_host->ctrl_base)) {
  1181. pr_err("%s: unable to map Dsi ctrl base\n", __func__);
  1182. ret = PTR_ERR(msm_host->ctrl_base);
  1183. goto fail;
  1184. }
  1185. msm_host->cfg_hnd = dsi_get_config(msm_host);
  1186. if (!msm_host->cfg_hnd) {
  1187. ret = -EINVAL;
  1188. pr_err("%s: get config failed\n", __func__);
  1189. goto fail;
  1190. }
  1191. /* fixup base address by io offset */
  1192. msm_host->ctrl_base += msm_host->cfg_hnd->cfg->io_offset;
  1193. ret = dsi_regulator_init(msm_host);
  1194. if (ret) {
  1195. pr_err("%s: regulator init failed\n", __func__);
  1196. goto fail;
  1197. }
  1198. msm_host->rx_buf = devm_kzalloc(&pdev->dev, SZ_4K, GFP_KERNEL);
  1199. if (!msm_host->rx_buf) {
  1200. pr_err("%s: alloc rx temp buf failed\n", __func__);
  1201. goto fail;
  1202. }
  1203. init_completion(&msm_host->dma_comp);
  1204. init_completion(&msm_host->video_comp);
  1205. mutex_init(&msm_host->dev_mutex);
  1206. mutex_init(&msm_host->cmd_mutex);
  1207. mutex_init(&msm_host->clk_mutex);
  1208. spin_lock_init(&msm_host->intr_lock);
  1209. /* setup workqueue */
  1210. msm_host->workqueue = alloc_ordered_workqueue("dsi_drm_work", 0);
  1211. INIT_WORK(&msm_host->err_work, dsi_err_worker);
  1212. msm_dsi->host = &msm_host->base;
  1213. msm_dsi->id = msm_host->id;
  1214. DBG("Dsi Host %d initialized", msm_host->id);
  1215. return 0;
  1216. fail:
  1217. return ret;
  1218. }
  1219. void msm_dsi_host_destroy(struct mipi_dsi_host *host)
  1220. {
  1221. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1222. DBG("");
  1223. dsi_tx_buf_free(msm_host);
  1224. if (msm_host->workqueue) {
  1225. flush_workqueue(msm_host->workqueue);
  1226. destroy_workqueue(msm_host->workqueue);
  1227. msm_host->workqueue = NULL;
  1228. }
  1229. mutex_destroy(&msm_host->clk_mutex);
  1230. mutex_destroy(&msm_host->cmd_mutex);
  1231. mutex_destroy(&msm_host->dev_mutex);
  1232. }
  1233. int msm_dsi_host_modeset_init(struct mipi_dsi_host *host,
  1234. struct drm_device *dev)
  1235. {
  1236. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1237. struct platform_device *pdev = msm_host->pdev;
  1238. int ret;
  1239. msm_host->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
  1240. if (msm_host->irq < 0) {
  1241. ret = msm_host->irq;
  1242. dev_err(dev->dev, "failed to get irq: %d\n", ret);
  1243. return ret;
  1244. }
  1245. ret = devm_request_irq(&pdev->dev, msm_host->irq,
  1246. dsi_host_irq, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  1247. "dsi_isr", msm_host);
  1248. if (ret < 0) {
  1249. dev_err(&pdev->dev, "failed to request IRQ%u: %d\n",
  1250. msm_host->irq, ret);
  1251. return ret;
  1252. }
  1253. msm_host->dev = dev;
  1254. ret = dsi_tx_buf_alloc(msm_host, SZ_4K);
  1255. if (ret) {
  1256. pr_err("%s: alloc tx gem obj failed, %d\n", __func__, ret);
  1257. return ret;
  1258. }
  1259. return 0;
  1260. }
  1261. int msm_dsi_host_register(struct mipi_dsi_host *host, bool check_defer)
  1262. {
  1263. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1264. int ret;
  1265. /* Register mipi dsi host */
  1266. if (!msm_host->registered) {
  1267. host->dev = &msm_host->pdev->dev;
  1268. host->ops = &dsi_host_ops;
  1269. ret = mipi_dsi_host_register(host);
  1270. if (ret)
  1271. return ret;
  1272. msm_host->registered = true;
  1273. /* If the panel driver has not been probed after host register,
  1274. * we should defer the host's probe.
  1275. * It makes sure panel is connected when fbcon detects
  1276. * connector status and gets the proper display mode to
  1277. * create framebuffer.
  1278. * Don't try to defer if there is nothing connected to the dsi
  1279. * output
  1280. */
  1281. if (check_defer && msm_host->device_node) {
  1282. if (!of_drm_find_panel(msm_host->device_node))
  1283. if (!of_drm_find_bridge(msm_host->device_node))
  1284. return -EPROBE_DEFER;
  1285. }
  1286. }
  1287. return 0;
  1288. }
  1289. void msm_dsi_host_unregister(struct mipi_dsi_host *host)
  1290. {
  1291. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1292. if (msm_host->registered) {
  1293. mipi_dsi_host_unregister(host);
  1294. host->dev = NULL;
  1295. host->ops = NULL;
  1296. msm_host->registered = false;
  1297. }
  1298. }
  1299. int msm_dsi_host_xfer_prepare(struct mipi_dsi_host *host,
  1300. const struct mipi_dsi_msg *msg)
  1301. {
  1302. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1303. /* TODO: make sure dsi_cmd_mdp is idle.
  1304. * Since DSI6G v1.2.0, we can set DSI_TRIG_CTRL.BLOCK_DMA_WITHIN_FRAME
  1305. * to ask H/W to wait until cmd mdp is idle. S/W wait is not needed.
  1306. * How to handle the old versions? Wait for mdp cmd done?
  1307. */
  1308. /*
  1309. * mdss interrupt is generated in mdp core clock domain
  1310. * mdp clock need to be enabled to receive dsi interrupt
  1311. */
  1312. dsi_clk_ctrl(msm_host, 1);
  1313. /* TODO: vote for bus bandwidth */
  1314. if (!(msg->flags & MIPI_DSI_MSG_USE_LPM))
  1315. dsi_set_tx_power_mode(0, msm_host);
  1316. msm_host->dma_cmd_ctrl_restore = dsi_read(msm_host, REG_DSI_CTRL);
  1317. dsi_write(msm_host, REG_DSI_CTRL,
  1318. msm_host->dma_cmd_ctrl_restore |
  1319. DSI_CTRL_CMD_MODE_EN |
  1320. DSI_CTRL_ENABLE);
  1321. dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_DMA_DONE, 1);
  1322. return 0;
  1323. }
  1324. void msm_dsi_host_xfer_restore(struct mipi_dsi_host *host,
  1325. const struct mipi_dsi_msg *msg)
  1326. {
  1327. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1328. dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_DMA_DONE, 0);
  1329. dsi_write(msm_host, REG_DSI_CTRL, msm_host->dma_cmd_ctrl_restore);
  1330. if (!(msg->flags & MIPI_DSI_MSG_USE_LPM))
  1331. dsi_set_tx_power_mode(1, msm_host);
  1332. /* TODO: unvote for bus bandwidth */
  1333. dsi_clk_ctrl(msm_host, 0);
  1334. }
  1335. int msm_dsi_host_cmd_tx(struct mipi_dsi_host *host,
  1336. const struct mipi_dsi_msg *msg)
  1337. {
  1338. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1339. return dsi_cmds2buf_tx(msm_host, msg);
  1340. }
  1341. int msm_dsi_host_cmd_rx(struct mipi_dsi_host *host,
  1342. const struct mipi_dsi_msg *msg)
  1343. {
  1344. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1345. const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
  1346. int data_byte, rx_byte, dlen, end;
  1347. int short_response, diff, pkt_size, ret = 0;
  1348. char cmd;
  1349. int rlen = msg->rx_len;
  1350. u8 *buf;
  1351. if (rlen <= 2) {
  1352. short_response = 1;
  1353. pkt_size = rlen;
  1354. rx_byte = 4;
  1355. } else {
  1356. short_response = 0;
  1357. data_byte = 10; /* first read */
  1358. if (rlen < data_byte)
  1359. pkt_size = rlen;
  1360. else
  1361. pkt_size = data_byte;
  1362. rx_byte = data_byte + 6; /* 4 header + 2 crc */
  1363. }
  1364. buf = msm_host->rx_buf;
  1365. end = 0;
  1366. while (!end) {
  1367. u8 tx[2] = {pkt_size & 0xff, pkt_size >> 8};
  1368. struct mipi_dsi_msg max_pkt_size_msg = {
  1369. .channel = msg->channel,
  1370. .type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
  1371. .tx_len = 2,
  1372. .tx_buf = tx,
  1373. };
  1374. DBG("rlen=%d pkt_size=%d rx_byte=%d",
  1375. rlen, pkt_size, rx_byte);
  1376. ret = dsi_cmds2buf_tx(msm_host, &max_pkt_size_msg);
  1377. if (ret < 2) {
  1378. pr_err("%s: Set max pkt size failed, %d\n",
  1379. __func__, ret);
  1380. return -EINVAL;
  1381. }
  1382. if ((cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) &&
  1383. (cfg_hnd->minor >= MSM_DSI_6G_VER_MINOR_V1_1)) {
  1384. /* Clear the RDBK_DATA registers */
  1385. dsi_write(msm_host, REG_DSI_RDBK_DATA_CTRL,
  1386. DSI_RDBK_DATA_CTRL_CLR);
  1387. wmb(); /* make sure the RDBK registers are cleared */
  1388. dsi_write(msm_host, REG_DSI_RDBK_DATA_CTRL, 0);
  1389. wmb(); /* release cleared status before transfer */
  1390. }
  1391. ret = dsi_cmds2buf_tx(msm_host, msg);
  1392. if (ret < msg->tx_len) {
  1393. pr_err("%s: Read cmd Tx failed, %d\n", __func__, ret);
  1394. return ret;
  1395. }
  1396. /*
  1397. * once cmd_dma_done interrupt received,
  1398. * return data from client is ready and stored
  1399. * at RDBK_DATA register already
  1400. * since rx fifo is 16 bytes, dcs header is kept at first loop,
  1401. * after that dcs header lost during shift into registers
  1402. */
  1403. dlen = dsi_cmd_dma_rx(msm_host, buf, rx_byte, pkt_size);
  1404. if (dlen <= 0)
  1405. return 0;
  1406. if (short_response)
  1407. break;
  1408. if (rlen <= data_byte) {
  1409. diff = data_byte - rlen;
  1410. end = 1;
  1411. } else {
  1412. diff = 0;
  1413. rlen -= data_byte;
  1414. }
  1415. if (!end) {
  1416. dlen -= 2; /* 2 crc */
  1417. dlen -= diff;
  1418. buf += dlen; /* next start position */
  1419. data_byte = 14; /* NOT first read */
  1420. if (rlen < data_byte)
  1421. pkt_size += rlen;
  1422. else
  1423. pkt_size += data_byte;
  1424. DBG("buf=%p dlen=%d diff=%d", buf, dlen, diff);
  1425. }
  1426. }
  1427. /*
  1428. * For single Long read, if the requested rlen < 10,
  1429. * we need to shift the start position of rx
  1430. * data buffer to skip the bytes which are not
  1431. * updated.
  1432. */
  1433. if (pkt_size < 10 && !short_response)
  1434. buf = msm_host->rx_buf + (10 - rlen);
  1435. else
  1436. buf = msm_host->rx_buf;
  1437. cmd = buf[0];
  1438. switch (cmd) {
  1439. case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
  1440. pr_err("%s: rx ACK_ERR_PACLAGE\n", __func__);
  1441. ret = 0;
  1442. break;
  1443. case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE:
  1444. case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
  1445. ret = dsi_short_read1_resp(buf, msg);
  1446. break;
  1447. case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE:
  1448. case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
  1449. ret = dsi_short_read2_resp(buf, msg);
  1450. break;
  1451. case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
  1452. case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
  1453. ret = dsi_long_read_resp(buf, msg);
  1454. break;
  1455. default:
  1456. pr_warn("%s:Invalid response cmd\n", __func__);
  1457. ret = 0;
  1458. }
  1459. return ret;
  1460. }
  1461. void msm_dsi_host_cmd_xfer_commit(struct mipi_dsi_host *host, u32 iova, u32 len)
  1462. {
  1463. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1464. dsi_write(msm_host, REG_DSI_DMA_BASE, iova);
  1465. dsi_write(msm_host, REG_DSI_DMA_LEN, len);
  1466. dsi_write(msm_host, REG_DSI_TRIG_DMA, 1);
  1467. /* Make sure trigger happens */
  1468. wmb();
  1469. }
  1470. int msm_dsi_host_set_src_pll(struct mipi_dsi_host *host,
  1471. struct msm_dsi_pll *src_pll)
  1472. {
  1473. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1474. struct clk *byte_clk_provider, *pixel_clk_provider;
  1475. int ret;
  1476. ret = msm_dsi_pll_get_clk_provider(src_pll,
  1477. &byte_clk_provider, &pixel_clk_provider);
  1478. if (ret) {
  1479. pr_info("%s: can't get provider from pll, don't set parent\n",
  1480. __func__);
  1481. return 0;
  1482. }
  1483. ret = clk_set_parent(msm_host->byte_clk_src, byte_clk_provider);
  1484. if (ret) {
  1485. pr_err("%s: can't set parent to byte_clk_src. ret=%d\n",
  1486. __func__, ret);
  1487. goto exit;
  1488. }
  1489. ret = clk_set_parent(msm_host->pixel_clk_src, pixel_clk_provider);
  1490. if (ret) {
  1491. pr_err("%s: can't set parent to pixel_clk_src. ret=%d\n",
  1492. __func__, ret);
  1493. goto exit;
  1494. }
  1495. exit:
  1496. return ret;
  1497. }
  1498. int msm_dsi_host_enable(struct mipi_dsi_host *host)
  1499. {
  1500. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1501. dsi_op_mode_config(msm_host,
  1502. !!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO), true);
  1503. /* TODO: clock should be turned off for command mode,
  1504. * and only turned on before MDP START.
  1505. * This part of code should be enabled once mdp driver support it.
  1506. */
  1507. /* if (msm_panel->mode == MSM_DSI_CMD_MODE)
  1508. dsi_clk_ctrl(msm_host, 0); */
  1509. return 0;
  1510. }
  1511. int msm_dsi_host_disable(struct mipi_dsi_host *host)
  1512. {
  1513. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1514. dsi_op_mode_config(msm_host,
  1515. !!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO), false);
  1516. /* Since we have disabled INTF, the video engine won't stop so that
  1517. * the cmd engine will be blocked.
  1518. * Reset to disable video engine so that we can send off cmd.
  1519. */
  1520. dsi_sw_reset(msm_host);
  1521. return 0;
  1522. }
  1523. int msm_dsi_host_power_on(struct mipi_dsi_host *host)
  1524. {
  1525. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1526. u32 clk_pre = 0, clk_post = 0;
  1527. int ret = 0;
  1528. mutex_lock(&msm_host->dev_mutex);
  1529. if (msm_host->power_on) {
  1530. DBG("dsi host already on");
  1531. goto unlock_ret;
  1532. }
  1533. ret = dsi_calc_clk_rate(msm_host);
  1534. if (ret) {
  1535. pr_err("%s: unable to calc clk rate, %d\n", __func__, ret);
  1536. goto unlock_ret;
  1537. }
  1538. ret = dsi_host_regulator_enable(msm_host);
  1539. if (ret) {
  1540. pr_err("%s:Failed to enable vregs.ret=%d\n",
  1541. __func__, ret);
  1542. goto unlock_ret;
  1543. }
  1544. ret = dsi_bus_clk_enable(msm_host);
  1545. if (ret) {
  1546. pr_err("%s: failed to enable bus clocks, %d\n", __func__, ret);
  1547. goto fail_disable_reg;
  1548. }
  1549. dsi_phy_sw_reset(msm_host);
  1550. ret = msm_dsi_manager_phy_enable(msm_host->id,
  1551. msm_host->byte_clk_rate * 8,
  1552. clk_get_rate(msm_host->esc_clk),
  1553. &clk_pre, &clk_post);
  1554. dsi_bus_clk_disable(msm_host);
  1555. if (ret) {
  1556. pr_err("%s: failed to enable phy, %d\n", __func__, ret);
  1557. goto fail_disable_reg;
  1558. }
  1559. ret = dsi_clk_ctrl(msm_host, 1);
  1560. if (ret) {
  1561. pr_err("%s: failed to enable clocks. ret=%d\n", __func__, ret);
  1562. goto fail_disable_reg;
  1563. }
  1564. ret = pinctrl_pm_select_default_state(&msm_host->pdev->dev);
  1565. if (ret) {
  1566. pr_err("%s: failed to set pinctrl default state, %d\n",
  1567. __func__, ret);
  1568. goto fail_disable_clk;
  1569. }
  1570. dsi_timing_setup(msm_host);
  1571. dsi_sw_reset(msm_host);
  1572. dsi_ctrl_config(msm_host, true, clk_pre, clk_post);
  1573. if (msm_host->disp_en_gpio)
  1574. gpiod_set_value(msm_host->disp_en_gpio, 1);
  1575. msm_host->power_on = true;
  1576. mutex_unlock(&msm_host->dev_mutex);
  1577. return 0;
  1578. fail_disable_clk:
  1579. dsi_clk_ctrl(msm_host, 0);
  1580. fail_disable_reg:
  1581. dsi_host_regulator_disable(msm_host);
  1582. unlock_ret:
  1583. mutex_unlock(&msm_host->dev_mutex);
  1584. return ret;
  1585. }
  1586. int msm_dsi_host_power_off(struct mipi_dsi_host *host)
  1587. {
  1588. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1589. mutex_lock(&msm_host->dev_mutex);
  1590. if (!msm_host->power_on) {
  1591. DBG("dsi host already off");
  1592. goto unlock_ret;
  1593. }
  1594. dsi_ctrl_config(msm_host, false, 0, 0);
  1595. if (msm_host->disp_en_gpio)
  1596. gpiod_set_value(msm_host->disp_en_gpio, 0);
  1597. pinctrl_pm_select_sleep_state(&msm_host->pdev->dev);
  1598. msm_dsi_manager_phy_disable(msm_host->id);
  1599. dsi_clk_ctrl(msm_host, 0);
  1600. dsi_host_regulator_disable(msm_host);
  1601. DBG("-");
  1602. msm_host->power_on = false;
  1603. unlock_ret:
  1604. mutex_unlock(&msm_host->dev_mutex);
  1605. return 0;
  1606. }
  1607. int msm_dsi_host_set_display_mode(struct mipi_dsi_host *host,
  1608. struct drm_display_mode *mode)
  1609. {
  1610. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1611. if (msm_host->mode) {
  1612. drm_mode_destroy(msm_host->dev, msm_host->mode);
  1613. msm_host->mode = NULL;
  1614. }
  1615. msm_host->mode = drm_mode_duplicate(msm_host->dev, mode);
  1616. if (IS_ERR(msm_host->mode)) {
  1617. pr_err("%s: cannot duplicate mode\n", __func__);
  1618. return PTR_ERR(msm_host->mode);
  1619. }
  1620. return 0;
  1621. }
  1622. struct drm_panel *msm_dsi_host_get_panel(struct mipi_dsi_host *host,
  1623. unsigned long *panel_flags)
  1624. {
  1625. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1626. struct drm_panel *panel;
  1627. panel = of_drm_find_panel(msm_host->device_node);
  1628. if (panel_flags)
  1629. *panel_flags = msm_host->mode_flags;
  1630. return panel;
  1631. }
  1632. struct drm_bridge *msm_dsi_host_get_bridge(struct mipi_dsi_host *host)
  1633. {
  1634. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1635. return of_drm_find_bridge(msm_host->device_node);
  1636. }