dsi_host.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376
  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. mutex_unlock(&dev->struct_mutex);
  860. if (ret) {
  861. pr_err("%s: failed to get iova, %d\n", __func__, ret);
  862. return ret;
  863. }
  864. if (iova & 0x07) {
  865. pr_err("%s: buf NOT 8 bytes aligned\n", __func__);
  866. return -EINVAL;
  867. }
  868. msm_host->tx_size = msm_host->tx_gem_obj->size;
  869. } else {
  870. msm_host->tx_buf = dma_alloc_coherent(dev->dev, size,
  871. &msm_host->tx_buf_paddr, GFP_KERNEL);
  872. if (!msm_host->tx_buf) {
  873. ret = -ENOMEM;
  874. pr_err("%s: failed to allocate tx buf, %d\n",
  875. __func__, ret);
  876. return ret;
  877. }
  878. msm_host->tx_size = size;
  879. }
  880. return 0;
  881. }
  882. static void dsi_tx_buf_free(struct msm_dsi_host *msm_host)
  883. {
  884. struct drm_device *dev = msm_host->dev;
  885. if (msm_host->tx_gem_obj) {
  886. msm_gem_put_iova(msm_host->tx_gem_obj, 0);
  887. drm_gem_object_put_unlocked(msm_host->tx_gem_obj);
  888. msm_host->tx_gem_obj = NULL;
  889. }
  890. if (msm_host->tx_buf)
  891. dma_free_coherent(dev->dev, msm_host->tx_size, msm_host->tx_buf,
  892. msm_host->tx_buf_paddr);
  893. }
  894. /*
  895. * prepare cmd buffer to be txed
  896. */
  897. static int dsi_cmd_dma_add(struct msm_dsi_host *msm_host,
  898. const struct mipi_dsi_msg *msg)
  899. {
  900. const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
  901. struct mipi_dsi_packet packet;
  902. int len;
  903. int ret;
  904. u8 *data;
  905. ret = mipi_dsi_create_packet(&packet, msg);
  906. if (ret) {
  907. pr_err("%s: create packet failed, %d\n", __func__, ret);
  908. return ret;
  909. }
  910. len = (packet.size + 3) & (~0x3);
  911. if (len > msm_host->tx_size) {
  912. pr_err("%s: packet size is too big\n", __func__);
  913. return -EINVAL;
  914. }
  915. if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) {
  916. data = msm_gem_get_vaddr(msm_host->tx_gem_obj);
  917. if (IS_ERR(data)) {
  918. ret = PTR_ERR(data);
  919. pr_err("%s: get vaddr failed, %d\n", __func__, ret);
  920. return ret;
  921. }
  922. } else {
  923. data = msm_host->tx_buf;
  924. }
  925. /* MSM specific command format in memory */
  926. data[0] = packet.header[1];
  927. data[1] = packet.header[2];
  928. data[2] = packet.header[0];
  929. data[3] = BIT(7); /* Last packet */
  930. if (mipi_dsi_packet_format_is_long(msg->type))
  931. data[3] |= BIT(6);
  932. if (msg->rx_buf && msg->rx_len)
  933. data[3] |= BIT(5);
  934. /* Long packet */
  935. if (packet.payload && packet.payload_length)
  936. memcpy(data + 4, packet.payload, packet.payload_length);
  937. /* Append 0xff to the end */
  938. if (packet.size < len)
  939. memset(data + packet.size, 0xff, len - packet.size);
  940. if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G)
  941. msm_gem_put_vaddr(msm_host->tx_gem_obj);
  942. return len;
  943. }
  944. /*
  945. * dsi_short_read1_resp: 1 parameter
  946. */
  947. static int dsi_short_read1_resp(u8 *buf, const struct mipi_dsi_msg *msg)
  948. {
  949. u8 *data = msg->rx_buf;
  950. if (data && (msg->rx_len >= 1)) {
  951. *data = buf[1]; /* strip out dcs type */
  952. return 1;
  953. } else {
  954. pr_err("%s: read data does not match with rx_buf len %zu\n",
  955. __func__, msg->rx_len);
  956. return -EINVAL;
  957. }
  958. }
  959. /*
  960. * dsi_short_read2_resp: 2 parameter
  961. */
  962. static int dsi_short_read2_resp(u8 *buf, const struct mipi_dsi_msg *msg)
  963. {
  964. u8 *data = msg->rx_buf;
  965. if (data && (msg->rx_len >= 2)) {
  966. data[0] = buf[1]; /* strip out dcs type */
  967. data[1] = buf[2];
  968. return 2;
  969. } else {
  970. pr_err("%s: read data does not match with rx_buf len %zu\n",
  971. __func__, msg->rx_len);
  972. return -EINVAL;
  973. }
  974. }
  975. static int dsi_long_read_resp(u8 *buf, const struct mipi_dsi_msg *msg)
  976. {
  977. /* strip out 4 byte dcs header */
  978. if (msg->rx_buf && msg->rx_len)
  979. memcpy(msg->rx_buf, buf + 4, msg->rx_len);
  980. return msg->rx_len;
  981. }
  982. static int dsi_cmd_dma_tx(struct msm_dsi_host *msm_host, int len)
  983. {
  984. const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
  985. struct drm_device *dev = msm_host->dev;
  986. struct msm_drm_private *priv = dev->dev_private;
  987. int ret;
  988. uint64_t dma_base;
  989. bool triggered;
  990. if (cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) {
  991. ret = msm_gem_get_iova(msm_host->tx_gem_obj,
  992. priv->kms->aspace, &dma_base);
  993. if (ret) {
  994. pr_err("%s: failed to get iova: %d\n", __func__, ret);
  995. return ret;
  996. }
  997. } else {
  998. dma_base = msm_host->tx_buf_paddr;
  999. }
  1000. reinit_completion(&msm_host->dma_comp);
  1001. dsi_wait4video_eng_busy(msm_host);
  1002. triggered = msm_dsi_manager_cmd_xfer_trigger(
  1003. msm_host->id, dma_base, len);
  1004. if (triggered) {
  1005. ret = wait_for_completion_timeout(&msm_host->dma_comp,
  1006. msecs_to_jiffies(200));
  1007. DBG("ret=%d", ret);
  1008. if (ret == 0)
  1009. ret = -ETIMEDOUT;
  1010. else
  1011. ret = len;
  1012. } else
  1013. ret = len;
  1014. return ret;
  1015. }
  1016. static int dsi_cmd_dma_rx(struct msm_dsi_host *msm_host,
  1017. u8 *buf, int rx_byte, int pkt_size)
  1018. {
  1019. u32 *lp, *temp, data;
  1020. int i, j = 0, cnt;
  1021. u32 read_cnt;
  1022. u8 reg[16];
  1023. int repeated_bytes = 0;
  1024. int buf_offset = buf - msm_host->rx_buf;
  1025. lp = (u32 *)buf;
  1026. temp = (u32 *)reg;
  1027. cnt = (rx_byte + 3) >> 2;
  1028. if (cnt > 4)
  1029. cnt = 4; /* 4 x 32 bits registers only */
  1030. if (rx_byte == 4)
  1031. read_cnt = 4;
  1032. else
  1033. read_cnt = pkt_size + 6;
  1034. /*
  1035. * In case of multiple reads from the panel, after the first read, there
  1036. * is possibility that there are some bytes in the payload repeating in
  1037. * the RDBK_DATA registers. Since we read all the parameters from the
  1038. * panel right from the first byte for every pass. We need to skip the
  1039. * repeating bytes and then append the new parameters to the rx buffer.
  1040. */
  1041. if (read_cnt > 16) {
  1042. int bytes_shifted;
  1043. /* Any data more than 16 bytes will be shifted out.
  1044. * The temp read buffer should already contain these bytes.
  1045. * The remaining bytes in read buffer are the repeated bytes.
  1046. */
  1047. bytes_shifted = read_cnt - 16;
  1048. repeated_bytes = buf_offset - bytes_shifted;
  1049. }
  1050. for (i = cnt - 1; i >= 0; i--) {
  1051. data = dsi_read(msm_host, REG_DSI_RDBK_DATA(i));
  1052. *temp++ = ntohl(data); /* to host byte order */
  1053. DBG("data = 0x%x and ntohl(data) = 0x%x", data, ntohl(data));
  1054. }
  1055. for (i = repeated_bytes; i < 16; i++)
  1056. buf[j++] = reg[i];
  1057. return j;
  1058. }
  1059. static int dsi_cmds2buf_tx(struct msm_dsi_host *msm_host,
  1060. const struct mipi_dsi_msg *msg)
  1061. {
  1062. int len, ret;
  1063. int bllp_len = msm_host->mode->hdisplay *
  1064. dsi_get_bpp(msm_host->format) / 8;
  1065. len = dsi_cmd_dma_add(msm_host, msg);
  1066. if (!len) {
  1067. pr_err("%s: failed to add cmd type = 0x%x\n",
  1068. __func__, msg->type);
  1069. return -EINVAL;
  1070. }
  1071. /* for video mode, do not send cmds more than
  1072. * one pixel line, since it only transmit it
  1073. * during BLLP.
  1074. */
  1075. /* TODO: if the command is sent in LP mode, the bit rate is only
  1076. * half of esc clk rate. In this case, if the video is already
  1077. * actively streaming, we need to check more carefully if the
  1078. * command can be fit into one BLLP.
  1079. */
  1080. if ((msm_host->mode_flags & MIPI_DSI_MODE_VIDEO) && (len > bllp_len)) {
  1081. pr_err("%s: cmd cannot fit into BLLP period, len=%d\n",
  1082. __func__, len);
  1083. return -EINVAL;
  1084. }
  1085. ret = dsi_cmd_dma_tx(msm_host, len);
  1086. if (ret < len) {
  1087. pr_err("%s: cmd dma tx failed, type=0x%x, data0=0x%x, len=%d\n",
  1088. __func__, msg->type, (*(u8 *)(msg->tx_buf)), len);
  1089. return -ECOMM;
  1090. }
  1091. return len;
  1092. }
  1093. static void dsi_sw_reset_restore(struct msm_dsi_host *msm_host)
  1094. {
  1095. u32 data0, data1;
  1096. data0 = dsi_read(msm_host, REG_DSI_CTRL);
  1097. data1 = data0;
  1098. data1 &= ~DSI_CTRL_ENABLE;
  1099. dsi_write(msm_host, REG_DSI_CTRL, data1);
  1100. /*
  1101. * dsi controller need to be disabled before
  1102. * clocks turned on
  1103. */
  1104. wmb();
  1105. dsi_write(msm_host, REG_DSI_CLK_CTRL, DSI_CLK_CTRL_ENABLE_CLKS);
  1106. wmb(); /* make sure clocks enabled */
  1107. /* dsi controller can only be reset while clocks are running */
  1108. dsi_write(msm_host, REG_DSI_RESET, 1);
  1109. wmb(); /* make sure reset happen */
  1110. dsi_write(msm_host, REG_DSI_RESET, 0);
  1111. wmb(); /* controller out of reset */
  1112. dsi_write(msm_host, REG_DSI_CTRL, data0);
  1113. wmb(); /* make sure dsi controller enabled again */
  1114. }
  1115. static void dsi_hpd_worker(struct work_struct *work)
  1116. {
  1117. struct msm_dsi_host *msm_host =
  1118. container_of(work, struct msm_dsi_host, hpd_work);
  1119. drm_helper_hpd_irq_event(msm_host->dev);
  1120. }
  1121. static void dsi_err_worker(struct work_struct *work)
  1122. {
  1123. struct msm_dsi_host *msm_host =
  1124. container_of(work, struct msm_dsi_host, err_work);
  1125. u32 status = msm_host->err_work_state;
  1126. pr_err_ratelimited("%s: status=%x\n", __func__, status);
  1127. if (status & DSI_ERR_STATE_MDP_FIFO_UNDERFLOW)
  1128. dsi_sw_reset_restore(msm_host);
  1129. /* It is safe to clear here because error irq is disabled. */
  1130. msm_host->err_work_state = 0;
  1131. /* enable dsi error interrupt */
  1132. dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 1);
  1133. }
  1134. static void dsi_ack_err_status(struct msm_dsi_host *msm_host)
  1135. {
  1136. u32 status;
  1137. status = dsi_read(msm_host, REG_DSI_ACK_ERR_STATUS);
  1138. if (status) {
  1139. dsi_write(msm_host, REG_DSI_ACK_ERR_STATUS, status);
  1140. /* Writing of an extra 0 needed to clear error bits */
  1141. dsi_write(msm_host, REG_DSI_ACK_ERR_STATUS, 0);
  1142. msm_host->err_work_state |= DSI_ERR_STATE_ACK;
  1143. }
  1144. }
  1145. static void dsi_timeout_status(struct msm_dsi_host *msm_host)
  1146. {
  1147. u32 status;
  1148. status = dsi_read(msm_host, REG_DSI_TIMEOUT_STATUS);
  1149. if (status) {
  1150. dsi_write(msm_host, REG_DSI_TIMEOUT_STATUS, status);
  1151. msm_host->err_work_state |= DSI_ERR_STATE_TIMEOUT;
  1152. }
  1153. }
  1154. static void dsi_dln0_phy_err(struct msm_dsi_host *msm_host)
  1155. {
  1156. u32 status;
  1157. status = dsi_read(msm_host, REG_DSI_DLN0_PHY_ERR);
  1158. if (status & (DSI_DLN0_PHY_ERR_DLN0_ERR_ESC |
  1159. DSI_DLN0_PHY_ERR_DLN0_ERR_SYNC_ESC |
  1160. DSI_DLN0_PHY_ERR_DLN0_ERR_CONTROL |
  1161. DSI_DLN0_PHY_ERR_DLN0_ERR_CONTENTION_LP0 |
  1162. DSI_DLN0_PHY_ERR_DLN0_ERR_CONTENTION_LP1)) {
  1163. dsi_write(msm_host, REG_DSI_DLN0_PHY_ERR, status);
  1164. msm_host->err_work_state |= DSI_ERR_STATE_DLN0_PHY;
  1165. }
  1166. }
  1167. static void dsi_fifo_status(struct msm_dsi_host *msm_host)
  1168. {
  1169. u32 status;
  1170. status = dsi_read(msm_host, REG_DSI_FIFO_STATUS);
  1171. /* fifo underflow, overflow */
  1172. if (status) {
  1173. dsi_write(msm_host, REG_DSI_FIFO_STATUS, status);
  1174. msm_host->err_work_state |= DSI_ERR_STATE_FIFO;
  1175. if (status & DSI_FIFO_STATUS_CMD_MDP_FIFO_UNDERFLOW)
  1176. msm_host->err_work_state |=
  1177. DSI_ERR_STATE_MDP_FIFO_UNDERFLOW;
  1178. }
  1179. }
  1180. static void dsi_status(struct msm_dsi_host *msm_host)
  1181. {
  1182. u32 status;
  1183. status = dsi_read(msm_host, REG_DSI_STATUS0);
  1184. if (status & DSI_STATUS0_INTERLEAVE_OP_CONTENTION) {
  1185. dsi_write(msm_host, REG_DSI_STATUS0, status);
  1186. msm_host->err_work_state |=
  1187. DSI_ERR_STATE_INTERLEAVE_OP_CONTENTION;
  1188. }
  1189. }
  1190. static void dsi_clk_status(struct msm_dsi_host *msm_host)
  1191. {
  1192. u32 status;
  1193. status = dsi_read(msm_host, REG_DSI_CLK_STATUS);
  1194. if (status & DSI_CLK_STATUS_PLL_UNLOCKED) {
  1195. dsi_write(msm_host, REG_DSI_CLK_STATUS, status);
  1196. msm_host->err_work_state |= DSI_ERR_STATE_PLL_UNLOCKED;
  1197. }
  1198. }
  1199. static void dsi_error(struct msm_dsi_host *msm_host)
  1200. {
  1201. /* disable dsi error interrupt */
  1202. dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_ERROR, 0);
  1203. dsi_clk_status(msm_host);
  1204. dsi_fifo_status(msm_host);
  1205. dsi_ack_err_status(msm_host);
  1206. dsi_timeout_status(msm_host);
  1207. dsi_status(msm_host);
  1208. dsi_dln0_phy_err(msm_host);
  1209. queue_work(msm_host->workqueue, &msm_host->err_work);
  1210. }
  1211. static irqreturn_t dsi_host_irq(int irq, void *ptr)
  1212. {
  1213. struct msm_dsi_host *msm_host = ptr;
  1214. u32 isr;
  1215. unsigned long flags;
  1216. if (!msm_host->ctrl_base)
  1217. return IRQ_HANDLED;
  1218. spin_lock_irqsave(&msm_host->intr_lock, flags);
  1219. isr = dsi_read(msm_host, REG_DSI_INTR_CTRL);
  1220. dsi_write(msm_host, REG_DSI_INTR_CTRL, isr);
  1221. spin_unlock_irqrestore(&msm_host->intr_lock, flags);
  1222. DBG("isr=0x%x, id=%d", isr, msm_host->id);
  1223. if (isr & DSI_IRQ_ERROR)
  1224. dsi_error(msm_host);
  1225. if (isr & DSI_IRQ_VIDEO_DONE)
  1226. complete(&msm_host->video_comp);
  1227. if (isr & DSI_IRQ_CMD_DMA_DONE)
  1228. complete(&msm_host->dma_comp);
  1229. return IRQ_HANDLED;
  1230. }
  1231. static int dsi_host_init_panel_gpios(struct msm_dsi_host *msm_host,
  1232. struct device *panel_device)
  1233. {
  1234. msm_host->disp_en_gpio = devm_gpiod_get_optional(panel_device,
  1235. "disp-enable",
  1236. GPIOD_OUT_LOW);
  1237. if (IS_ERR(msm_host->disp_en_gpio)) {
  1238. DBG("cannot get disp-enable-gpios %ld",
  1239. PTR_ERR(msm_host->disp_en_gpio));
  1240. return PTR_ERR(msm_host->disp_en_gpio);
  1241. }
  1242. msm_host->te_gpio = devm_gpiod_get_optional(panel_device, "disp-te",
  1243. GPIOD_IN);
  1244. if (IS_ERR(msm_host->te_gpio)) {
  1245. DBG("cannot get disp-te-gpios %ld", PTR_ERR(msm_host->te_gpio));
  1246. return PTR_ERR(msm_host->te_gpio);
  1247. }
  1248. return 0;
  1249. }
  1250. static int dsi_host_attach(struct mipi_dsi_host *host,
  1251. struct mipi_dsi_device *dsi)
  1252. {
  1253. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1254. int ret;
  1255. if (dsi->lanes > msm_host->num_data_lanes)
  1256. return -EINVAL;
  1257. msm_host->channel = dsi->channel;
  1258. msm_host->lanes = dsi->lanes;
  1259. msm_host->format = dsi->format;
  1260. msm_host->mode_flags = dsi->mode_flags;
  1261. msm_dsi_manager_attach_dsi_device(msm_host->id, dsi->mode_flags);
  1262. /* Some gpios defined in panel DT need to be controlled by host */
  1263. ret = dsi_host_init_panel_gpios(msm_host, &dsi->dev);
  1264. if (ret)
  1265. return ret;
  1266. DBG("id=%d", msm_host->id);
  1267. if (msm_host->dev)
  1268. queue_work(msm_host->workqueue, &msm_host->hpd_work);
  1269. return 0;
  1270. }
  1271. static int dsi_host_detach(struct mipi_dsi_host *host,
  1272. struct mipi_dsi_device *dsi)
  1273. {
  1274. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1275. msm_host->device_node = NULL;
  1276. DBG("id=%d", msm_host->id);
  1277. if (msm_host->dev)
  1278. queue_work(msm_host->workqueue, &msm_host->hpd_work);
  1279. return 0;
  1280. }
  1281. static ssize_t dsi_host_transfer(struct mipi_dsi_host *host,
  1282. const struct mipi_dsi_msg *msg)
  1283. {
  1284. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1285. int ret;
  1286. if (!msg || !msm_host->power_on)
  1287. return -EINVAL;
  1288. mutex_lock(&msm_host->cmd_mutex);
  1289. ret = msm_dsi_manager_cmd_xfer(msm_host->id, msg);
  1290. mutex_unlock(&msm_host->cmd_mutex);
  1291. return ret;
  1292. }
  1293. static struct mipi_dsi_host_ops dsi_host_ops = {
  1294. .attach = dsi_host_attach,
  1295. .detach = dsi_host_detach,
  1296. .transfer = dsi_host_transfer,
  1297. };
  1298. /*
  1299. * List of supported physical to logical lane mappings.
  1300. * For example, the 2nd entry represents the following mapping:
  1301. *
  1302. * "3012": Logic 3->Phys 0; Logic 0->Phys 1; Logic 1->Phys 2; Logic 2->Phys 3;
  1303. */
  1304. static const int supported_data_lane_swaps[][4] = {
  1305. { 0, 1, 2, 3 },
  1306. { 3, 0, 1, 2 },
  1307. { 2, 3, 0, 1 },
  1308. { 1, 2, 3, 0 },
  1309. { 0, 3, 2, 1 },
  1310. { 1, 0, 3, 2 },
  1311. { 2, 1, 0, 3 },
  1312. { 3, 2, 1, 0 },
  1313. };
  1314. static int dsi_host_parse_lane_data(struct msm_dsi_host *msm_host,
  1315. struct device_node *ep)
  1316. {
  1317. struct device *dev = &msm_host->pdev->dev;
  1318. struct property *prop;
  1319. u32 lane_map[4];
  1320. int ret, i, len, num_lanes;
  1321. prop = of_find_property(ep, "data-lanes", &len);
  1322. if (!prop) {
  1323. dev_dbg(dev,
  1324. "failed to find data lane mapping, using default\n");
  1325. return 0;
  1326. }
  1327. num_lanes = len / sizeof(u32);
  1328. if (num_lanes < 1 || num_lanes > 4) {
  1329. dev_err(dev, "bad number of data lanes\n");
  1330. return -EINVAL;
  1331. }
  1332. msm_host->num_data_lanes = num_lanes;
  1333. ret = of_property_read_u32_array(ep, "data-lanes", lane_map,
  1334. num_lanes);
  1335. if (ret) {
  1336. dev_err(dev, "failed to read lane data\n");
  1337. return ret;
  1338. }
  1339. /*
  1340. * compare DT specified physical-logical lane mappings with the ones
  1341. * supported by hardware
  1342. */
  1343. for (i = 0; i < ARRAY_SIZE(supported_data_lane_swaps); i++) {
  1344. const int *swap = supported_data_lane_swaps[i];
  1345. int j;
  1346. /*
  1347. * the data-lanes array we get from DT has a logical->physical
  1348. * mapping. The "data lane swap" register field represents
  1349. * supported configurations in a physical->logical mapping.
  1350. * Translate the DT mapping to what we understand and find a
  1351. * configuration that works.
  1352. */
  1353. for (j = 0; j < num_lanes; j++) {
  1354. if (lane_map[j] < 0 || lane_map[j] > 3)
  1355. dev_err(dev, "bad physical lane entry %u\n",
  1356. lane_map[j]);
  1357. if (swap[lane_map[j]] != j)
  1358. break;
  1359. }
  1360. if (j == num_lanes) {
  1361. msm_host->dlane_swap = i;
  1362. return 0;
  1363. }
  1364. }
  1365. return -EINVAL;
  1366. }
  1367. static int dsi_host_parse_dt(struct msm_dsi_host *msm_host)
  1368. {
  1369. struct device *dev = &msm_host->pdev->dev;
  1370. struct device_node *np = dev->of_node;
  1371. struct device_node *endpoint, *device_node;
  1372. int ret = 0;
  1373. /*
  1374. * Get the endpoint of the output port of the DSI host. In our case,
  1375. * this is mapped to port number with reg = 1. Don't return an error if
  1376. * the remote endpoint isn't defined. It's possible that there is
  1377. * nothing connected to the dsi output.
  1378. */
  1379. endpoint = of_graph_get_endpoint_by_regs(np, 1, -1);
  1380. if (!endpoint) {
  1381. dev_dbg(dev, "%s: no endpoint\n", __func__);
  1382. return 0;
  1383. }
  1384. ret = dsi_host_parse_lane_data(msm_host, endpoint);
  1385. if (ret) {
  1386. dev_err(dev, "%s: invalid lane configuration %d\n",
  1387. __func__, ret);
  1388. goto err;
  1389. }
  1390. /* Get panel node from the output port's endpoint data */
  1391. device_node = of_graph_get_remote_node(np, 1, 0);
  1392. if (!device_node) {
  1393. dev_dbg(dev, "%s: no valid device\n", __func__);
  1394. goto err;
  1395. }
  1396. msm_host->device_node = device_node;
  1397. if (of_property_read_bool(np, "syscon-sfpb")) {
  1398. msm_host->sfpb = syscon_regmap_lookup_by_phandle(np,
  1399. "syscon-sfpb");
  1400. if (IS_ERR(msm_host->sfpb)) {
  1401. dev_err(dev, "%s: failed to get sfpb regmap\n",
  1402. __func__);
  1403. ret = PTR_ERR(msm_host->sfpb);
  1404. }
  1405. }
  1406. of_node_put(device_node);
  1407. err:
  1408. of_node_put(endpoint);
  1409. return ret;
  1410. }
  1411. static int dsi_host_get_id(struct msm_dsi_host *msm_host)
  1412. {
  1413. struct platform_device *pdev = msm_host->pdev;
  1414. const struct msm_dsi_config *cfg = msm_host->cfg_hnd->cfg;
  1415. struct resource *res;
  1416. int i;
  1417. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dsi_ctrl");
  1418. if (!res)
  1419. return -EINVAL;
  1420. for (i = 0; i < cfg->num_dsi; i++) {
  1421. if (cfg->io_start[i] == res->start)
  1422. return i;
  1423. }
  1424. return -EINVAL;
  1425. }
  1426. int msm_dsi_host_init(struct msm_dsi *msm_dsi)
  1427. {
  1428. struct msm_dsi_host *msm_host = NULL;
  1429. struct platform_device *pdev = msm_dsi->pdev;
  1430. int ret;
  1431. msm_host = devm_kzalloc(&pdev->dev, sizeof(*msm_host), GFP_KERNEL);
  1432. if (!msm_host) {
  1433. pr_err("%s: FAILED: cannot alloc dsi host\n",
  1434. __func__);
  1435. ret = -ENOMEM;
  1436. goto fail;
  1437. }
  1438. msm_host->pdev = pdev;
  1439. msm_dsi->host = &msm_host->base;
  1440. ret = dsi_host_parse_dt(msm_host);
  1441. if (ret) {
  1442. pr_err("%s: failed to parse dt\n", __func__);
  1443. goto fail;
  1444. }
  1445. msm_host->ctrl_base = msm_ioremap(pdev, "dsi_ctrl", "DSI CTRL");
  1446. if (IS_ERR(msm_host->ctrl_base)) {
  1447. pr_err("%s: unable to map Dsi ctrl base\n", __func__);
  1448. ret = PTR_ERR(msm_host->ctrl_base);
  1449. goto fail;
  1450. }
  1451. pm_runtime_enable(&pdev->dev);
  1452. msm_host->cfg_hnd = dsi_get_config(msm_host);
  1453. if (!msm_host->cfg_hnd) {
  1454. ret = -EINVAL;
  1455. pr_err("%s: get config failed\n", __func__);
  1456. goto fail;
  1457. }
  1458. msm_host->id = dsi_host_get_id(msm_host);
  1459. if (msm_host->id < 0) {
  1460. ret = msm_host->id;
  1461. pr_err("%s: unable to identify DSI host index\n", __func__);
  1462. goto fail;
  1463. }
  1464. /* fixup base address by io offset */
  1465. msm_host->ctrl_base += msm_host->cfg_hnd->cfg->io_offset;
  1466. ret = dsi_regulator_init(msm_host);
  1467. if (ret) {
  1468. pr_err("%s: regulator init failed\n", __func__);
  1469. goto fail;
  1470. }
  1471. ret = dsi_clk_init(msm_host);
  1472. if (ret) {
  1473. pr_err("%s: unable to initialize dsi clks\n", __func__);
  1474. goto fail;
  1475. }
  1476. msm_host->rx_buf = devm_kzalloc(&pdev->dev, SZ_4K, GFP_KERNEL);
  1477. if (!msm_host->rx_buf) {
  1478. ret = -ENOMEM;
  1479. pr_err("%s: alloc rx temp buf failed\n", __func__);
  1480. goto fail;
  1481. }
  1482. init_completion(&msm_host->dma_comp);
  1483. init_completion(&msm_host->video_comp);
  1484. mutex_init(&msm_host->dev_mutex);
  1485. mutex_init(&msm_host->cmd_mutex);
  1486. spin_lock_init(&msm_host->intr_lock);
  1487. /* setup workqueue */
  1488. msm_host->workqueue = alloc_ordered_workqueue("dsi_drm_work", 0);
  1489. INIT_WORK(&msm_host->err_work, dsi_err_worker);
  1490. INIT_WORK(&msm_host->hpd_work, dsi_hpd_worker);
  1491. msm_dsi->id = msm_host->id;
  1492. DBG("Dsi Host %d initialized", msm_host->id);
  1493. return 0;
  1494. fail:
  1495. return ret;
  1496. }
  1497. void msm_dsi_host_destroy(struct mipi_dsi_host *host)
  1498. {
  1499. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1500. DBG("");
  1501. dsi_tx_buf_free(msm_host);
  1502. if (msm_host->workqueue) {
  1503. flush_workqueue(msm_host->workqueue);
  1504. destroy_workqueue(msm_host->workqueue);
  1505. msm_host->workqueue = NULL;
  1506. }
  1507. mutex_destroy(&msm_host->cmd_mutex);
  1508. mutex_destroy(&msm_host->dev_mutex);
  1509. pm_runtime_disable(&msm_host->pdev->dev);
  1510. }
  1511. int msm_dsi_host_modeset_init(struct mipi_dsi_host *host,
  1512. struct drm_device *dev)
  1513. {
  1514. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1515. struct platform_device *pdev = msm_host->pdev;
  1516. int ret;
  1517. msm_host->irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
  1518. if (msm_host->irq < 0) {
  1519. ret = msm_host->irq;
  1520. dev_err(dev->dev, "failed to get irq: %d\n", ret);
  1521. return ret;
  1522. }
  1523. ret = devm_request_irq(&pdev->dev, msm_host->irq,
  1524. dsi_host_irq, IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  1525. "dsi_isr", msm_host);
  1526. if (ret < 0) {
  1527. dev_err(&pdev->dev, "failed to request IRQ%u: %d\n",
  1528. msm_host->irq, ret);
  1529. return ret;
  1530. }
  1531. msm_host->dev = dev;
  1532. ret = dsi_tx_buf_alloc(msm_host, SZ_4K);
  1533. if (ret) {
  1534. pr_err("%s: alloc tx gem obj failed, %d\n", __func__, ret);
  1535. return ret;
  1536. }
  1537. return 0;
  1538. }
  1539. int msm_dsi_host_register(struct mipi_dsi_host *host, bool check_defer)
  1540. {
  1541. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1542. int ret;
  1543. /* Register mipi dsi host */
  1544. if (!msm_host->registered) {
  1545. host->dev = &msm_host->pdev->dev;
  1546. host->ops = &dsi_host_ops;
  1547. ret = mipi_dsi_host_register(host);
  1548. if (ret)
  1549. return ret;
  1550. msm_host->registered = true;
  1551. /* If the panel driver has not been probed after host register,
  1552. * we should defer the host's probe.
  1553. * It makes sure panel is connected when fbcon detects
  1554. * connector status and gets the proper display mode to
  1555. * create framebuffer.
  1556. * Don't try to defer if there is nothing connected to the dsi
  1557. * output
  1558. */
  1559. if (check_defer && msm_host->device_node) {
  1560. if (!of_drm_find_panel(msm_host->device_node))
  1561. if (!of_drm_find_bridge(msm_host->device_node))
  1562. return -EPROBE_DEFER;
  1563. }
  1564. }
  1565. return 0;
  1566. }
  1567. void msm_dsi_host_unregister(struct mipi_dsi_host *host)
  1568. {
  1569. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1570. if (msm_host->registered) {
  1571. mipi_dsi_host_unregister(host);
  1572. host->dev = NULL;
  1573. host->ops = NULL;
  1574. msm_host->registered = false;
  1575. }
  1576. }
  1577. int msm_dsi_host_xfer_prepare(struct mipi_dsi_host *host,
  1578. const struct mipi_dsi_msg *msg)
  1579. {
  1580. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1581. /* TODO: make sure dsi_cmd_mdp is idle.
  1582. * Since DSI6G v1.2.0, we can set DSI_TRIG_CTRL.BLOCK_DMA_WITHIN_FRAME
  1583. * to ask H/W to wait until cmd mdp is idle. S/W wait is not needed.
  1584. * How to handle the old versions? Wait for mdp cmd done?
  1585. */
  1586. /*
  1587. * mdss interrupt is generated in mdp core clock domain
  1588. * mdp clock need to be enabled to receive dsi interrupt
  1589. */
  1590. pm_runtime_get_sync(&msm_host->pdev->dev);
  1591. dsi_link_clk_enable(msm_host);
  1592. /* TODO: vote for bus bandwidth */
  1593. if (!(msg->flags & MIPI_DSI_MSG_USE_LPM))
  1594. dsi_set_tx_power_mode(0, msm_host);
  1595. msm_host->dma_cmd_ctrl_restore = dsi_read(msm_host, REG_DSI_CTRL);
  1596. dsi_write(msm_host, REG_DSI_CTRL,
  1597. msm_host->dma_cmd_ctrl_restore |
  1598. DSI_CTRL_CMD_MODE_EN |
  1599. DSI_CTRL_ENABLE);
  1600. dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_DMA_DONE, 1);
  1601. return 0;
  1602. }
  1603. void msm_dsi_host_xfer_restore(struct mipi_dsi_host *host,
  1604. const struct mipi_dsi_msg *msg)
  1605. {
  1606. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1607. dsi_intr_ctrl(msm_host, DSI_IRQ_MASK_CMD_DMA_DONE, 0);
  1608. dsi_write(msm_host, REG_DSI_CTRL, msm_host->dma_cmd_ctrl_restore);
  1609. if (!(msg->flags & MIPI_DSI_MSG_USE_LPM))
  1610. dsi_set_tx_power_mode(1, msm_host);
  1611. /* TODO: unvote for bus bandwidth */
  1612. dsi_link_clk_disable(msm_host);
  1613. pm_runtime_put_autosuspend(&msm_host->pdev->dev);
  1614. }
  1615. int msm_dsi_host_cmd_tx(struct mipi_dsi_host *host,
  1616. const struct mipi_dsi_msg *msg)
  1617. {
  1618. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1619. return dsi_cmds2buf_tx(msm_host, msg);
  1620. }
  1621. int msm_dsi_host_cmd_rx(struct mipi_dsi_host *host,
  1622. const struct mipi_dsi_msg *msg)
  1623. {
  1624. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1625. const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
  1626. int data_byte, rx_byte, dlen, end;
  1627. int short_response, diff, pkt_size, ret = 0;
  1628. char cmd;
  1629. int rlen = msg->rx_len;
  1630. u8 *buf;
  1631. if (rlen <= 2) {
  1632. short_response = 1;
  1633. pkt_size = rlen;
  1634. rx_byte = 4;
  1635. } else {
  1636. short_response = 0;
  1637. data_byte = 10; /* first read */
  1638. if (rlen < data_byte)
  1639. pkt_size = rlen;
  1640. else
  1641. pkt_size = data_byte;
  1642. rx_byte = data_byte + 6; /* 4 header + 2 crc */
  1643. }
  1644. buf = msm_host->rx_buf;
  1645. end = 0;
  1646. while (!end) {
  1647. u8 tx[2] = {pkt_size & 0xff, pkt_size >> 8};
  1648. struct mipi_dsi_msg max_pkt_size_msg = {
  1649. .channel = msg->channel,
  1650. .type = MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE,
  1651. .tx_len = 2,
  1652. .tx_buf = tx,
  1653. };
  1654. DBG("rlen=%d pkt_size=%d rx_byte=%d",
  1655. rlen, pkt_size, rx_byte);
  1656. ret = dsi_cmds2buf_tx(msm_host, &max_pkt_size_msg);
  1657. if (ret < 2) {
  1658. pr_err("%s: Set max pkt size failed, %d\n",
  1659. __func__, ret);
  1660. return -EINVAL;
  1661. }
  1662. if ((cfg_hnd->major == MSM_DSI_VER_MAJOR_6G) &&
  1663. (cfg_hnd->minor >= MSM_DSI_6G_VER_MINOR_V1_1)) {
  1664. /* Clear the RDBK_DATA registers */
  1665. dsi_write(msm_host, REG_DSI_RDBK_DATA_CTRL,
  1666. DSI_RDBK_DATA_CTRL_CLR);
  1667. wmb(); /* make sure the RDBK registers are cleared */
  1668. dsi_write(msm_host, REG_DSI_RDBK_DATA_CTRL, 0);
  1669. wmb(); /* release cleared status before transfer */
  1670. }
  1671. ret = dsi_cmds2buf_tx(msm_host, msg);
  1672. if (ret < msg->tx_len) {
  1673. pr_err("%s: Read cmd Tx failed, %d\n", __func__, ret);
  1674. return ret;
  1675. }
  1676. /*
  1677. * once cmd_dma_done interrupt received,
  1678. * return data from client is ready and stored
  1679. * at RDBK_DATA register already
  1680. * since rx fifo is 16 bytes, dcs header is kept at first loop,
  1681. * after that dcs header lost during shift into registers
  1682. */
  1683. dlen = dsi_cmd_dma_rx(msm_host, buf, rx_byte, pkt_size);
  1684. if (dlen <= 0)
  1685. return 0;
  1686. if (short_response)
  1687. break;
  1688. if (rlen <= data_byte) {
  1689. diff = data_byte - rlen;
  1690. end = 1;
  1691. } else {
  1692. diff = 0;
  1693. rlen -= data_byte;
  1694. }
  1695. if (!end) {
  1696. dlen -= 2; /* 2 crc */
  1697. dlen -= diff;
  1698. buf += dlen; /* next start position */
  1699. data_byte = 14; /* NOT first read */
  1700. if (rlen < data_byte)
  1701. pkt_size += rlen;
  1702. else
  1703. pkt_size += data_byte;
  1704. DBG("buf=%p dlen=%d diff=%d", buf, dlen, diff);
  1705. }
  1706. }
  1707. /*
  1708. * For single Long read, if the requested rlen < 10,
  1709. * we need to shift the start position of rx
  1710. * data buffer to skip the bytes which are not
  1711. * updated.
  1712. */
  1713. if (pkt_size < 10 && !short_response)
  1714. buf = msm_host->rx_buf + (10 - rlen);
  1715. else
  1716. buf = msm_host->rx_buf;
  1717. cmd = buf[0];
  1718. switch (cmd) {
  1719. case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
  1720. pr_err("%s: rx ACK_ERR_PACLAGE\n", __func__);
  1721. ret = 0;
  1722. break;
  1723. case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE:
  1724. case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
  1725. ret = dsi_short_read1_resp(buf, msg);
  1726. break;
  1727. case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE:
  1728. case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
  1729. ret = dsi_short_read2_resp(buf, msg);
  1730. break;
  1731. case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
  1732. case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
  1733. ret = dsi_long_read_resp(buf, msg);
  1734. break;
  1735. default:
  1736. pr_warn("%s:Invalid response cmd\n", __func__);
  1737. ret = 0;
  1738. }
  1739. return ret;
  1740. }
  1741. void msm_dsi_host_cmd_xfer_commit(struct mipi_dsi_host *host, u32 dma_base,
  1742. u32 len)
  1743. {
  1744. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1745. dsi_write(msm_host, REG_DSI_DMA_BASE, dma_base);
  1746. dsi_write(msm_host, REG_DSI_DMA_LEN, len);
  1747. dsi_write(msm_host, REG_DSI_TRIG_DMA, 1);
  1748. /* Make sure trigger happens */
  1749. wmb();
  1750. }
  1751. int msm_dsi_host_set_src_pll(struct mipi_dsi_host *host,
  1752. struct msm_dsi_pll *src_pll)
  1753. {
  1754. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1755. const struct msm_dsi_cfg_handler *cfg_hnd = msm_host->cfg_hnd;
  1756. struct clk *byte_clk_provider, *pixel_clk_provider;
  1757. int ret;
  1758. ret = msm_dsi_pll_get_clk_provider(src_pll,
  1759. &byte_clk_provider, &pixel_clk_provider);
  1760. if (ret) {
  1761. pr_info("%s: can't get provider from pll, don't set parent\n",
  1762. __func__);
  1763. return 0;
  1764. }
  1765. ret = clk_set_parent(msm_host->byte_clk_src, byte_clk_provider);
  1766. if (ret) {
  1767. pr_err("%s: can't set parent to byte_clk_src. ret=%d\n",
  1768. __func__, ret);
  1769. goto exit;
  1770. }
  1771. ret = clk_set_parent(msm_host->pixel_clk_src, pixel_clk_provider);
  1772. if (ret) {
  1773. pr_err("%s: can't set parent to pixel_clk_src. ret=%d\n",
  1774. __func__, ret);
  1775. goto exit;
  1776. }
  1777. if (cfg_hnd->major == MSM_DSI_VER_MAJOR_V2) {
  1778. ret = clk_set_parent(msm_host->dsi_clk_src, pixel_clk_provider);
  1779. if (ret) {
  1780. pr_err("%s: can't set parent to dsi_clk_src. ret=%d\n",
  1781. __func__, ret);
  1782. goto exit;
  1783. }
  1784. ret = clk_set_parent(msm_host->esc_clk_src, byte_clk_provider);
  1785. if (ret) {
  1786. pr_err("%s: can't set parent to esc_clk_src. ret=%d\n",
  1787. __func__, ret);
  1788. goto exit;
  1789. }
  1790. }
  1791. exit:
  1792. return ret;
  1793. }
  1794. void msm_dsi_host_reset_phy(struct mipi_dsi_host *host)
  1795. {
  1796. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1797. DBG("");
  1798. dsi_write(msm_host, REG_DSI_PHY_RESET, DSI_PHY_RESET_RESET);
  1799. /* Make sure fully reset */
  1800. wmb();
  1801. udelay(1000);
  1802. dsi_write(msm_host, REG_DSI_PHY_RESET, 0);
  1803. udelay(100);
  1804. }
  1805. void msm_dsi_host_get_phy_clk_req(struct mipi_dsi_host *host,
  1806. struct msm_dsi_phy_clk_request *clk_req)
  1807. {
  1808. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1809. int ret;
  1810. ret = dsi_calc_clk_rate(msm_host);
  1811. if (ret) {
  1812. pr_err("%s: unable to calc clk rate, %d\n", __func__, ret);
  1813. return;
  1814. }
  1815. clk_req->bitclk_rate = msm_host->byte_clk_rate * 8;
  1816. clk_req->escclk_rate = msm_host->esc_clk_rate;
  1817. }
  1818. int msm_dsi_host_enable(struct mipi_dsi_host *host)
  1819. {
  1820. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1821. dsi_op_mode_config(msm_host,
  1822. !!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO), true);
  1823. /* TODO: clock should be turned off for command mode,
  1824. * and only turned on before MDP START.
  1825. * This part of code should be enabled once mdp driver support it.
  1826. */
  1827. /* if (msm_panel->mode == MSM_DSI_CMD_MODE) {
  1828. * dsi_link_clk_disable(msm_host);
  1829. * pm_runtime_put_autosuspend(&msm_host->pdev->dev);
  1830. * }
  1831. */
  1832. msm_host->enabled = true;
  1833. return 0;
  1834. }
  1835. int msm_dsi_host_disable(struct mipi_dsi_host *host)
  1836. {
  1837. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1838. msm_host->enabled = false;
  1839. dsi_op_mode_config(msm_host,
  1840. !!(msm_host->mode_flags & MIPI_DSI_MODE_VIDEO), false);
  1841. /* Since we have disabled INTF, the video engine won't stop so that
  1842. * the cmd engine will be blocked.
  1843. * Reset to disable video engine so that we can send off cmd.
  1844. */
  1845. dsi_sw_reset(msm_host);
  1846. return 0;
  1847. }
  1848. static void msm_dsi_sfpb_config(struct msm_dsi_host *msm_host, bool enable)
  1849. {
  1850. enum sfpb_ahb_arb_master_port_en en;
  1851. if (!msm_host->sfpb)
  1852. return;
  1853. en = enable ? SFPB_MASTER_PORT_ENABLE : SFPB_MASTER_PORT_DISABLE;
  1854. regmap_update_bits(msm_host->sfpb, REG_SFPB_GPREG,
  1855. SFPB_GPREG_MASTER_PORT_EN__MASK,
  1856. SFPB_GPREG_MASTER_PORT_EN(en));
  1857. }
  1858. int msm_dsi_host_power_on(struct mipi_dsi_host *host,
  1859. struct msm_dsi_phy_shared_timings *phy_shared_timings)
  1860. {
  1861. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1862. int ret = 0;
  1863. mutex_lock(&msm_host->dev_mutex);
  1864. if (msm_host->power_on) {
  1865. DBG("dsi host already on");
  1866. goto unlock_ret;
  1867. }
  1868. msm_dsi_sfpb_config(msm_host, true);
  1869. ret = dsi_host_regulator_enable(msm_host);
  1870. if (ret) {
  1871. pr_err("%s:Failed to enable vregs.ret=%d\n",
  1872. __func__, ret);
  1873. goto unlock_ret;
  1874. }
  1875. pm_runtime_get_sync(&msm_host->pdev->dev);
  1876. ret = dsi_link_clk_enable(msm_host);
  1877. if (ret) {
  1878. pr_err("%s: failed to enable link clocks. ret=%d\n",
  1879. __func__, ret);
  1880. goto fail_disable_reg;
  1881. }
  1882. ret = pinctrl_pm_select_default_state(&msm_host->pdev->dev);
  1883. if (ret) {
  1884. pr_err("%s: failed to set pinctrl default state, %d\n",
  1885. __func__, ret);
  1886. goto fail_disable_clk;
  1887. }
  1888. dsi_timing_setup(msm_host);
  1889. dsi_sw_reset(msm_host);
  1890. dsi_ctrl_config(msm_host, true, phy_shared_timings);
  1891. if (msm_host->disp_en_gpio)
  1892. gpiod_set_value(msm_host->disp_en_gpio, 1);
  1893. msm_host->power_on = true;
  1894. mutex_unlock(&msm_host->dev_mutex);
  1895. return 0;
  1896. fail_disable_clk:
  1897. dsi_link_clk_disable(msm_host);
  1898. pm_runtime_put_autosuspend(&msm_host->pdev->dev);
  1899. fail_disable_reg:
  1900. dsi_host_regulator_disable(msm_host);
  1901. unlock_ret:
  1902. mutex_unlock(&msm_host->dev_mutex);
  1903. return ret;
  1904. }
  1905. int msm_dsi_host_power_off(struct mipi_dsi_host *host)
  1906. {
  1907. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1908. mutex_lock(&msm_host->dev_mutex);
  1909. if (!msm_host->power_on) {
  1910. DBG("dsi host already off");
  1911. goto unlock_ret;
  1912. }
  1913. dsi_ctrl_config(msm_host, false, NULL);
  1914. if (msm_host->disp_en_gpio)
  1915. gpiod_set_value(msm_host->disp_en_gpio, 0);
  1916. pinctrl_pm_select_sleep_state(&msm_host->pdev->dev);
  1917. dsi_link_clk_disable(msm_host);
  1918. pm_runtime_put_autosuspend(&msm_host->pdev->dev);
  1919. dsi_host_regulator_disable(msm_host);
  1920. msm_dsi_sfpb_config(msm_host, false);
  1921. DBG("-");
  1922. msm_host->power_on = false;
  1923. unlock_ret:
  1924. mutex_unlock(&msm_host->dev_mutex);
  1925. return 0;
  1926. }
  1927. int msm_dsi_host_set_display_mode(struct mipi_dsi_host *host,
  1928. struct drm_display_mode *mode)
  1929. {
  1930. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1931. if (msm_host->mode) {
  1932. drm_mode_destroy(msm_host->dev, msm_host->mode);
  1933. msm_host->mode = NULL;
  1934. }
  1935. msm_host->mode = drm_mode_duplicate(msm_host->dev, mode);
  1936. if (!msm_host->mode) {
  1937. pr_err("%s: cannot duplicate mode\n", __func__);
  1938. return -ENOMEM;
  1939. }
  1940. return 0;
  1941. }
  1942. struct drm_panel *msm_dsi_host_get_panel(struct mipi_dsi_host *host,
  1943. unsigned long *panel_flags)
  1944. {
  1945. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1946. struct drm_panel *panel;
  1947. panel = of_drm_find_panel(msm_host->device_node);
  1948. if (panel_flags)
  1949. *panel_flags = msm_host->mode_flags;
  1950. return panel;
  1951. }
  1952. struct drm_bridge *msm_dsi_host_get_bridge(struct mipi_dsi_host *host)
  1953. {
  1954. struct msm_dsi_host *msm_host = to_msm_dsi_host(host);
  1955. return of_drm_find_bridge(msm_host->device_node);
  1956. }