dsi_host.c 58 KB

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