dsi.c 25 KB

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