dsi_host.c 56 KB

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