dsi_host.c 49 KB

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