dsi.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688
  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_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. 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. regulator_disable(dsi->vdd);
  866. return 0;
  867. }
  868. static const struct host1x_client_ops dsi_client_ops = {
  869. .init = tegra_dsi_init,
  870. .exit = tegra_dsi_exit,
  871. };
  872. static int tegra_dsi_setup_clocks(struct tegra_dsi *dsi)
  873. {
  874. struct clk *parent;
  875. int err;
  876. parent = clk_get_parent(dsi->clk);
  877. if (!parent)
  878. return -EINVAL;
  879. err = clk_set_parent(parent, dsi->clk_parent);
  880. if (err < 0)
  881. return err;
  882. return 0;
  883. }
  884. static const char * const error_report[16] = {
  885. "SoT Error",
  886. "SoT Sync Error",
  887. "EoT Sync Error",
  888. "Escape Mode Entry Command Error",
  889. "Low-Power Transmit Sync Error",
  890. "Peripheral Timeout Error",
  891. "False Control Error",
  892. "Contention Detected",
  893. "ECC Error, single-bit",
  894. "ECC Error, multi-bit",
  895. "Checksum Error",
  896. "DSI Data Type Not Recognized",
  897. "DSI VC ID Invalid",
  898. "Invalid Transmission Length",
  899. "Reserved",
  900. "DSI Protocol Violation",
  901. };
  902. static ssize_t tegra_dsi_read_response(struct tegra_dsi *dsi,
  903. const struct mipi_dsi_msg *msg,
  904. size_t count)
  905. {
  906. u8 *rx = msg->rx_buf;
  907. unsigned int i, j, k;
  908. size_t size = 0;
  909. u16 errors;
  910. u32 value;
  911. /* read and parse packet header */
  912. value = tegra_dsi_readl(dsi, DSI_RD_DATA);
  913. switch (value & 0x3f) {
  914. case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
  915. errors = (value >> 8) & 0xffff;
  916. dev_dbg(dsi->dev, "Acknowledge and error report: %04x\n",
  917. errors);
  918. for (i = 0; i < ARRAY_SIZE(error_report); i++)
  919. if (errors & BIT(i))
  920. dev_dbg(dsi->dev, " %2u: %s\n", i,
  921. error_report[i]);
  922. break;
  923. case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
  924. rx[0] = (value >> 8) & 0xff;
  925. size = 1;
  926. break;
  927. case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
  928. rx[0] = (value >> 8) & 0xff;
  929. rx[1] = (value >> 16) & 0xff;
  930. size = 2;
  931. break;
  932. case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
  933. size = ((value >> 8) & 0xff00) | ((value >> 8) & 0xff);
  934. break;
  935. case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
  936. size = ((value >> 8) & 0xff00) | ((value >> 8) & 0xff);
  937. break;
  938. default:
  939. dev_err(dsi->dev, "unhandled response type: %02x\n",
  940. value & 0x3f);
  941. return -EPROTO;
  942. }
  943. size = min(size, msg->rx_len);
  944. if (msg->rx_buf && size > 0) {
  945. for (i = 0, j = 0; i < count - 1; i++, j += 4) {
  946. u8 *rx = msg->rx_buf + j;
  947. value = tegra_dsi_readl(dsi, DSI_RD_DATA);
  948. for (k = 0; k < 4 && (j + k) < msg->rx_len; k++)
  949. rx[j + k] = (value >> (k << 3)) & 0xff;
  950. }
  951. }
  952. return size;
  953. }
  954. static int tegra_dsi_transmit(struct tegra_dsi *dsi, unsigned long timeout)
  955. {
  956. tegra_dsi_writel(dsi, DSI_TRIGGER_HOST, DSI_TRIGGER);
  957. timeout = jiffies + msecs_to_jiffies(timeout);
  958. while (time_before(jiffies, timeout)) {
  959. u32 value = tegra_dsi_readl(dsi, DSI_TRIGGER);
  960. if ((value & DSI_TRIGGER_HOST) == 0)
  961. return 0;
  962. usleep_range(1000, 2000);
  963. }
  964. DRM_DEBUG_KMS("timeout waiting for transmission to complete\n");
  965. return -ETIMEDOUT;
  966. }
  967. static int tegra_dsi_wait_for_response(struct tegra_dsi *dsi,
  968. unsigned long timeout)
  969. {
  970. timeout = jiffies + msecs_to_jiffies(250);
  971. while (time_before(jiffies, timeout)) {
  972. u32 value = tegra_dsi_readl(dsi, DSI_STATUS);
  973. u8 count = value & 0x1f;
  974. if (count > 0)
  975. return count;
  976. usleep_range(1000, 2000);
  977. }
  978. DRM_DEBUG_KMS("peripheral returned no data\n");
  979. return -ETIMEDOUT;
  980. }
  981. static void tegra_dsi_writesl(struct tegra_dsi *dsi, unsigned long offset,
  982. const void *buffer, size_t size)
  983. {
  984. const u8 *buf = buffer;
  985. size_t i, j;
  986. u32 value;
  987. for (j = 0; j < size; j += 4) {
  988. value = 0;
  989. for (i = 0; i < 4 && j + i < size; i++)
  990. value |= buf[j + i] << (i << 3);
  991. tegra_dsi_writel(dsi, value, DSI_WR_DATA);
  992. }
  993. }
  994. static ssize_t tegra_dsi_host_transfer(struct mipi_dsi_host *host,
  995. const struct mipi_dsi_msg *msg)
  996. {
  997. struct tegra_dsi *dsi = host_to_tegra(host);
  998. struct mipi_dsi_packet packet;
  999. const u8 *header;
  1000. size_t count;
  1001. ssize_t err;
  1002. u32 value;
  1003. err = mipi_dsi_create_packet(&packet, msg);
  1004. if (err < 0)
  1005. return err;
  1006. header = packet.header;
  1007. /* maximum FIFO depth is 1920 words */
  1008. if (packet.size > dsi->video_fifo_depth * 4)
  1009. return -ENOSPC;
  1010. /* reset underflow/overflow flags */
  1011. value = tegra_dsi_readl(dsi, DSI_STATUS);
  1012. if (value & (DSI_STATUS_UNDERFLOW | DSI_STATUS_OVERFLOW)) {
  1013. value = DSI_HOST_CONTROL_FIFO_RESET;
  1014. tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
  1015. usleep_range(10, 20);
  1016. }
  1017. value = tegra_dsi_readl(dsi, DSI_POWER_CONTROL);
  1018. value |= DSI_POWER_CONTROL_ENABLE;
  1019. tegra_dsi_writel(dsi, value, DSI_POWER_CONTROL);
  1020. usleep_range(5000, 10000);
  1021. value = DSI_HOST_CONTROL_CRC_RESET | DSI_HOST_CONTROL_TX_TRIG_HOST |
  1022. DSI_HOST_CONTROL_CS | DSI_HOST_CONTROL_ECC;
  1023. if ((msg->flags & MIPI_DSI_MSG_USE_LPM) == 0)
  1024. value |= DSI_HOST_CONTROL_HS;
  1025. /*
  1026. * The host FIFO has a maximum of 64 words, so larger transmissions
  1027. * need to use the video FIFO.
  1028. */
  1029. if (packet.size > dsi->host_fifo_depth * 4)
  1030. value |= DSI_HOST_CONTROL_FIFO_SEL;
  1031. tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
  1032. /*
  1033. * For reads and messages with explicitly requested ACK, generate a
  1034. * BTA sequence after the transmission of the packet.
  1035. */
  1036. if ((msg->flags & MIPI_DSI_MSG_REQ_ACK) ||
  1037. (msg->rx_buf && msg->rx_len > 0)) {
  1038. value = tegra_dsi_readl(dsi, DSI_HOST_CONTROL);
  1039. value |= DSI_HOST_CONTROL_PKT_BTA;
  1040. tegra_dsi_writel(dsi, value, DSI_HOST_CONTROL);
  1041. }
  1042. value = DSI_CONTROL_LANES(0) | DSI_CONTROL_HOST_ENABLE;
  1043. tegra_dsi_writel(dsi, value, DSI_CONTROL);
  1044. /* write packet header, ECC is generated by hardware */
  1045. value = header[2] << 16 | header[1] << 8 | header[0];
  1046. tegra_dsi_writel(dsi, value, DSI_WR_DATA);
  1047. /* write payload (if any) */
  1048. if (packet.payload_length > 0)
  1049. tegra_dsi_writesl(dsi, DSI_WR_DATA, packet.payload,
  1050. packet.payload_length);
  1051. err = tegra_dsi_transmit(dsi, 250);
  1052. if (err < 0)
  1053. return err;
  1054. if ((msg->flags & MIPI_DSI_MSG_REQ_ACK) ||
  1055. (msg->rx_buf && msg->rx_len > 0)) {
  1056. err = tegra_dsi_wait_for_response(dsi, 250);
  1057. if (err < 0)
  1058. return err;
  1059. count = err;
  1060. value = tegra_dsi_readl(dsi, DSI_RD_DATA);
  1061. switch (value) {
  1062. case 0x84:
  1063. /*
  1064. dev_dbg(dsi->dev, "ACK\n");
  1065. */
  1066. break;
  1067. case 0x87:
  1068. /*
  1069. dev_dbg(dsi->dev, "ESCAPE\n");
  1070. */
  1071. break;
  1072. default:
  1073. dev_err(dsi->dev, "unknown status: %08x\n", value);
  1074. break;
  1075. }
  1076. if (count > 1) {
  1077. err = tegra_dsi_read_response(dsi, msg, count);
  1078. if (err < 0)
  1079. dev_err(dsi->dev,
  1080. "failed to parse response: %zd\n",
  1081. err);
  1082. else {
  1083. /*
  1084. * For read commands, return the number of
  1085. * bytes returned by the peripheral.
  1086. */
  1087. count = err;
  1088. }
  1089. }
  1090. } else {
  1091. /*
  1092. * For write commands, we have transmitted the 4-byte header
  1093. * plus the variable-length payload.
  1094. */
  1095. count = 4 + packet.payload_length;
  1096. }
  1097. return count;
  1098. }
  1099. static int tegra_dsi_ganged_setup(struct tegra_dsi *dsi)
  1100. {
  1101. struct clk *parent;
  1102. int err;
  1103. /* make sure both DSI controllers share the same PLL */
  1104. parent = clk_get_parent(dsi->slave->clk);
  1105. if (!parent)
  1106. return -EINVAL;
  1107. err = clk_set_parent(parent, dsi->clk_parent);
  1108. if (err < 0)
  1109. return err;
  1110. return 0;
  1111. }
  1112. static int tegra_dsi_host_attach(struct mipi_dsi_host *host,
  1113. struct mipi_dsi_device *device)
  1114. {
  1115. struct tegra_dsi *dsi = host_to_tegra(host);
  1116. dsi->flags = device->mode_flags;
  1117. dsi->format = device->format;
  1118. dsi->lanes = device->lanes;
  1119. if (dsi->slave) {
  1120. int err;
  1121. dev_dbg(dsi->dev, "attaching dual-channel device %s\n",
  1122. dev_name(&device->dev));
  1123. err = tegra_dsi_ganged_setup(dsi);
  1124. if (err < 0) {
  1125. dev_err(dsi->dev, "failed to set up ganged mode: %d\n",
  1126. err);
  1127. return err;
  1128. }
  1129. }
  1130. /*
  1131. * Slaves don't have a panel associated with them, so they provide
  1132. * merely the second channel.
  1133. */
  1134. if (!dsi->master) {
  1135. struct tegra_output *output = &dsi->output;
  1136. output->panel = of_drm_find_panel(device->dev.of_node);
  1137. if (output->panel && output->connector.dev) {
  1138. drm_panel_attach(output->panel, &output->connector);
  1139. drm_helper_hpd_irq_event(output->connector.dev);
  1140. }
  1141. }
  1142. return 0;
  1143. }
  1144. static int tegra_dsi_host_detach(struct mipi_dsi_host *host,
  1145. struct mipi_dsi_device *device)
  1146. {
  1147. struct tegra_dsi *dsi = host_to_tegra(host);
  1148. struct tegra_output *output = &dsi->output;
  1149. if (output->panel && &device->dev == output->panel->dev) {
  1150. output->panel = NULL;
  1151. if (output->connector.dev)
  1152. drm_helper_hpd_irq_event(output->connector.dev);
  1153. }
  1154. return 0;
  1155. }
  1156. static const struct mipi_dsi_host_ops tegra_dsi_host_ops = {
  1157. .attach = tegra_dsi_host_attach,
  1158. .detach = tegra_dsi_host_detach,
  1159. .transfer = tegra_dsi_host_transfer,
  1160. };
  1161. static int tegra_dsi_ganged_probe(struct tegra_dsi *dsi)
  1162. {
  1163. struct device_node *np;
  1164. np = of_parse_phandle(dsi->dev->of_node, "nvidia,ganged-mode", 0);
  1165. if (np) {
  1166. struct platform_device *gangster = of_find_device_by_node(np);
  1167. dsi->slave = platform_get_drvdata(gangster);
  1168. of_node_put(np);
  1169. if (!dsi->slave)
  1170. return -EPROBE_DEFER;
  1171. dsi->slave->master = dsi;
  1172. }
  1173. return 0;
  1174. }
  1175. static int tegra_dsi_probe(struct platform_device *pdev)
  1176. {
  1177. struct tegra_dsi *dsi;
  1178. struct resource *regs;
  1179. int err;
  1180. dsi = devm_kzalloc(&pdev->dev, sizeof(*dsi), GFP_KERNEL);
  1181. if (!dsi)
  1182. return -ENOMEM;
  1183. dsi->output.dev = dsi->dev = &pdev->dev;
  1184. dsi->video_fifo_depth = 1920;
  1185. dsi->host_fifo_depth = 64;
  1186. err = tegra_dsi_ganged_probe(dsi);
  1187. if (err < 0)
  1188. return err;
  1189. err = tegra_output_probe(&dsi->output);
  1190. if (err < 0)
  1191. return err;
  1192. dsi->output.connector.polled = DRM_CONNECTOR_POLL_HPD;
  1193. /*
  1194. * Assume these values by default. When a DSI peripheral driver
  1195. * attaches to the DSI host, the parameters will be taken from
  1196. * the attached device.
  1197. */
  1198. dsi->flags = MIPI_DSI_MODE_VIDEO;
  1199. dsi->format = MIPI_DSI_FMT_RGB888;
  1200. dsi->lanes = 4;
  1201. if (!pdev->dev.pm_domain) {
  1202. dsi->rst = devm_reset_control_get(&pdev->dev, "dsi");
  1203. if (IS_ERR(dsi->rst))
  1204. return PTR_ERR(dsi->rst);
  1205. }
  1206. dsi->clk = devm_clk_get(&pdev->dev, NULL);
  1207. if (IS_ERR(dsi->clk)) {
  1208. dev_err(&pdev->dev, "cannot get DSI clock\n");
  1209. return PTR_ERR(dsi->clk);
  1210. }
  1211. dsi->clk_lp = devm_clk_get(&pdev->dev, "lp");
  1212. if (IS_ERR(dsi->clk_lp)) {
  1213. dev_err(&pdev->dev, "cannot get low-power clock\n");
  1214. return PTR_ERR(dsi->clk_lp);
  1215. }
  1216. dsi->clk_parent = devm_clk_get(&pdev->dev, "parent");
  1217. if (IS_ERR(dsi->clk_parent)) {
  1218. dev_err(&pdev->dev, "cannot get parent clock\n");
  1219. return PTR_ERR(dsi->clk_parent);
  1220. }
  1221. dsi->vdd = devm_regulator_get(&pdev->dev, "avdd-dsi-csi");
  1222. if (IS_ERR(dsi->vdd)) {
  1223. dev_err(&pdev->dev, "cannot get VDD supply\n");
  1224. return PTR_ERR(dsi->vdd);
  1225. }
  1226. err = tegra_dsi_setup_clocks(dsi);
  1227. if (err < 0) {
  1228. dev_err(&pdev->dev, "cannot setup clocks\n");
  1229. return err;
  1230. }
  1231. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1232. dsi->regs = devm_ioremap_resource(&pdev->dev, regs);
  1233. if (IS_ERR(dsi->regs))
  1234. return PTR_ERR(dsi->regs);
  1235. dsi->mipi = tegra_mipi_request(&pdev->dev);
  1236. if (IS_ERR(dsi->mipi))
  1237. return PTR_ERR(dsi->mipi);
  1238. dsi->host.ops = &tegra_dsi_host_ops;
  1239. dsi->host.dev = &pdev->dev;
  1240. err = mipi_dsi_host_register(&dsi->host);
  1241. if (err < 0) {
  1242. dev_err(&pdev->dev, "failed to register DSI host: %d\n", err);
  1243. goto mipi_free;
  1244. }
  1245. platform_set_drvdata(pdev, dsi);
  1246. pm_runtime_enable(&pdev->dev);
  1247. INIT_LIST_HEAD(&dsi->client.list);
  1248. dsi->client.ops = &dsi_client_ops;
  1249. dsi->client.dev = &pdev->dev;
  1250. err = host1x_client_register(&dsi->client);
  1251. if (err < 0) {
  1252. dev_err(&pdev->dev, "failed to register host1x client: %d\n",
  1253. err);
  1254. goto unregister;
  1255. }
  1256. return 0;
  1257. unregister:
  1258. mipi_dsi_host_unregister(&dsi->host);
  1259. mipi_free:
  1260. tegra_mipi_free(dsi->mipi);
  1261. return err;
  1262. }
  1263. static int tegra_dsi_remove(struct platform_device *pdev)
  1264. {
  1265. struct tegra_dsi *dsi = platform_get_drvdata(pdev);
  1266. int err;
  1267. pm_runtime_disable(&pdev->dev);
  1268. err = host1x_client_unregister(&dsi->client);
  1269. if (err < 0) {
  1270. dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
  1271. err);
  1272. return err;
  1273. }
  1274. tegra_output_remove(&dsi->output);
  1275. mipi_dsi_host_unregister(&dsi->host);
  1276. tegra_mipi_free(dsi->mipi);
  1277. return 0;
  1278. }
  1279. #ifdef CONFIG_PM
  1280. static int tegra_dsi_suspend(struct device *dev)
  1281. {
  1282. struct tegra_dsi *dsi = dev_get_drvdata(dev);
  1283. int err;
  1284. if (dsi->rst) {
  1285. err = reset_control_assert(dsi->rst);
  1286. if (err < 0) {
  1287. dev_err(dev, "failed to assert reset: %d\n", err);
  1288. return err;
  1289. }
  1290. }
  1291. usleep_range(1000, 2000);
  1292. clk_disable_unprepare(dsi->clk_lp);
  1293. clk_disable_unprepare(dsi->clk);
  1294. regulator_disable(dsi->vdd);
  1295. return 0;
  1296. }
  1297. static int tegra_dsi_resume(struct device *dev)
  1298. {
  1299. struct tegra_dsi *dsi = dev_get_drvdata(dev);
  1300. int err;
  1301. err = regulator_enable(dsi->vdd);
  1302. if (err < 0) {
  1303. dev_err(dsi->dev, "failed to enable VDD supply: %d\n", err);
  1304. return err;
  1305. }
  1306. err = clk_prepare_enable(dsi->clk);
  1307. if (err < 0) {
  1308. dev_err(dev, "cannot enable DSI clock: %d\n", err);
  1309. goto disable_vdd;
  1310. }
  1311. err = clk_prepare_enable(dsi->clk_lp);
  1312. if (err < 0) {
  1313. dev_err(dev, "cannot enable low-power clock: %d\n", err);
  1314. goto disable_clk;
  1315. }
  1316. usleep_range(1000, 2000);
  1317. if (dsi->rst) {
  1318. err = reset_control_deassert(dsi->rst);
  1319. if (err < 0) {
  1320. dev_err(dev, "cannot assert reset: %d\n", err);
  1321. goto disable_clk_lp;
  1322. }
  1323. }
  1324. return 0;
  1325. disable_clk_lp:
  1326. clk_disable_unprepare(dsi->clk_lp);
  1327. disable_clk:
  1328. clk_disable_unprepare(dsi->clk);
  1329. disable_vdd:
  1330. regulator_disable(dsi->vdd);
  1331. return err;
  1332. }
  1333. #endif
  1334. static const struct dev_pm_ops tegra_dsi_pm_ops = {
  1335. SET_RUNTIME_PM_OPS(tegra_dsi_suspend, tegra_dsi_resume, NULL)
  1336. };
  1337. static const struct of_device_id tegra_dsi_of_match[] = {
  1338. { .compatible = "nvidia,tegra210-dsi", },
  1339. { .compatible = "nvidia,tegra132-dsi", },
  1340. { .compatible = "nvidia,tegra124-dsi", },
  1341. { .compatible = "nvidia,tegra114-dsi", },
  1342. { },
  1343. };
  1344. MODULE_DEVICE_TABLE(of, tegra_dsi_of_match);
  1345. struct platform_driver tegra_dsi_driver = {
  1346. .driver = {
  1347. .name = "tegra-dsi",
  1348. .of_match_table = tegra_dsi_of_match,
  1349. .pm = &tegra_dsi_pm_ops,
  1350. },
  1351. .probe = tegra_dsi_probe,
  1352. .remove = tegra_dsi_remove,
  1353. };