dsi.c 40 KB

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