mcde_dsi.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #include <linux/clk.h>
  3. #include <linux/component.h>
  4. #include <linux/delay.h>
  5. #include <linux/io.h>
  6. #include <linux/mfd/syscon.h>
  7. #include <linux/module.h>
  8. #include <linux/of.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/regmap.h>
  11. #include <linux/regulator/consumer.h>
  12. #include <video/mipi_display.h>
  13. #include <drm/drm_atomic_helper.h>
  14. #include <drm/drm_bridge.h>
  15. #include <drm/drm_device.h>
  16. #include <drm/drm_drv.h>
  17. #include <drm/drm_encoder.h>
  18. #include <drm/drm_mipi_dsi.h>
  19. #include <drm/drm_modeset_helper_vtables.h>
  20. #include <drm/drm_of.h>
  21. #include <drm/drm_panel.h>
  22. #include <drm/drm_print.h>
  23. #include <drm/drm_probe_helper.h>
  24. #include "mcde_drm.h"
  25. #include "mcde_dsi_regs.h"
  26. #define DSI_DEFAULT_LP_FREQ_HZ 19200000
  27. #define DSI_DEFAULT_HS_FREQ_HZ 420160000
  28. /* PRCMU DSI reset registers */
  29. #define PRCM_DSI_SW_RESET 0x324
  30. #define PRCM_DSI_SW_RESET_DSI0_SW_RESETN BIT(0)
  31. #define PRCM_DSI_SW_RESET_DSI1_SW_RESETN BIT(1)
  32. #define PRCM_DSI_SW_RESET_DSI2_SW_RESETN BIT(2)
  33. struct mcde_dsi {
  34. struct device *dev;
  35. struct mcde *mcde;
  36. struct drm_bridge bridge;
  37. struct drm_panel *panel;
  38. struct drm_bridge *bridge_out;
  39. struct mipi_dsi_host dsi_host;
  40. struct mipi_dsi_device *mdsi;
  41. const struct drm_display_mode *mode;
  42. struct clk *hs_clk;
  43. struct clk *lp_clk;
  44. unsigned long hs_freq;
  45. unsigned long lp_freq;
  46. bool unused;
  47. void __iomem *regs;
  48. struct regmap *prcmu;
  49. };
  50. static inline struct mcde_dsi *bridge_to_mcde_dsi(struct drm_bridge *bridge)
  51. {
  52. return container_of(bridge, struct mcde_dsi, bridge);
  53. }
  54. static inline struct mcde_dsi *host_to_mcde_dsi(struct mipi_dsi_host *h)
  55. {
  56. return container_of(h, struct mcde_dsi, dsi_host);
  57. }
  58. bool mcde_dsi_irq(struct mipi_dsi_device *mdsi)
  59. {
  60. struct mcde_dsi *d;
  61. u32 val;
  62. bool te_received = false;
  63. d = host_to_mcde_dsi(mdsi->host);
  64. dev_dbg(d->dev, "%s called\n", __func__);
  65. val = readl(d->regs + DSI_DIRECT_CMD_STS_FLAG);
  66. if (val)
  67. dev_dbg(d->dev, "DSI_DIRECT_CMD_STS_FLAG = %08x\n", val);
  68. if (val & DSI_DIRECT_CMD_STS_WRITE_COMPLETED)
  69. dev_dbg(d->dev, "direct command write completed\n");
  70. if (val & DSI_DIRECT_CMD_STS_TE_RECEIVED) {
  71. te_received = true;
  72. dev_dbg(d->dev, "direct command TE received\n");
  73. }
  74. if (val & DSI_DIRECT_CMD_STS_ACKNOWLEDGE_WITH_ERR_RECEIVED)
  75. dev_err(d->dev, "direct command ACK ERR received\n");
  76. if (val & DSI_DIRECT_CMD_STS_READ_COMPLETED_WITH_ERR)
  77. dev_err(d->dev, "direct command read ERR received\n");
  78. /* Mask off the ACK value and clear status */
  79. writel(val, d->regs + DSI_DIRECT_CMD_STS_CLR);
  80. val = readl(d->regs + DSI_CMD_MODE_STS_FLAG);
  81. if (val)
  82. dev_dbg(d->dev, "DSI_CMD_MODE_STS_FLAG = %08x\n", val);
  83. if (val & DSI_CMD_MODE_STS_ERR_NO_TE)
  84. /* This happens all the time (safe to ignore) */
  85. dev_dbg(d->dev, "CMD mode no TE\n");
  86. if (val & DSI_CMD_MODE_STS_ERR_TE_MISS)
  87. /* This happens all the time (safe to ignore) */
  88. dev_dbg(d->dev, "CMD mode TE miss\n");
  89. if (val & DSI_CMD_MODE_STS_ERR_SDI1_UNDERRUN)
  90. dev_err(d->dev, "CMD mode SD1 underrun\n");
  91. if (val & DSI_CMD_MODE_STS_ERR_SDI2_UNDERRUN)
  92. dev_err(d->dev, "CMD mode SD2 underrun\n");
  93. if (val & DSI_CMD_MODE_STS_ERR_UNWANTED_RD)
  94. dev_err(d->dev, "CMD mode unwanted RD\n");
  95. writel(val, d->regs + DSI_CMD_MODE_STS_CLR);
  96. val = readl(d->regs + DSI_DIRECT_CMD_RD_STS_FLAG);
  97. if (val)
  98. dev_dbg(d->dev, "DSI_DIRECT_CMD_RD_STS_FLAG = %08x\n", val);
  99. writel(val, d->regs + DSI_DIRECT_CMD_RD_STS_CLR);
  100. val = readl(d->regs + DSI_TG_STS_FLAG);
  101. if (val)
  102. dev_dbg(d->dev, "DSI_TG_STS_FLAG = %08x\n", val);
  103. writel(val, d->regs + DSI_TG_STS_CLR);
  104. val = readl(d->regs + DSI_VID_MODE_STS_FLAG);
  105. if (val)
  106. dev_dbg(d->dev, "DSI_VID_MODE_STS_FLAG = %08x\n", val);
  107. if (val & DSI_VID_MODE_STS_VSG_RUNNING)
  108. dev_dbg(d->dev, "VID mode VSG running\n");
  109. if (val & DSI_VID_MODE_STS_ERR_MISSING_DATA)
  110. dev_err(d->dev, "VID mode missing data\n");
  111. if (val & DSI_VID_MODE_STS_ERR_MISSING_HSYNC)
  112. dev_err(d->dev, "VID mode missing HSYNC\n");
  113. if (val & DSI_VID_MODE_STS_ERR_MISSING_VSYNC)
  114. dev_err(d->dev, "VID mode missing VSYNC\n");
  115. if (val & DSI_VID_MODE_STS_REG_ERR_SMALL_LENGTH)
  116. dev_err(d->dev, "VID mode less bytes than expected between two HSYNC\n");
  117. if (val & DSI_VID_MODE_STS_REG_ERR_SMALL_HEIGHT)
  118. dev_err(d->dev, "VID mode less lines than expected between two VSYNC\n");
  119. if (val & (DSI_VID_MODE_STS_ERR_BURSTWRITE |
  120. DSI_VID_MODE_STS_ERR_LINEWRITE |
  121. DSI_VID_MODE_STS_ERR_LONGREAD))
  122. dev_err(d->dev, "VID mode read/write error\n");
  123. if (val & DSI_VID_MODE_STS_ERR_VRS_WRONG_LENGTH)
  124. dev_err(d->dev, "VID mode received packets differ from expected size\n");
  125. if (val & DSI_VID_MODE_STS_VSG_RECOVERY)
  126. dev_err(d->dev, "VID mode VSG in recovery mode\n");
  127. writel(val, d->regs + DSI_VID_MODE_STS_CLR);
  128. return te_received;
  129. }
  130. static void mcde_dsi_attach_to_mcde(struct mcde_dsi *d)
  131. {
  132. d->mcde->mdsi = d->mdsi;
  133. /*
  134. * Select the way the DSI data flow is pushing to the display:
  135. * currently we just support video or command mode depending
  136. * on the type of display. Video mode defaults to using the
  137. * formatter itself for synchronization (stateless video panel).
  138. *
  139. * FIXME: add flags to struct mipi_dsi_device .flags to indicate
  140. * displays that require BTA (bus turn around) so we can handle
  141. * such displays as well. Figure out how to properly handle
  142. * single frame on-demand updates with DRM for command mode
  143. * displays (MCDE_COMMAND_ONESHOT_FLOW).
  144. */
  145. if (d->mdsi->mode_flags & MIPI_DSI_MODE_VIDEO)
  146. d->mcde->flow_mode = MCDE_VIDEO_FORMATTER_FLOW;
  147. else
  148. d->mcde->flow_mode = MCDE_COMMAND_TE_FLOW;
  149. }
  150. static int mcde_dsi_host_attach(struct mipi_dsi_host *host,
  151. struct mipi_dsi_device *mdsi)
  152. {
  153. struct mcde_dsi *d = host_to_mcde_dsi(host);
  154. if (mdsi->lanes < 1 || mdsi->lanes > 2) {
  155. DRM_ERROR("dsi device params invalid, 1 or 2 lanes supported\n");
  156. return -EINVAL;
  157. }
  158. dev_info(d->dev, "attached DSI device with %d lanes\n", mdsi->lanes);
  159. /* MIPI_DSI_FMT_RGB88 etc */
  160. dev_info(d->dev, "format %08x, %dbpp\n", mdsi->format,
  161. mipi_dsi_pixel_format_to_bpp(mdsi->format));
  162. dev_info(d->dev, "mode flags: %08lx\n", mdsi->mode_flags);
  163. d->mdsi = mdsi;
  164. if (d->mcde)
  165. mcde_dsi_attach_to_mcde(d);
  166. return 0;
  167. }
  168. static int mcde_dsi_host_detach(struct mipi_dsi_host *host,
  169. struct mipi_dsi_device *mdsi)
  170. {
  171. struct mcde_dsi *d = host_to_mcde_dsi(host);
  172. d->mdsi = NULL;
  173. if (d->mcde)
  174. d->mcde->mdsi = NULL;
  175. return 0;
  176. }
  177. #define MCDE_DSI_HOST_IS_READ(type) \
  178. ((type == MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM) || \
  179. (type == MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM) || \
  180. (type == MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM) || \
  181. (type == MIPI_DSI_DCS_READ))
  182. static int mcde_dsi_execute_transfer(struct mcde_dsi *d,
  183. const struct mipi_dsi_msg *msg)
  184. {
  185. const u32 loop_delay_us = 10; /* us */
  186. u32 loop_counter;
  187. size_t txlen = msg->tx_len;
  188. size_t rxlen = msg->rx_len;
  189. int i;
  190. u32 val;
  191. int ret;
  192. writel(~0, d->regs + DSI_DIRECT_CMD_STS_CLR);
  193. writel(~0, d->regs + DSI_CMD_MODE_STS_CLR);
  194. /* Send command */
  195. writel(1, d->regs + DSI_DIRECT_CMD_SEND);
  196. loop_counter = 1000 * 1000 / loop_delay_us;
  197. if (MCDE_DSI_HOST_IS_READ(msg->type)) {
  198. /* Read command */
  199. while (!(readl(d->regs + DSI_DIRECT_CMD_STS) &
  200. (DSI_DIRECT_CMD_STS_READ_COMPLETED |
  201. DSI_DIRECT_CMD_STS_READ_COMPLETED_WITH_ERR))
  202. && --loop_counter)
  203. usleep_range(loop_delay_us, (loop_delay_us * 3) / 2);
  204. if (!loop_counter) {
  205. dev_err(d->dev, "DSI read timeout!\n");
  206. /* Set exit code and retry */
  207. return -ETIME;
  208. }
  209. } else {
  210. /* Writing only */
  211. while (!(readl(d->regs + DSI_DIRECT_CMD_STS) &
  212. DSI_DIRECT_CMD_STS_WRITE_COMPLETED)
  213. && --loop_counter)
  214. usleep_range(loop_delay_us, (loop_delay_us * 3) / 2);
  215. if (!loop_counter) {
  216. /* Set exit code and retry */
  217. dev_err(d->dev, "DSI write timeout!\n");
  218. return -ETIME;
  219. }
  220. }
  221. val = readl(d->regs + DSI_DIRECT_CMD_STS);
  222. if (val & DSI_DIRECT_CMD_STS_READ_COMPLETED_WITH_ERR) {
  223. dev_err(d->dev, "read completed with error\n");
  224. writel(1, d->regs + DSI_DIRECT_CMD_RD_INIT);
  225. return -EIO;
  226. }
  227. if (val & DSI_DIRECT_CMD_STS_ACKNOWLEDGE_WITH_ERR_RECEIVED) {
  228. val >>= DSI_DIRECT_CMD_STS_ACK_VAL_SHIFT;
  229. dev_err(d->dev, "error during transmission: %04x\n",
  230. val);
  231. return -EIO;
  232. }
  233. if (!MCDE_DSI_HOST_IS_READ(msg->type)) {
  234. /* Return number of bytes written */
  235. ret = txlen;
  236. } else {
  237. /* OK this is a read command, get the response */
  238. u32 rdsz;
  239. u32 rddat;
  240. u8 *rx = msg->rx_buf;
  241. rdsz = readl(d->regs + DSI_DIRECT_CMD_RD_PROPERTY);
  242. rdsz &= DSI_DIRECT_CMD_RD_PROPERTY_RD_SIZE_MASK;
  243. rddat = readl(d->regs + DSI_DIRECT_CMD_RDDAT);
  244. if (rdsz < rxlen) {
  245. dev_err(d->dev, "read error, requested %zd got %d\n",
  246. rxlen, rdsz);
  247. return -EIO;
  248. }
  249. /* FIXME: read more than 4 bytes */
  250. for (i = 0; i < 4 && i < rxlen; i++)
  251. rx[i] = (rddat >> (i * 8)) & 0xff;
  252. ret = rdsz;
  253. }
  254. /* Successful transmission */
  255. return ret;
  256. }
  257. static ssize_t mcde_dsi_host_transfer(struct mipi_dsi_host *host,
  258. const struct mipi_dsi_msg *msg)
  259. {
  260. struct mcde_dsi *d = host_to_mcde_dsi(host);
  261. const u8 *tx = msg->tx_buf;
  262. size_t txlen = msg->tx_len;
  263. size_t rxlen = msg->rx_len;
  264. unsigned int retries = 0;
  265. u32 val;
  266. int ret;
  267. int i;
  268. if (txlen > 16) {
  269. dev_err(d->dev,
  270. "dunno how to write more than 16 bytes yet\n");
  271. return -EIO;
  272. }
  273. if (rxlen > 4) {
  274. dev_err(d->dev,
  275. "dunno how to read more than 4 bytes yet\n");
  276. return -EIO;
  277. }
  278. dev_dbg(d->dev,
  279. "message to channel %d, write %zd bytes read %zd bytes\n",
  280. msg->channel, txlen, rxlen);
  281. /* Command "nature" */
  282. if (MCDE_DSI_HOST_IS_READ(msg->type))
  283. /* MCTL_MAIN_DATA_CTL already set up */
  284. val = DSI_DIRECT_CMD_MAIN_SETTINGS_CMD_NAT_READ;
  285. else
  286. val = DSI_DIRECT_CMD_MAIN_SETTINGS_CMD_NAT_WRITE;
  287. /*
  288. * More than 2 bytes will not fit in a single packet, so it's
  289. * time to set the "long not short" bit. One byte is used by
  290. * the MIPI DCS command leaving just one byte for the payload
  291. * in a short package.
  292. */
  293. if (mipi_dsi_packet_format_is_long(msg->type))
  294. val |= DSI_DIRECT_CMD_MAIN_SETTINGS_CMD_LONGNOTSHORT;
  295. val |= 0 << DSI_DIRECT_CMD_MAIN_SETTINGS_CMD_ID_SHIFT;
  296. val |= txlen << DSI_DIRECT_CMD_MAIN_SETTINGS_CMD_SIZE_SHIFT;
  297. val |= DSI_DIRECT_CMD_MAIN_SETTINGS_CMD_LP_EN;
  298. val |= msg->type << DSI_DIRECT_CMD_MAIN_SETTINGS_CMD_HEAD_SHIFT;
  299. writel(val, d->regs + DSI_DIRECT_CMD_MAIN_SETTINGS);
  300. /* MIPI DCS command is part of the data */
  301. if (txlen > 0) {
  302. val = 0;
  303. for (i = 0; i < 4 && i < txlen; i++)
  304. val |= tx[i] << (i * 8);
  305. }
  306. writel(val, d->regs + DSI_DIRECT_CMD_WRDAT0);
  307. if (txlen > 4) {
  308. val = 0;
  309. for (i = 0; i < 4 && (i + 4) < txlen; i++)
  310. val |= tx[i + 4] << (i * 8);
  311. writel(val, d->regs + DSI_DIRECT_CMD_WRDAT1);
  312. }
  313. if (txlen > 8) {
  314. val = 0;
  315. for (i = 0; i < 4 && (i + 8) < txlen; i++)
  316. val |= tx[i + 8] << (i * 8);
  317. writel(val, d->regs + DSI_DIRECT_CMD_WRDAT2);
  318. }
  319. if (txlen > 12) {
  320. val = 0;
  321. for (i = 0; i < 4 && (i + 12) < txlen; i++)
  322. val |= tx[i + 12] << (i * 8);
  323. writel(val, d->regs + DSI_DIRECT_CMD_WRDAT3);
  324. }
  325. while (retries < 3) {
  326. ret = mcde_dsi_execute_transfer(d, msg);
  327. if (ret >= 0)
  328. break;
  329. retries++;
  330. }
  331. if (ret < 0 && retries)
  332. dev_err(d->dev, "gave up after %d retries\n", retries);
  333. /* Clear any errors */
  334. writel(~0, d->regs + DSI_DIRECT_CMD_STS_CLR);
  335. writel(~0, d->regs + DSI_CMD_MODE_STS_CLR);
  336. return ret;
  337. }
  338. static const struct mipi_dsi_host_ops mcde_dsi_host_ops = {
  339. .attach = mcde_dsi_host_attach,
  340. .detach = mcde_dsi_host_detach,
  341. .transfer = mcde_dsi_host_transfer,
  342. };
  343. /* This sends a direct (short) command to request TE */
  344. void mcde_dsi_te_request(struct mipi_dsi_device *mdsi)
  345. {
  346. struct mcde_dsi *d;
  347. u32 val;
  348. d = host_to_mcde_dsi(mdsi->host);
  349. /* Command "nature" TE request */
  350. val = DSI_DIRECT_CMD_MAIN_SETTINGS_CMD_NAT_TE_REQ;
  351. val |= 0 << DSI_DIRECT_CMD_MAIN_SETTINGS_CMD_ID_SHIFT;
  352. val |= 2 << DSI_DIRECT_CMD_MAIN_SETTINGS_CMD_SIZE_SHIFT;
  353. val |= DSI_DIRECT_CMD_MAIN_SETTINGS_CMD_LP_EN;
  354. val |= MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM <<
  355. DSI_DIRECT_CMD_MAIN_SETTINGS_CMD_HEAD_SHIFT;
  356. writel(val, d->regs + DSI_DIRECT_CMD_MAIN_SETTINGS);
  357. /* Clear TE reveived and error status bits and enables them */
  358. writel(DSI_DIRECT_CMD_STS_CLR_TE_RECEIVED_CLR |
  359. DSI_DIRECT_CMD_STS_CLR_ACKNOWLEDGE_WITH_ERR_RECEIVED_CLR,
  360. d->regs + DSI_DIRECT_CMD_STS_CLR);
  361. val = readl(d->regs + DSI_DIRECT_CMD_STS_CTL);
  362. val |= DSI_DIRECT_CMD_STS_CTL_TE_RECEIVED_EN;
  363. val |= DSI_DIRECT_CMD_STS_CTL_ACKNOWLEDGE_WITH_ERR_EN;
  364. writel(val, d->regs + DSI_DIRECT_CMD_STS_CTL);
  365. /* Clear and enable no TE or TE missing status */
  366. writel(DSI_CMD_MODE_STS_CLR_ERR_NO_TE_CLR |
  367. DSI_CMD_MODE_STS_CLR_ERR_TE_MISS_CLR,
  368. d->regs + DSI_CMD_MODE_STS_CLR);
  369. val = readl(d->regs + DSI_CMD_MODE_STS_CTL);
  370. val |= DSI_CMD_MODE_STS_CTL_ERR_NO_TE_EN;
  371. val |= DSI_CMD_MODE_STS_CTL_ERR_TE_MISS_EN;
  372. writel(val, d->regs + DSI_CMD_MODE_STS_CTL);
  373. /* Send this TE request command */
  374. writel(1, d->regs + DSI_DIRECT_CMD_SEND);
  375. }
  376. static void mcde_dsi_setup_video_mode(struct mcde_dsi *d,
  377. const struct drm_display_mode *mode)
  378. {
  379. /* cpp, characters per pixel, number of bytes per pixel */
  380. u8 cpp = mipi_dsi_pixel_format_to_bpp(d->mdsi->format) / 8;
  381. u64 pclk;
  382. u64 bpl;
  383. int hfp;
  384. int hbp;
  385. int hsa;
  386. u32 blkline_pck, line_duration;
  387. u32 val;
  388. val = 0;
  389. if (d->mdsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
  390. val |= DSI_VID_MAIN_CTL_BURST_MODE;
  391. if (d->mdsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) {
  392. val |= DSI_VID_MAIN_CTL_SYNC_PULSE_ACTIVE;
  393. val |= DSI_VID_MAIN_CTL_SYNC_PULSE_HORIZONTAL;
  394. }
  395. /* RGB header and pixel mode */
  396. switch (d->mdsi->format) {
  397. case MIPI_DSI_FMT_RGB565:
  398. val |= MIPI_DSI_PACKED_PIXEL_STREAM_16 <<
  399. DSI_VID_MAIN_CTL_HEADER_SHIFT;
  400. val |= DSI_VID_MAIN_CTL_VID_PIXEL_MODE_16BITS;
  401. break;
  402. case MIPI_DSI_FMT_RGB666_PACKED:
  403. val |= MIPI_DSI_PACKED_PIXEL_STREAM_18 <<
  404. DSI_VID_MAIN_CTL_HEADER_SHIFT;
  405. val |= DSI_VID_MAIN_CTL_VID_PIXEL_MODE_18BITS;
  406. break;
  407. case MIPI_DSI_FMT_RGB666:
  408. val |= MIPI_DSI_PIXEL_STREAM_3BYTE_18
  409. << DSI_VID_MAIN_CTL_HEADER_SHIFT;
  410. val |= DSI_VID_MAIN_CTL_VID_PIXEL_MODE_18BITS_LOOSE;
  411. break;
  412. case MIPI_DSI_FMT_RGB888:
  413. val |= MIPI_DSI_PACKED_PIXEL_STREAM_24 <<
  414. DSI_VID_MAIN_CTL_HEADER_SHIFT;
  415. val |= DSI_VID_MAIN_CTL_VID_PIXEL_MODE_24BITS;
  416. break;
  417. default:
  418. dev_err(d->dev, "unknown pixel mode\n");
  419. return;
  420. }
  421. /* TODO: TVG (test video generator) could be enabled here */
  422. /*
  423. * During vertical blanking: go to LP mode
  424. * Like with the EOL setting, if this is not set, the EOL area will be
  425. * filled with NULL or blanking packets in the vblank area.
  426. * FIXME: some Samsung phones and display panels such as s6e63m0 use
  427. * DSI_VID_MAIN_CTL_REG_BLKLINE_MODE_BLANKING here instead,
  428. * figure out how to properly configure that from the panel.
  429. */
  430. val |= DSI_VID_MAIN_CTL_REG_BLKLINE_MODE_LP_0;
  431. /*
  432. * During EOL: go to LP mode. If this is not set, the EOL area will be
  433. * filled with NULL or blanking packets.
  434. */
  435. val |= DSI_VID_MAIN_CTL_REG_BLKEOL_MODE_LP_0;
  436. /* Recovery mode 1 */
  437. val |= 1 << DSI_VID_MAIN_CTL_RECOVERY_MODE_SHIFT;
  438. /* All other fields zero */
  439. writel(val, d->regs + DSI_VID_MAIN_CTL);
  440. /* Vertical frame parameters are pretty straight-forward */
  441. val = mode->vdisplay << DSI_VID_VSIZE_VACT_LENGTH_SHIFT;
  442. /* vertical front porch */
  443. val |= (mode->vsync_start - mode->vdisplay)
  444. << DSI_VID_VSIZE_VFP_LENGTH_SHIFT;
  445. /* vertical sync active */
  446. val |= (mode->vsync_end - mode->vsync_start)
  447. << DSI_VID_VSIZE_VSA_LENGTH_SHIFT;
  448. /* vertical back porch */
  449. val |= (mode->vtotal - mode->vsync_end)
  450. << DSI_VID_VSIZE_VBP_LENGTH_SHIFT;
  451. writel(val, d->regs + DSI_VID_VSIZE);
  452. /*
  453. * Horizontal frame parameters:
  454. * horizontal resolution is given in pixels but must be re-calculated
  455. * into bytes since this is what the hardware expects, these registers
  456. * define the payload size of the packet.
  457. *
  458. * hfp = horizontal front porch in bytes
  459. * hbp = horizontal back porch in bytes
  460. * hsa = horizontal sync active in bytes
  461. *
  462. * 6 + 2 is HFP header + checksum
  463. */
  464. hfp = (mode->hsync_start - mode->hdisplay) * cpp - 6 - 2;
  465. if (d->mdsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) {
  466. /*
  467. * Use sync pulse for sync: explicit HSA time
  468. * 6 is HBP header + checksum
  469. * 4 is RGB header + checksum
  470. */
  471. hbp = (mode->htotal - mode->hsync_end) * cpp - 4 - 6;
  472. /*
  473. * 6 is HBP header + checksum
  474. * 4 is HSW packet bytes
  475. * 4 is RGB header + checksum
  476. */
  477. hsa = (mode->hsync_end - mode->hsync_start) * cpp - 4 - 4 - 6;
  478. } else {
  479. /*
  480. * Use event for sync: HBP includes both back porch and sync
  481. * 6 is HBP header + checksum
  482. * 4 is HSW packet bytes
  483. * 4 is RGB header + checksum
  484. */
  485. hbp = (mode->htotal - mode->hsync_start) * cpp - 4 - 4 - 6;
  486. /* HSA is not present in this mode and set to 0 */
  487. hsa = 0;
  488. }
  489. if (hfp < 0) {
  490. dev_info(d->dev, "hfp negative, set to 0\n");
  491. hfp = 0;
  492. }
  493. if (hbp < 0) {
  494. dev_info(d->dev, "hbp negative, set to 0\n");
  495. hbp = 0;
  496. }
  497. if (hsa < 0) {
  498. dev_info(d->dev, "hsa negative, set to 0\n");
  499. hsa = 0;
  500. }
  501. dev_dbg(d->dev, "hfp: %u, hbp: %u, hsa: %u bytes\n",
  502. hfp, hbp, hsa);
  503. /* Frame parameters: horizontal sync active */
  504. val = hsa << DSI_VID_HSIZE1_HSA_LENGTH_SHIFT;
  505. /* horizontal back porch */
  506. val |= hbp << DSI_VID_HSIZE1_HBP_LENGTH_SHIFT;
  507. /* horizontal front porch */
  508. val |= hfp << DSI_VID_HSIZE1_HFP_LENGTH_SHIFT;
  509. writel(val, d->regs + DSI_VID_HSIZE1);
  510. /* RGB data length (visible bytes on one scanline) */
  511. val = mode->hdisplay * cpp;
  512. writel(val, d->regs + DSI_VID_HSIZE2);
  513. dev_dbg(d->dev, "RGB length, visible area on a line: %u bytes\n", val);
  514. /*
  515. * Calculate the time between two pixels in picoseconds using
  516. * the supplied refresh rate and total resolution including
  517. * porches and sync.
  518. */
  519. /* (ps/s) / (pixels/s) = ps/pixels */
  520. pclk = DIV_ROUND_UP_ULL(1000000000000, (mode->clock * 1000));
  521. dev_dbg(d->dev, "picoseconds between two pixels: %llu\n",
  522. pclk);
  523. /*
  524. * How many bytes per line will this update frequency yield?
  525. *
  526. * Calculate the number of picoseconds for one scanline (1), then
  527. * divide by 1000000000000 (2) to get in pixels per second we
  528. * want to output.
  529. *
  530. * Multiply with number of bytes per second at this video display
  531. * frequency (3) to get number of bytes transferred during this
  532. * time. Notice that we use the frequency the display wants,
  533. * not what we actually get from the DSI PLL, which is hs_freq.
  534. *
  535. * These arithmetics are done in a different order to avoid
  536. * overflow.
  537. */
  538. bpl = pclk * mode->htotal; /* (1) picoseconds per line */
  539. dev_dbg(d->dev, "picoseconds per line: %llu\n", bpl);
  540. /* Multiply with bytes per second (3) */
  541. bpl *= (d->mdsi->hs_rate / 8);
  542. /* Pixels per second (2) */
  543. bpl = DIV_ROUND_DOWN_ULL(bpl, 1000000); /* microseconds */
  544. bpl = DIV_ROUND_DOWN_ULL(bpl, 1000000); /* seconds */
  545. /* parallel transactions in all lanes */
  546. bpl *= d->mdsi->lanes;
  547. dev_dbg(d->dev,
  548. "calculated bytes per line: %llu @ %d Hz with HS %lu Hz\n",
  549. bpl, drm_mode_vrefresh(mode), d->mdsi->hs_rate);
  550. /*
  551. * 6 is header + checksum, header = 4 bytes, checksum = 2 bytes
  552. * 4 is short packet for vsync/hsync
  553. */
  554. if (d->mdsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) {
  555. /* Set the event packet size to 0 (not used) */
  556. writel(0, d->regs + DSI_VID_BLKSIZE1);
  557. /*
  558. * FIXME: isn't the hsync width in pixels? The porch and
  559. * sync area size is in pixels here, but this -6
  560. * seems to be for bytes. It looks like this in the vendor
  561. * code though. Is it completely untested?
  562. */
  563. blkline_pck = bpl - (mode->hsync_end - mode->hsync_start) - 6;
  564. val = blkline_pck << DSI_VID_BLKSIZE2_BLKLINE_PULSE_PCK_SHIFT;
  565. writel(val, d->regs + DSI_VID_BLKSIZE2);
  566. } else {
  567. /* Set the sync pulse packet size to 0 (not used) */
  568. writel(0, d->regs + DSI_VID_BLKSIZE2);
  569. /* Specifying payload size in bytes (-4-6 from manual) */
  570. blkline_pck = bpl - 4 - 6;
  571. if (blkline_pck > 0x1FFF)
  572. dev_err(d->dev, "blkline_pck too big %d bytes\n",
  573. blkline_pck);
  574. val = blkline_pck << DSI_VID_BLKSIZE1_BLKLINE_EVENT_PCK_SHIFT;
  575. val &= DSI_VID_BLKSIZE1_BLKLINE_EVENT_PCK_MASK;
  576. writel(val, d->regs + DSI_VID_BLKSIZE1);
  577. }
  578. /*
  579. * The line duration is used to scale back the frequency from
  580. * the max frequency supported by the HS clock to the desired
  581. * update frequency in vrefresh.
  582. */
  583. line_duration = blkline_pck + 6;
  584. /*
  585. * The datasheet contains this complex condition to decreasing
  586. * the line duration by 1 under very specific circumstances.
  587. * Here we also imply that LP is used during burst EOL.
  588. */
  589. if (d->mdsi->lanes == 2 && (hsa & 0x01) && (hfp & 0x01)
  590. && (d->mdsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST))
  591. line_duration--;
  592. line_duration = DIV_ROUND_CLOSEST(line_duration, d->mdsi->lanes);
  593. dev_dbg(d->dev, "line duration %u bytes\n", line_duration);
  594. val = line_duration << DSI_VID_DPHY_TIME_REG_LINE_DURATION_SHIFT;
  595. /*
  596. * This is the time to perform LP->HS on D-PHY
  597. * FIXME: nowhere to get this from: DT property on the DSI?
  598. * The manual says this is "system dependent".
  599. * values like 48 and 72 seen in the vendor code.
  600. */
  601. val |= 48 << DSI_VID_DPHY_TIME_REG_WAKEUP_TIME_SHIFT;
  602. writel(val, d->regs + DSI_VID_DPHY_TIME);
  603. /*
  604. * See the manual figure 657 page 2203 for understanding the impact
  605. * of the different burst mode settings.
  606. */
  607. if (d->mdsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST) {
  608. int blkeol_pck, blkeol_duration;
  609. /*
  610. * Packet size at EOL for burst mode, this is only used
  611. * if DSI_VID_MAIN_CTL_REG_BLKEOL_MODE_LP_0 is NOT set,
  612. * but we instead send NULL or blanking packets at EOL.
  613. * This is given in number of bytes.
  614. *
  615. * See the manual page 2198 for the 13 reg_blkeol_pck bits.
  616. */
  617. blkeol_pck = bpl - (mode->htotal * cpp) - 6;
  618. if (blkeol_pck < 0) {
  619. dev_err(d->dev, "video block does not fit on line!\n");
  620. dev_err(d->dev,
  621. "calculated bytes per line: %llu @ %d Hz\n",
  622. bpl, drm_mode_vrefresh(mode));
  623. dev_err(d->dev,
  624. "bytes per line (blkline_pck) %u bytes\n",
  625. blkline_pck);
  626. dev_err(d->dev,
  627. "blkeol_pck becomes %d bytes\n", blkeol_pck);
  628. return;
  629. }
  630. dev_dbg(d->dev, "BLKEOL packet: %d bytes\n", blkeol_pck);
  631. val = readl(d->regs + DSI_VID_BLKSIZE1);
  632. val &= ~DSI_VID_BLKSIZE1_BLKEOL_PCK_MASK;
  633. val |= blkeol_pck << DSI_VID_BLKSIZE1_BLKEOL_PCK_SHIFT;
  634. writel(val, d->regs + DSI_VID_BLKSIZE1);
  635. /* Use the same value for exact burst limit */
  636. val = blkeol_pck <<
  637. DSI_VID_VCA_SETTING2_EXACT_BURST_LIMIT_SHIFT;
  638. val &= DSI_VID_VCA_SETTING2_EXACT_BURST_LIMIT_MASK;
  639. writel(val, d->regs + DSI_VID_VCA_SETTING2);
  640. /*
  641. * This BLKEOL duration is claimed to be the duration in clock
  642. * cycles of the BLLP end-of-line (EOL) period for each line if
  643. * DSI_VID_MAIN_CTL_REG_BLKEOL_MODE_LP_0 is set.
  644. *
  645. * It is hard to trust the manuals' claim that this is in clock
  646. * cycles as we mimic the behaviour of the vendor code, which
  647. * appears to write a number of bytes that would have been
  648. * transferred on a single lane.
  649. *
  650. * See the manual figure 657 page 2203 and page 2198 for the 13
  651. * reg_blkeol_duration bits.
  652. *
  653. * FIXME: should this also be set up also for non-burst mode
  654. * according to figure 565 page 2202?
  655. */
  656. blkeol_duration = DIV_ROUND_CLOSEST(blkeol_pck + 6,
  657. d->mdsi->lanes);
  658. dev_dbg(d->dev, "BLKEOL duration: %d clock cycles\n",
  659. blkeol_duration);
  660. val = readl(d->regs + DSI_VID_PCK_TIME);
  661. val &= ~DSI_VID_PCK_TIME_BLKEOL_DURATION_MASK;
  662. val |= blkeol_duration <<
  663. DSI_VID_PCK_TIME_BLKEOL_DURATION_SHIFT;
  664. writel(val, d->regs + DSI_VID_PCK_TIME);
  665. /* Max burst limit, this is given in bytes */
  666. val = readl(d->regs + DSI_VID_VCA_SETTING1);
  667. val &= ~DSI_VID_VCA_SETTING1_MAX_BURST_LIMIT_MASK;
  668. val |= (blkeol_pck - 6) <<
  669. DSI_VID_VCA_SETTING1_MAX_BURST_LIMIT_SHIFT;
  670. writel(val, d->regs + DSI_VID_VCA_SETTING1);
  671. }
  672. /* Maximum line limit */
  673. val = readl(d->regs + DSI_VID_VCA_SETTING2);
  674. val &= ~DSI_VID_VCA_SETTING2_MAX_LINE_LIMIT_MASK;
  675. val |= (blkline_pck - 6) <<
  676. DSI_VID_VCA_SETTING2_MAX_LINE_LIMIT_SHIFT;
  677. writel(val, d->regs + DSI_VID_VCA_SETTING2);
  678. dev_dbg(d->dev, "blkline pck: %d bytes\n", blkline_pck - 6);
  679. }
  680. static void mcde_dsi_start(struct mcde_dsi *d)
  681. {
  682. unsigned long hs_freq;
  683. u32 val;
  684. int i;
  685. /* No integration mode */
  686. writel(0, d->regs + DSI_MCTL_INTEGRATION_MODE);
  687. /* Enable the DSI port, from drivers/video/mcde/dsilink_v2.c */
  688. val = DSI_MCTL_MAIN_DATA_CTL_LINK_EN |
  689. DSI_MCTL_MAIN_DATA_CTL_BTA_EN |
  690. DSI_MCTL_MAIN_DATA_CTL_READ_EN |
  691. DSI_MCTL_MAIN_DATA_CTL_REG_TE_EN;
  692. if (!(d->mdsi->mode_flags & MIPI_DSI_MODE_NO_EOT_PACKET))
  693. val |= DSI_MCTL_MAIN_DATA_CTL_HOST_EOT_GEN;
  694. writel(val, d->regs + DSI_MCTL_MAIN_DATA_CTL);
  695. /* Set a high command timeout, clear other fields */
  696. val = 0x3ff << DSI_CMD_MODE_CTL_TE_TIMEOUT_SHIFT;
  697. writel(val, d->regs + DSI_CMD_MODE_CTL);
  698. /*
  699. * UI_X4 is described as "unit interval times four"
  700. * I guess since DSI packets are 4 bytes wide, one unit
  701. * is one byte.
  702. */
  703. hs_freq = clk_get_rate(d->hs_clk);
  704. hs_freq /= 1000000; /* MHz */
  705. val = 4000 / hs_freq;
  706. dev_dbg(d->dev, "UI value: %d\n", val);
  707. val <<= DSI_MCTL_DPHY_STATIC_UI_X4_SHIFT;
  708. val &= DSI_MCTL_DPHY_STATIC_UI_X4_MASK;
  709. writel(val, d->regs + DSI_MCTL_DPHY_STATIC);
  710. /*
  711. * Enable clocking: 0x0f (something?) between each burst,
  712. * enable the second lane if needed, enable continuous clock if
  713. * needed, enable switch into ULPM (ultra-low power mode) on
  714. * all the lines.
  715. */
  716. val = 0x0f << DSI_MCTL_MAIN_PHY_CTL_WAIT_BURST_TIME_SHIFT;
  717. if (d->mdsi->lanes == 2)
  718. val |= DSI_MCTL_MAIN_PHY_CTL_LANE2_EN;
  719. if (!(d->mdsi->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS))
  720. val |= DSI_MCTL_MAIN_PHY_CTL_CLK_CONTINUOUS;
  721. val |= DSI_MCTL_MAIN_PHY_CTL_CLK_ULPM_EN |
  722. DSI_MCTL_MAIN_PHY_CTL_DAT1_ULPM_EN |
  723. DSI_MCTL_MAIN_PHY_CTL_DAT2_ULPM_EN;
  724. writel(val, d->regs + DSI_MCTL_MAIN_PHY_CTL);
  725. val = (1 << DSI_MCTL_ULPOUT_TIME_CKLANE_ULPOUT_TIME_SHIFT) |
  726. (1 << DSI_MCTL_ULPOUT_TIME_DATA_ULPOUT_TIME_SHIFT);
  727. writel(val, d->regs + DSI_MCTL_ULPOUT_TIME);
  728. writel(DSI_DPHY_LANES_TRIM_DPHY_SPECS_90_81B_0_90,
  729. d->regs + DSI_DPHY_LANES_TRIM);
  730. /* High PHY timeout */
  731. val = (0x0f << DSI_MCTL_DPHY_TIMEOUT_CLK_DIV_SHIFT) |
  732. (0x3fff << DSI_MCTL_DPHY_TIMEOUT_HSTX_TO_VAL_SHIFT) |
  733. (0x3fff << DSI_MCTL_DPHY_TIMEOUT_LPRX_TO_VAL_SHIFT);
  734. writel(val, d->regs + DSI_MCTL_DPHY_TIMEOUT);
  735. val = DSI_MCTL_MAIN_EN_PLL_START |
  736. DSI_MCTL_MAIN_EN_CKLANE_EN |
  737. DSI_MCTL_MAIN_EN_DAT1_EN |
  738. DSI_MCTL_MAIN_EN_IF1_EN;
  739. if (d->mdsi->lanes == 2)
  740. val |= DSI_MCTL_MAIN_EN_DAT2_EN;
  741. writel(val, d->regs + DSI_MCTL_MAIN_EN);
  742. /* Wait for the PLL to lock and the clock and data lines to come up */
  743. i = 0;
  744. val = DSI_MCTL_MAIN_STS_PLL_LOCK |
  745. DSI_MCTL_MAIN_STS_CLKLANE_READY |
  746. DSI_MCTL_MAIN_STS_DAT1_READY;
  747. if (d->mdsi->lanes == 2)
  748. val |= DSI_MCTL_MAIN_STS_DAT2_READY;
  749. while ((readl(d->regs + DSI_MCTL_MAIN_STS) & val) != val) {
  750. /* Sleep for a millisecond */
  751. usleep_range(1000, 1500);
  752. if (i++ == 100) {
  753. dev_warn(d->dev, "DSI lanes did not start up\n");
  754. return;
  755. }
  756. }
  757. /* TODO needed? */
  758. /* Command mode, clear IF1 ID */
  759. val = readl(d->regs + DSI_CMD_MODE_CTL);
  760. /*
  761. * If we enable low-power mode here,
  762. * then display updates become really slow.
  763. */
  764. if (d->mdsi->mode_flags & MIPI_DSI_MODE_LPM)
  765. val |= DSI_CMD_MODE_CTL_IF1_LP_EN;
  766. val &= ~DSI_CMD_MODE_CTL_IF1_ID_MASK;
  767. writel(val, d->regs + DSI_CMD_MODE_CTL);
  768. /* Wait for DSI PHY to initialize */
  769. usleep_range(100, 200);
  770. dev_info(d->dev, "DSI link enabled\n");
  771. }
  772. /*
  773. * Notice that this is called from inside the display controller
  774. * and not from the bridge callbacks.
  775. */
  776. void mcde_dsi_enable(struct drm_bridge *bridge)
  777. {
  778. struct mcde_dsi *d = bridge_to_mcde_dsi(bridge);
  779. unsigned long hs_freq, lp_freq;
  780. u32 val;
  781. int ret;
  782. /* Copy maximum clock frequencies */
  783. if (d->mdsi->lp_rate)
  784. lp_freq = d->mdsi->lp_rate;
  785. else
  786. lp_freq = DSI_DEFAULT_LP_FREQ_HZ;
  787. if (d->mdsi->hs_rate)
  788. hs_freq = d->mdsi->hs_rate;
  789. else
  790. hs_freq = DSI_DEFAULT_HS_FREQ_HZ;
  791. /* Enable LP (Low Power, Energy Save, ES) and HS (High Speed) clocks */
  792. d->lp_freq = clk_round_rate(d->lp_clk, lp_freq);
  793. ret = clk_set_rate(d->lp_clk, d->lp_freq);
  794. if (ret)
  795. dev_err(d->dev, "failed to set LP clock rate %lu Hz\n",
  796. d->lp_freq);
  797. d->hs_freq = clk_round_rate(d->hs_clk, hs_freq);
  798. ret = clk_set_rate(d->hs_clk, d->hs_freq);
  799. if (ret)
  800. dev_err(d->dev, "failed to set HS clock rate %lu Hz\n",
  801. d->hs_freq);
  802. /* Start clocks */
  803. ret = clk_prepare_enable(d->lp_clk);
  804. if (ret)
  805. dev_err(d->dev, "failed to enable LP clock\n");
  806. else
  807. dev_info(d->dev, "DSI LP clock rate %lu Hz\n",
  808. d->lp_freq);
  809. ret = clk_prepare_enable(d->hs_clk);
  810. if (ret)
  811. dev_err(d->dev, "failed to enable HS clock\n");
  812. else
  813. dev_info(d->dev, "DSI HS clock rate %lu Hz\n",
  814. d->hs_freq);
  815. /* Assert RESET through the PRCMU, active low */
  816. /* FIXME: which DSI block? */
  817. regmap_update_bits(d->prcmu, PRCM_DSI_SW_RESET,
  818. PRCM_DSI_SW_RESET_DSI0_SW_RESETN, 0);
  819. usleep_range(100, 200);
  820. /* De-assert RESET again */
  821. regmap_update_bits(d->prcmu, PRCM_DSI_SW_RESET,
  822. PRCM_DSI_SW_RESET_DSI0_SW_RESETN,
  823. PRCM_DSI_SW_RESET_DSI0_SW_RESETN);
  824. /* Start up the hardware */
  825. mcde_dsi_start(d);
  826. if (d->mdsi->mode_flags & MIPI_DSI_MODE_VIDEO) {
  827. /* Set up the video mode from the DRM mode */
  828. mcde_dsi_setup_video_mode(d, d->mode);
  829. /* Put IF1 into video mode */
  830. val = readl(d->regs + DSI_MCTL_MAIN_DATA_CTL);
  831. val |= DSI_MCTL_MAIN_DATA_CTL_IF1_MODE;
  832. writel(val, d->regs + DSI_MCTL_MAIN_DATA_CTL);
  833. /* Disable command mode on IF1 */
  834. val = readl(d->regs + DSI_CMD_MODE_CTL);
  835. val &= ~DSI_CMD_MODE_CTL_IF1_LP_EN;
  836. writel(val, d->regs + DSI_CMD_MODE_CTL);
  837. /* Enable some error interrupts */
  838. val = readl(d->regs + DSI_VID_MODE_STS_CTL);
  839. val |= DSI_VID_MODE_STS_CTL_ERR_MISSING_VSYNC;
  840. val |= DSI_VID_MODE_STS_CTL_ERR_MISSING_DATA;
  841. writel(val, d->regs + DSI_VID_MODE_STS_CTL);
  842. /* Enable video mode */
  843. val = readl(d->regs + DSI_MCTL_MAIN_DATA_CTL);
  844. val |= DSI_MCTL_MAIN_DATA_CTL_VID_EN;
  845. writel(val, d->regs + DSI_MCTL_MAIN_DATA_CTL);
  846. } else {
  847. /* Command mode, clear IF1 ID */
  848. val = readl(d->regs + DSI_CMD_MODE_CTL);
  849. /*
  850. * If we enable low-power mode here
  851. * the display updates become really slow.
  852. */
  853. if (d->mdsi->mode_flags & MIPI_DSI_MODE_LPM)
  854. val |= DSI_CMD_MODE_CTL_IF1_LP_EN;
  855. val &= ~DSI_CMD_MODE_CTL_IF1_ID_MASK;
  856. writel(val, d->regs + DSI_CMD_MODE_CTL);
  857. }
  858. dev_info(d->dev, "enabled MCDE DSI master\n");
  859. }
  860. static void mcde_dsi_bridge_mode_set(struct drm_bridge *bridge,
  861. const struct drm_display_mode *mode,
  862. const struct drm_display_mode *adj)
  863. {
  864. struct mcde_dsi *d = bridge_to_mcde_dsi(bridge);
  865. if (!d->mdsi) {
  866. dev_err(d->dev, "no DSI device attached to encoder!\n");
  867. return;
  868. }
  869. d->mode = mode;
  870. dev_info(d->dev, "set DSI master to %dx%d %u Hz %s mode\n",
  871. mode->hdisplay, mode->vdisplay, mode->clock * 1000,
  872. (d->mdsi->mode_flags & MIPI_DSI_MODE_VIDEO) ? "VIDEO" : "CMD"
  873. );
  874. }
  875. static void mcde_dsi_wait_for_command_mode_stop(struct mcde_dsi *d)
  876. {
  877. u32 val;
  878. int i;
  879. /*
  880. * Wait until we get out of command mode
  881. * CSM = Command State Machine
  882. */
  883. i = 0;
  884. val = DSI_CMD_MODE_STS_CSM_RUNNING;
  885. while ((readl(d->regs + DSI_CMD_MODE_STS) & val) == val) {
  886. /* Sleep for a millisecond */
  887. usleep_range(1000, 2000);
  888. if (i++ == 100) {
  889. dev_warn(d->dev,
  890. "could not get out of command mode\n");
  891. return;
  892. }
  893. }
  894. }
  895. static void mcde_dsi_wait_for_video_mode_stop(struct mcde_dsi *d)
  896. {
  897. u32 val;
  898. int i;
  899. /* Wait until we get out og video mode */
  900. i = 0;
  901. val = DSI_VID_MODE_STS_VSG_RUNNING;
  902. while ((readl(d->regs + DSI_VID_MODE_STS) & val) == val) {
  903. /* Sleep for a millisecond */
  904. usleep_range(1000, 2000);
  905. if (i++ == 100) {
  906. dev_warn(d->dev,
  907. "could not get out of video mode\n");
  908. return;
  909. }
  910. }
  911. }
  912. /*
  913. * Notice that this is called from inside the display controller
  914. * and not from the bridge callbacks.
  915. */
  916. void mcde_dsi_disable(struct drm_bridge *bridge)
  917. {
  918. struct mcde_dsi *d = bridge_to_mcde_dsi(bridge);
  919. u32 val;
  920. if (d->mdsi->mode_flags & MIPI_DSI_MODE_VIDEO) {
  921. /* Stop video mode */
  922. val = readl(d->regs + DSI_MCTL_MAIN_DATA_CTL);
  923. val &= ~DSI_MCTL_MAIN_DATA_CTL_VID_EN;
  924. writel(val, d->regs + DSI_MCTL_MAIN_DATA_CTL);
  925. mcde_dsi_wait_for_video_mode_stop(d);
  926. } else {
  927. /* Stop command mode */
  928. mcde_dsi_wait_for_command_mode_stop(d);
  929. }
  930. /*
  931. * Stop clocks and terminate any DSI traffic here so the panel can
  932. * send commands to shut down the display using DSI direct write until
  933. * this point.
  934. */
  935. /* Disable all error interrupts */
  936. writel(0, d->regs + DSI_VID_MODE_STS_CTL);
  937. clk_disable_unprepare(d->hs_clk);
  938. clk_disable_unprepare(d->lp_clk);
  939. }
  940. static int mcde_dsi_bridge_attach(struct drm_bridge *bridge,
  941. struct drm_encoder *encoder,
  942. enum drm_bridge_attach_flags flags)
  943. {
  944. struct mcde_dsi *d = bridge_to_mcde_dsi(bridge);
  945. struct drm_device *drm = bridge->dev;
  946. if (!drm_core_check_feature(drm, DRIVER_ATOMIC)) {
  947. dev_err(d->dev, "we need atomic updates\n");
  948. return -ENOTSUPP;
  949. }
  950. /* Attach the DSI bridge to the output (panel etc) bridge */
  951. return drm_bridge_attach(encoder, d->bridge_out, bridge, flags);
  952. }
  953. static const struct drm_bridge_funcs mcde_dsi_bridge_funcs = {
  954. .attach = mcde_dsi_bridge_attach,
  955. .mode_set = mcde_dsi_bridge_mode_set,
  956. };
  957. static int mcde_dsi_bind(struct device *dev, struct device *master,
  958. void *data)
  959. {
  960. struct drm_device *drm = data;
  961. struct mcde *mcde = to_mcde(drm);
  962. struct mcde_dsi *d = dev_get_drvdata(dev);
  963. struct device_node *child;
  964. struct drm_panel *panel = NULL;
  965. struct drm_bridge *bridge = NULL;
  966. if (!of_get_available_child_count(dev->of_node)) {
  967. dev_info(dev, "unused DSI interface\n");
  968. d->unused = true;
  969. return 0;
  970. }
  971. d->mcde = mcde;
  972. /* If the display attached before binding, set this up */
  973. if (d->mdsi)
  974. mcde_dsi_attach_to_mcde(d);
  975. /* Obtain the clocks */
  976. d->hs_clk = devm_clk_get(dev, "hs");
  977. if (IS_ERR(d->hs_clk)) {
  978. dev_err(dev, "unable to get HS clock\n");
  979. return PTR_ERR(d->hs_clk);
  980. }
  981. d->lp_clk = devm_clk_get(dev, "lp");
  982. if (IS_ERR(d->lp_clk)) {
  983. dev_err(dev, "unable to get LP clock\n");
  984. return PTR_ERR(d->lp_clk);
  985. }
  986. /* Look for a panel as a child to this node */
  987. for_each_available_child_of_node(dev->of_node, child) {
  988. panel = of_drm_find_panel(child);
  989. if (IS_ERR(panel)) {
  990. dev_err(dev, "failed to find panel try bridge (%ld)\n",
  991. PTR_ERR(panel));
  992. panel = NULL;
  993. bridge = of_drm_find_bridge(child);
  994. if (!bridge) {
  995. dev_err(dev, "failed to find bridge\n");
  996. of_node_put(child);
  997. return -EINVAL;
  998. }
  999. }
  1000. }
  1001. if (panel) {
  1002. bridge = drm_panel_bridge_add_typed(panel,
  1003. DRM_MODE_CONNECTOR_DSI);
  1004. if (IS_ERR(bridge)) {
  1005. dev_err(dev, "error adding panel bridge\n");
  1006. return PTR_ERR(bridge);
  1007. }
  1008. dev_info(dev, "connected to panel\n");
  1009. d->panel = panel;
  1010. } else if (bridge) {
  1011. /* TODO: AV8100 HDMI encoder goes here for example */
  1012. dev_info(dev, "connected to non-panel bridge (unsupported)\n");
  1013. return -ENODEV;
  1014. } else {
  1015. dev_err(dev, "no panel or bridge\n");
  1016. return -ENODEV;
  1017. }
  1018. d->bridge_out = bridge;
  1019. /* Create a bridge for this DSI channel */
  1020. d->bridge.of_node = dev->of_node;
  1021. drm_bridge_add(&d->bridge);
  1022. /* TODO: first come first serve, use a list */
  1023. mcde->bridge = &d->bridge;
  1024. dev_info(dev, "initialized MCDE DSI bridge\n");
  1025. return 0;
  1026. }
  1027. static void mcde_dsi_unbind(struct device *dev, struct device *master,
  1028. void *data)
  1029. {
  1030. struct mcde_dsi *d = dev_get_drvdata(dev);
  1031. if (d->panel)
  1032. drm_panel_bridge_remove(d->bridge_out);
  1033. regmap_update_bits(d->prcmu, PRCM_DSI_SW_RESET,
  1034. PRCM_DSI_SW_RESET_DSI0_SW_RESETN, 0);
  1035. }
  1036. static const struct component_ops mcde_dsi_component_ops = {
  1037. .bind = mcde_dsi_bind,
  1038. .unbind = mcde_dsi_unbind,
  1039. };
  1040. static int mcde_dsi_probe(struct platform_device *pdev)
  1041. {
  1042. struct device *dev = &pdev->dev;
  1043. struct mcde_dsi *d;
  1044. struct mipi_dsi_host *host;
  1045. u32 dsi_id;
  1046. int ret;
  1047. d = devm_drm_bridge_alloc(dev, struct mcde_dsi, bridge, &mcde_dsi_bridge_funcs);
  1048. if (IS_ERR(d))
  1049. return PTR_ERR(d);
  1050. d->dev = dev;
  1051. platform_set_drvdata(pdev, d);
  1052. /* Get a handle on the PRCMU so we can do reset */
  1053. d->prcmu =
  1054. syscon_regmap_lookup_by_compatible("stericsson,db8500-prcmu");
  1055. if (IS_ERR(d->prcmu)) {
  1056. dev_err(dev, "no PRCMU regmap\n");
  1057. return PTR_ERR(d->prcmu);
  1058. }
  1059. d->regs = devm_platform_ioremap_resource(pdev, 0);
  1060. if (IS_ERR(d->regs))
  1061. return PTR_ERR(d->regs);
  1062. dsi_id = readl(d->regs + DSI_ID_REG);
  1063. dev_info(dev, "HW revision 0x%08x\n", dsi_id);
  1064. host = &d->dsi_host;
  1065. host->dev = dev;
  1066. host->ops = &mcde_dsi_host_ops;
  1067. ret = mipi_dsi_host_register(host);
  1068. if (ret < 0) {
  1069. dev_err(dev, "failed to register DSI host: %d\n", ret);
  1070. return ret;
  1071. }
  1072. dev_info(dev, "registered DSI host\n");
  1073. platform_set_drvdata(pdev, d);
  1074. return component_add(dev, &mcde_dsi_component_ops);
  1075. }
  1076. static void mcde_dsi_remove(struct platform_device *pdev)
  1077. {
  1078. struct mcde_dsi *d = platform_get_drvdata(pdev);
  1079. component_del(&pdev->dev, &mcde_dsi_component_ops);
  1080. mipi_dsi_host_unregister(&d->dsi_host);
  1081. }
  1082. static const struct of_device_id mcde_dsi_of_match[] = {
  1083. {
  1084. .compatible = "ste,mcde-dsi",
  1085. },
  1086. {},
  1087. };
  1088. struct platform_driver mcde_dsi_driver = {
  1089. .driver = {
  1090. .name = "mcde-dsi",
  1091. .of_match_table = mcde_dsi_of_match,
  1092. },
  1093. .probe = mcde_dsi_probe,
  1094. .remove = mcde_dsi_remove,
  1095. };