dsi.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708
  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/of_platform.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/reset.h>
  17. #include <linux/regulator/consumer.h>
  18. #include <drm/drm_atomic_helper.h>
  19. #include <drm/drm_mipi_dsi.h>
  20. #include <drm/drm_panel.h>
  21. #include <video/mipi_display.h>
  22. #include "dc.h"
  23. #include "drm.h"
  24. #include "dsi.h"
  25. #include "mipi-phy.h"
  26. struct tegra_dsi_state {
  27. struct drm_connector_state base;
  28. struct mipi_dphy_timing timing;
  29. unsigned long period;
  30. unsigned int vrefresh;
  31. unsigned int lanes;
  32. unsigned long pclk;
  33. unsigned long bclk;
  34. enum tegra_dsi_format format;
  35. unsigned int mul;
  36. unsigned int div;
  37. };
  38. static inline struct tegra_dsi_state *
  39. to_dsi_state(struct drm_connector_state *state)
  40. {
  41. return container_of(state, struct tegra_dsi_state, base);
  42. }
  43. struct tegra_dsi {
  44. struct host1x_client client;
  45. struct tegra_output output;
  46. struct device *dev;
  47. void __iomem *regs;
  48. struct reset_control *rst;
  49. struct clk *clk_parent;
  50. struct clk *clk_lp;
  51. struct clk *clk;
  52. struct drm_info_list *debugfs_files;
  53. struct drm_minor *minor;
  54. struct dentry *debugfs;
  55. unsigned long flags;
  56. enum mipi_dsi_pixel_format format;
  57. unsigned int lanes;
  58. struct tegra_mipi_device *mipi;
  59. struct mipi_dsi_host host;
  60. struct regulator *vdd;
  61. unsigned int video_fifo_depth;
  62. unsigned int host_fifo_depth;
  63. /* for ganged-mode support */
  64. struct tegra_dsi *master;
  65. struct tegra_dsi *slave;
  66. };
  67. static inline struct tegra_dsi *
  68. host1x_client_to_dsi(struct host1x_client *client)
  69. {
  70. return container_of(client, struct tegra_dsi, client);
  71. }
  72. static inline struct tegra_dsi *host_to_tegra(struct mipi_dsi_host *host)
  73. {
  74. return container_of(host, struct tegra_dsi, host);
  75. }
  76. static inline struct tegra_dsi *to_dsi(struct tegra_output *output)
  77. {
  78. return container_of(output, struct tegra_dsi, output);
  79. }
  80. static struct tegra_dsi_state *tegra_dsi_get_state(struct tegra_dsi *dsi)
  81. {
  82. return to_dsi_state(dsi->output.connector.state);
  83. }
  84. static inline u32 tegra_dsi_readl(struct tegra_dsi *dsi, unsigned long reg)
  85. {
  86. return readl(dsi->regs + (reg << 2));
  87. }
  88. static inline void tegra_dsi_writel(struct tegra_dsi *dsi, u32 value,
  89. unsigned long reg)
  90. {
  91. writel(value, dsi->regs + (reg << 2));
  92. }
  93. static int tegra_dsi_show_regs(struct seq_file *s, void *data)
  94. {
  95. struct drm_info_node *node = s->private;
  96. struct tegra_dsi *dsi = node->info_ent->data;
  97. struct drm_crtc *crtc = dsi->output.encoder.crtc;
  98. struct drm_device *drm = node->minor->dev;
  99. int err = 0;
  100. drm_modeset_lock_all(drm);
  101. if (!crtc || !crtc->state->active) {
  102. err = -EBUSY;
  103. goto unlock;
  104. }
  105. #define DUMP_REG(name) \
  106. seq_printf(s, "%-32s %#05x %08x\n", #name, name, \
  107. tegra_dsi_readl(dsi, name))
  108. DUMP_REG(DSI_INCR_SYNCPT);
  109. DUMP_REG(DSI_INCR_SYNCPT_CONTROL);
  110. DUMP_REG(DSI_INCR_SYNCPT_ERROR);
  111. DUMP_REG(DSI_CTXSW);
  112. DUMP_REG(DSI_RD_DATA);
  113. DUMP_REG(DSI_WR_DATA);
  114. DUMP_REG(DSI_POWER_CONTROL);
  115. DUMP_REG(DSI_INT_ENABLE);
  116. DUMP_REG(DSI_INT_STATUS);
  117. DUMP_REG(DSI_INT_MASK);
  118. DUMP_REG(DSI_HOST_CONTROL);
  119. DUMP_REG(DSI_CONTROL);
  120. DUMP_REG(DSI_SOL_DELAY);
  121. DUMP_REG(DSI_MAX_THRESHOLD);
  122. DUMP_REG(DSI_TRIGGER);
  123. DUMP_REG(DSI_TX_CRC);
  124. DUMP_REG(DSI_STATUS);
  125. DUMP_REG(DSI_INIT_SEQ_CONTROL);
  126. DUMP_REG(DSI_INIT_SEQ_DATA_0);
  127. DUMP_REG(DSI_INIT_SEQ_DATA_1);
  128. DUMP_REG(DSI_INIT_SEQ_DATA_2);
  129. DUMP_REG(DSI_INIT_SEQ_DATA_3);
  130. DUMP_REG(DSI_INIT_SEQ_DATA_4);
  131. DUMP_REG(DSI_INIT_SEQ_DATA_5);
  132. DUMP_REG(DSI_INIT_SEQ_DATA_6);
  133. DUMP_REG(DSI_INIT_SEQ_DATA_7);
  134. DUMP_REG(DSI_PKT_SEQ_0_LO);
  135. DUMP_REG(DSI_PKT_SEQ_0_HI);
  136. DUMP_REG(DSI_PKT_SEQ_1_LO);
  137. DUMP_REG(DSI_PKT_SEQ_1_HI);
  138. DUMP_REG(DSI_PKT_SEQ_2_LO);
  139. DUMP_REG(DSI_PKT_SEQ_2_HI);
  140. DUMP_REG(DSI_PKT_SEQ_3_LO);
  141. DUMP_REG(DSI_PKT_SEQ_3_HI);
  142. DUMP_REG(DSI_PKT_SEQ_4_LO);
  143. DUMP_REG(DSI_PKT_SEQ_4_HI);
  144. DUMP_REG(DSI_PKT_SEQ_5_LO);
  145. DUMP_REG(DSI_PKT_SEQ_5_HI);
  146. DUMP_REG(DSI_DCS_CMDS);
  147. DUMP_REG(DSI_PKT_LEN_0_1);
  148. DUMP_REG(DSI_PKT_LEN_2_3);
  149. DUMP_REG(DSI_PKT_LEN_4_5);
  150. DUMP_REG(DSI_PKT_LEN_6_7);
  151. DUMP_REG(DSI_PHY_TIMING_0);
  152. DUMP_REG(DSI_PHY_TIMING_1);
  153. DUMP_REG(DSI_PHY_TIMING_2);
  154. DUMP_REG(DSI_BTA_TIMING);
  155. DUMP_REG(DSI_TIMEOUT_0);
  156. DUMP_REG(DSI_TIMEOUT_1);
  157. DUMP_REG(DSI_TO_TALLY);
  158. DUMP_REG(DSI_PAD_CONTROL_0);
  159. DUMP_REG(DSI_PAD_CONTROL_CD);
  160. DUMP_REG(DSI_PAD_CD_STATUS);
  161. DUMP_REG(DSI_VIDEO_MODE_CONTROL);
  162. DUMP_REG(DSI_PAD_CONTROL_1);
  163. DUMP_REG(DSI_PAD_CONTROL_2);
  164. DUMP_REG(DSI_PAD_CONTROL_3);
  165. DUMP_REG(DSI_PAD_CONTROL_4);
  166. DUMP_REG(DSI_GANGED_MODE_CONTROL);
  167. DUMP_REG(DSI_GANGED_MODE_START);
  168. DUMP_REG(DSI_GANGED_MODE_SIZE);
  169. DUMP_REG(DSI_RAW_DATA_BYTE_COUNT);
  170. DUMP_REG(DSI_ULTRA_LOW_POWER_CONTROL);
  171. DUMP_REG(DSI_INIT_SEQ_DATA_8);
  172. DUMP_REG(DSI_INIT_SEQ_DATA_9);
  173. DUMP_REG(DSI_INIT_SEQ_DATA_10);
  174. DUMP_REG(DSI_INIT_SEQ_DATA_11);
  175. DUMP_REG(DSI_INIT_SEQ_DATA_12);
  176. DUMP_REG(DSI_INIT_SEQ_DATA_13);
  177. DUMP_REG(DSI_INIT_SEQ_DATA_14);
  178. DUMP_REG(DSI_INIT_SEQ_DATA_15);
  179. #undef DUMP_REG
  180. unlock:
  181. drm_modeset_unlock_all(drm);
  182. return err;
  183. }
  184. static struct drm_info_list debugfs_files[] = {
  185. { "regs", tegra_dsi_show_regs, 0, NULL },
  186. };
  187. static int tegra_dsi_debugfs_init(struct tegra_dsi *dsi,
  188. struct drm_minor *minor)
  189. {
  190. const char *name = dev_name(dsi->dev);
  191. unsigned int i;
  192. int err;
  193. dsi->debugfs = debugfs_create_dir(name, minor->debugfs_root);
  194. if (!dsi->debugfs)
  195. return -ENOMEM;
  196. dsi->debugfs_files = kmemdup(debugfs_files, sizeof(debugfs_files),
  197. GFP_KERNEL);
  198. if (!dsi->debugfs_files) {
  199. err = -ENOMEM;
  200. goto remove;
  201. }
  202. for (i = 0; i < ARRAY_SIZE(debugfs_files); i++)
  203. dsi->debugfs_files[i].data = dsi;
  204. err = drm_debugfs_create_files(dsi->debugfs_files,
  205. ARRAY_SIZE(debugfs_files),
  206. dsi->debugfs, minor);
  207. if (err < 0)
  208. goto free;
  209. dsi->minor = minor;
  210. return 0;
  211. free:
  212. kfree(dsi->debugfs_files);
  213. dsi->debugfs_files = NULL;
  214. remove:
  215. debugfs_remove(dsi->debugfs);
  216. dsi->debugfs = NULL;
  217. return err;
  218. }
  219. static void tegra_dsi_debugfs_exit(struct tegra_dsi *dsi)
  220. {
  221. drm_debugfs_remove_files(dsi->debugfs_files, ARRAY_SIZE(debugfs_files),
  222. dsi->minor);
  223. dsi->minor = NULL;
  224. kfree(dsi->debugfs_files);
  225. dsi->debugfs_files = NULL;
  226. debugfs_remove(dsi->debugfs);
  227. dsi->debugfs = NULL;
  228. }
  229. #define PKT_ID0(id) ((((id) & 0x3f) << 3) | (1 << 9))
  230. #define PKT_LEN0(len) (((len) & 0x07) << 0)
  231. #define PKT_ID1(id) ((((id) & 0x3f) << 13) | (1 << 19))
  232. #define PKT_LEN1(len) (((len) & 0x07) << 10)
  233. #define PKT_ID2(id) ((((id) & 0x3f) << 23) | (1 << 29))
  234. #define PKT_LEN2(len) (((len) & 0x07) << 20)
  235. #define PKT_LP (1 << 30)
  236. #define NUM_PKT_SEQ 12
  237. /*
  238. * non-burst mode with sync pulses
  239. */
  240. static const u32 pkt_seq_video_non_burst_sync_pulses[NUM_PKT_SEQ] = {
  241. [ 0] = PKT_ID0(MIPI_DSI_V_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. PKT_LP,
  245. [ 1] = 0,
  246. [ 2] = PKT_ID0(MIPI_DSI_V_SYNC_END) | PKT_LEN0(0) |
  247. PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
  248. PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
  249. PKT_LP,
  250. [ 3] = 0,
  251. [ 4] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
  252. PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
  253. PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
  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(1) |
  258. PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0),
  259. [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
  260. PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
  261. PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
  262. [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
  263. PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
  264. PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0) |
  265. PKT_LP,
  266. [ 9] = 0,
  267. [10] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
  268. PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(1) |
  269. PKT_ID2(MIPI_DSI_H_SYNC_END) | PKT_LEN2(0),
  270. [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(2) |
  271. PKT_ID1(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN1(3) |
  272. PKT_ID2(MIPI_DSI_BLANKING_PACKET) | PKT_LEN2(4),
  273. };
  274. /*
  275. * non-burst mode with sync events
  276. */
  277. static const u32 pkt_seq_video_non_burst_sync_events[NUM_PKT_SEQ] = {
  278. [ 0] = PKT_ID0(MIPI_DSI_V_SYNC_START) | PKT_LEN0(0) |
  279. PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
  280. PKT_LP,
  281. [ 1] = 0,
  282. [ 2] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
  283. PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
  284. PKT_LP,
  285. [ 3] = 0,
  286. [ 4] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
  287. PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
  288. PKT_LP,
  289. [ 5] = 0,
  290. [ 6] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
  291. PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(2) |
  292. PKT_ID2(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN2(3),
  293. [ 7] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(4),
  294. [ 8] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
  295. PKT_ID1(MIPI_DSI_END_OF_TRANSMISSION) | PKT_LEN1(7) |
  296. PKT_LP,
  297. [ 9] = 0,
  298. [10] = PKT_ID0(MIPI_DSI_H_SYNC_START) | PKT_LEN0(0) |
  299. PKT_ID1(MIPI_DSI_BLANKING_PACKET) | PKT_LEN1(2) |
  300. PKT_ID2(MIPI_DSI_PACKED_PIXEL_STREAM_24) | PKT_LEN2(3),
  301. [11] = PKT_ID0(MIPI_DSI_BLANKING_PACKET) | PKT_LEN0(4),
  302. };
  303. static const u32 pkt_seq_command_mode[NUM_PKT_SEQ] = {
  304. [ 0] = 0,
  305. [ 1] = 0,
  306. [ 2] = 0,
  307. [ 3] = 0,
  308. [ 4] = 0,
  309. [ 5] = 0,
  310. [ 6] = PKT_ID0(MIPI_DSI_DCS_LONG_WRITE) | PKT_LEN0(3) | PKT_LP,
  311. [ 7] = 0,
  312. [ 8] = 0,
  313. [ 9] = 0,
  314. [10] = PKT_ID0(MIPI_DSI_DCS_LONG_WRITE) | PKT_LEN0(5) | PKT_LP,
  315. [11] = 0,
  316. };
  317. static void tegra_dsi_set_phy_timing(struct tegra_dsi *dsi,
  318. unsigned long period,
  319. const struct mipi_dphy_timing *timing)
  320. {
  321. u32 value;
  322. value = DSI_TIMING_FIELD(timing->hsexit, period, 1) << 24 |
  323. DSI_TIMING_FIELD(timing->hstrail, period, 0) << 16 |
  324. DSI_TIMING_FIELD(timing->hszero, period, 3) << 8 |
  325. DSI_TIMING_FIELD(timing->hsprepare, period, 1);
  326. tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_0);
  327. value = DSI_TIMING_FIELD(timing->clktrail, period, 1) << 24 |
  328. DSI_TIMING_FIELD(timing->clkpost, period, 1) << 16 |
  329. DSI_TIMING_FIELD(timing->clkzero, period, 1) << 8 |
  330. DSI_TIMING_FIELD(timing->lpx, period, 1);
  331. tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_1);
  332. value = DSI_TIMING_FIELD(timing->clkprepare, period, 1) << 16 |
  333. DSI_TIMING_FIELD(timing->clkpre, period, 1) << 8 |
  334. DSI_TIMING_FIELD(0xff * period, period, 0) << 0;
  335. tegra_dsi_writel(dsi, value, DSI_PHY_TIMING_2);
  336. value = DSI_TIMING_FIELD(timing->taget, period, 1) << 16 |
  337. DSI_TIMING_FIELD(timing->tasure, period, 1) << 8 |
  338. DSI_TIMING_FIELD(timing->tago, period, 1);
  339. tegra_dsi_writel(dsi, value, DSI_BTA_TIMING);
  340. if (dsi->slave)
  341. tegra_dsi_set_phy_timing(dsi->slave, period, timing);
  342. }
  343. static int tegra_dsi_get_muldiv(enum mipi_dsi_pixel_format format,
  344. unsigned int *mulp, unsigned int *divp)
  345. {
  346. switch (format) {
  347. case MIPI_DSI_FMT_RGB666_PACKED:
  348. case MIPI_DSI_FMT_RGB888:
  349. *mulp = 3;
  350. *divp = 1;
  351. break;
  352. case MIPI_DSI_FMT_RGB565:
  353. *mulp = 2;
  354. *divp = 1;
  355. break;
  356. case MIPI_DSI_FMT_RGB666:
  357. *mulp = 9;
  358. *divp = 4;
  359. break;
  360. default:
  361. return -EINVAL;
  362. }
  363. return 0;
  364. }
  365. static int tegra_dsi_get_format(enum mipi_dsi_pixel_format format,
  366. enum tegra_dsi_format *fmt)
  367. {
  368. switch (format) {
  369. case MIPI_DSI_FMT_RGB888:
  370. *fmt = TEGRA_DSI_FORMAT_24P;
  371. break;
  372. case MIPI_DSI_FMT_RGB666:
  373. *fmt = TEGRA_DSI_FORMAT_18NP;
  374. break;
  375. case MIPI_DSI_FMT_RGB666_PACKED:
  376. *fmt = TEGRA_DSI_FORMAT_18P;
  377. break;
  378. case MIPI_DSI_FMT_RGB565:
  379. *fmt = TEGRA_DSI_FORMAT_16P;
  380. break;
  381. default:
  382. return -EINVAL;
  383. }
  384. return 0;
  385. }
  386. static void tegra_dsi_ganged_enable(struct tegra_dsi *dsi, unsigned int start,
  387. unsigned int size)
  388. {
  389. u32 value;
  390. tegra_dsi_writel(dsi, start, DSI_GANGED_MODE_START);
  391. tegra_dsi_writel(dsi, size << 16 | size, DSI_GANGED_MODE_SIZE);
  392. value = DSI_GANGED_MODE_CONTROL_ENABLE;
  393. tegra_dsi_writel(dsi, value, DSI_GANGED_MODE_CONTROL);
  394. }
  395. static void tegra_dsi_enable(struct tegra_dsi *dsi)
  396. {
  397. u32 value;
  398. value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
  399. value |= DSI_POWER_CONTROL_ENABLE;
  400. tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
  401. if (dsi->slave)
  402. tegra_dsi_enable(dsi->slave);
  403. }
  404. static unsigned int tegra_dsi_get_lanes(struct tegra_dsi *dsi)
  405. {
  406. if (dsi->master)
  407. return dsi->master->lanes + dsi->lanes;
  408. if (dsi->slave)
  409. return dsi->lanes + dsi->slave->lanes;
  410. return dsi->lanes;
  411. }
  412. static void tegra_dsi_configure(struct tegra_dsi *dsi, unsigned int pipe,
  413. const struct drm_display_mode *mode)
  414. {
  415. unsigned int hact, hsw, hbp, hfp, i, mul, div;
  416. struct tegra_dsi_state *state;
  417. const u32 *pkt_seq;
  418. u32 value;
  419. /* XXX: pass in state into this function? */
  420. if (dsi->master)
  421. state = tegra_dsi_get_state(dsi->master);
  422. else
  423. state = tegra_dsi_get_state(dsi);
  424. mul = state->mul;
  425. div = state->div;
  426. if (dsi->flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) {
  427. DRM_DEBUG_KMS("Non-burst video mode with sync pulses\n");
  428. pkt_seq = pkt_seq_video_non_burst_sync_pulses;
  429. } else if (dsi->flags & MIPI_DSI_MODE_VIDEO) {
  430. DRM_DEBUG_KMS("Non-burst video mode with sync events\n");
  431. pkt_seq = pkt_seq_video_non_burst_sync_events;
  432. } else {
  433. DRM_DEBUG_KMS("Command mode\n");
  434. pkt_seq = pkt_seq_command_mode;
  435. }
  436. value = DSI_CONTROL_CHANNEL(0) |
  437. DSI_CONTROL_FORMAT(state->format) |
  438. DSI_CONTROL_LANES(dsi->lanes - 1) |
  439. DSI_CONTROL_SOURCE(pipe);
  440. tegra_dsi_writel(dsi, value, DSI_CONTROL);
  441. tegra_dsi_writel(dsi, dsi->video_fifo_depth, DSI_MAX_THRESHOLD);
  442. value = DSI_HOST_CONTROL_HS;
  443. tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
  444. value = tegra_dsi_readl(dsi, DSI_CONTROL);
  445. if (dsi->flags & MIPI_DSI_CLOCK_NON_CONTINUOUS)
  446. value |= DSI_CONTROL_HS_CLK_CTRL;
  447. value &= ~DSI_CONTROL_TX_TRIG(3);
  448. /* enable DCS commands for command mode */
  449. if (dsi->flags & MIPI_DSI_MODE_VIDEO)
  450. value &= ~DSI_CONTROL_DCS_ENABLE;
  451. else
  452. value |= DSI_CONTROL_DCS_ENABLE;
  453. value |= DSI_CONTROL_VIDEO_ENABLE;
  454. value &= ~DSI_CONTROL_HOST_ENABLE;
  455. tegra_dsi_writel(dsi, value, DSI_CONTROL);
  456. for (i = 0; i < NUM_PKT_SEQ; i++)
  457. tegra_dsi_writel(dsi, pkt_seq[i], DSI_PKT_SEQ_0_LO + i);
  458. if (dsi->flags & MIPI_DSI_MODE_VIDEO) {
  459. /* horizontal active pixels */
  460. hact = mode->hdisplay * mul / div;
  461. /* horizontal sync width */
  462. hsw = (mode->hsync_end - mode->hsync_start) * mul / div;
  463. /* horizontal back porch */
  464. hbp = (mode->htotal - mode->hsync_end) * mul / div;
  465. if ((dsi->flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE) == 0)
  466. hbp += hsw;
  467. /* horizontal front porch */
  468. hfp = (mode->hsync_start - mode->hdisplay) * mul / div;
  469. /* subtract packet overhead */
  470. hsw -= 10;
  471. hbp -= 14;
  472. hfp -= 8;
  473. tegra_dsi_writel(dsi, hsw << 16 | 0, DSI_PKT_LEN_0_1);
  474. tegra_dsi_writel(dsi, hact << 16 | hbp, DSI_PKT_LEN_2_3);
  475. tegra_dsi_writel(dsi, hfp, DSI_PKT_LEN_4_5);
  476. tegra_dsi_writel(dsi, 0x0f0f << 16, DSI_PKT_LEN_6_7);
  477. /* set SOL delay (for non-burst mode only) */
  478. tegra_dsi_writel(dsi, 8 * mul / div, DSI_SOL_DELAY);
  479. /* TODO: implement ganged mode */
  480. } else {
  481. u16 bytes;
  482. if (dsi->master || dsi->slave) {
  483. /*
  484. * For ganged mode, assume symmetric left-right mode.
  485. */
  486. bytes = 1 + (mode->hdisplay / 2) * mul / div;
  487. } else {
  488. /* 1 byte (DCS command) + pixel data */
  489. bytes = 1 + mode->hdisplay * mul / div;
  490. }
  491. tegra_dsi_writel(dsi, 0, DSI_PKT_LEN_0_1);
  492. tegra_dsi_writel(dsi, bytes << 16, DSI_PKT_LEN_2_3);
  493. tegra_dsi_writel(dsi, bytes << 16, DSI_PKT_LEN_4_5);
  494. tegra_dsi_writel(dsi, 0, DSI_PKT_LEN_6_7);
  495. value = MIPI_DCS_WRITE_MEMORY_START << 8 |
  496. MIPI_DCS_WRITE_MEMORY_CONTINUE;
  497. tegra_dsi_writel(dsi, value, DSI_DCS_CMDS);
  498. /* set SOL delay */
  499. if (dsi->master || dsi->slave) {
  500. unsigned long delay, bclk, bclk_ganged;
  501. unsigned int lanes = state->lanes;
  502. /* SOL to valid, valid to FIFO and FIFO write delay */
  503. delay = 4 + 4 + 2;
  504. delay = DIV_ROUND_UP(delay * mul, div * lanes);
  505. /* FIFO read delay */
  506. delay = delay + 6;
  507. bclk = DIV_ROUND_UP(mode->htotal * mul, div * lanes);
  508. bclk_ganged = DIV_ROUND_UP(bclk * lanes / 2, lanes);
  509. value = bclk - bclk_ganged + delay + 20;
  510. } else {
  511. /* TODO: revisit for non-ganged mode */
  512. value = 8 * mul / div;
  513. }
  514. tegra_dsi_writel(dsi, value, DSI_SOL_DELAY);
  515. }
  516. if (dsi->slave) {
  517. tegra_dsi_configure(dsi->slave, pipe, mode);
  518. /*
  519. * TODO: Support modes other than symmetrical left-right
  520. * split.
  521. */
  522. tegra_dsi_ganged_enable(dsi, 0, mode->hdisplay / 2);
  523. tegra_dsi_ganged_enable(dsi->slave, mode->hdisplay / 2,
  524. mode->hdisplay / 2);
  525. }
  526. }
  527. static int tegra_dsi_wait_idle(struct tegra_dsi *dsi, unsigned long timeout)
  528. {
  529. u32 value;
  530. timeout = jiffies + msecs_to_jiffies(timeout);
  531. while (time_before(jiffies, timeout)) {
  532. value = tegra_dsi_readl(dsi, DSI_STATUS);
  533. if (value & DSI_STATUS_IDLE)
  534. return 0;
  535. usleep_range(1000, 2000);
  536. }
  537. return -ETIMEDOUT;
  538. }
  539. static void tegra_dsi_video_disable(struct tegra_dsi *dsi)
  540. {
  541. u32 value;
  542. value = tegra_dsi_readl(dsi, DSI_CONTROL);
  543. value &= ~DSI_CONTROL_VIDEO_ENABLE;
  544. tegra_dsi_writel(dsi, value, DSI_CONTROL);
  545. if (dsi->slave)
  546. tegra_dsi_video_disable(dsi->slave);
  547. }
  548. static void tegra_dsi_ganged_disable(struct tegra_dsi *dsi)
  549. {
  550. tegra_dsi_writel(dsi, 0, DSI_GANGED_MODE_START);
  551. tegra_dsi_writel(dsi, 0, DSI_GANGED_MODE_SIZE);
  552. tegra_dsi_writel(dsi, 0, DSI_GANGED_MODE_CONTROL);
  553. }
  554. static int tegra_dsi_pad_enable(struct tegra_dsi *dsi)
  555. {
  556. u32 value;
  557. value = DSI_PAD_CONTROL_VS1_PULLDN(0) | DSI_PAD_CONTROL_VS1_PDIO(0);
  558. tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_0);
  559. return 0;
  560. }
  561. static int tegra_dsi_pad_calibrate(struct tegra_dsi *dsi)
  562. {
  563. u32 value;
  564. /*
  565. * XXX Is this still needed? The module reset is deasserted right
  566. * before this function is called.
  567. */
  568. tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_0);
  569. tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_1);
  570. tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_2);
  571. tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_3);
  572. tegra_dsi_writel(dsi, 0, DSI_PAD_CONTROL_4);
  573. /* start calibration */
  574. tegra_dsi_pad_enable(dsi);
  575. value = DSI_PAD_SLEW_UP(0x7) | DSI_PAD_SLEW_DN(0x7) |
  576. DSI_PAD_LP_UP(0x1) | DSI_PAD_LP_DN(0x1) |
  577. DSI_PAD_OUT_CLK(0x0);
  578. tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_2);
  579. value = DSI_PAD_PREEMP_PD_CLK(0x3) | DSI_PAD_PREEMP_PU_CLK(0x3) |
  580. DSI_PAD_PREEMP_PD(0x03) | DSI_PAD_PREEMP_PU(0x3);
  581. tegra_dsi_writel(dsi, value, DSI_PAD_CONTROL_3);
  582. return tegra_mipi_calibrate(dsi->mipi);
  583. }
  584. static void tegra_dsi_set_timeout(struct tegra_dsi *dsi, unsigned long bclk,
  585. unsigned int vrefresh)
  586. {
  587. unsigned int timeout;
  588. u32 value;
  589. /* one frame high-speed transmission timeout */
  590. timeout = (bclk / vrefresh) / 512;
  591. value = DSI_TIMEOUT_LRX(0x2000) | DSI_TIMEOUT_HTX(timeout);
  592. tegra_dsi_writel(dsi, value, DSI_TIMEOUT_0);
  593. /* 2 ms peripheral timeout for panel */
  594. timeout = 2 * bclk / 512 * 1000;
  595. value = DSI_TIMEOUT_PR(timeout) | DSI_TIMEOUT_TA(0x2000);
  596. tegra_dsi_writel(dsi, value, DSI_TIMEOUT_1);
  597. value = DSI_TALLY_TA(0) | DSI_TALLY_LRX(0) | DSI_TALLY_HTX(0);
  598. tegra_dsi_writel(dsi, value, DSI_TO_TALLY);
  599. if (dsi->slave)
  600. tegra_dsi_set_timeout(dsi->slave, bclk, vrefresh);
  601. }
  602. static void tegra_dsi_disable(struct tegra_dsi *dsi)
  603. {
  604. u32 value;
  605. if (dsi->slave) {
  606. tegra_dsi_ganged_disable(dsi->slave);
  607. tegra_dsi_ganged_disable(dsi);
  608. }
  609. value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
  610. value &= ~DSI_POWER_CONTROL_ENABLE;
  611. tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
  612. if (dsi->slave)
  613. tegra_dsi_disable(dsi->slave);
  614. usleep_range(5000, 10000);
  615. }
  616. static void tegra_dsi_soft_reset(struct tegra_dsi *dsi)
  617. {
  618. u32 value;
  619. value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
  620. value &= ~DSI_POWER_CONTROL_ENABLE;
  621. tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
  622. usleep_range(300, 1000);
  623. value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
  624. value |= DSI_POWER_CONTROL_ENABLE;
  625. tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
  626. usleep_range(300, 1000);
  627. value = tegra_dsi_readl(dsi, DSI_TRIGGER);
  628. if (value)
  629. tegra_dsi_writel(dsi, 0, DSI_TRIGGER);
  630. if (dsi->slave)
  631. tegra_dsi_soft_reset(dsi->slave);
  632. }
  633. static void tegra_dsi_connector_reset(struct drm_connector *connector)
  634. {
  635. struct tegra_dsi_state *state = kzalloc(sizeof(*state), GFP_KERNEL);
  636. if (!state)
  637. return;
  638. if (connector->state) {
  639. __drm_atomic_helper_connector_destroy_state(connector->state);
  640. kfree(connector->state);
  641. }
  642. __drm_atomic_helper_connector_reset(connector, &state->base);
  643. }
  644. static struct drm_connector_state *
  645. tegra_dsi_connector_duplicate_state(struct drm_connector *connector)
  646. {
  647. struct tegra_dsi_state *state = to_dsi_state(connector->state);
  648. struct tegra_dsi_state *copy;
  649. copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
  650. if (!copy)
  651. return NULL;
  652. __drm_atomic_helper_connector_duplicate_state(connector,
  653. &copy->base);
  654. return &copy->base;
  655. }
  656. static const struct drm_connector_funcs tegra_dsi_connector_funcs = {
  657. .reset = tegra_dsi_connector_reset,
  658. .detect = tegra_output_connector_detect,
  659. .fill_modes = drm_helper_probe_single_connector_modes,
  660. .destroy = tegra_output_connector_destroy,
  661. .atomic_duplicate_state = tegra_dsi_connector_duplicate_state,
  662. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  663. };
  664. static enum drm_mode_status
  665. tegra_dsi_connector_mode_valid(struct drm_connector *connector,
  666. struct drm_display_mode *mode)
  667. {
  668. return MODE_OK;
  669. }
  670. static const struct drm_connector_helper_funcs tegra_dsi_connector_helper_funcs = {
  671. .get_modes = tegra_output_connector_get_modes,
  672. .mode_valid = tegra_dsi_connector_mode_valid,
  673. };
  674. static const struct drm_encoder_funcs tegra_dsi_encoder_funcs = {
  675. .destroy = tegra_output_encoder_destroy,
  676. };
  677. static void tegra_dsi_unprepare(struct tegra_dsi *dsi)
  678. {
  679. int err;
  680. if (dsi->slave)
  681. tegra_dsi_unprepare(dsi->slave);
  682. err = tegra_mipi_disable(dsi->mipi);
  683. if (err < 0)
  684. dev_err(dsi->dev, "failed to disable MIPI calibration: %d\n",
  685. err);
  686. pm_runtime_put(dsi->dev);
  687. }
  688. static void tegra_dsi_encoder_disable(struct drm_encoder *encoder)
  689. {
  690. struct tegra_output *output = encoder_to_output(encoder);
  691. struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
  692. struct tegra_dsi *dsi = to_dsi(output);
  693. u32 value;
  694. int err;
  695. if (output->panel)
  696. drm_panel_disable(output->panel);
  697. tegra_dsi_video_disable(dsi);
  698. /*
  699. * The following accesses registers of the display controller, so make
  700. * sure it's only executed when the output is attached to one.
  701. */
  702. if (dc) {
  703. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  704. value &= ~DSI_ENABLE;
  705. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  706. tegra_dc_commit(dc);
  707. }
  708. err = tegra_dsi_wait_idle(dsi, 100);
  709. if (err < 0)
  710. dev_dbg(dsi->dev, "failed to idle DSI: %d\n", err);
  711. tegra_dsi_soft_reset(dsi);
  712. if (output->panel)
  713. drm_panel_unprepare(output->panel);
  714. tegra_dsi_disable(dsi);
  715. tegra_dsi_unprepare(dsi);
  716. }
  717. static void tegra_dsi_prepare(struct tegra_dsi *dsi)
  718. {
  719. int err;
  720. pm_runtime_get_sync(dsi->dev);
  721. err = tegra_mipi_enable(dsi->mipi);
  722. if (err < 0)
  723. dev_err(dsi->dev, "failed to enable MIPI calibration: %d\n",
  724. err);
  725. err = tegra_dsi_pad_calibrate(dsi);
  726. if (err < 0)
  727. dev_err(dsi->dev, "MIPI calibration failed: %d\n", err);
  728. if (dsi->slave)
  729. tegra_dsi_prepare(dsi->slave);
  730. }
  731. static void tegra_dsi_encoder_enable(struct drm_encoder *encoder)
  732. {
  733. struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
  734. struct tegra_output *output = encoder_to_output(encoder);
  735. struct tegra_dc *dc = to_tegra_dc(encoder->crtc);
  736. struct tegra_dsi *dsi = to_dsi(output);
  737. struct tegra_dsi_state *state;
  738. u32 value;
  739. tegra_dsi_prepare(dsi);
  740. state = tegra_dsi_get_state(dsi);
  741. tegra_dsi_set_timeout(dsi, state->bclk, state->vrefresh);
  742. /*
  743. * The D-PHY timing fields are expressed in byte-clock cycles, so
  744. * multiply the period by 8.
  745. */
  746. tegra_dsi_set_phy_timing(dsi, state->period * 8, &state->timing);
  747. if (output->panel)
  748. drm_panel_prepare(output->panel);
  749. tegra_dsi_configure(dsi, dc->pipe, mode);
  750. /* enable display controller */
  751. value = tegra_dc_readl(dc, DC_DISP_DISP_WIN_OPTIONS);
  752. value |= DSI_ENABLE;
  753. tegra_dc_writel(dc, value, DC_DISP_DISP_WIN_OPTIONS);
  754. tegra_dc_commit(dc);
  755. /* enable DSI controller */
  756. tegra_dsi_enable(dsi);
  757. if (output->panel)
  758. drm_panel_enable(output->panel);
  759. }
  760. static int
  761. tegra_dsi_encoder_atomic_check(struct drm_encoder *encoder,
  762. struct drm_crtc_state *crtc_state,
  763. struct drm_connector_state *conn_state)
  764. {
  765. struct tegra_output *output = encoder_to_output(encoder);
  766. struct tegra_dsi_state *state = to_dsi_state(conn_state);
  767. struct tegra_dc *dc = to_tegra_dc(conn_state->crtc);
  768. struct tegra_dsi *dsi = to_dsi(output);
  769. unsigned int scdiv;
  770. unsigned long plld;
  771. int err;
  772. state->pclk = crtc_state->mode.clock * 1000;
  773. err = tegra_dsi_get_muldiv(dsi->format, &state->mul, &state->div);
  774. if (err < 0)
  775. return err;
  776. state->lanes = tegra_dsi_get_lanes(dsi);
  777. err = tegra_dsi_get_format(dsi->format, &state->format);
  778. if (err < 0)
  779. return err;
  780. state->vrefresh = drm_mode_vrefresh(&crtc_state->mode);
  781. /* compute byte clock */
  782. state->bclk = (state->pclk * state->mul) / (state->div * state->lanes);
  783. DRM_DEBUG_KMS("mul: %u, div: %u, lanes: %u\n", state->mul, state->div,
  784. state->lanes);
  785. DRM_DEBUG_KMS("format: %u, vrefresh: %u\n", state->format,
  786. state->vrefresh);
  787. DRM_DEBUG_KMS("bclk: %lu\n", state->bclk);
  788. /*
  789. * Compute bit clock and round up to the next MHz.
  790. */
  791. plld = DIV_ROUND_UP(state->bclk * 8, USEC_PER_SEC) * USEC_PER_SEC;
  792. state->period = DIV_ROUND_CLOSEST(NSEC_PER_SEC, plld);
  793. err = mipi_dphy_timing_get_default(&state->timing, state->period);
  794. if (err < 0)
  795. return err;
  796. err = mipi_dphy_timing_validate(&state->timing, state->period);
  797. if (err < 0) {
  798. dev_err(dsi->dev, "failed to validate D-PHY timing: %d\n", err);
  799. return err;
  800. }
  801. /*
  802. * We divide the frequency by two here, but we make up for that by
  803. * setting the shift clock divider (further below) to half of the
  804. * correct value.
  805. */
  806. plld /= 2;
  807. /*
  808. * Derive pixel clock from bit clock using the shift clock divider.
  809. * Note that this is only half of what we would expect, but we need
  810. * that to make up for the fact that we divided the bit clock by a
  811. * factor of two above.
  812. *
  813. * It's not clear exactly why this is necessary, but the display is
  814. * not working properly otherwise. Perhaps the PLLs cannot generate
  815. * frequencies sufficiently high.
  816. */
  817. scdiv = ((8 * state->mul) / (state->div * state->lanes)) - 2;
  818. err = tegra_dc_state_setup_clock(dc, crtc_state, dsi->clk_parent,
  819. plld, scdiv);
  820. if (err < 0) {
  821. dev_err(output->dev, "failed to setup CRTC state: %d\n", err);
  822. return err;
  823. }
  824. return err;
  825. }
  826. static const struct drm_encoder_helper_funcs tegra_dsi_encoder_helper_funcs = {
  827. .disable = tegra_dsi_encoder_disable,
  828. .enable = tegra_dsi_encoder_enable,
  829. .atomic_check = tegra_dsi_encoder_atomic_check,
  830. };
  831. static int tegra_dsi_init(struct host1x_client *client)
  832. {
  833. struct drm_device *drm = dev_get_drvdata(client->parent);
  834. struct tegra_dsi *dsi = host1x_client_to_dsi(client);
  835. int err;
  836. /* Gangsters must not register their own outputs. */
  837. if (!dsi->master) {
  838. dsi->output.dev = client->dev;
  839. drm_connector_init(drm, &dsi->output.connector,
  840. &tegra_dsi_connector_funcs,
  841. DRM_MODE_CONNECTOR_DSI);
  842. drm_connector_helper_add(&dsi->output.connector,
  843. &tegra_dsi_connector_helper_funcs);
  844. dsi->output.connector.dpms = DRM_MODE_DPMS_OFF;
  845. drm_encoder_init(drm, &dsi->output.encoder,
  846. &tegra_dsi_encoder_funcs,
  847. DRM_MODE_ENCODER_DSI, NULL);
  848. drm_encoder_helper_add(&dsi->output.encoder,
  849. &tegra_dsi_encoder_helper_funcs);
  850. drm_mode_connector_attach_encoder(&dsi->output.connector,
  851. &dsi->output.encoder);
  852. drm_connector_register(&dsi->output.connector);
  853. err = tegra_output_init(drm, &dsi->output);
  854. if (err < 0)
  855. dev_err(dsi->dev, "failed to initialize output: %d\n",
  856. err);
  857. dsi->output.encoder.possible_crtcs = 0x3;
  858. }
  859. if (IS_ENABLED(CONFIG_DEBUG_FS)) {
  860. err = tegra_dsi_debugfs_init(dsi, drm->primary);
  861. if (err < 0)
  862. dev_err(dsi->dev, "debugfs setup failed: %d\n", err);
  863. }
  864. return 0;
  865. }
  866. static int tegra_dsi_exit(struct host1x_client *client)
  867. {
  868. struct tegra_dsi *dsi = host1x_client_to_dsi(client);
  869. tegra_output_exit(&dsi->output);
  870. if (IS_ENABLED(CONFIG_DEBUG_FS))
  871. tegra_dsi_debugfs_exit(dsi);
  872. regulator_disable(dsi->vdd);
  873. return 0;
  874. }
  875. static const struct host1x_client_ops dsi_client_ops = {
  876. .init = tegra_dsi_init,
  877. .exit = tegra_dsi_exit,
  878. };
  879. static int tegra_dsi_setup_clocks(struct tegra_dsi *dsi)
  880. {
  881. struct clk *parent;
  882. int err;
  883. parent = clk_get_parent(dsi->clk);
  884. if (!parent)
  885. return -EINVAL;
  886. err = clk_set_parent(parent, dsi->clk_parent);
  887. if (err < 0)
  888. return err;
  889. return 0;
  890. }
  891. static const char * const error_report[16] = {
  892. "SoT Error",
  893. "SoT Sync Error",
  894. "EoT Sync Error",
  895. "Escape Mode Entry Command Error",
  896. "Low-Power Transmit Sync Error",
  897. "Peripheral Timeout Error",
  898. "False Control Error",
  899. "Contention Detected",
  900. "ECC Error, single-bit",
  901. "ECC Error, multi-bit",
  902. "Checksum Error",
  903. "DSI Data Type Not Recognized",
  904. "DSI VC ID Invalid",
  905. "Invalid Transmission Length",
  906. "Reserved",
  907. "DSI Protocol Violation",
  908. };
  909. static ssize_t tegra_dsi_read_response(struct tegra_dsi *dsi,
  910. const struct mipi_dsi_msg *msg,
  911. size_t count)
  912. {
  913. u8 *rx = msg->rx_buf;
  914. unsigned int i, j, k;
  915. size_t size = 0;
  916. u16 errors;
  917. u32 value;
  918. /* read and parse packet header */
  919. value = tegra_dsi_readl(dsi, DSI_RD_DATA);
  920. switch (value & 0x3f) {
  921. case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
  922. errors = (value >> 8) & 0xffff;
  923. dev_dbg(dsi->dev, "Acknowledge and error report: %04x\n",
  924. errors);
  925. for (i = 0; i < ARRAY_SIZE(error_report); i++)
  926. if (errors & BIT(i))
  927. dev_dbg(dsi->dev, " %2u: %s\n", i,
  928. error_report[i]);
  929. break;
  930. case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
  931. rx[0] = (value >> 8) & 0xff;
  932. size = 1;
  933. break;
  934. case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
  935. rx[0] = (value >> 8) & 0xff;
  936. rx[1] = (value >> 16) & 0xff;
  937. size = 2;
  938. break;
  939. case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
  940. size = ((value >> 8) & 0xff00) | ((value >> 8) & 0xff);
  941. break;
  942. case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
  943. size = ((value >> 8) & 0xff00) | ((value >> 8) & 0xff);
  944. break;
  945. default:
  946. dev_err(dsi->dev, "unhandled response type: %02x\n",
  947. value & 0x3f);
  948. return -EPROTO;
  949. }
  950. size = min(size, msg->rx_len);
  951. if (msg->rx_buf && size > 0) {
  952. for (i = 0, j = 0; i < count - 1; i++, j += 4) {
  953. u8 *rx = msg->rx_buf + j;
  954. value = tegra_dsi_readl(dsi, DSI_RD_DATA);
  955. for (k = 0; k < 4 && (j + k) < msg->rx_len; k++)
  956. rx[j + k] = (value >> (k << 3)) & 0xff;
  957. }
  958. }
  959. return size;
  960. }
  961. static int tegra_dsi_transmit(struct tegra_dsi *dsi, unsigned long timeout)
  962. {
  963. tegra_dsi_writel(dsi, DSI_TRIGGER_HOST, DSI_TRIGGER);
  964. timeout = jiffies + msecs_to_jiffies(timeout);
  965. while (time_before(jiffies, timeout)) {
  966. u32 value = tegra_dsi_readl(dsi, DSI_TRIGGER);
  967. if ((value & DSI_TRIGGER_HOST) == 0)
  968. return 0;
  969. usleep_range(1000, 2000);
  970. }
  971. DRM_DEBUG_KMS("timeout waiting for transmission to complete\n");
  972. return -ETIMEDOUT;
  973. }
  974. static int tegra_dsi_wait_for_response(struct tegra_dsi *dsi,
  975. unsigned long timeout)
  976. {
  977. timeout = jiffies + msecs_to_jiffies(250);
  978. while (time_before(jiffies, timeout)) {
  979. u32 value = tegra_dsi_readl(dsi, DSI_STATUS);
  980. u8 count = value & 0x1f;
  981. if (count > 0)
  982. return count;
  983. usleep_range(1000, 2000);
  984. }
  985. DRM_DEBUG_KMS("peripheral returned no data\n");
  986. return -ETIMEDOUT;
  987. }
  988. static void tegra_dsi_writesl(struct tegra_dsi *dsi, unsigned long offset,
  989. const void *buffer, size_t size)
  990. {
  991. const u8 *buf = buffer;
  992. size_t i, j;
  993. u32 value;
  994. for (j = 0; j < size; j += 4) {
  995. value = 0;
  996. for (i = 0; i < 4 && j + i < size; i++)
  997. value |= buf[j + i] << (i << 3);
  998. tegra_dsi_writel(dsi, value, DSI_WR_DATA);
  999. }
  1000. }
  1001. static ssize_t tegra_dsi_host_transfer(struct mipi_dsi_host *host,
  1002. const struct mipi_dsi_msg *msg)
  1003. {
  1004. struct tegra_dsi *dsi = host_to_tegra(host);
  1005. struct mipi_dsi_packet packet;
  1006. const u8 *header;
  1007. size_t count;
  1008. ssize_t err;
  1009. u32 value;
  1010. err = mipi_dsi_create_packet(&packet, msg);
  1011. if (err < 0)
  1012. return err;
  1013. header = packet.header;
  1014. /* maximum FIFO depth is 1920 words */
  1015. if (packet.size > dsi->video_fifo_depth * 4)
  1016. return -ENOSPC;
  1017. /* reset underflow/overflow flags */
  1018. value = tegra_dsi_readl(dsi, DSI_STATUS);
  1019. if (value & (DSI_STATUS_UNDERFLOW | DSI_STATUS_OVERFLOW)) {
  1020. value = DSI_HOST_CONTROL_FIFO_RESET;
  1021. tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
  1022. usleep_range(10, 20);
  1023. }
  1024. value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
  1025. value |= DSI_POWER_CONTROL_ENABLE;
  1026. tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
  1027. usleep_range(5000, 10000);
  1028. value = DSI_HOST_CONTROL_CRC_RESET | DSI_HOST_CONTROL_TX_TRIG_HOST |
  1029. DSI_HOST_CONTROL_CS | DSI_HOST_CONTROL_ECC;
  1030. if ((msg->flags & MIPI_DSI_MSG_USE_LPM) == 0)
  1031. value |= DSI_HOST_CONTROL_HS;
  1032. /*
  1033. * The host FIFO has a maximum of 64 words, so larger transmissions
  1034. * need to use the video FIFO.
  1035. */
  1036. if (packet.size > dsi->host_fifo_depth * 4)
  1037. value |= DSI_HOST_CONTROL_FIFO_SEL;
  1038. tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
  1039. /*
  1040. * For reads and messages with explicitly requested ACK, generate a
  1041. * BTA sequence after the transmission of the packet.
  1042. */
  1043. if ((msg->flags & MIPI_DSI_MSG_REQ_ACK) ||
  1044. (msg->rx_buf && msg->rx_len > 0)) {
  1045. value = tegra_dsi_readl(dsi, DSI_HOST_CONTROL);
  1046. value |= DSI_HOST_CONTROL_PKT_BTA;
  1047. tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
  1048. }
  1049. value = DSI_CONTROL_LANES(0) | DSI_CONTROL_HOST_ENABLE;
  1050. tegra_dsi_writel(dsi, value, DSI_CONTROL);
  1051. /* write packet header, ECC is generated by hardware */
  1052. value = header[2] << 16 | header[1] << 8 | header[0];
  1053. tegra_dsi_writel(dsi, value, DSI_WR_DATA);
  1054. /* write payload (if any) */
  1055. if (packet.payload_length > 0)
  1056. tegra_dsi_writesl(dsi, DSI_WR_DATA, packet.payload,
  1057. packet.payload_length);
  1058. err = tegra_dsi_transmit(dsi, 250);
  1059. if (err < 0)
  1060. return err;
  1061. if ((msg->flags & MIPI_DSI_MSG_REQ_ACK) ||
  1062. (msg->rx_buf && msg->rx_len > 0)) {
  1063. err = tegra_dsi_wait_for_response(dsi, 250);
  1064. if (err < 0)
  1065. return err;
  1066. count = err;
  1067. value = tegra_dsi_readl(dsi, DSI_RD_DATA);
  1068. switch (value) {
  1069. case 0x84:
  1070. /*
  1071. dev_dbg(dsi->dev, "ACK\n");
  1072. */
  1073. break;
  1074. case 0x87:
  1075. /*
  1076. dev_dbg(dsi->dev, "ESCAPE\n");
  1077. */
  1078. break;
  1079. default:
  1080. dev_err(dsi->dev, "unknown status: %08x\n", value);
  1081. break;
  1082. }
  1083. if (count > 1) {
  1084. err = tegra_dsi_read_response(dsi, msg, count);
  1085. if (err < 0)
  1086. dev_err(dsi->dev,
  1087. "failed to parse response: %zd\n",
  1088. err);
  1089. else {
  1090. /*
  1091. * For read commands, return the number of
  1092. * bytes returned by the peripheral.
  1093. */
  1094. count = err;
  1095. }
  1096. }
  1097. } else {
  1098. /*
  1099. * For write commands, we have transmitted the 4-byte header
  1100. * plus the variable-length payload.
  1101. */
  1102. count = 4 + packet.payload_length;
  1103. }
  1104. return count;
  1105. }
  1106. static int tegra_dsi_ganged_setup(struct tegra_dsi *dsi)
  1107. {
  1108. struct clk *parent;
  1109. int err;
  1110. /* make sure both DSI controllers share the same PLL */
  1111. parent = clk_get_parent(dsi->slave->clk);
  1112. if (!parent)
  1113. return -EINVAL;
  1114. err = clk_set_parent(parent, dsi->clk_parent);
  1115. if (err < 0)
  1116. return err;
  1117. return 0;
  1118. }
  1119. static int tegra_dsi_host_attach(struct mipi_dsi_host *host,
  1120. struct mipi_dsi_device *device)
  1121. {
  1122. struct tegra_dsi *dsi = host_to_tegra(host);
  1123. dsi->flags = device->mode_flags;
  1124. dsi->format = device->format;
  1125. dsi->lanes = device->lanes;
  1126. if (dsi->slave) {
  1127. int err;
  1128. dev_dbg(dsi->dev, "attaching dual-channel device %s\n",
  1129. dev_name(&device->dev));
  1130. err = tegra_dsi_ganged_setup(dsi);
  1131. if (err < 0) {
  1132. dev_err(dsi->dev, "failed to set up ganged mode: %d\n",
  1133. err);
  1134. return err;
  1135. }
  1136. }
  1137. /*
  1138. * Slaves don't have a panel associated with them, so they provide
  1139. * merely the second channel.
  1140. */
  1141. if (!dsi->master) {
  1142. struct tegra_output *output = &dsi->output;
  1143. output->panel = of_drm_find_panel(device->dev.of_node);
  1144. if (output->panel && output->connector.dev) {
  1145. drm_panel_attach(output->panel, &output->connector);
  1146. drm_helper_hpd_irq_event(output->connector.dev);
  1147. }
  1148. }
  1149. return 0;
  1150. }
  1151. static int tegra_dsi_host_detach(struct mipi_dsi_host *host,
  1152. struct mipi_dsi_device *device)
  1153. {
  1154. struct tegra_dsi *dsi = host_to_tegra(host);
  1155. struct tegra_output *output = &dsi->output;
  1156. if (output->panel && &device->dev == output->panel->dev) {
  1157. output->panel = NULL;
  1158. if (output->connector.dev)
  1159. drm_helper_hpd_irq_event(output->connector.dev);
  1160. }
  1161. return 0;
  1162. }
  1163. static const struct mipi_dsi_host_ops tegra_dsi_host_ops = {
  1164. .attach = tegra_dsi_host_attach,
  1165. .detach = tegra_dsi_host_detach,
  1166. .transfer = tegra_dsi_host_transfer,
  1167. };
  1168. static int tegra_dsi_ganged_probe(struct tegra_dsi *dsi)
  1169. {
  1170. struct device_node *np;
  1171. np = of_parse_phandle(dsi->dev->of_node, "nvidia,ganged-mode", 0);
  1172. if (np) {
  1173. struct platform_device *gangster = of_find_device_by_node(np);
  1174. dsi->slave = platform_get_drvdata(gangster);
  1175. of_node_put(np);
  1176. if (!dsi->slave)
  1177. return -EPROBE_DEFER;
  1178. dsi->slave->master = dsi;
  1179. }
  1180. return 0;
  1181. }
  1182. static int tegra_dsi_probe(struct platform_device *pdev)
  1183. {
  1184. struct tegra_dsi *dsi;
  1185. struct resource *regs;
  1186. int err;
  1187. dsi = devm_kzalloc(&pdev->dev, sizeof(*dsi), GFP_KERNEL);
  1188. if (!dsi)
  1189. return -ENOMEM;
  1190. dsi->output.dev = dsi->dev = &pdev->dev;
  1191. dsi->video_fifo_depth = 1920;
  1192. dsi->host_fifo_depth = 64;
  1193. err = tegra_dsi_ganged_probe(dsi);
  1194. if (err < 0)
  1195. return err;
  1196. err = tegra_output_probe(&dsi->output);
  1197. if (err < 0)
  1198. return err;
  1199. dsi->output.connector.polled = DRM_CONNECTOR_POLL_HPD;
  1200. /*
  1201. * Assume these values by default. When a DSI peripheral driver
  1202. * attaches to the DSI host, the parameters will be taken from
  1203. * the attached device.
  1204. */
  1205. dsi->flags = MIPI_DSI_MODE_VIDEO;
  1206. dsi->format = MIPI_DSI_FMT_RGB888;
  1207. dsi->lanes = 4;
  1208. if (!pdev->dev.pm_domain) {
  1209. dsi->rst = devm_reset_control_get(&pdev->dev, "dsi");
  1210. if (IS_ERR(dsi->rst))
  1211. return PTR_ERR(dsi->rst);
  1212. }
  1213. dsi->clk = devm_clk_get(&pdev->dev, NULL);
  1214. if (IS_ERR(dsi->clk)) {
  1215. dev_err(&pdev->dev, "cannot get DSI clock\n");
  1216. return PTR_ERR(dsi->clk);
  1217. }
  1218. dsi->clk_lp = devm_clk_get(&pdev->dev, "lp");
  1219. if (IS_ERR(dsi->clk_lp)) {
  1220. dev_err(&pdev->dev, "cannot get low-power clock\n");
  1221. return PTR_ERR(dsi->clk_lp);
  1222. }
  1223. dsi->clk_parent = devm_clk_get(&pdev->dev, "parent");
  1224. if (IS_ERR(dsi->clk_parent)) {
  1225. dev_err(&pdev->dev, "cannot get parent clock\n");
  1226. return PTR_ERR(dsi->clk_parent);
  1227. }
  1228. dsi->vdd = devm_regulator_get(&pdev->dev, "avdd-dsi-csi");
  1229. if (IS_ERR(dsi->vdd)) {
  1230. dev_err(&pdev->dev, "cannot get VDD supply\n");
  1231. return PTR_ERR(dsi->vdd);
  1232. }
  1233. err = tegra_dsi_setup_clocks(dsi);
  1234. if (err < 0) {
  1235. dev_err(&pdev->dev, "cannot setup clocks\n");
  1236. return err;
  1237. }
  1238. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1239. dsi->regs = devm_ioremap_resource(&pdev->dev, regs);
  1240. if (IS_ERR(dsi->regs))
  1241. return PTR_ERR(dsi->regs);
  1242. dsi->mipi = tegra_mipi_request(&pdev->dev);
  1243. if (IS_ERR(dsi->mipi))
  1244. return PTR_ERR(dsi->mipi);
  1245. dsi->host.ops = &tegra_dsi_host_ops;
  1246. dsi->host.dev = &pdev->dev;
  1247. err = mipi_dsi_host_register(&dsi->host);
  1248. if (err < 0) {
  1249. dev_err(&pdev->dev, "failed to register DSI host: %d\n", err);
  1250. goto mipi_free;
  1251. }
  1252. platform_set_drvdata(pdev, dsi);
  1253. pm_runtime_enable(&pdev->dev);
  1254. INIT_LIST_HEAD(&dsi->client.list);
  1255. dsi->client.ops = &dsi_client_ops;
  1256. dsi->client.dev = &pdev->dev;
  1257. err = host1x_client_register(&dsi->client);
  1258. if (err < 0) {
  1259. dev_err(&pdev->dev, "failed to register host1x client: %d\n",
  1260. err);
  1261. goto unregister;
  1262. }
  1263. return 0;
  1264. unregister:
  1265. mipi_dsi_host_unregister(&dsi->host);
  1266. mipi_free:
  1267. tegra_mipi_free(dsi->mipi);
  1268. return err;
  1269. }
  1270. static int tegra_dsi_remove(struct platform_device *pdev)
  1271. {
  1272. struct tegra_dsi *dsi = platform_get_drvdata(pdev);
  1273. int err;
  1274. pm_runtime_disable(&pdev->dev);
  1275. err = host1x_client_unregister(&dsi->client);
  1276. if (err < 0) {
  1277. dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
  1278. err);
  1279. return err;
  1280. }
  1281. tegra_output_remove(&dsi->output);
  1282. mipi_dsi_host_unregister(&dsi->host);
  1283. tegra_mipi_free(dsi->mipi);
  1284. return 0;
  1285. }
  1286. #ifdef CONFIG_PM
  1287. static int tegra_dsi_suspend(struct device *dev)
  1288. {
  1289. struct tegra_dsi *dsi = dev_get_drvdata(dev);
  1290. int err;
  1291. if (dsi->rst) {
  1292. err = reset_control_assert(dsi->rst);
  1293. if (err < 0) {
  1294. dev_err(dev, "failed to assert reset: %d\n", err);
  1295. return err;
  1296. }
  1297. }
  1298. usleep_range(1000, 2000);
  1299. clk_disable_unprepare(dsi->clk_lp);
  1300. clk_disable_unprepare(dsi->clk);
  1301. regulator_disable(dsi->vdd);
  1302. return 0;
  1303. }
  1304. static int tegra_dsi_resume(struct device *dev)
  1305. {
  1306. struct tegra_dsi *dsi = dev_get_drvdata(dev);
  1307. int err;
  1308. err = regulator_enable(dsi->vdd);
  1309. if (err < 0) {
  1310. dev_err(dsi->dev, "failed to enable VDD supply: %d\n", err);
  1311. return err;
  1312. }
  1313. err = clk_prepare_enable(dsi->clk);
  1314. if (err < 0) {
  1315. dev_err(dev, "cannot enable DSI clock: %d\n", err);
  1316. goto disable_vdd;
  1317. }
  1318. err = clk_prepare_enable(dsi->clk_lp);
  1319. if (err < 0) {
  1320. dev_err(dev, "cannot enable low-power clock: %d\n", err);
  1321. goto disable_clk;
  1322. }
  1323. usleep_range(1000, 2000);
  1324. if (dsi->rst) {
  1325. err = reset_control_deassert(dsi->rst);
  1326. if (err < 0) {
  1327. dev_err(dev, "cannot assert reset: %d\n", err);
  1328. goto disable_clk_lp;
  1329. }
  1330. }
  1331. return 0;
  1332. disable_clk_lp:
  1333. clk_disable_unprepare(dsi->clk_lp);
  1334. disable_clk:
  1335. clk_disable_unprepare(dsi->clk);
  1336. disable_vdd:
  1337. regulator_disable(dsi->vdd);
  1338. return err;
  1339. }
  1340. #endif
  1341. static const struct dev_pm_ops tegra_dsi_pm_ops = {
  1342. SET_RUNTIME_PM_OPS(tegra_dsi_suspend, tegra_dsi_resume, NULL)
  1343. };
  1344. static const struct of_device_id tegra_dsi_of_match[] = {
  1345. { .compatible = "nvidia,tegra210-dsi", },
  1346. { .compatible = "nvidia,tegra132-dsi", },
  1347. { .compatible = "nvidia,tegra124-dsi", },
  1348. { .compatible = "nvidia,tegra114-dsi", },
  1349. { },
  1350. };
  1351. MODULE_DEVICE_TABLE(of, tegra_dsi_of_match);
  1352. struct platform_driver tegra_dsi_driver = {
  1353. .driver = {
  1354. .name = "tegra-dsi",
  1355. .of_match_table = tegra_dsi_of_match,
  1356. .pm = &tegra_dsi_pm_ops,
  1357. },
  1358. .probe = tegra_dsi_probe,
  1359. .remove = tegra_dsi_remove,
  1360. };