dsi.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. /*
  2. * Copyright (C) 2013 NVIDIA Corporation
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software and its
  5. * documentation for any purpose is hereby granted without fee, provided that
  6. * the above copyright notice appear in all copies and that both that copyright
  7. * notice and this permission notice appear in supporting documentation, and
  8. * that the name of the copyright holders not be used in advertising or
  9. * publicity pertaining to distribution of the software without specific,
  10. * written prior permission. The copyright holders make no representations
  11. * about the suitability of this software for any purpose. It is provided "as
  12. * is" without express or implied warranty.
  13. *
  14. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  16. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  19. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  20. * OF THIS SOFTWARE.
  21. */
  22. #include <linux/clk.h>
  23. #include <linux/debugfs.h>
  24. #include <linux/host1x.h>
  25. #include <linux/module.h>
  26. #include <linux/of.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/reset.h>
  29. #include <drm/drm_mipi_dsi.h>
  30. #include <drm/drm_panel.h>
  31. #include <video/mipi_display.h>
  32. #include "dc.h"
  33. #include "drm.h"
  34. #include "dsi.h"
  35. #include "mipi-phy.h"
  36. #define DSI_VIDEO_FIFO_DEPTH (1920 / 4)
  37. #define DSI_HOST_FIFO_DEPTH 64
  38. struct tegra_dsi {
  39. struct host1x_client client;
  40. struct tegra_output output;
  41. struct device *dev;
  42. void __iomem *regs;
  43. struct reset_control *rst;
  44. struct clk *clk_parent;
  45. struct clk *clk_lp;
  46. struct clk *clk;
  47. struct drm_info_list *debugfs_files;
  48. struct drm_minor *minor;
  49. struct dentry *debugfs;
  50. enum mipi_dsi_pixel_format format;
  51. unsigned int lanes;
  52. struct tegra_mipi_device *mipi;
  53. struct mipi_dsi_host host;
  54. };
  55. static inline struct tegra_dsi *
  56. host1x_client_to_dsi(struct host1x_client *client)
  57. {
  58. return container_of(client, struct tegra_dsi, client);
  59. }
  60. static inline struct tegra_dsi *host_to_tegra(struct mipi_dsi_host *host)
  61. {
  62. return container_of(host, struct tegra_dsi, host);
  63. }
  64. static inline struct tegra_dsi *to_dsi(struct tegra_output *output)
  65. {
  66. return container_of(output, struct tegra_dsi, output);
  67. }
  68. static inline unsigned long tegra_dsi_readl(struct tegra_dsi *dsi,
  69. unsigned long reg)
  70. {
  71. return readl(dsi->regs + (reg << 2));
  72. }
  73. static inline void tegra_dsi_writel(struct tegra_dsi *dsi, unsigned long value,
  74. unsigned long reg)
  75. {
  76. writel(value, dsi->regs + (reg << 2));
  77. }
  78. static int tegra_dsi_show_regs(struct seq_file *s, void *data)
  79. {
  80. struct drm_info_node *node = s->private;
  81. struct tegra_dsi *dsi = node->info_ent->data;
  82. #define DUMP_REG(name) \
  83. seq_printf(s, "%-32s %#05x %08lx\n", #name, name, \
  84. tegra_dsi_readl(dsi, name))
  85. DUMP_REG(DSI_INCR_SYNCPT);
  86. DUMP_REG(DSI_INCR_SYNCPT_CONTROL);
  87. DUMP_REG(DSI_INCR_SYNCPT_ERROR);
  88. DUMP_REG(DSI_CTXSW);
  89. DUMP_REG(DSI_RD_DATA);
  90. DUMP_REG(DSI_WR_DATA);
  91. DUMP_REG(DSI_POWER_CONTROL);
  92. DUMP_REG(DSI_INT_ENABLE);
  93. DUMP_REG(DSI_INT_STATUS);
  94. DUMP_REG(DSI_INT_MASK);
  95. DUMP_REG(DSI_HOST_CONTROL);
  96. DUMP_REG(DSI_CONTROL);
  97. DUMP_REG(DSI_SOL_DELAY);
  98. DUMP_REG(DSI_MAX_THRESHOLD);
  99. DUMP_REG(DSI_TRIGGER);
  100. DUMP_REG(DSI_TX_CRC);
  101. DUMP_REG(DSI_STATUS);
  102. DUMP_REG(DSI_INIT_SEQ_CONTROL);
  103. DUMP_REG(DSI_INIT_SEQ_DATA_0);
  104. DUMP_REG(DSI_INIT_SEQ_DATA_1);
  105. DUMP_REG(DSI_INIT_SEQ_DATA_2);
  106. DUMP_REG(DSI_INIT_SEQ_DATA_3);
  107. DUMP_REG(DSI_INIT_SEQ_DATA_4);
  108. DUMP_REG(DSI_INIT_SEQ_DATA_5);
  109. DUMP_REG(DSI_INIT_SEQ_DATA_6);
  110. DUMP_REG(DSI_INIT_SEQ_DATA_7);
  111. DUMP_REG(DSI_PKT_SEQ_0_LO);
  112. DUMP_REG(DSI_PKT_SEQ_0_HI);
  113. DUMP_REG(DSI_PKT_SEQ_1_LO);
  114. DUMP_REG(DSI_PKT_SEQ_1_HI);
  115. DUMP_REG(DSI_PKT_SEQ_2_LO);
  116. DUMP_REG(DSI_PKT_SEQ_2_HI);
  117. DUMP_REG(DSI_PKT_SEQ_3_LO);
  118. DUMP_REG(DSI_PKT_SEQ_3_HI);
  119. DUMP_REG(DSI_PKT_SEQ_4_LO);
  120. DUMP_REG(DSI_PKT_SEQ_4_HI);
  121. DUMP_REG(DSI_PKT_SEQ_5_LO);
  122. DUMP_REG(DSI_PKT_SEQ_5_HI);
  123. DUMP_REG(DSI_DCS_CMDS);
  124. DUMP_REG(DSI_PKT_LEN_0_1);
  125. DUMP_REG(DSI_PKT_LEN_2_3);
  126. DUMP_REG(DSI_PKT_LEN_4_5);
  127. DUMP_REG(DSI_PKT_LEN_6_7);
  128. DUMP_REG(DSI_PHY_TIMING_0);
  129. DUMP_REG(DSI_PHY_TIMING_1);
  130. DUMP_REG(DSI_PHY_TIMING_2);
  131. DUMP_REG(DSI_BTA_TIMING);
  132. DUMP_REG(DSI_TIMEOUT_0);
  133. DUMP_REG(DSI_TIMEOUT_1);
  134. DUMP_REG(DSI_TO_TALLY);
  135. DUMP_REG(DSI_PAD_CONTROL_0);
  136. DUMP_REG(DSI_PAD_CONTROL_CD);
  137. DUMP_REG(DSI_PAD_CD_STATUS);
  138. DUMP_REG(DSI_VIDEO_MODE_CONTROL);
  139. DUMP_REG(DSI_PAD_CONTROL_1);
  140. DUMP_REG(DSI_PAD_CONTROL_2);
  141. DUMP_REG(DSI_PAD_CONTROL_3);
  142. DUMP_REG(DSI_PAD_CONTROL_4);
  143. DUMP_REG(DSI_GANGED_MODE_CONTROL);
  144. DUMP_REG(DSI_GANGED_MODE_START);
  145. DUMP_REG(DSI_GANGED_MODE_SIZE);
  146. DUMP_REG(DSI_RAW_DATA_BYTE_COUNT);
  147. DUMP_REG(DSI_ULTRA_LOW_POWER_CONTROL);
  148. DUMP_REG(DSI_INIT_SEQ_DATA_8);
  149. DUMP_REG(DSI_INIT_SEQ_DATA_9);
  150. DUMP_REG(DSI_INIT_SEQ_DATA_10);
  151. DUMP_REG(DSI_INIT_SEQ_DATA_11);
  152. DUMP_REG(DSI_INIT_SEQ_DATA_12);
  153. DUMP_REG(DSI_INIT_SEQ_DATA_13);
  154. DUMP_REG(DSI_INIT_SEQ_DATA_14);
  155. DUMP_REG(DSI_INIT_SEQ_DATA_15);
  156. #undef DUMP_REG
  157. return 0;
  158. }
  159. static struct drm_info_list debugfs_files[] = {
  160. { "regs", tegra_dsi_show_regs, 0, NULL },
  161. };
  162. static int tegra_dsi_debugfs_init(struct tegra_dsi *dsi,
  163. struct drm_minor *minor)
  164. {
  165. const char *name = dev_name(dsi->dev);
  166. unsigned int i;
  167. int err;
  168. dsi->debugfs = debugfs_create_dir(name, minor->debugfs_root);
  169. if (!dsi->debugfs)
  170. return -ENOMEM;
  171. dsi->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
  172. GFP_KERNEL);
  173. if (!dsi->debugfs_files) {
  174. err = -ENOMEM;
  175. goto remove;
  176. }
  177. for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
  178. dsi->debugfs_files[i].data = dsi;
  179. err = drm_debugfs_create_files(dsi->debugfs_files,
  180. ARRAY_SIZE(debugfs_files),
  181. dsi->debugfs, minor);
  182. if (err < 0)
  183. goto free;
  184. dsi->minor = minor;
  185. return 0;
  186. free:
  187. kfree(dsi->debugfs_files);
  188. dsi->debugfs_files = NULL;
  189. remove:
  190. debugfs_remove(dsi->debugfs);
  191. dsi->debugfs = NULL;
  192. return err;
  193. }
  194. static int tegra_dsi_debugfs_exit(struct tegra_dsi *dsi)
  195. {
  196. drm_debugfs_remove_files(dsi->debugfs_files, ARRAY_SIZE(debugfs_files),
  197. dsi->minor);
  198. dsi->minor = NULL;
  199. kfree(dsi->debugfs_files);
  200. dsi->debugfs_files = NULL;
  201. debugfs_remove(dsi->debugfs);
  202. dsi->debugfs = NULL;
  203. return 0;
  204. }
  205. #define PKT_ID0(id) ((((id) & 0x3f) << 3) | (1 << 9))
  206. #define PKT_LEN0(len) (((len) & 0x07) << 0)
  207. #define PKT_ID1(id) ((((id) & 0x3f) << 13) | (1 << 19))
  208. #define PKT_LEN1(len) (((len) & 0x07) << 10)
  209. #define PKT_ID2(id) ((((id) & 0x3f) << 23) | (1 << 29))
  210. #define PKT_LEN2(len) (((len) & 0x07) << 20)
  211. #define PKT_LP (1 << 30)
  212. #define NUM_PKT_SEQ 12
  213. /* non-burst mode with sync-end */
  214. static const u32 pkt_seq_vnb_syne[NUM_PKT_SEQ] = {
  215. [ 0] = PKT_ID0(MIPI_DSI_V_SYNC_START) | PKT_LEN0(0) |
  216. PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
  217. PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
  218. PKT_LP,
  219. [ 1] = 0,
  220. [ 2] = PKT_ID0(MIPI_DSI_V_SYNC_END) | PKT_LEN0(0) |
  221. PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
  222. PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
  223. PKT_LP,
  224. [ 3] = 0,
  225. [ 4] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
  226. PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
  227. PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
  228. PKT_LP,
  229. [ 5] = 0,
  230. [ 6] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
  231. PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
  232. PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0),
  233. [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
  234. PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
  235. PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
  236. [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
  237. PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
  238. PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
  239. PKT_LP,
  240. [ 9] = 0,
  241. [10] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
  242. PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
  243. PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0),
  244. [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
  245. PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
  246. PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
  247. };
  248. static int tegra_dsi_set_phy_timing(struct tegra_dsi *dsi)
  249. {
  250. struct mipi_dphy_timing timing;
  251. unsigned long value, period;
  252. long rate;
  253. int err;
  254. rate = clk_get_rate(dsi->clk);
  255. if (rate < 0)
  256. return rate;
  257. period = DIV_ROUND_CLOSEST(1000000000UL, rate * 2);
  258. err = mipi_dphy_timing_get_default(&timing, period);
  259. if (err < 0)
  260. return err;
  261. err = mipi_dphy_timing_validate(&timing, period);
  262. if (err < 0) {
  263. dev_err(dsi->dev, "failed to validate D-PHY timing: %d\n", err);
  264. return err;
  265. }
  266. /*
  267. * The D-PHY timing fields below are expressed in byte-clock cycles,
  268. * so multiply the period by 8.
  269. */
  270. period *= 8;
  271. value = DSI_TIMING_FIELD(timing.hsexit, period, 1) << 24 |
  272. DSI_TIMING_FIELD(timing.hstrail, period, 0) << 16 |
  273. DSI_TIMING_FIELD(timing.hszero, period, 3) << 8 |
  274. DSI_TIMING_FIELD(timing.hsprepare, period, 1);
  275. tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_0);
  276. value = DSI_TIMING_FIELD(timing.clktrail, period, 1) << 24 |
  277. DSI_TIMING_FIELD(timing.clkpost, period, 1) << 16 |
  278. DSI_TIMING_FIELD(timing.clkzero, period, 1) << 8 |
  279. DSI_TIMING_FIELD(timing.lpx, period, 1);
  280. tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_1);
  281. value = DSI_TIMING_FIELD(timing.clkprepare, period, 1) << 16 |
  282. DSI_TIMING_FIELD(timing.clkpre, period, 1) << 8 |
  283. DSI_TIMING_FIELD(0xff * period, period, 0) << 0;
  284. tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_2);
  285. value = DSI_TIMING_FIELD(timing.taget, period, 1) << 16 |
  286. DSI_TIMING_FIELD(timing.tasure, period, 1) << 8 |
  287. DSI_TIMING_FIELD(timing.tago, period, 1);
  288. tegra_dsi_writel(dsi, value, DSI_BTA_TIMING);
  289. return 0;
  290. }
  291. static int tegra_dsi_get_muldiv(enum mipi_dsi_pixel_format format,
  292. unsigned int *mulp, unsigned int *divp)
  293. {
  294. switch (format) {
  295. case MIPI_DSI_FMT_RGB666_PACKED:
  296. case MIPI_DSI_FMT_RGB888:
  297. *mulp = 3;
  298. *divp = 1;
  299. break;
  300. case MIPI_DSI_FMT_RGB565:
  301. *mulp = 2;
  302. *divp = 1;
  303. break;
  304. case MIPI_DSI_FMT_RGB666:
  305. *mulp = 9;
  306. *divp = 4;
  307. break;
  308. default:
  309. return -EINVAL;
  310. }
  311. return 0;
  312. }
  313. static int tegra_output_dsi_enable(struct tegra_output *output)
  314. {
  315. struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
  316. struct drm_display_mode *mode = &dc->base.mode;
  317. unsigned int hact, hsw, hbp, hfp, i, mul, div;
  318. struct tegra_dsi *dsi = to_dsi(output);
  319. /* FIXME: don't hardcode this */
  320. const u32 *pkt_seq = pkt_seq_vnb_syne;
  321. unsigned long value;
  322. int err;
  323. err = tegra_dsi_get_muldiv(dsi->format, &mul, &div);
  324. if (err < 0)
  325. return err;
  326. err = clk_enable(dsi->clk);
  327. if (err < 0)
  328. return err;
  329. reset_control_deassert(dsi->rst);
  330. value = DSI_CONTROL_CHANNEL(0) | DSI_CONTROL_FORMAT(dsi->format) |
  331. DSI_CONTROL_LANES(dsi->lanes - 1) |
  332. DSI_CONTROL_SOURCE(dc->pipe);
  333. tegra_dsi_writel(dsi, value, DSI_CONTROL);
  334. tegra_dsi_writel(dsi, DSI_VIDEO_FIFO_DEPTH, DSI_MAX_THRESHOLD);
  335. value = DSI_HOST_CONTROL_HS | DSI_HOST_CONTROL_CS |
  336. DSI_HOST_CONTROL_ECC;
  337. tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
  338. value = tegra_dsi_readl(dsi, DSI_CONTROL);
  339. value |= DSI_CONTROL_HS_CLK_CTRL;
  340. value &= ~DSI_CONTROL_TX_TRIG(3);
  341. value &= ~DSI_CONTROL_DCS_ENABLE;
  342. value |= DSI_CONTROL_VIDEO_ENABLE;
  343. value &= ~DSI_CONTROL_HOST_ENABLE;
  344. tegra_dsi_writel(dsi, value, DSI_CONTROL);
  345. err = tegra_dsi_set_phy_timing(dsi);
  346. if (err < 0)
  347. return err;
  348. for (i = 0; i < NUM_PKT_SEQ; i++)
  349. tegra_dsi_writel(dsi, pkt_seq[i], DSI_PKT_SEQ_0_LO + i);
  350. /* horizontal active pixels */
  351. hact = mode->hdisplay * mul / div;
  352. /* horizontal sync width */
  353. hsw = (mode->hsync_end - mode->hsync_start) * mul / div;
  354. hsw -= 10;
  355. /* horizontal back porch */
  356. hbp = (mode->htotal - mode->hsync_end) * mul / div;
  357. hbp -= 14;
  358. /* horizontal front porch */
  359. hfp = (mode->hsync_start - mode->hdisplay) * mul / div;
  360. hfp -= 8;
  361. tegra_dsi_writel(dsi, hsw << 16 | 0, DSI_PKT_LEN_0_1);
  362. tegra_dsi_writel(dsi, hact << 16 | hbp, DSI_PKT_LEN_2_3);
  363. tegra_dsi_writel(dsi, hfp, DSI_PKT_LEN_4_5);
  364. tegra_dsi_writel(dsi, 0x0f0f << 16, DSI_PKT_LEN_6_7);
  365. /* set SOL delay */
  366. tegra_dsi_writel(dsi, 8 * mul / div, DSI_SOL_DELAY);
  367. /* enable display controller */
  368. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  369. value |= DSI_ENABLE;
  370. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  371. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
  372. value &= ~DISP_CTRL_MODE_MASK;
  373. value |= DISP_CTRL_MODE_C_DISPLAY;
  374. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
  375. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
  376. value |= PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
  377. PW4_ENABLE | PM0_ENABLE | PM1_ENABLE;
  378. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
  379. tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
  380. tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
  381. /* enable DSI controller */
  382. value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
  383. value |= DSI_POWER_CONTROL_ENABLE;
  384. tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
  385. return 0;
  386. }
  387. static int tegra_output_dsi_disable(struct tegra_output *output)
  388. {
  389. struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
  390. struct tegra_dsi *dsi = to_dsi(output);
  391. unsigned long value;
  392. /* disable DSI controller */
  393. value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
  394. value &= DSI_POWER_CONTROL_ENABLE;
  395. tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
  396. /*
  397. * The following accesses registers of the display controller, so make
  398. * sure it's only executed when the output is attached to one.
  399. */
  400. if (dc) {
  401. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_POWER_CONTROL);
  402. value &= ~(PW0_ENABLE | PW1_ENABLE | PW2_ENABLE | PW3_ENABLE |
  403. PW4_ENABLE | PM0_ENABLE | PM1_ENABLE);
  404. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_POWER_CONTROL);
  405. value = tegra_dc_readl(dc, DC_CMD_DISPLAY_COMMAND);
  406. value &= ~DISP_CTRL_MODE_MASK;
  407. tegra_dc_writel(dc, value, DC_CMD_DISPLAY_COMMAND);
  408. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  409. value &= ~DSI_ENABLE;
  410. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  411. tegra_dc_writel(dc, GENERAL_ACT_REQ << 8, DC_CMD_STATE_CONTROL);
  412. tegra_dc_writel(dc, GENERAL_ACT_REQ, DC_CMD_STATE_CONTROL);
  413. }
  414. clk_disable(dsi->clk);
  415. return 0;
  416. }
  417. static int tegra_output_dsi_setup_clock(struct tegra_output *output,
  418. struct clk *clk, unsigned long pclk)
  419. {
  420. struct tegra_dc *dc = to_tegra_dc(output->encoder.crtc);
  421. struct drm_display_mode *mode = &dc->base.mode;
  422. unsigned int timeout, mul, div, vrefresh;
  423. struct tegra_dsi *dsi = to_dsi(output);
  424. unsigned long bclk, plld, value;
  425. struct clk *base;
  426. int err;
  427. err = tegra_dsi_get_muldiv(dsi->format, &mul, &div);
  428. if (err < 0)
  429. return err;
  430. vrefresh = drm_mode_vrefresh(mode);
  431. pclk = mode->htotal * mode->vtotal * vrefresh;
  432. bclk = (pclk * mul) / (div * dsi->lanes);
  433. plld = DIV_ROUND_UP(bclk * 8, 1000000);
  434. pclk = (plld * 1000000) / 2;
  435. err = clk_set_parent(clk, dsi->clk_parent);
  436. if (err < 0) {
  437. dev_err(dsi->dev, "failed to set parent clock: %d\n", err);
  438. return err;
  439. }
  440. base = clk_get_parent(dsi->clk_parent);
  441. /*
  442. * This assumes that the parent clock is pll_d_out0 or pll_d2_out
  443. * respectively, each of which divides the base pll_d by 2.
  444. */
  445. err = clk_set_rate(base, pclk * 2);
  446. if (err < 0) {
  447. dev_err(dsi->dev, "failed to set base clock rate to %lu Hz\n",
  448. pclk * 2);
  449. return err;
  450. }
  451. /*
  452. * XXX: Move the below somewhere else so that we don't need to have
  453. * access to the vrefresh in this function?
  454. */
  455. /* one frame high-speed transmission timeout */
  456. timeout = (bclk / vrefresh) / 512;
  457. value = DSI_TIMEOUT_LRX(0x2000) | DSI_TIMEOUT_HTX(timeout);
  458. tegra_dsi_writel(dsi, value, DSI_TIMEOUT_0);
  459. /* 2 ms peripheral timeout for panel */
  460. timeout = 2 * bclk / 512 * 1000;
  461. value = DSI_TIMEOUT_PR(timeout) | DSI_TIMEOUT_TA(0x2000);
  462. tegra_dsi_writel(dsi, value, DSI_TIMEOUT_1);
  463. value = DSI_TALLY_TA(0) | DSI_TALLY_LRX(0) | DSI_TALLY_HTX(0);
  464. tegra_dsi_writel(dsi, value, DSI_TO_TALLY);
  465. return 0;
  466. }
  467. static int tegra_output_dsi_check_mode(struct tegra_output *output,
  468. struct drm_display_mode *mode,
  469. enum drm_mode_status *status)
  470. {
  471. /*
  472. * FIXME: For now, always assume that the mode is okay.
  473. */
  474. *status = MODE_OK;
  475. return 0;
  476. }
  477. static const struct tegra_output_ops dsi_ops = {
  478. .enable = tegra_output_dsi_enable,
  479. .disable = tegra_output_dsi_disable,
  480. .setup_clock = tegra_output_dsi_setup_clock,
  481. .check_mode = tegra_output_dsi_check_mode,
  482. };
  483. static int tegra_dsi_pad_enable(struct tegra_dsi *dsi)
  484. {
  485. unsigned long value;
  486. value = DSI_PAD_CONTROL_VS1_PULLDN(0) | DSI_PAD_CONTROL_VS1_PDIO(0);
  487. tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_0);
  488. return 0;
  489. }
  490. static int tegra_dsi_pad_calibrate(struct tegra_dsi *dsi)
  491. {
  492. unsigned long value;
  493. tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_0);
  494. tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_1);
  495. tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_2);
  496. tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_3);
  497. tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_4);
  498. /* start calibration */
  499. tegra_dsi_pad_enable(dsi);
  500. value = DSI_PAD_SLEW_UP(0x7) | DSI_PAD_SLEW_DN(0x7) |
  501. DSI_PAD_LP_UP(0x1) | DSI_PAD_LP_DN(0x1) |
  502. DSI_PAD_OUT_CLK(0x0);
  503. tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_2);
  504. return tegra_mipi_calibrate(dsi->mipi);
  505. }
  506. static int tegra_dsi_init(struct host1x_client *client)
  507. {
  508. struct tegra_drm *tegra = dev_get_drvdata(client->parent);
  509. struct tegra_dsi *dsi = host1x_client_to_dsi(client);
  510. unsigned long value, i;
  511. int err;
  512. dsi->output.type = TEGRA_OUTPUT_DSI;
  513. dsi->output.dev = client->dev;
  514. dsi->output.ops = &dsi_ops;
  515. err = tegra_output_init(tegra->drm, &dsi->output);
  516. if (err < 0) {
  517. dev_err(client->dev, "output setup failed: %d\n", err);
  518. return err;
  519. }
  520. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  521. err = tegra_dsi_debugfs_init(dsi, tegra->drm->primary);
  522. if (err < 0)
  523. dev_err(dsi->dev, "debugfs setup failed: %d\n", err);
  524. }
  525. /*
  526. * enable high-speed mode, checksum generation, ECC generation and
  527. * disable raw mode
  528. */
  529. value = tegra_dsi_readl(dsi, DSI_HOST_CONTROL);
  530. value |= DSI_HOST_CONTROL_ECC | DSI_HOST_CONTROL_CS |
  531. DSI_HOST_CONTROL_HS;
  532. value &= ~DSI_HOST_CONTROL_RAW;
  533. tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
  534. tegra_dsi_writel(dsi, 0, DSI_SOL_DELAY);
  535. tegra_dsi_writel(dsi, 0, DSI_MAX_THRESHOLD);
  536. tegra_dsi_writel(dsi, 0, DSI_INIT_SEQ_CONTROL);
  537. for (i = 0; i < 8; i++) {
  538. tegra_dsi_writel(dsi, 0, DSI_INIT_SEQ_DATA_0 + i);
  539. tegra_dsi_writel(dsi, 0, DSI_INIT_SEQ_DATA_8 + i);
  540. }
  541. for (i = 0; i < 12; i++)
  542. tegra_dsi_writel(dsi, 0, DSI_PKT_SEQ_0_LO + i);
  543. tegra_dsi_writel(dsi, 0, DSI_DCS_CMDS);
  544. err = tegra_dsi_pad_calibrate(dsi);
  545. if (err < 0) {
  546. dev_err(dsi->dev, "MIPI calibration failed: %d\n", err);
  547. return err;
  548. }
  549. tegra_dsi_writel(dsi, DSI_POWER_CONTROL_ENABLE, DSI_POWER_CONTROL);
  550. usleep_range(300, 1000);
  551. return 0;
  552. }
  553. static int tegra_dsi_exit(struct host1x_client *client)
  554. {
  555. struct tegra_dsi *dsi = host1x_client_to_dsi(client);
  556. int err;
  557. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  558. err = tegra_dsi_debugfs_exit(dsi);
  559. if (err < 0)
  560. dev_err(dsi->dev, "debugfs cleanup failed: %d\n", err);
  561. }
  562. err = tegra_output_disable(&dsi->output);
  563. if (err < 0) {
  564. dev_err(client->dev, "output failed to disable: %d\n", err);
  565. return err;
  566. }
  567. err = tegra_output_exit(&dsi->output);
  568. if (err < 0) {
  569. dev_err(client->dev, "output cleanup failed: %d\n", err);
  570. return err;
  571. }
  572. return 0;
  573. }
  574. static const struct host1x_client_ops dsi_client_ops = {
  575. .init = tegra_dsi_init,
  576. .exit = tegra_dsi_exit,
  577. };
  578. static int tegra_dsi_setup_clocks(struct tegra_dsi *dsi)
  579. {
  580. struct clk *parent;
  581. int err;
  582. parent = clk_get_parent(dsi->clk);
  583. if (!parent)
  584. return -EINVAL;
  585. err = clk_set_parent(parent, dsi->clk_parent);
  586. if (err < 0)
  587. return err;
  588. return 0;
  589. }
  590. static void tegra_dsi_initialize(struct tegra_dsi *dsi)
  591. {
  592. unsigned int i;
  593. tegra_dsi_writel(dsi, 0, DSI_POWER_CONTROL);
  594. tegra_dsi_writel(dsi, 0, DSI_INT_ENABLE);
  595. tegra_dsi_writel(dsi, 0, DSI_INT_STATUS);
  596. tegra_dsi_writel(dsi, 0, DSI_INT_MASK);
  597. tegra_dsi_writel(dsi, 0, DSI_HOST_CONTROL);
  598. tegra_dsi_writel(dsi, 0, DSI_CONTROL);
  599. tegra_dsi_writel(dsi, 0, DSI_SOL_DELAY);
  600. tegra_dsi_writel(dsi, 0, DSI_MAX_THRESHOLD);
  601. tegra_dsi_writel(dsi, 0, DSI_INIT_SEQ_CONTROL);
  602. for (i = 0; i < 8; i++) {
  603. tegra_dsi_writel(dsi, 0, DSI_INIT_SEQ_DATA_0 + i);
  604. tegra_dsi_writel(dsi, 0, DSI_INIT_SEQ_DATA_8 + i);
  605. }
  606. for (i = 0; i < 12; i++)
  607. tegra_dsi_writel(dsi, 0, DSI_PKT_SEQ_0_LO + i);
  608. tegra_dsi_writel(dsi, 0, DSI_DCS_CMDS);
  609. for (i = 0; i < 4; i++)
  610. tegra_dsi_writel(dsi, 0, DSI_PKT_LEN_0_1 + i);
  611. tegra_dsi_writel(dsi, 0x00000000, DSI_PHY_TIMING_0);
  612. tegra_dsi_writel(dsi, 0x00000000, DSI_PHY_TIMING_1);
  613. tegra_dsi_writel(dsi, 0x000000ff, DSI_PHY_TIMING_2);
  614. tegra_dsi_writel(dsi, 0x00000000, DSI_BTA_TIMING);
  615. tegra_dsi_writel(dsi, 0, DSI_TIMEOUT_0);
  616. tegra_dsi_writel(dsi, 0, DSI_TIMEOUT_1);
  617. tegra_dsi_writel(dsi, 0, DSI_TO_TALLY);
  618. tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_0);
  619. tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_CD);
  620. tegra_dsi_writel(dsi, 0, DSI_PAD_CD_STATUS);
  621. tegra_dsi_writel(dsi, 0, DSI_VIDEO_MODE_CONTROL);
  622. tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_1);
  623. tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_2);
  624. tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_3);
  625. tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_4);
  626. tegra_dsi_writel(dsi, 0, DSI_GANGED_MODE_CONTROL);
  627. tegra_dsi_writel(dsi, 0, DSI_GANGED_MODE_START);
  628. tegra_dsi_writel(dsi, 0, DSI_GANGED_MODE_SIZE);
  629. }
  630. static int tegra_dsi_host_attach(struct mipi_dsi_host *host,
  631. struct mipi_dsi_device *device)
  632. {
  633. struct tegra_dsi *dsi = host_to_tegra(host);
  634. struct tegra_output *output = &dsi->output;
  635. dsi->format = device->format;
  636. dsi->lanes = device->lanes;
  637. output->panel = of_drm_find_panel(device->dev.of_node);
  638. if (output->panel) {
  639. if (output->connector.dev)
  640. drm_helper_hpd_irq_event(output->connector.dev);
  641. }
  642. return 0;
  643. }
  644. static int tegra_dsi_host_detach(struct mipi_dsi_host *host,
  645. struct mipi_dsi_device *device)
  646. {
  647. struct tegra_dsi *dsi = host_to_tegra(host);
  648. struct tegra_output *output = &dsi->output;
  649. if (output->panel && &device->dev == output->panel->dev) {
  650. if (output->connector.dev)
  651. drm_helper_hpd_irq_event(output->connector.dev);
  652. output->panel = NULL;
  653. }
  654. return 0;
  655. }
  656. static const struct mipi_dsi_host_ops tegra_dsi_host_ops = {
  657. .attach = tegra_dsi_host_attach,
  658. .detach = tegra_dsi_host_detach,
  659. };
  660. static int tegra_dsi_probe(struct platform_device *pdev)
  661. {
  662. struct tegra_dsi *dsi;
  663. struct resource *regs;
  664. int err;
  665. dsi = devm_kzalloc(&pdev->dev, sizeof(*dsi), GFP_KERNEL);
  666. if (!dsi)
  667. return -ENOMEM;
  668. dsi->output.dev = dsi->dev = &pdev->dev;
  669. err = tegra_output_probe(&dsi->output);
  670. if (err < 0)
  671. return err;
  672. /*
  673. * Assume these values by default. When a DSI peripheral driver
  674. * attaches to the DSI host, the parameters will be taken from
  675. * the attached device.
  676. */
  677. dsi->format = MIPI_DSI_FMT_RGB888;
  678. dsi->lanes = 4;
  679. dsi->rst = devm_reset_control_get(&pdev->dev, "dsi");
  680. if (IS_ERR(dsi->rst))
  681. return PTR_ERR(dsi->rst);
  682. dsi->clk = devm_clk_get(&pdev->dev, NULL);
  683. if (IS_ERR(dsi->clk)) {
  684. dev_err(&pdev->dev, "cannot get DSI clock\n");
  685. return PTR_ERR(dsi->clk);
  686. }
  687. err = clk_prepare_enable(dsi->clk);
  688. if (err < 0) {
  689. dev_err(&pdev->dev, "cannot enable DSI clock\n");
  690. return err;
  691. }
  692. dsi->clk_lp = devm_clk_get(&pdev->dev, "lp");
  693. if (IS_ERR(dsi->clk_lp)) {
  694. dev_err(&pdev->dev, "cannot get low-power clock\n");
  695. return PTR_ERR(dsi->clk_lp);
  696. }
  697. err = clk_prepare_enable(dsi->clk_lp);
  698. if (err < 0) {
  699. dev_err(&pdev->dev, "cannot enable low-power clock\n");
  700. return err;
  701. }
  702. dsi->clk_parent = devm_clk_get(&pdev->dev, "parent");
  703. if (IS_ERR(dsi->clk_parent)) {
  704. dev_err(&pdev->dev, "cannot get parent clock\n");
  705. return PTR_ERR(dsi->clk_parent);
  706. }
  707. err = clk_prepare_enable(dsi->clk_parent);
  708. if (err < 0) {
  709. dev_err(&pdev->dev, "cannot enable parent clock\n");
  710. return err;
  711. }
  712. err = tegra_dsi_setup_clocks(dsi);
  713. if (err < 0) {
  714. dev_err(&pdev->dev, "cannot setup clocks\n");
  715. return err;
  716. }
  717. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  718. dsi->regs = devm_ioremap_resource(&pdev->dev, regs);
  719. if (IS_ERR(dsi->regs))
  720. return PTR_ERR(dsi->regs);
  721. tegra_dsi_initialize(dsi);
  722. dsi->mipi = tegra_mipi_request(&pdev->dev);
  723. if (IS_ERR(dsi->mipi))
  724. return PTR_ERR(dsi->mipi);
  725. dsi->host.ops = &tegra_dsi_host_ops;
  726. dsi->host.dev = &pdev->dev;
  727. err = mipi_dsi_host_register(&dsi->host);
  728. if (err < 0) {
  729. dev_err(&pdev->dev, "failed to register DSI host: %d\n", err);
  730. return err;
  731. }
  732. INIT_LIST_HEAD(&dsi->client.list);
  733. dsi->client.ops = &dsi_client_ops;
  734. dsi->client.dev = &pdev->dev;
  735. err = host1x_client_register(&dsi->client);
  736. if (err < 0) {
  737. dev_err(&pdev->dev, "failed to register host1x client: %d\n",
  738. err);
  739. return err;
  740. }
  741. platform_set_drvdata(pdev, dsi);
  742. return 0;
  743. }
  744. static int tegra_dsi_remove(struct platform_device *pdev)
  745. {
  746. struct tegra_dsi *dsi = platform_get_drvdata(pdev);
  747. int err;
  748. err = host1x_client_unregister(&dsi->client);
  749. if (err < 0) {
  750. dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
  751. err);
  752. return err;
  753. }
  754. mipi_dsi_host_unregister(&dsi->host);
  755. tegra_mipi_free(dsi->mipi);
  756. clk_disable_unprepare(dsi->clk_parent);
  757. clk_disable_unprepare(dsi->clk_lp);
  758. clk_disable_unprepare(dsi->clk);
  759. err = tegra_output_remove(&dsi->output);
  760. if (err < 0) {
  761. dev_err(&pdev->dev, "failed to remove output: %d\n", err);
  762. return err;
  763. }
  764. return 0;
  765. }
  766. static const struct of_device_id tegra_dsi_of_match[] = {
  767. { .compatible = "nvidia,tegra114-dsi", },
  768. { },
  769. };
  770. struct platform_driver tegra_dsi_driver = {
  771. .driver = {
  772. .name = "tegra-dsi",
  773. .of_match_table = tegra_dsi_of_match,
  774. },
  775. .probe = tegra_dsi_probe,
  776. .remove = tegra_dsi_remove,
  777. };