dsi.c 42 KB

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