dsi_host.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994
  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. bool ack_error = false;
  937. u32 read_cnt;
  938. u8 reg[16];
  939. int repeated_bytes = 0;
  940. int buf_offset = buf - msm_host->rx_buf;
  941. lp = (u32 *)buf;
  942. temp = (u32 *)reg;
  943. cnt = (rx_byte + 3) >> 2;
  944. if (cnt > 4)
  945. cnt = 4; /* 4 x 32 bits registers only */
  946. /* Calculate real read data count */
  947. read_cnt = dsi_read(msm_host, 0x1d4) >> 16;
  948. ack_error = (rx_byte == 4) ?
  949. (read_cnt == 8) : /* short pkt + 4-byte error pkt */
  950. (read_cnt == (pkt_size + 6 + 4)); /* long pkt+4-byte error pkt*/
  951. if (ack_error)
  952. read_cnt -= 4; /* Remove 4 byte error pkt */
  953. /*
  954. * In case of multiple reads from the panel, after the first read, there
  955. * is possibility that there are some bytes in the payload repeating in
  956. * the RDBK_DATA registers. Since we read all the parameters from the
  957. * panel right from the first byte for every pass. We need to skip the
  958. * repeating bytes and then append the new parameters to the rx buffer.
  959. */
  960. if (read_cnt > 16) {
  961. int bytes_shifted;
  962. /* Any data more than 16 bytes will be shifted out.
  963. * The temp read buffer should already contain these bytes.
  964. * The remaining bytes in read buffer are the repeated bytes.
  965. */
  966. bytes_shifted = read_cnt - 16;
  967. repeated_bytes = buf_offset - bytes_shifted;
  968. }
  969. for (i = cnt - 1; i >= 0; i--) {
  970. data = dsi_read(msm_host, REG_DSI_RDBK_DATA(i));
  971. *temp++ = ntohl(data); /* to host byte order */
  972. DBG("data = 0x%x and ntohl(data) = 0x%x", data, ntohl(data));
  973. }
  974. for (i = repeated_bytes; i < 16; i++)
  975. buf[j++] = reg[i];
  976. return j;
  977. }
  978. static int dsi_cmds2buf_tx(struct msm_dsi_host *msm_host,
  979. const struct mipi_dsi_msg *msg)
  980. {
  981. int len, ret;
  982. int bllp_len = msm_host->mode->hdisplay *
  983. dsi_get_bpp(msm_host->format) / 8;
  984. len = dsi_cmd_dma_add(msm_host->tx_gem_obj, msg);
  985. if (!len) {
  986. pr_err("%s: failed to add cmd type = 0x%x\n",
  987. __func__, msg->type);
  988. return -EINVAL;
  989. }
  990. /* for video mode, do not send cmds more than
  991. * one pixel line, since it only transmit it
  992. * during BLLP.
  993. */
  994. /* TODO: if the command is sent in LP mode, the bit rate is only
  995. * half of esc clk rate. In this case, if the video is already
  996. * actively streaming, we need to check more carefully if the
  997. * command can be fit into one BLLP.
  998. */
  999. if ((msm_host->mode_flags & MIPI_DSI_MODE_VIDEO) && (len > bllp_len)) {
  1000. pr_err("%s: cmd cannot fit into BLLP period, len=%d\n",
  1001. __func__, len);
  1002. return -EINVAL;
  1003. }
  1004. ret = dsi_cmd_dma_tx(msm_host, len);
  1005. if (ret < len) {
  1006. pr_err("%s: cmd dma tx failed, type=0x%x, data0=0x%x, len=%d\n",
  1007. __func__, msg->type, (*(u8 *)(msg->tx_buf)), len);
  1008. return -ECOMM;
  1009. }
  1010. return len;
  1011. }
  1012. static void dsi_sw_reset_restore(struct msm_dsi_host *msm_host)
  1013. {
  1014. u32 data0, data1;
  1015. data0 = dsi_read(msm_host, REG_DSI_CTRL);
  1016. data1 = data0;
  1017. data1 &= ~DSI_CTRL_ENABLE;
  1018. dsi_write(msm_host, REG_DSI_CTRL, data1);
  1019. /*
  1020. * dsi controller need to be disabled before
  1021. * clocks turned on
  1022. */
  1023. wmb();
  1024. dsi_write(msm_host, REG_DSI_CLK_CTRL, DSI_CLK_CTRL_ENABLE_CLKS);
  1025. wmb(); /* make sure clocks enabled */
  1026. /* dsi controller can only be reset while clocks are running */
  1027. dsi_write(msm_host, REG_DSI_RESET, 1);
  1028. wmb(); /* make sure reset happen */
  1029. dsi_write(msm_host, REG_DSI_RESET, 0);
  1030. wmb(); /* controller out of reset */
  1031. dsi_write(msm_host, REG_DSI_CTRL, data0);
  1032. wmb(); /* make sure dsi controller enabled again */
  1033. }
  1034. static void dsi_err_worker(struct work_struct *work)
  1035. {
  1036. struct msm_dsi_host *msm_host =
  1037. container_of(work, struct msm_dsi_host, err_work);
  1038. u32 status = msm_host->err_work_state;
  1039. pr_err_ratelimited("%s: status=%x\n", __func__, status);
  1040. if (status & DSI_ERR_STATE_MDP_FIFO_UNDERFLOW)
  1041. dsi_sw_reset_restore(msm_host);
  1042. /* It is safe to clear here because error irq is disabled. */
  1043. msm_host->err_work_state = 0;
  1044. /* enable dsi error interrupt */
  1045. dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 1);
  1046. }
  1047. static void dsi_ack_err_status(struct msm_dsi_host *msm_host)
  1048. {
  1049. u32 status;
  1050. status = dsi_read(msm_host, REG_DSI_ACK_ERR_STATUS);
  1051. if (status) {
  1052. dsi_write(msm_host, REG_DSI_ACK_ERR_STATUS, status);
  1053. /* Writing of an extra 0 needed to clear error bits */
  1054. dsi_write(msm_host, REG_DSI_ACK_ERR_STATUS, 0);
  1055. msm_host->err_work_state |= DSI_ERR_STATE_ACK;
  1056. }
  1057. }
  1058. static void dsi_timeout_status(struct msm_dsi_host *msm_host)
  1059. {
  1060. u32 status;
  1061. status = dsi_read(msm_host, REG_DSI_TIMEOUT_STATUS);
  1062. if (status) {
  1063. dsi_write(msm_host, REG_DSI_TIMEOUT_STATUS, status);
  1064. msm_host->err_work_state |= DSI_ERR_STATE_TIMEOUT;
  1065. }
  1066. }
  1067. static void dsi_dln0_phy_err(struct msm_dsi_host *msm_host)
  1068. {
  1069. u32 status;
  1070. status = dsi_read(msm_host, REG_DSI_DLN0_PHY_ERR);
  1071. if (status) {
  1072. dsi_write(msm_host, REG_DSI_DLN0_PHY_ERR, status);
  1073. msm_host->err_work_state |= DSI_ERR_STATE_DLN0_PHY;
  1074. }
  1075. }
  1076. static void dsi_fifo_status(struct msm_dsi_host *msm_host)
  1077. {
  1078. u32 status;
  1079. status = dsi_read(msm_host, REG_DSI_FIFO_STATUS);
  1080. /* fifo underflow, overflow */
  1081. if (status) {
  1082. dsi_write(msm_host, REG_DSI_FIFO_STATUS, status);
  1083. msm_host->err_work_state |= DSI_ERR_STATE_FIFO;
  1084. if (status & DSI_FIFO_STATUS_CMD_MDP_FIFO_UNDERFLOW)
  1085. msm_host->err_work_state |=
  1086. DSI_ERR_STATE_MDP_FIFO_UNDERFLOW;
  1087. }
  1088. }
  1089. static void dsi_status(struct msm_dsi_host *msm_host)
  1090. {
  1091. u32 status;
  1092. status = dsi_read(msm_host, REG_DSI_STATUS0);
  1093. if (status & DSI_STATUS0_INTERLEAVE_OP_CONTENTION) {
  1094. dsi_write(msm_host, REG_DSI_STATUS0, status);
  1095. msm_host->err_work_state |=
  1096. DSI_ERR_STATE_INTERLEAVE_OP_CONTENTION;
  1097. }
  1098. }
  1099. static void dsi_clk_status(struct msm_dsi_host *msm_host)
  1100. {
  1101. u32 status;
  1102. status = dsi_read(msm_host, REG_DSI_CLK_STATUS);
  1103. if (status & DSI_CLK_STATUS_PLL_UNLOCKED) {
  1104. dsi_write(msm_host, REG_DSI_CLK_STATUS, status);
  1105. msm_host->err_work_state |= DSI_ERR_STATE_PLL_UNLOCKED;
  1106. }
  1107. }
  1108. static void dsi_error(struct msm_dsi_host *msm_host)
  1109. {
  1110. /* disable dsi error interrupt */
  1111. dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 0);
  1112. dsi_clk_status(msm_host);
  1113. dsi_fifo_status(msm_host);
  1114. dsi_ack_err_status(msm_host);
  1115. dsi_timeout_status(msm_host);
  1116. dsi_status(msm_host);
  1117. dsi_dln0_phy_err(msm_host);
  1118. queue_work(msm_host->workqueue, &msm_host->err_work);
  1119. }
  1120. static irqreturn_t dsi_host_irq(int irq, void *ptr)
  1121. {
  1122. struct msm_dsi_host *msm_host = ptr;
  1123. u32 isr;
  1124. unsigned long flags;
  1125. if (!msm_host->ctrl_base)
  1126. return IRQ_HANDLED;
  1127. spin_lock_irqsave(&msm_host->intr_lock, flags);
  1128. isr = dsi_read(msm_host, REG_DSI_INTR_CTRL);
  1129. dsi_write(msm_host, REG_DSI_INTR_CTRL, isr);
  1130. spin_unlock_irqrestore(&msm_host->intr_lock, flags);
  1131. DBG("isr=0x%x, id=%d", isr, msm_host->id);
  1132. if (isr & DSI_IRQ_ERROR)
  1133. dsi_error(msm_host);
  1134. if (isr & DSI_IRQ_VIDEO_DONE)
  1135. complete(&msm_host->video_comp);
  1136. if (isr & DSI_IRQ_CMD_DMA_DONE)
  1137. complete(&msm_host->dma_comp);
  1138. return IRQ_HANDLED;
  1139. }
  1140. static int dsi_host_init_panel_gpios(struct msm_dsi_host *msm_host,
  1141. struct device *panel_device)
  1142. {
  1143. int ret;
  1144. msm_host->disp_en_gpio = devm_gpiod_get(panel_device,
  1145. "disp-enable");
  1146. if (IS_ERR(msm_host->disp_en_gpio)) {
  1147. DBG("cannot get disp-enable-gpios %ld",
  1148. PTR_ERR(msm_host->disp_en_gpio));
  1149. msm_host->disp_en_gpio = NULL;
  1150. }
  1151. if (msm_host->disp_en_gpio) {
  1152. ret = gpiod_direction_output(msm_host->disp_en_gpio, 0);
  1153. if (ret) {
  1154. pr_err("cannot set dir to disp-en-gpios %d\n", ret);
  1155. return ret;
  1156. }
  1157. }
  1158. msm_host->te_gpio = devm_gpiod_get(panel_device, "disp-te");
  1159. if (IS_ERR(msm_host->te_gpio)) {
  1160. DBG("cannot get disp-te-gpios %ld", PTR_ERR(msm_host->te_gpio));
  1161. msm_host->te_gpio = NULL;
  1162. }
  1163. if (msm_host->te_gpio) {
  1164. ret = gpiod_direction_input(msm_host->te_gpio);
  1165. if (ret) {
  1166. pr_err("%s: cannot set dir to disp-te-gpios, %d\n",
  1167. __func__, ret);
  1168. return ret;
  1169. }
  1170. }
  1171. return 0;
  1172. }
  1173. static int dsi_host_attach(struct mipi_dsi_host *host,
  1174. struct mipi_dsi_device *dsi)
  1175. {
  1176. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1177. int ret;
  1178. msm_host->channel = dsi->channel;
  1179. msm_host->lanes = dsi->lanes;
  1180. msm_host->format = dsi->format;
  1181. msm_host->mode_flags = dsi->mode_flags;
  1182. msm_host->panel_node = dsi->dev.of_node;
  1183. /* Some gpios defined in panel DT need to be controlled by host */
  1184. ret = dsi_host_init_panel_gpios(msm_host, &dsi->dev);
  1185. if (ret)
  1186. return ret;
  1187. DBG("id=%d", msm_host->id);
  1188. if (msm_host->dev)
  1189. drm_helper_hpd_irq_event(msm_host->dev);
  1190. return 0;
  1191. }
  1192. static int dsi_host_detach(struct mipi_dsi_host *host,
  1193. struct mipi_dsi_device *dsi)
  1194. {
  1195. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1196. msm_host->panel_node = NULL;
  1197. DBG("id=%d", msm_host->id);
  1198. if (msm_host->dev)
  1199. drm_helper_hpd_irq_event(msm_host->dev);
  1200. return 0;
  1201. }
  1202. static ssize_t dsi_host_transfer(struct mipi_dsi_host *host,
  1203. const struct mipi_dsi_msg *msg)
  1204. {
  1205. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1206. int ret;
  1207. if (!msg || !msm_host->power_on)
  1208. return -EINVAL;
  1209. mutex_lock(&msm_host->cmd_mutex);
  1210. ret = msm_dsi_manager_cmd_xfer(msm_host->id, msg);
  1211. mutex_unlock(&msm_host->cmd_mutex);
  1212. return ret;
  1213. }
  1214. static struct mipi_dsi_host_ops dsi_host_ops = {
  1215. .attach = dsi_host_attach,
  1216. .detach = dsi_host_detach,
  1217. .transfer = dsi_host_transfer,
  1218. };
  1219. int msm_dsi_host_init(struct msm_dsi *msm_dsi)
  1220. {
  1221. struct msm_dsi_host *msm_host = NULL;
  1222. struct platform_device *pdev = msm_dsi->pdev;
  1223. int ret;
  1224. msm_host = devm_kzalloc(&pdev->dev, sizeof(*msm_host), GFP_KERNEL);
  1225. if (!msm_host) {
  1226. pr_err("%s: FAILED: cannot alloc dsi host\n",
  1227. __func__);
  1228. ret = -ENOMEM;
  1229. goto fail;
  1230. }
  1231. ret = of_property_read_u32(pdev->dev.of_node,
  1232. "qcom,dsi-host-index", &msm_host->id);
  1233. if (ret) {
  1234. dev_err(&pdev->dev,
  1235. "%s: host index not specified, ret=%d\n",
  1236. __func__, ret);
  1237. goto fail;
  1238. }
  1239. msm_host->pdev = pdev;
  1240. ret = dsi_clk_init(msm_host);
  1241. if (ret) {
  1242. pr_err("%s: unable to initialize dsi clks\n", __func__);
  1243. goto fail;
  1244. }
  1245. msm_host->ctrl_base = msm_ioremap(pdev, "dsi_ctrl", "DSI CTRL");
  1246. if (IS_ERR(msm_host->ctrl_base)) {
  1247. pr_err("%s: unable to map Dsi ctrl base\n", __func__);
  1248. ret = PTR_ERR(msm_host->ctrl_base);
  1249. goto fail;
  1250. }
  1251. msm_host->cfg = dsi_get_config(msm_host);
  1252. if (!msm_host->cfg) {
  1253. ret = -EINVAL;
  1254. pr_err("%s: get config failed\n", __func__);
  1255. goto fail;
  1256. }
  1257. ret = dsi_regulator_init(msm_host);
  1258. if (ret) {
  1259. pr_err("%s: regulator init failed\n", __func__);
  1260. goto fail;
  1261. }
  1262. msm_host->rx_buf = devm_kzalloc(&pdev->dev, SZ_4K, GFP_KERNEL);
  1263. if (!msm_host->rx_buf) {
  1264. pr_err("%s: alloc rx temp buf failed\n", __func__);
  1265. goto fail;
  1266. }
  1267. init_completion(&msm_host->dma_comp);
  1268. init_completion(&msm_host->video_comp);
  1269. mutex_init(&msm_host->dev_mutex);
  1270. mutex_init(&msm_host->cmd_mutex);
  1271. mutex_init(&msm_host->clk_mutex);
  1272. spin_lock_init(&msm_host->intr_lock);
  1273. /* setup workqueue */
  1274. msm_host->workqueue = alloc_ordered_workqueue("dsi_drm_work", 0);
  1275. INIT_WORK(&msm_host->err_work, dsi_err_worker);
  1276. msm_dsi->phy = msm_dsi_phy_init(pdev, msm_host->cfg->phy_type,
  1277. msm_host->id);
  1278. if (!msm_dsi->phy) {
  1279. ret = -EINVAL;
  1280. pr_err("%s: phy init failed\n", __func__);
  1281. goto fail;
  1282. }
  1283. msm_dsi->host = &msm_host->base;
  1284. msm_dsi->id = msm_host->id;
  1285. DBG("Dsi Host %d initialized", msm_host->id);
  1286. return 0;
  1287. fail:
  1288. return ret;
  1289. }
  1290. void msm_dsi_host_destroy(struct mipi_dsi_host *host)
  1291. {
  1292. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1293. DBG("");
  1294. dsi_tx_buf_free(msm_host);
  1295. if (msm_host->workqueue) {
  1296. flush_workqueue(msm_host->workqueue);
  1297. destroy_workqueue(msm_host->workqueue);
  1298. msm_host->workqueue = NULL;
  1299. }
  1300. mutex_destroy(&msm_host->clk_mutex);
  1301. mutex_destroy(&msm_host->cmd_mutex);
  1302. mutex_destroy(&msm_host->dev_mutex);
  1303. }
  1304. int msm_dsi_host_modeset_init(struct mipi_dsi_host *host,
  1305. struct drm_device *dev)
  1306. {
  1307. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1308. struct platform_device *pdev = msm_host->pdev;
  1309. int ret;
  1310. msm_host->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
  1311. if (msm_host->irq < 0) {
  1312. ret = msm_host->irq;
  1313. dev_err(dev->dev, "failed to get irq: %d\n", ret);
  1314. return ret;
  1315. }
  1316. ret = devm_request_irq(&pdev->dev, msm_host->irq,
  1317. dsi_host_irq, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  1318. "dsi_isr", msm_host);
  1319. if (ret < 0) {
  1320. dev_err(&pdev->dev, "failed to request IRQ%u: %d\n",
  1321. msm_host->irq, ret);
  1322. return ret;
  1323. }
  1324. msm_host->dev = dev;
  1325. ret = dsi_tx_buf_alloc(msm_host, SZ_4K);
  1326. if (ret) {
  1327. pr_err("%s: alloc tx gem obj failed, %d\n", __func__, ret);
  1328. return ret;
  1329. }
  1330. return 0;
  1331. }
  1332. int msm_dsi_host_register(struct mipi_dsi_host *host, bool check_defer)
  1333. {
  1334. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1335. struct device_node *node;
  1336. int ret;
  1337. /* Register mipi dsi host */
  1338. if (!msm_host->registered) {
  1339. host->dev = &msm_host->pdev->dev;
  1340. host->ops = &dsi_host_ops;
  1341. ret = mipi_dsi_host_register(host);
  1342. if (ret)
  1343. return ret;
  1344. msm_host->registered = true;
  1345. /* If the panel driver has not been probed after host register,
  1346. * we should defer the host's probe.
  1347. * It makes sure panel is connected when fbcon detects
  1348. * connector status and gets the proper display mode to
  1349. * create framebuffer.
  1350. */
  1351. if (check_defer) {
  1352. node = of_get_child_by_name(msm_host->pdev->dev.of_node,
  1353. "panel");
  1354. if (node) {
  1355. if (!of_drm_find_panel(node))
  1356. return -EPROBE_DEFER;
  1357. }
  1358. }
  1359. }
  1360. return 0;
  1361. }
  1362. void msm_dsi_host_unregister(struct mipi_dsi_host *host)
  1363. {
  1364. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1365. if (msm_host->registered) {
  1366. mipi_dsi_host_unregister(host);
  1367. host->dev = NULL;
  1368. host->ops = NULL;
  1369. msm_host->registered = false;
  1370. }
  1371. }
  1372. int msm_dsi_host_xfer_prepare(struct mipi_dsi_host *host,
  1373. const struct mipi_dsi_msg *msg)
  1374. {
  1375. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1376. /* TODO: make sure dsi_cmd_mdp is idle.
  1377. * Since DSI6G v1.2.0, we can set DSI_TRIG_CTRL.BLOCK_DMA_WITHIN_FRAME
  1378. * to ask H/W to wait until cmd mdp is idle. S/W wait is not needed.
  1379. * How to handle the old versions? Wait for mdp cmd done?
  1380. */
  1381. /*
  1382. * mdss interrupt is generated in mdp core clock domain
  1383. * mdp clock need to be enabled to receive dsi interrupt
  1384. */
  1385. dsi_clk_ctrl(msm_host, 1);
  1386. /* TODO: vote for bus bandwidth */
  1387. if (!(msg->flags & MIPI_DSI_MSG_USE_LPM))
  1388. dsi_set_tx_power_mode(0, msm_host);
  1389. msm_host->dma_cmd_ctrl_restore = dsi_read(msm_host, REG_DSI_CTRL);
  1390. dsi_write(msm_host, REG_DSI_CTRL,
  1391. msm_host->dma_cmd_ctrl_restore |
  1392. DSI_CTRL_CMD_MODE_EN |
  1393. DSI_CTRL_ENABLE);
  1394. dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_DMA_DONE, 1);
  1395. return 0;
  1396. }
  1397. void msm_dsi_host_xfer_restore(struct mipi_dsi_host *host,
  1398. const struct mipi_dsi_msg *msg)
  1399. {
  1400. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1401. dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_DMA_DONE, 0);
  1402. dsi_write(msm_host, REG_DSI_CTRL, msm_host->dma_cmd_ctrl_restore);
  1403. if (!(msg->flags & MIPI_DSI_MSG_USE_LPM))
  1404. dsi_set_tx_power_mode(1, msm_host);
  1405. /* TODO: unvote for bus bandwidth */
  1406. dsi_clk_ctrl(msm_host, 0);
  1407. }
  1408. int msm_dsi_host_cmd_tx(struct mipi_dsi_host *host,
  1409. const struct mipi_dsi_msg *msg)
  1410. {
  1411. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1412. return dsi_cmds2buf_tx(msm_host, msg);
  1413. }
  1414. int msm_dsi_host_cmd_rx(struct mipi_dsi_host *host,
  1415. const struct mipi_dsi_msg *msg)
  1416. {
  1417. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1418. int data_byte, rx_byte, dlen, end;
  1419. int short_response, diff, pkt_size, ret = 0;
  1420. char cmd;
  1421. int rlen = msg->rx_len;
  1422. u8 *buf;
  1423. if (rlen <= 2) {
  1424. short_response = 1;
  1425. pkt_size = rlen;
  1426. rx_byte = 4;
  1427. } else {
  1428. short_response = 0;
  1429. data_byte = 10; /* first read */
  1430. if (rlen < data_byte)
  1431. pkt_size = rlen;
  1432. else
  1433. pkt_size = data_byte;
  1434. rx_byte = data_byte + 6; /* 4 header + 2 crc */
  1435. }
  1436. buf = msm_host->rx_buf;
  1437. end = 0;
  1438. while (!end) {
  1439. u8 tx[2] = {pkt_size & 0xff, pkt_size >> 8};
  1440. struct mipi_dsi_msg max_pkt_size_msg = {
  1441. .channel = msg->channel,
  1442. .type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
  1443. .tx_len = 2,
  1444. .tx_buf = tx,
  1445. };
  1446. DBG("rlen=%d pkt_size=%d rx_byte=%d",
  1447. rlen, pkt_size, rx_byte);
  1448. ret = dsi_cmds2buf_tx(msm_host, &max_pkt_size_msg);
  1449. if (ret < 2) {
  1450. pr_err("%s: Set max pkt size failed, %d\n",
  1451. __func__, ret);
  1452. return -EINVAL;
  1453. }
  1454. if ((msm_host->cfg->major == MSM_DSI_VER_MAJOR_6G) &&
  1455. (msm_host->cfg->minor >= MSM_DSI_6G_VER_MINOR_V1_1)) {
  1456. /* Clear the RDBK_DATA registers */
  1457. dsi_write(msm_host, REG_DSI_RDBK_DATA_CTRL,
  1458. DSI_RDBK_DATA_CTRL_CLR);
  1459. wmb(); /* make sure the RDBK registers are cleared */
  1460. dsi_write(msm_host, REG_DSI_RDBK_DATA_CTRL, 0);
  1461. wmb(); /* release cleared status before transfer */
  1462. }
  1463. ret = dsi_cmds2buf_tx(msm_host, msg);
  1464. if (ret < msg->tx_len) {
  1465. pr_err("%s: Read cmd Tx failed, %d\n", __func__, ret);
  1466. return ret;
  1467. }
  1468. /*
  1469. * once cmd_dma_done interrupt received,
  1470. * return data from client is ready and stored
  1471. * at RDBK_DATA register already
  1472. * since rx fifo is 16 bytes, dcs header is kept at first loop,
  1473. * after that dcs header lost during shift into registers
  1474. */
  1475. dlen = dsi_cmd_dma_rx(msm_host, buf, rx_byte, pkt_size);
  1476. if (dlen <= 0)
  1477. return 0;
  1478. if (short_response)
  1479. break;
  1480. if (rlen <= data_byte) {
  1481. diff = data_byte - rlen;
  1482. end = 1;
  1483. } else {
  1484. diff = 0;
  1485. rlen -= data_byte;
  1486. }
  1487. if (!end) {
  1488. dlen -= 2; /* 2 crc */
  1489. dlen -= diff;
  1490. buf += dlen; /* next start position */
  1491. data_byte = 14; /* NOT first read */
  1492. if (rlen < data_byte)
  1493. pkt_size += rlen;
  1494. else
  1495. pkt_size += data_byte;
  1496. DBG("buf=%p dlen=%d diff=%d", buf, dlen, diff);
  1497. }
  1498. }
  1499. /*
  1500. * For single Long read, if the requested rlen < 10,
  1501. * we need to shift the start position of rx
  1502. * data buffer to skip the bytes which are not
  1503. * updated.
  1504. */
  1505. if (pkt_size < 10 && !short_response)
  1506. buf = msm_host->rx_buf + (10 - rlen);
  1507. else
  1508. buf = msm_host->rx_buf;
  1509. cmd = buf[0];
  1510. switch (cmd) {
  1511. case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
  1512. pr_err("%s: rx ACK_ERR_PACLAGE\n", __func__);
  1513. ret = 0;
  1514. break;
  1515. case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE:
  1516. case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
  1517. ret = dsi_short_read1_resp(buf, msg);
  1518. break;
  1519. case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE:
  1520. case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
  1521. ret = dsi_short_read2_resp(buf, msg);
  1522. break;
  1523. case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
  1524. case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
  1525. ret = dsi_long_read_resp(buf, msg);
  1526. break;
  1527. default:
  1528. pr_warn("%s:Invalid response cmd\n", __func__);
  1529. ret = 0;
  1530. }
  1531. return ret;
  1532. }
  1533. void msm_dsi_host_cmd_xfer_commit(struct mipi_dsi_host *host, u32 iova, u32 len)
  1534. {
  1535. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1536. dsi_write(msm_host, REG_DSI_DMA_BASE, iova);
  1537. dsi_write(msm_host, REG_DSI_DMA_LEN, len);
  1538. dsi_write(msm_host, REG_DSI_TRIG_DMA, 1);
  1539. /* Make sure trigger happens */
  1540. wmb();
  1541. }
  1542. int msm_dsi_host_enable(struct mipi_dsi_host *host)
  1543. {
  1544. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1545. dsi_op_mode_config(msm_host,
  1546. !!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO), true);
  1547. /* TODO: clock should be turned off for command mode,
  1548. * and only turned on before MDP START.
  1549. * This part of code should be enabled once mdp driver support it.
  1550. */
  1551. /* if (msm_panel->mode == MSM_DSI_CMD_MODE)
  1552. dsi_clk_ctrl(msm_host, 0); */
  1553. return 0;
  1554. }
  1555. int msm_dsi_host_disable(struct mipi_dsi_host *host)
  1556. {
  1557. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1558. dsi_op_mode_config(msm_host,
  1559. !!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO), false);
  1560. /* Since we have disabled INTF, the video engine won't stop so that
  1561. * the cmd engine will be blocked.
  1562. * Reset to disable video engine so that we can send off cmd.
  1563. */
  1564. dsi_sw_reset(msm_host);
  1565. return 0;
  1566. }
  1567. int msm_dsi_host_power_on(struct mipi_dsi_host *host)
  1568. {
  1569. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1570. u32 clk_pre = 0, clk_post = 0;
  1571. int ret = 0;
  1572. mutex_lock(&msm_host->dev_mutex);
  1573. if (msm_host->power_on) {
  1574. DBG("dsi host already on");
  1575. goto unlock_ret;
  1576. }
  1577. ret = dsi_calc_clk_rate(msm_host);
  1578. if (ret) {
  1579. pr_err("%s: unable to calc clk rate, %d\n", __func__, ret);
  1580. goto unlock_ret;
  1581. }
  1582. ret = dsi_host_regulator_enable(msm_host);
  1583. if (ret) {
  1584. pr_err("%s:Failed to enable vregs.ret=%d\n",
  1585. __func__, ret);
  1586. goto unlock_ret;
  1587. }
  1588. ret = dsi_bus_clk_enable(msm_host);
  1589. if (ret) {
  1590. pr_err("%s: failed to enable bus clocks, %d\n", __func__, ret);
  1591. goto fail_disable_reg;
  1592. }
  1593. dsi_phy_sw_reset(msm_host);
  1594. ret = msm_dsi_manager_phy_enable(msm_host->id,
  1595. msm_host->byte_clk_rate * 8,
  1596. clk_get_rate(msm_host->esc_clk),
  1597. &clk_pre, &clk_post);
  1598. dsi_bus_clk_disable(msm_host);
  1599. if (ret) {
  1600. pr_err("%s: failed to enable phy, %d\n", __func__, ret);
  1601. goto fail_disable_reg;
  1602. }
  1603. ret = dsi_clk_ctrl(msm_host, 1);
  1604. if (ret) {
  1605. pr_err("%s: failed to enable clocks. ret=%d\n", __func__, ret);
  1606. goto fail_disable_reg;
  1607. }
  1608. dsi_timing_setup(msm_host);
  1609. dsi_sw_reset(msm_host);
  1610. dsi_ctrl_config(msm_host, true, clk_pre, clk_post);
  1611. if (msm_host->disp_en_gpio)
  1612. gpiod_set_value(msm_host->disp_en_gpio, 1);
  1613. msm_host->power_on = true;
  1614. mutex_unlock(&msm_host->dev_mutex);
  1615. return 0;
  1616. fail_disable_reg:
  1617. dsi_host_regulator_disable(msm_host);
  1618. unlock_ret:
  1619. mutex_unlock(&msm_host->dev_mutex);
  1620. return ret;
  1621. }
  1622. int msm_dsi_host_power_off(struct mipi_dsi_host *host)
  1623. {
  1624. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1625. mutex_lock(&msm_host->dev_mutex);
  1626. if (!msm_host->power_on) {
  1627. DBG("dsi host already off");
  1628. goto unlock_ret;
  1629. }
  1630. dsi_ctrl_config(msm_host, false, 0, 0);
  1631. if (msm_host->disp_en_gpio)
  1632. gpiod_set_value(msm_host->disp_en_gpio, 0);
  1633. msm_dsi_manager_phy_disable(msm_host->id);
  1634. dsi_clk_ctrl(msm_host, 0);
  1635. dsi_host_regulator_disable(msm_host);
  1636. DBG("-");
  1637. msm_host->power_on = false;
  1638. unlock_ret:
  1639. mutex_unlock(&msm_host->dev_mutex);
  1640. return 0;
  1641. }
  1642. int msm_dsi_host_set_display_mode(struct mipi_dsi_host *host,
  1643. struct drm_display_mode *mode)
  1644. {
  1645. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1646. if (msm_host->mode) {
  1647. drm_mode_destroy(msm_host->dev, msm_host->mode);
  1648. msm_host->mode = NULL;
  1649. }
  1650. msm_host->mode = drm_mode_duplicate(msm_host->dev, mode);
  1651. if (IS_ERR(msm_host->mode)) {
  1652. pr_err("%s: cannot duplicate mode\n", __func__);
  1653. return PTR_ERR(msm_host->mode);
  1654. }
  1655. return 0;
  1656. }
  1657. struct drm_panel *msm_dsi_host_get_panel(struct mipi_dsi_host *host,
  1658. unsigned long *panel_flags)
  1659. {
  1660. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1661. struct drm_panel *panel;
  1662. panel = of_drm_find_panel(msm_host->panel_node);
  1663. if (panel_flags)
  1664. *panel_flags = msm_host->mode_flags;
  1665. return panel;
  1666. }