dsi_host.c 60 KB

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