dsi.c 42 KB

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