dw-mipi-dsi.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349
  1. /*
  2. * Copyright (c) 2016, Fuzhou Rockchip Electronics Co., Ltd
  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 as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/component.h>
  11. #include <linux/iopoll.h>
  12. #include <linux/math64.h>
  13. #include <linux/module.h>
  14. #include <linux/of_device.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/regmap.h>
  17. #include <linux/reset.h>
  18. #include <linux/mfd/syscon.h>
  19. #include <drm/drm_atomic_helper.h>
  20. #include <drm/drm_crtc.h>
  21. #include <drm/drm_crtc_helper.h>
  22. #include <drm/drm_mipi_dsi.h>
  23. #include <drm/drm_of.h>
  24. #include <drm/drm_panel.h>
  25. #include <drm/drmP.h>
  26. #include <video/mipi_display.h>
  27. #include "rockchip_drm_drv.h"
  28. #include "rockchip_drm_vop.h"
  29. #define DRIVER_NAME "dw-mipi-dsi"
  30. #define RK3288_GRF_SOC_CON6 0x025c
  31. #define RK3288_DSI0_SEL_VOP_LIT BIT(6)
  32. #define RK3288_DSI1_SEL_VOP_LIT BIT(9)
  33. #define RK3399_GRF_SOC_CON20 0x6250
  34. #define RK3399_DSI0_SEL_VOP_LIT BIT(0)
  35. #define RK3399_DSI1_SEL_VOP_LIT BIT(4)
  36. /* disable turnrequest, turndisable, forcetxstopmode, forcerxmode */
  37. #define RK3399_GRF_SOC_CON22 0x6258
  38. #define RK3399_GRF_DSI_MODE 0xffff0000
  39. #define DSI_VERSION 0x00
  40. #define DSI_PWR_UP 0x04
  41. #define RESET 0
  42. #define POWERUP BIT(0)
  43. #define DSI_CLKMGR_CFG 0x08
  44. #define TO_CLK_DIVIDSION(div) (((div) & 0xff) << 8)
  45. #define TX_ESC_CLK_DIVIDSION(div) (((div) & 0xff) << 0)
  46. #define DSI_DPI_VCID 0x0c
  47. #define DPI_VID(vid) (((vid) & 0x3) << 0)
  48. #define DSI_DPI_COLOR_CODING 0x10
  49. #define EN18_LOOSELY BIT(8)
  50. #define DPI_COLOR_CODING_16BIT_1 0x0
  51. #define DPI_COLOR_CODING_16BIT_2 0x1
  52. #define DPI_COLOR_CODING_16BIT_3 0x2
  53. #define DPI_COLOR_CODING_18BIT_1 0x3
  54. #define DPI_COLOR_CODING_18BIT_2 0x4
  55. #define DPI_COLOR_CODING_24BIT 0x5
  56. #define DSI_DPI_CFG_POL 0x14
  57. #define COLORM_ACTIVE_LOW BIT(4)
  58. #define SHUTD_ACTIVE_LOW BIT(3)
  59. #define HSYNC_ACTIVE_LOW BIT(2)
  60. #define VSYNC_ACTIVE_LOW BIT(1)
  61. #define DATAEN_ACTIVE_LOW BIT(0)
  62. #define DSI_DPI_LP_CMD_TIM 0x18
  63. #define OUTVACT_LPCMD_TIME(p) (((p) & 0xff) << 16)
  64. #define INVACT_LPCMD_TIME(p) ((p) & 0xff)
  65. #define DSI_DBI_CFG 0x20
  66. #define DSI_DBI_CMDSIZE 0x28
  67. #define DSI_PCKHDL_CFG 0x2c
  68. #define EN_CRC_RX BIT(4)
  69. #define EN_ECC_RX BIT(3)
  70. #define EN_BTA BIT(2)
  71. #define EN_EOTP_RX BIT(1)
  72. #define EN_EOTP_TX BIT(0)
  73. #define DSI_MODE_CFG 0x34
  74. #define ENABLE_VIDEO_MODE 0
  75. #define ENABLE_CMD_MODE BIT(0)
  76. #define DSI_VID_MODE_CFG 0x38
  77. #define FRAME_BTA_ACK BIT(14)
  78. #define ENABLE_LOW_POWER (0x3f << 8)
  79. #define ENABLE_LOW_POWER_MASK (0x3f << 8)
  80. #define VID_MODE_TYPE_NON_BURST_SYNC_PULSES 0x0
  81. #define VID_MODE_TYPE_NON_BURST_SYNC_EVENTS 0x1
  82. #define VID_MODE_TYPE_BURST 0x2
  83. #define VID_MODE_TYPE_MASK 0x3
  84. #define DSI_VID_PKT_SIZE 0x3c
  85. #define VID_PKT_SIZE(p) (((p) & 0x3fff) << 0)
  86. #define VID_PKT_MAX_SIZE 0x3fff
  87. #define DSI_VID_HSA_TIME 0x48
  88. #define DSI_VID_HBP_TIME 0x4c
  89. #define DSI_VID_HLINE_TIME 0x50
  90. #define DSI_VID_VSA_LINES 0x54
  91. #define DSI_VID_VBP_LINES 0x58
  92. #define DSI_VID_VFP_LINES 0x5c
  93. #define DSI_VID_VACTIVE_LINES 0x60
  94. #define DSI_CMD_MODE_CFG 0x68
  95. #define MAX_RD_PKT_SIZE_LP BIT(24)
  96. #define DCS_LW_TX_LP BIT(19)
  97. #define DCS_SR_0P_TX_LP BIT(18)
  98. #define DCS_SW_1P_TX_LP BIT(17)
  99. #define DCS_SW_0P_TX_LP BIT(16)
  100. #define GEN_LW_TX_LP BIT(14)
  101. #define GEN_SR_2P_TX_LP BIT(13)
  102. #define GEN_SR_1P_TX_LP BIT(12)
  103. #define GEN_SR_0P_TX_LP BIT(11)
  104. #define GEN_SW_2P_TX_LP BIT(10)
  105. #define GEN_SW_1P_TX_LP BIT(9)
  106. #define GEN_SW_0P_TX_LP BIT(8)
  107. #define EN_ACK_RQST BIT(1)
  108. #define EN_TEAR_FX BIT(0)
  109. #define CMD_MODE_ALL_LP (MAX_RD_PKT_SIZE_LP | \
  110. DCS_LW_TX_LP | \
  111. DCS_SR_0P_TX_LP | \
  112. DCS_SW_1P_TX_LP | \
  113. DCS_SW_0P_TX_LP | \
  114. GEN_LW_TX_LP | \
  115. GEN_SR_2P_TX_LP | \
  116. GEN_SR_1P_TX_LP | \
  117. GEN_SR_0P_TX_LP | \
  118. GEN_SW_2P_TX_LP | \
  119. GEN_SW_1P_TX_LP | \
  120. GEN_SW_0P_TX_LP)
  121. #define DSI_GEN_HDR 0x6c
  122. #define GEN_HDATA(data) (((data) & 0xffff) << 8)
  123. #define GEN_HDATA_MASK (0xffff << 8)
  124. #define GEN_HTYPE(type) (((type) & 0xff) << 0)
  125. #define GEN_HTYPE_MASK 0xff
  126. #define DSI_GEN_PLD_DATA 0x70
  127. #define DSI_CMD_PKT_STATUS 0x74
  128. #define GEN_CMD_EMPTY BIT(0)
  129. #define GEN_CMD_FULL BIT(1)
  130. #define GEN_PLD_W_EMPTY BIT(2)
  131. #define GEN_PLD_W_FULL BIT(3)
  132. #define GEN_PLD_R_EMPTY BIT(4)
  133. #define GEN_PLD_R_FULL BIT(5)
  134. #define GEN_RD_CMD_BUSY BIT(6)
  135. #define DSI_TO_CNT_CFG 0x78
  136. #define HSTX_TO_CNT(p) (((p) & 0xffff) << 16)
  137. #define LPRX_TO_CNT(p) ((p) & 0xffff)
  138. #define DSI_BTA_TO_CNT 0x8c
  139. #define DSI_LPCLK_CTRL 0x94
  140. #define AUTO_CLKLANE_CTRL BIT(1)
  141. #define PHY_TXREQUESTCLKHS BIT(0)
  142. #define DSI_PHY_TMR_LPCLK_CFG 0x98
  143. #define PHY_CLKHS2LP_TIME(lbcc) (((lbcc) & 0x3ff) << 16)
  144. #define PHY_CLKLP2HS_TIME(lbcc) ((lbcc) & 0x3ff)
  145. #define DSI_PHY_TMR_CFG 0x9c
  146. #define PHY_HS2LP_TIME(lbcc) (((lbcc) & 0xff) << 24)
  147. #define PHY_LP2HS_TIME(lbcc) (((lbcc) & 0xff) << 16)
  148. #define MAX_RD_TIME(lbcc) ((lbcc) & 0x7fff)
  149. #define DSI_PHY_RSTZ 0xa0
  150. #define PHY_DISFORCEPLL 0
  151. #define PHY_ENFORCEPLL BIT(3)
  152. #define PHY_DISABLECLK 0
  153. #define PHY_ENABLECLK BIT(2)
  154. #define PHY_RSTZ 0
  155. #define PHY_UNRSTZ BIT(1)
  156. #define PHY_SHUTDOWNZ 0
  157. #define PHY_UNSHUTDOWNZ BIT(0)
  158. #define DSI_PHY_IF_CFG 0xa4
  159. #define N_LANES(n) ((((n) - 1) & 0x3) << 0)
  160. #define PHY_STOP_WAIT_TIME(cycle) (((cycle) & 0xff) << 8)
  161. #define DSI_PHY_STATUS 0xb0
  162. #define LOCK BIT(0)
  163. #define STOP_STATE_CLK_LANE BIT(2)
  164. #define DSI_PHY_TST_CTRL0 0xb4
  165. #define PHY_TESTCLK BIT(1)
  166. #define PHY_UNTESTCLK 0
  167. #define PHY_TESTCLR BIT(0)
  168. #define PHY_UNTESTCLR 0
  169. #define DSI_PHY_TST_CTRL1 0xb8
  170. #define PHY_TESTEN BIT(16)
  171. #define PHY_UNTESTEN 0
  172. #define PHY_TESTDOUT(n) (((n) & 0xff) << 8)
  173. #define PHY_TESTDIN(n) (((n) & 0xff) << 0)
  174. #define DSI_INT_ST0 0xbc
  175. #define DSI_INT_ST1 0xc0
  176. #define DSI_INT_MSK0 0xc4
  177. #define DSI_INT_MSK1 0xc8
  178. #define PHY_STATUS_TIMEOUT_US 10000
  179. #define CMD_PKT_STATUS_TIMEOUT_US 20000
  180. #define BYPASS_VCO_RANGE BIT(7)
  181. #define VCO_RANGE_CON_SEL(val) (((val) & 0x7) << 3)
  182. #define VCO_IN_CAP_CON_DEFAULT (0x0 << 1)
  183. #define VCO_IN_CAP_CON_LOW (0x1 << 1)
  184. #define VCO_IN_CAP_CON_HIGH (0x2 << 1)
  185. #define REF_BIAS_CUR_SEL BIT(0)
  186. #define CP_CURRENT_3MA BIT(3)
  187. #define CP_PROGRAM_EN BIT(7)
  188. #define LPF_PROGRAM_EN BIT(6)
  189. #define LPF_RESISTORS_20_KOHM 0
  190. #define HSFREQRANGE_SEL(val) (((val) & 0x3f) << 1)
  191. #define INPUT_DIVIDER(val) (((val) - 1) & 0x7f)
  192. #define LOW_PROGRAM_EN 0
  193. #define HIGH_PROGRAM_EN BIT(7)
  194. #define LOOP_DIV_LOW_SEL(val) (((val) - 1) & 0x1f)
  195. #define LOOP_DIV_HIGH_SEL(val) ((((val) - 1) >> 5) & 0x1f)
  196. #define PLL_LOOP_DIV_EN BIT(5)
  197. #define PLL_INPUT_DIV_EN BIT(4)
  198. #define POWER_CONTROL BIT(6)
  199. #define INTERNAL_REG_CURRENT BIT(3)
  200. #define BIAS_BLOCK_ON BIT(2)
  201. #define BANDGAP_ON BIT(0)
  202. #define TER_RESISTOR_HIGH BIT(7)
  203. #define TER_RESISTOR_LOW 0
  204. #define LEVEL_SHIFTERS_ON BIT(6)
  205. #define TER_CAL_DONE BIT(5)
  206. #define SETRD_MAX (0x7 << 2)
  207. #define POWER_MANAGE BIT(1)
  208. #define TER_RESISTORS_ON BIT(0)
  209. #define BIASEXTR_SEL(val) ((val) & 0x7)
  210. #define BANDGAP_SEL(val) ((val) & 0x7)
  211. #define TLP_PROGRAM_EN BIT(7)
  212. #define THS_PRE_PROGRAM_EN BIT(7)
  213. #define THS_ZERO_PROGRAM_EN BIT(6)
  214. #define DW_MIPI_NEEDS_PHY_CFG_CLK BIT(0)
  215. #define DW_MIPI_NEEDS_GRF_CLK BIT(1)
  216. enum {
  217. BANDGAP_97_07,
  218. BANDGAP_98_05,
  219. BANDGAP_99_02,
  220. BANDGAP_100_00,
  221. BANDGAP_93_17,
  222. BANDGAP_94_15,
  223. BANDGAP_95_12,
  224. BANDGAP_96_10,
  225. };
  226. enum {
  227. BIASEXTR_87_1,
  228. BIASEXTR_91_5,
  229. BIASEXTR_95_9,
  230. BIASEXTR_100,
  231. BIASEXTR_105_94,
  232. BIASEXTR_111_88,
  233. BIASEXTR_118_8,
  234. BIASEXTR_127_7,
  235. };
  236. struct dw_mipi_dsi_plat_data {
  237. u32 dsi0_en_bit;
  238. u32 dsi1_en_bit;
  239. u32 grf_switch_reg;
  240. u32 grf_dsi0_mode;
  241. u32 grf_dsi0_mode_reg;
  242. unsigned int flags;
  243. unsigned int max_data_lanes;
  244. };
  245. struct dw_mipi_dsi {
  246. struct drm_encoder encoder;
  247. struct drm_connector connector;
  248. struct mipi_dsi_host dsi_host;
  249. struct drm_panel *panel;
  250. struct device *dev;
  251. struct regmap *grf_regmap;
  252. void __iomem *base;
  253. struct clk *grf_clk;
  254. struct clk *pllref_clk;
  255. struct clk *pclk;
  256. struct clk *phy_cfg_clk;
  257. int dpms_mode;
  258. unsigned int lane_mbps; /* per lane */
  259. u32 channel;
  260. u32 lanes;
  261. u32 format;
  262. u16 input_div;
  263. u16 feedback_div;
  264. unsigned long mode_flags;
  265. const struct dw_mipi_dsi_plat_data *pdata;
  266. };
  267. enum dw_mipi_dsi_mode {
  268. DW_MIPI_DSI_CMD_MODE,
  269. DW_MIPI_DSI_VID_MODE,
  270. };
  271. struct dphy_pll_testdin_map {
  272. unsigned int max_mbps;
  273. u8 testdin;
  274. };
  275. /* The table is based on 27MHz DPHY pll reference clock. */
  276. static const struct dphy_pll_testdin_map dptdin_map[] = {
  277. { 90, 0x00}, { 100, 0x10}, { 110, 0x20}, { 130, 0x01},
  278. { 140, 0x11}, { 150, 0x21}, { 170, 0x02}, { 180, 0x12},
  279. { 200, 0x22}, { 220, 0x03}, { 240, 0x13}, { 250, 0x23},
  280. { 270, 0x04}, { 300, 0x14}, { 330, 0x05}, { 360, 0x15},
  281. { 400, 0x25}, { 450, 0x06}, { 500, 0x16}, { 550, 0x07},
  282. { 600, 0x17}, { 650, 0x08}, { 700, 0x18}, { 750, 0x09},
  283. { 800, 0x19}, { 850, 0x29}, { 900, 0x39}, { 950, 0x0a},
  284. {1000, 0x1a}, {1050, 0x2a}, {1100, 0x3a}, {1150, 0x0b},
  285. {1200, 0x1b}, {1250, 0x2b}, {1300, 0x3b}, {1350, 0x0c},
  286. {1400, 0x1c}, {1450, 0x2c}, {1500, 0x3c}
  287. };
  288. static int max_mbps_to_testdin(unsigned int max_mbps)
  289. {
  290. int i;
  291. for (i = 0; i < ARRAY_SIZE(dptdin_map); i++)
  292. if (dptdin_map[i].max_mbps > max_mbps)
  293. return dptdin_map[i].testdin;
  294. return -EINVAL;
  295. }
  296. /*
  297. * The controller should generate 2 frames before
  298. * preparing the peripheral.
  299. */
  300. static void dw_mipi_dsi_wait_for_two_frames(struct drm_display_mode *mode)
  301. {
  302. int refresh, two_frames;
  303. refresh = drm_mode_vrefresh(mode);
  304. two_frames = DIV_ROUND_UP(MSEC_PER_SEC, refresh) * 2;
  305. msleep(two_frames);
  306. }
  307. static inline struct dw_mipi_dsi *host_to_dsi(struct mipi_dsi_host *host)
  308. {
  309. return container_of(host, struct dw_mipi_dsi, dsi_host);
  310. }
  311. static inline struct dw_mipi_dsi *con_to_dsi(struct drm_connector *con)
  312. {
  313. return container_of(con, struct dw_mipi_dsi, connector);
  314. }
  315. static inline struct dw_mipi_dsi *encoder_to_dsi(struct drm_encoder *encoder)
  316. {
  317. return container_of(encoder, struct dw_mipi_dsi, encoder);
  318. }
  319. static inline void dsi_write(struct dw_mipi_dsi *dsi, u32 reg, u32 val)
  320. {
  321. writel(val, dsi->base + reg);
  322. }
  323. static inline u32 dsi_read(struct dw_mipi_dsi *dsi, u32 reg)
  324. {
  325. return readl(dsi->base + reg);
  326. }
  327. static void dw_mipi_dsi_phy_write(struct dw_mipi_dsi *dsi, u8 test_code,
  328. u8 test_data)
  329. {
  330. /*
  331. * With the falling edge on TESTCLK, the TESTDIN[7:0] signal content
  332. * is latched internally as the current test code. Test data is
  333. * programmed internally by rising edge on TESTCLK.
  334. */
  335. dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_TESTCLK | PHY_UNTESTCLR);
  336. dsi_write(dsi, DSI_PHY_TST_CTRL1, PHY_TESTEN | PHY_TESTDOUT(0) |
  337. PHY_TESTDIN(test_code));
  338. dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_UNTESTCLK | PHY_UNTESTCLR);
  339. dsi_write(dsi, DSI_PHY_TST_CTRL1, PHY_UNTESTEN | PHY_TESTDOUT(0) |
  340. PHY_TESTDIN(test_data));
  341. dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_TESTCLK | PHY_UNTESTCLR);
  342. }
  343. /**
  344. * ns2bc - Nanoseconds to byte clock cycles
  345. */
  346. static inline unsigned int ns2bc(struct dw_mipi_dsi *dsi, int ns)
  347. {
  348. return DIV_ROUND_UP(ns * dsi->lane_mbps / 8, 1000);
  349. }
  350. /**
  351. * ns2ui - Nanoseconds to UI time periods
  352. */
  353. static inline unsigned int ns2ui(struct dw_mipi_dsi *dsi, int ns)
  354. {
  355. return DIV_ROUND_UP(ns * dsi->lane_mbps, 1000);
  356. }
  357. static int dw_mipi_dsi_phy_init(struct dw_mipi_dsi *dsi)
  358. {
  359. int ret, testdin, vco, val;
  360. vco = (dsi->lane_mbps < 200) ? 0 : (dsi->lane_mbps + 100) / 200;
  361. testdin = max_mbps_to_testdin(dsi->lane_mbps);
  362. if (testdin < 0) {
  363. DRM_DEV_ERROR(dsi->dev,
  364. "failed to get testdin for %dmbps lane clock\n",
  365. dsi->lane_mbps);
  366. return testdin;
  367. }
  368. /* Start by clearing PHY state */
  369. dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_UNTESTCLR);
  370. dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_TESTCLR);
  371. dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_UNTESTCLR);
  372. ret = clk_prepare_enable(dsi->phy_cfg_clk);
  373. if (ret) {
  374. DRM_DEV_ERROR(dsi->dev, "Failed to enable phy_cfg_clk\n");
  375. return ret;
  376. }
  377. dw_mipi_dsi_phy_write(dsi, 0x10, BYPASS_VCO_RANGE |
  378. VCO_RANGE_CON_SEL(vco) |
  379. VCO_IN_CAP_CON_LOW |
  380. REF_BIAS_CUR_SEL);
  381. dw_mipi_dsi_phy_write(dsi, 0x11, CP_CURRENT_3MA);
  382. dw_mipi_dsi_phy_write(dsi, 0x12, CP_PROGRAM_EN | LPF_PROGRAM_EN |
  383. LPF_RESISTORS_20_KOHM);
  384. dw_mipi_dsi_phy_write(dsi, 0x44, HSFREQRANGE_SEL(testdin));
  385. dw_mipi_dsi_phy_write(dsi, 0x17, INPUT_DIVIDER(dsi->input_div));
  386. dw_mipi_dsi_phy_write(dsi, 0x18, LOOP_DIV_LOW_SEL(dsi->feedback_div) |
  387. LOW_PROGRAM_EN);
  388. dw_mipi_dsi_phy_write(dsi, 0x18, LOOP_DIV_HIGH_SEL(dsi->feedback_div) |
  389. HIGH_PROGRAM_EN);
  390. dw_mipi_dsi_phy_write(dsi, 0x19, PLL_LOOP_DIV_EN | PLL_INPUT_DIV_EN);
  391. dw_mipi_dsi_phy_write(dsi, 0x22, LOW_PROGRAM_EN |
  392. BIASEXTR_SEL(BIASEXTR_127_7));
  393. dw_mipi_dsi_phy_write(dsi, 0x22, HIGH_PROGRAM_EN |
  394. BANDGAP_SEL(BANDGAP_96_10));
  395. dw_mipi_dsi_phy_write(dsi, 0x20, POWER_CONTROL | INTERNAL_REG_CURRENT |
  396. BIAS_BLOCK_ON | BANDGAP_ON);
  397. dw_mipi_dsi_phy_write(dsi, 0x21, TER_RESISTOR_LOW | TER_CAL_DONE |
  398. SETRD_MAX | TER_RESISTORS_ON);
  399. dw_mipi_dsi_phy_write(dsi, 0x21, TER_RESISTOR_HIGH | LEVEL_SHIFTERS_ON |
  400. SETRD_MAX | POWER_MANAGE |
  401. TER_RESISTORS_ON);
  402. dw_mipi_dsi_phy_write(dsi, 0x60, TLP_PROGRAM_EN | ns2bc(dsi, 500));
  403. dw_mipi_dsi_phy_write(dsi, 0x61, THS_PRE_PROGRAM_EN | ns2ui(dsi, 40));
  404. dw_mipi_dsi_phy_write(dsi, 0x62, THS_ZERO_PROGRAM_EN | ns2bc(dsi, 300));
  405. dw_mipi_dsi_phy_write(dsi, 0x63, THS_PRE_PROGRAM_EN | ns2ui(dsi, 100));
  406. dw_mipi_dsi_phy_write(dsi, 0x64, BIT(5) | ns2bc(dsi, 100));
  407. dw_mipi_dsi_phy_write(dsi, 0x65, BIT(5) | (ns2bc(dsi, 60) + 7));
  408. dw_mipi_dsi_phy_write(dsi, 0x70, TLP_PROGRAM_EN | ns2bc(dsi, 500));
  409. dw_mipi_dsi_phy_write(dsi, 0x71,
  410. THS_PRE_PROGRAM_EN | (ns2ui(dsi, 50) + 5));
  411. dw_mipi_dsi_phy_write(dsi, 0x72,
  412. THS_ZERO_PROGRAM_EN | (ns2bc(dsi, 140) + 2));
  413. dw_mipi_dsi_phy_write(dsi, 0x73,
  414. THS_PRE_PROGRAM_EN | (ns2ui(dsi, 60) + 8));
  415. dw_mipi_dsi_phy_write(dsi, 0x74, BIT(5) | ns2bc(dsi, 100));
  416. dsi_write(dsi, DSI_PHY_RSTZ, PHY_ENFORCEPLL | PHY_ENABLECLK |
  417. PHY_UNRSTZ | PHY_UNSHUTDOWNZ);
  418. ret = readl_poll_timeout(dsi->base + DSI_PHY_STATUS,
  419. val, val & LOCK, 1000, PHY_STATUS_TIMEOUT_US);
  420. if (ret < 0) {
  421. DRM_DEV_ERROR(dsi->dev, "failed to wait for phy lock state\n");
  422. goto phy_init_end;
  423. }
  424. ret = readl_poll_timeout(dsi->base + DSI_PHY_STATUS,
  425. val, val & STOP_STATE_CLK_LANE, 1000,
  426. PHY_STATUS_TIMEOUT_US);
  427. if (ret < 0)
  428. DRM_DEV_ERROR(dsi->dev,
  429. "failed to wait for phy clk lane stop state\n");
  430. phy_init_end:
  431. clk_disable_unprepare(dsi->phy_cfg_clk);
  432. return ret;
  433. }
  434. static int dw_mipi_dsi_get_lane_bps(struct dw_mipi_dsi *dsi,
  435. struct drm_display_mode *mode)
  436. {
  437. unsigned int i, pre;
  438. unsigned long mpclk, pllref, tmp;
  439. unsigned int m = 1, n = 1, target_mbps = 1000;
  440. unsigned int max_mbps = dptdin_map[ARRAY_SIZE(dptdin_map) - 1].max_mbps;
  441. int bpp;
  442. bpp = mipi_dsi_pixel_format_to_bpp(dsi->format);
  443. if (bpp < 0) {
  444. DRM_DEV_ERROR(dsi->dev,
  445. "failed to get bpp for pixel format %d\n",
  446. dsi->format);
  447. return bpp;
  448. }
  449. mpclk = DIV_ROUND_UP(mode->clock, MSEC_PER_SEC);
  450. if (mpclk) {
  451. /* take 1 / 0.8, since mbps must big than bandwidth of RGB */
  452. tmp = mpclk * (bpp / dsi->lanes) * 10 / 8;
  453. if (tmp < max_mbps)
  454. target_mbps = tmp;
  455. else
  456. DRM_DEV_ERROR(dsi->dev,
  457. "DPHY clock frequency is out of range\n");
  458. }
  459. pllref = DIV_ROUND_UP(clk_get_rate(dsi->pllref_clk), USEC_PER_SEC);
  460. tmp = pllref;
  461. /*
  462. * The limits on the PLL divisor are:
  463. *
  464. * 5MHz <= (pllref / n) <= 40MHz
  465. *
  466. * we walk over these values in descreasing order so that if we hit
  467. * an exact match for target_mbps it is more likely that "m" will be
  468. * even.
  469. *
  470. * TODO: ensure that "m" is even after this loop.
  471. */
  472. for (i = pllref / 5; i > (pllref / 40); i--) {
  473. pre = pllref / i;
  474. if ((tmp > (target_mbps % pre)) && (target_mbps / pre < 512)) {
  475. tmp = target_mbps % pre;
  476. n = i;
  477. m = target_mbps / pre;
  478. }
  479. if (tmp == 0)
  480. break;
  481. }
  482. dsi->lane_mbps = pllref / n * m;
  483. dsi->input_div = n;
  484. dsi->feedback_div = m;
  485. return 0;
  486. }
  487. static int dw_mipi_dsi_host_attach(struct mipi_dsi_host *host,
  488. struct mipi_dsi_device *device)
  489. {
  490. struct dw_mipi_dsi *dsi = host_to_dsi(host);
  491. if (device->lanes > dsi->pdata->max_data_lanes) {
  492. DRM_DEV_ERROR(dsi->dev,
  493. "the number of data lanes(%u) is too many\n",
  494. device->lanes);
  495. return -EINVAL;
  496. }
  497. dsi->lanes = device->lanes;
  498. dsi->channel = device->channel;
  499. dsi->format = device->format;
  500. dsi->mode_flags = device->mode_flags;
  501. dsi->panel = of_drm_find_panel(device->dev.of_node);
  502. if (dsi->panel)
  503. return drm_panel_attach(dsi->panel, &dsi->connector);
  504. return -EINVAL;
  505. }
  506. static int dw_mipi_dsi_host_detach(struct mipi_dsi_host *host,
  507. struct mipi_dsi_device *device)
  508. {
  509. struct dw_mipi_dsi *dsi = host_to_dsi(host);
  510. drm_panel_detach(dsi->panel);
  511. return 0;
  512. }
  513. static void dw_mipi_message_config(struct dw_mipi_dsi *dsi,
  514. const struct mipi_dsi_msg *msg)
  515. {
  516. bool lpm = msg->flags & MIPI_DSI_MSG_USE_LPM;
  517. u32 val = 0;
  518. if (msg->flags & MIPI_DSI_MSG_REQ_ACK)
  519. val |= EN_ACK_RQST;
  520. if (lpm)
  521. val |= CMD_MODE_ALL_LP;
  522. dsi_write(dsi, DSI_LPCLK_CTRL, lpm ? 0 : PHY_TXREQUESTCLKHS);
  523. dsi_write(dsi, DSI_CMD_MODE_CFG, val);
  524. }
  525. static int dw_mipi_dsi_gen_pkt_hdr_write(struct dw_mipi_dsi *dsi, u32 hdr_val)
  526. {
  527. int ret;
  528. u32 val, mask;
  529. ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
  530. val, !(val & GEN_CMD_FULL), 1000,
  531. CMD_PKT_STATUS_TIMEOUT_US);
  532. if (ret < 0) {
  533. DRM_DEV_ERROR(dsi->dev,
  534. "failed to get available command FIFO\n");
  535. return ret;
  536. }
  537. dsi_write(dsi, DSI_GEN_HDR, hdr_val);
  538. mask = GEN_CMD_EMPTY | GEN_PLD_W_EMPTY;
  539. ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
  540. val, (val & mask) == mask,
  541. 1000, CMD_PKT_STATUS_TIMEOUT_US);
  542. if (ret < 0) {
  543. DRM_DEV_ERROR(dsi->dev, "failed to write command FIFO\n");
  544. return ret;
  545. }
  546. return 0;
  547. }
  548. static int dw_mipi_dsi_dcs_short_write(struct dw_mipi_dsi *dsi,
  549. const struct mipi_dsi_msg *msg)
  550. {
  551. const u8 *tx_buf = msg->tx_buf;
  552. u16 data = 0;
  553. u32 val;
  554. if (msg->tx_len > 0)
  555. data |= tx_buf[0];
  556. if (msg->tx_len > 1)
  557. data |= tx_buf[1] << 8;
  558. if (msg->tx_len > 2) {
  559. DRM_DEV_ERROR(dsi->dev,
  560. "too long tx buf length %zu for short write\n",
  561. msg->tx_len);
  562. return -EINVAL;
  563. }
  564. val = GEN_HDATA(data) | GEN_HTYPE(msg->type);
  565. return dw_mipi_dsi_gen_pkt_hdr_write(dsi, val);
  566. }
  567. static int dw_mipi_dsi_dcs_long_write(struct dw_mipi_dsi *dsi,
  568. const struct mipi_dsi_msg *msg)
  569. {
  570. const u8 *tx_buf = msg->tx_buf;
  571. int len = msg->tx_len, pld_data_bytes = sizeof(u32), ret;
  572. u32 hdr_val = GEN_HDATA(msg->tx_len) | GEN_HTYPE(msg->type);
  573. u32 remainder;
  574. u32 val;
  575. if (msg->tx_len < 3) {
  576. DRM_DEV_ERROR(dsi->dev,
  577. "wrong tx buf length %zu for long write\n",
  578. msg->tx_len);
  579. return -EINVAL;
  580. }
  581. while (DIV_ROUND_UP(len, pld_data_bytes)) {
  582. if (len < pld_data_bytes) {
  583. remainder = 0;
  584. memcpy(&remainder, tx_buf, len);
  585. dsi_write(dsi, DSI_GEN_PLD_DATA, remainder);
  586. len = 0;
  587. } else {
  588. memcpy(&remainder, tx_buf, pld_data_bytes);
  589. dsi_write(dsi, DSI_GEN_PLD_DATA, remainder);
  590. tx_buf += pld_data_bytes;
  591. len -= pld_data_bytes;
  592. }
  593. ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
  594. val, !(val & GEN_PLD_W_FULL), 1000,
  595. CMD_PKT_STATUS_TIMEOUT_US);
  596. if (ret < 0) {
  597. DRM_DEV_ERROR(dsi->dev,
  598. "failed to get available write payload FIFO\n");
  599. return ret;
  600. }
  601. }
  602. return dw_mipi_dsi_gen_pkt_hdr_write(dsi, hdr_val);
  603. }
  604. static ssize_t dw_mipi_dsi_host_transfer(struct mipi_dsi_host *host,
  605. const struct mipi_dsi_msg *msg)
  606. {
  607. struct dw_mipi_dsi *dsi = host_to_dsi(host);
  608. int ret;
  609. dw_mipi_message_config(dsi, msg);
  610. switch (msg->type) {
  611. case MIPI_DSI_DCS_SHORT_WRITE:
  612. case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
  613. case MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE:
  614. ret = dw_mipi_dsi_dcs_short_write(dsi, msg);
  615. break;
  616. case MIPI_DSI_DCS_LONG_WRITE:
  617. ret = dw_mipi_dsi_dcs_long_write(dsi, msg);
  618. break;
  619. default:
  620. DRM_DEV_ERROR(dsi->dev, "unsupported message type 0x%02x\n",
  621. msg->type);
  622. ret = -EINVAL;
  623. }
  624. return ret;
  625. }
  626. static const struct mipi_dsi_host_ops dw_mipi_dsi_host_ops = {
  627. .attach = dw_mipi_dsi_host_attach,
  628. .detach = dw_mipi_dsi_host_detach,
  629. .transfer = dw_mipi_dsi_host_transfer,
  630. };
  631. static void dw_mipi_dsi_video_mode_config(struct dw_mipi_dsi *dsi)
  632. {
  633. u32 val;
  634. val = ENABLE_LOW_POWER;
  635. if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
  636. val |= VID_MODE_TYPE_BURST;
  637. else if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
  638. val |= VID_MODE_TYPE_NON_BURST_SYNC_PULSES;
  639. else
  640. val |= VID_MODE_TYPE_NON_BURST_SYNC_EVENTS;
  641. dsi_write(dsi, DSI_VID_MODE_CFG, val);
  642. }
  643. static void dw_mipi_dsi_set_mode(struct dw_mipi_dsi *dsi,
  644. enum dw_mipi_dsi_mode mode)
  645. {
  646. if (mode == DW_MIPI_DSI_CMD_MODE) {
  647. dsi_write(dsi, DSI_PWR_UP, RESET);
  648. dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
  649. dsi_write(dsi, DSI_PWR_UP, POWERUP);
  650. } else {
  651. dsi_write(dsi, DSI_PWR_UP, RESET);
  652. dsi_write(dsi, DSI_MODE_CFG, ENABLE_VIDEO_MODE);
  653. dw_mipi_dsi_video_mode_config(dsi);
  654. dsi_write(dsi, DSI_LPCLK_CTRL, PHY_TXREQUESTCLKHS);
  655. dsi_write(dsi, DSI_PWR_UP, POWERUP);
  656. }
  657. }
  658. static void dw_mipi_dsi_disable(struct dw_mipi_dsi *dsi)
  659. {
  660. dsi_write(dsi, DSI_PWR_UP, RESET);
  661. dsi_write(dsi, DSI_PHY_RSTZ, PHY_RSTZ);
  662. }
  663. static void dw_mipi_dsi_init(struct dw_mipi_dsi *dsi)
  664. {
  665. /*
  666. * The maximum permitted escape clock is 20MHz and it is derived from
  667. * lanebyteclk, which is running at "lane_mbps / 8". Thus we want:
  668. *
  669. * (lane_mbps >> 3) / esc_clk_division < 20
  670. * which is:
  671. * (lane_mbps >> 3) / 20 > esc_clk_division
  672. */
  673. u32 esc_clk_division = (dsi->lane_mbps >> 3) / 20 + 1;
  674. dsi_write(dsi, DSI_PWR_UP, RESET);
  675. dsi_write(dsi, DSI_PHY_RSTZ, PHY_DISFORCEPLL | PHY_DISABLECLK
  676. | PHY_RSTZ | PHY_SHUTDOWNZ);
  677. dsi_write(dsi, DSI_CLKMGR_CFG, TO_CLK_DIVIDSION(10) |
  678. TX_ESC_CLK_DIVIDSION(esc_clk_division));
  679. }
  680. static void dw_mipi_dsi_dpi_config(struct dw_mipi_dsi *dsi,
  681. struct drm_display_mode *mode)
  682. {
  683. u32 val = 0, color = 0;
  684. switch (dsi->format) {
  685. case MIPI_DSI_FMT_RGB888:
  686. color = DPI_COLOR_CODING_24BIT;
  687. break;
  688. case MIPI_DSI_FMT_RGB666:
  689. color = DPI_COLOR_CODING_18BIT_2 | EN18_LOOSELY;
  690. break;
  691. case MIPI_DSI_FMT_RGB666_PACKED:
  692. color = DPI_COLOR_CODING_18BIT_1;
  693. break;
  694. case MIPI_DSI_FMT_RGB565:
  695. color = DPI_COLOR_CODING_16BIT_1;
  696. break;
  697. }
  698. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  699. val |= VSYNC_ACTIVE_LOW;
  700. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  701. val |= HSYNC_ACTIVE_LOW;
  702. dsi_write(dsi, DSI_DPI_VCID, DPI_VID(dsi->channel));
  703. dsi_write(dsi, DSI_DPI_COLOR_CODING, color);
  704. dsi_write(dsi, DSI_DPI_CFG_POL, val);
  705. dsi_write(dsi, DSI_DPI_LP_CMD_TIM, OUTVACT_LPCMD_TIME(4)
  706. | INVACT_LPCMD_TIME(4));
  707. }
  708. static void dw_mipi_dsi_packet_handler_config(struct dw_mipi_dsi *dsi)
  709. {
  710. dsi_write(dsi, DSI_PCKHDL_CFG, EN_CRC_RX | EN_ECC_RX | EN_BTA);
  711. }
  712. static void dw_mipi_dsi_video_packet_config(struct dw_mipi_dsi *dsi,
  713. struct drm_display_mode *mode)
  714. {
  715. dsi_write(dsi, DSI_VID_PKT_SIZE, VID_PKT_SIZE(mode->hdisplay));
  716. }
  717. static void dw_mipi_dsi_command_mode_config(struct dw_mipi_dsi *dsi)
  718. {
  719. dsi_write(dsi, DSI_TO_CNT_CFG, HSTX_TO_CNT(1000) | LPRX_TO_CNT(1000));
  720. dsi_write(dsi, DSI_BTA_TO_CNT, 0xd00);
  721. dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
  722. }
  723. /* Get lane byte clock cycles. */
  724. static u32 dw_mipi_dsi_get_hcomponent_lbcc(struct dw_mipi_dsi *dsi,
  725. struct drm_display_mode *mode,
  726. u32 hcomponent)
  727. {
  728. u32 frac, lbcc;
  729. lbcc = hcomponent * dsi->lane_mbps * MSEC_PER_SEC / 8;
  730. frac = lbcc % mode->clock;
  731. lbcc = lbcc / mode->clock;
  732. if (frac)
  733. lbcc++;
  734. return lbcc;
  735. }
  736. static void dw_mipi_dsi_line_timer_config(struct dw_mipi_dsi *dsi,
  737. struct drm_display_mode *mode)
  738. {
  739. u32 htotal, hsa, hbp, lbcc;
  740. htotal = mode->htotal;
  741. hsa = mode->hsync_end - mode->hsync_start;
  742. hbp = mode->htotal - mode->hsync_end;
  743. lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, htotal);
  744. dsi_write(dsi, DSI_VID_HLINE_TIME, lbcc);
  745. lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, hsa);
  746. dsi_write(dsi, DSI_VID_HSA_TIME, lbcc);
  747. lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, hbp);
  748. dsi_write(dsi, DSI_VID_HBP_TIME, lbcc);
  749. }
  750. static void dw_mipi_dsi_vertical_timing_config(struct dw_mipi_dsi *dsi,
  751. struct drm_display_mode *mode)
  752. {
  753. u32 vactive, vsa, vfp, vbp;
  754. vactive = mode->vdisplay;
  755. vsa = mode->vsync_end - mode->vsync_start;
  756. vfp = mode->vsync_start - mode->vdisplay;
  757. vbp = mode->vtotal - mode->vsync_end;
  758. dsi_write(dsi, DSI_VID_VACTIVE_LINES, vactive);
  759. dsi_write(dsi, DSI_VID_VSA_LINES, vsa);
  760. dsi_write(dsi, DSI_VID_VFP_LINES, vfp);
  761. dsi_write(dsi, DSI_VID_VBP_LINES, vbp);
  762. }
  763. static void dw_mipi_dsi_dphy_timing_config(struct dw_mipi_dsi *dsi)
  764. {
  765. dsi_write(dsi, DSI_PHY_TMR_CFG, PHY_HS2LP_TIME(0x40)
  766. | PHY_LP2HS_TIME(0x40) | MAX_RD_TIME(10000));
  767. dsi_write(dsi, DSI_PHY_TMR_LPCLK_CFG, PHY_CLKHS2LP_TIME(0x40)
  768. | PHY_CLKLP2HS_TIME(0x40));
  769. }
  770. static void dw_mipi_dsi_dphy_interface_config(struct dw_mipi_dsi *dsi)
  771. {
  772. dsi_write(dsi, DSI_PHY_IF_CFG, PHY_STOP_WAIT_TIME(0x20) |
  773. N_LANES(dsi->lanes));
  774. }
  775. static void dw_mipi_dsi_clear_err(struct dw_mipi_dsi *dsi)
  776. {
  777. dsi_read(dsi, DSI_INT_ST0);
  778. dsi_read(dsi, DSI_INT_ST1);
  779. dsi_write(dsi, DSI_INT_MSK0, 0);
  780. dsi_write(dsi, DSI_INT_MSK1, 0);
  781. }
  782. static void dw_mipi_dsi_encoder_disable(struct drm_encoder *encoder)
  783. {
  784. struct dw_mipi_dsi *dsi = encoder_to_dsi(encoder);
  785. if (dsi->dpms_mode != DRM_MODE_DPMS_ON)
  786. return;
  787. if (clk_prepare_enable(dsi->pclk)) {
  788. DRM_DEV_ERROR(dsi->dev, "Failed to enable pclk\n");
  789. return;
  790. }
  791. drm_panel_disable(dsi->panel);
  792. dw_mipi_dsi_set_mode(dsi, DW_MIPI_DSI_CMD_MODE);
  793. drm_panel_unprepare(dsi->panel);
  794. dw_mipi_dsi_disable(dsi);
  795. pm_runtime_put(dsi->dev);
  796. clk_disable_unprepare(dsi->pclk);
  797. dsi->dpms_mode = DRM_MODE_DPMS_OFF;
  798. }
  799. static void dw_mipi_dsi_encoder_enable(struct drm_encoder *encoder)
  800. {
  801. struct dw_mipi_dsi *dsi = encoder_to_dsi(encoder);
  802. struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
  803. const struct dw_mipi_dsi_plat_data *pdata = dsi->pdata;
  804. int mux = drm_of_encoder_active_endpoint_id(dsi->dev->of_node, encoder);
  805. u32 val;
  806. int ret;
  807. ret = dw_mipi_dsi_get_lane_bps(dsi, mode);
  808. if (ret < 0)
  809. return;
  810. if (dsi->dpms_mode == DRM_MODE_DPMS_ON)
  811. return;
  812. if (clk_prepare_enable(dsi->pclk)) {
  813. DRM_DEV_ERROR(dsi->dev, "Failed to enable pclk\n");
  814. return;
  815. }
  816. pm_runtime_get_sync(dsi->dev);
  817. dw_mipi_dsi_init(dsi);
  818. dw_mipi_dsi_dpi_config(dsi, mode);
  819. dw_mipi_dsi_packet_handler_config(dsi);
  820. dw_mipi_dsi_video_mode_config(dsi);
  821. dw_mipi_dsi_video_packet_config(dsi, mode);
  822. dw_mipi_dsi_command_mode_config(dsi);
  823. dw_mipi_dsi_line_timer_config(dsi, mode);
  824. dw_mipi_dsi_vertical_timing_config(dsi, mode);
  825. dw_mipi_dsi_dphy_timing_config(dsi);
  826. dw_mipi_dsi_dphy_interface_config(dsi);
  827. dw_mipi_dsi_clear_err(dsi);
  828. /*
  829. * For the RK3399, the clk of grf must be enabled before writing grf
  830. * register. And for RK3288 or other soc, this grf_clk must be NULL,
  831. * the clk_prepare_enable return true directly.
  832. */
  833. ret = clk_prepare_enable(dsi->grf_clk);
  834. if (ret) {
  835. DRM_DEV_ERROR(dsi->dev, "Failed to enable grf_clk: %d\n", ret);
  836. return;
  837. }
  838. if (pdata->grf_dsi0_mode_reg)
  839. regmap_write(dsi->grf_regmap, pdata->grf_dsi0_mode_reg,
  840. pdata->grf_dsi0_mode);
  841. dw_mipi_dsi_phy_init(dsi);
  842. dw_mipi_dsi_wait_for_two_frames(mode);
  843. dw_mipi_dsi_set_mode(dsi, DW_MIPI_DSI_CMD_MODE);
  844. if (drm_panel_prepare(dsi->panel))
  845. DRM_DEV_ERROR(dsi->dev, "failed to prepare panel\n");
  846. dw_mipi_dsi_set_mode(dsi, DW_MIPI_DSI_VID_MODE);
  847. drm_panel_enable(dsi->panel);
  848. clk_disable_unprepare(dsi->pclk);
  849. if (mux)
  850. val = pdata->dsi0_en_bit | (pdata->dsi0_en_bit << 16);
  851. else
  852. val = pdata->dsi0_en_bit << 16;
  853. regmap_write(dsi->grf_regmap, pdata->grf_switch_reg, val);
  854. DRM_DEV_DEBUG(dsi->dev,
  855. "vop %s output to dsi0\n", (mux) ? "LIT" : "BIG");
  856. dsi->dpms_mode = DRM_MODE_DPMS_ON;
  857. clk_disable_unprepare(dsi->grf_clk);
  858. }
  859. static int
  860. dw_mipi_dsi_encoder_atomic_check(struct drm_encoder *encoder,
  861. struct drm_crtc_state *crtc_state,
  862. struct drm_connector_state *conn_state)
  863. {
  864. struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
  865. struct dw_mipi_dsi *dsi = encoder_to_dsi(encoder);
  866. switch (dsi->format) {
  867. case MIPI_DSI_FMT_RGB888:
  868. s->output_mode = ROCKCHIP_OUT_MODE_P888;
  869. break;
  870. case MIPI_DSI_FMT_RGB666:
  871. s->output_mode = ROCKCHIP_OUT_MODE_P666;
  872. break;
  873. case MIPI_DSI_FMT_RGB565:
  874. s->output_mode = ROCKCHIP_OUT_MODE_P565;
  875. break;
  876. default:
  877. WARN_ON(1);
  878. return -EINVAL;
  879. }
  880. s->output_type = DRM_MODE_CONNECTOR_DSI;
  881. return 0;
  882. }
  883. static const struct drm_encoder_helper_funcs
  884. dw_mipi_dsi_encoder_helper_funcs = {
  885. .enable = dw_mipi_dsi_encoder_enable,
  886. .disable = dw_mipi_dsi_encoder_disable,
  887. .atomic_check = dw_mipi_dsi_encoder_atomic_check,
  888. };
  889. static const struct drm_encoder_funcs dw_mipi_dsi_encoder_funcs = {
  890. .destroy = drm_encoder_cleanup,
  891. };
  892. static int dw_mipi_dsi_connector_get_modes(struct drm_connector *connector)
  893. {
  894. struct dw_mipi_dsi *dsi = con_to_dsi(connector);
  895. return drm_panel_get_modes(dsi->panel);
  896. }
  897. static struct drm_connector_helper_funcs dw_mipi_dsi_connector_helper_funcs = {
  898. .get_modes = dw_mipi_dsi_connector_get_modes,
  899. };
  900. static void dw_mipi_dsi_drm_connector_destroy(struct drm_connector *connector)
  901. {
  902. drm_connector_unregister(connector);
  903. drm_connector_cleanup(connector);
  904. }
  905. static const struct drm_connector_funcs dw_mipi_dsi_atomic_connector_funcs = {
  906. .fill_modes = drm_helper_probe_single_connector_modes,
  907. .destroy = dw_mipi_dsi_drm_connector_destroy,
  908. .reset = drm_atomic_helper_connector_reset,
  909. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  910. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  911. };
  912. static int dw_mipi_dsi_register(struct drm_device *drm,
  913. struct dw_mipi_dsi *dsi)
  914. {
  915. struct drm_encoder *encoder = &dsi->encoder;
  916. struct drm_connector *connector = &dsi->connector;
  917. struct device *dev = dsi->dev;
  918. int ret;
  919. encoder->possible_crtcs = drm_of_find_possible_crtcs(drm,
  920. dev->of_node);
  921. /*
  922. * If we failed to find the CRTC(s) which this encoder is
  923. * supposed to be connected to, it's because the CRTC has
  924. * not been registered yet. Defer probing, and hope that
  925. * the required CRTC is added later.
  926. */
  927. if (encoder->possible_crtcs == 0)
  928. return -EPROBE_DEFER;
  929. drm_encoder_helper_add(&dsi->encoder,
  930. &dw_mipi_dsi_encoder_helper_funcs);
  931. ret = drm_encoder_init(drm, &dsi->encoder, &dw_mipi_dsi_encoder_funcs,
  932. DRM_MODE_ENCODER_DSI, NULL);
  933. if (ret) {
  934. DRM_DEV_ERROR(dev, "Failed to initialize encoder with drm\n");
  935. return ret;
  936. }
  937. drm_connector_helper_add(connector,
  938. &dw_mipi_dsi_connector_helper_funcs);
  939. drm_connector_init(drm, &dsi->connector,
  940. &dw_mipi_dsi_atomic_connector_funcs,
  941. DRM_MODE_CONNECTOR_DSI);
  942. drm_mode_connector_attach_encoder(connector, encoder);
  943. return 0;
  944. }
  945. static int rockchip_mipi_parse_dt(struct dw_mipi_dsi *dsi)
  946. {
  947. struct device_node *np = dsi->dev->of_node;
  948. dsi->grf_regmap = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
  949. if (IS_ERR(dsi->grf_regmap)) {
  950. DRM_DEV_ERROR(dsi->dev, "Unable to get rockchip,grf\n");
  951. return PTR_ERR(dsi->grf_regmap);
  952. }
  953. return 0;
  954. }
  955. static struct dw_mipi_dsi_plat_data rk3288_mipi_dsi_drv_data = {
  956. .dsi0_en_bit = RK3288_DSI0_SEL_VOP_LIT,
  957. .dsi1_en_bit = RK3288_DSI1_SEL_VOP_LIT,
  958. .grf_switch_reg = RK3288_GRF_SOC_CON6,
  959. .max_data_lanes = 4,
  960. };
  961. static struct dw_mipi_dsi_plat_data rk3399_mipi_dsi_drv_data = {
  962. .dsi0_en_bit = RK3399_DSI0_SEL_VOP_LIT,
  963. .dsi1_en_bit = RK3399_DSI1_SEL_VOP_LIT,
  964. .grf_switch_reg = RK3399_GRF_SOC_CON20,
  965. .grf_dsi0_mode = RK3399_GRF_DSI_MODE,
  966. .grf_dsi0_mode_reg = RK3399_GRF_SOC_CON22,
  967. .flags = DW_MIPI_NEEDS_PHY_CFG_CLK | DW_MIPI_NEEDS_GRF_CLK,
  968. .max_data_lanes = 4,
  969. };
  970. static const struct of_device_id dw_mipi_dsi_dt_ids[] = {
  971. {
  972. .compatible = "rockchip,rk3288-mipi-dsi",
  973. .data = &rk3288_mipi_dsi_drv_data,
  974. }, {
  975. .compatible = "rockchip,rk3399-mipi-dsi",
  976. .data = &rk3399_mipi_dsi_drv_data,
  977. },
  978. { /* sentinel */ }
  979. };
  980. MODULE_DEVICE_TABLE(of, dw_mipi_dsi_dt_ids);
  981. static int dw_mipi_dsi_bind(struct device *dev, struct device *master,
  982. void *data)
  983. {
  984. const struct of_device_id *of_id =
  985. of_match_device(dw_mipi_dsi_dt_ids, dev);
  986. const struct dw_mipi_dsi_plat_data *pdata = of_id->data;
  987. struct platform_device *pdev = to_platform_device(dev);
  988. struct reset_control *apb_rst;
  989. struct drm_device *drm = data;
  990. struct dw_mipi_dsi *dsi;
  991. struct resource *res;
  992. int ret;
  993. dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
  994. if (!dsi)
  995. return -ENOMEM;
  996. dsi->dev = dev;
  997. dsi->pdata = pdata;
  998. dsi->dpms_mode = DRM_MODE_DPMS_OFF;
  999. ret = rockchip_mipi_parse_dt(dsi);
  1000. if (ret)
  1001. return ret;
  1002. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1003. dsi->base = devm_ioremap_resource(dev, res);
  1004. if (IS_ERR(dsi->base))
  1005. return PTR_ERR(dsi->base);
  1006. dsi->pllref_clk = devm_clk_get(dev, "ref");
  1007. if (IS_ERR(dsi->pllref_clk)) {
  1008. ret = PTR_ERR(dsi->pllref_clk);
  1009. DRM_DEV_ERROR(dev,
  1010. "Unable to get pll reference clock: %d\n", ret);
  1011. return ret;
  1012. }
  1013. dsi->pclk = devm_clk_get(dev, "pclk");
  1014. if (IS_ERR(dsi->pclk)) {
  1015. ret = PTR_ERR(dsi->pclk);
  1016. DRM_DEV_ERROR(dev, "Unable to get pclk: %d\n", ret);
  1017. return ret;
  1018. }
  1019. /*
  1020. * Note that the reset was not defined in the initial device tree, so
  1021. * we have to be prepared for it not being found.
  1022. */
  1023. apb_rst = devm_reset_control_get(dev, "apb");
  1024. if (IS_ERR(apb_rst)) {
  1025. ret = PTR_ERR(apb_rst);
  1026. if (ret == -ENOENT) {
  1027. apb_rst = NULL;
  1028. } else {
  1029. DRM_DEV_ERROR(dev,
  1030. "Unable to get reset control: %d\n", ret);
  1031. return ret;
  1032. }
  1033. }
  1034. if (apb_rst) {
  1035. ret = clk_prepare_enable(dsi->pclk);
  1036. if (ret) {
  1037. DRM_DEV_ERROR(dev, "Failed to enable pclk\n");
  1038. return ret;
  1039. }
  1040. reset_control_assert(apb_rst);
  1041. usleep_range(10, 20);
  1042. reset_control_deassert(apb_rst);
  1043. clk_disable_unprepare(dsi->pclk);
  1044. }
  1045. if (pdata->flags & DW_MIPI_NEEDS_PHY_CFG_CLK) {
  1046. dsi->phy_cfg_clk = devm_clk_get(dev, "phy_cfg");
  1047. if (IS_ERR(dsi->phy_cfg_clk)) {
  1048. ret = PTR_ERR(dsi->phy_cfg_clk);
  1049. DRM_DEV_ERROR(dev,
  1050. "Unable to get phy_cfg_clk: %d\n", ret);
  1051. return ret;
  1052. }
  1053. }
  1054. if (pdata->flags & DW_MIPI_NEEDS_GRF_CLK) {
  1055. dsi->grf_clk = devm_clk_get(dev, "grf");
  1056. if (IS_ERR(dsi->grf_clk)) {
  1057. ret = PTR_ERR(dsi->grf_clk);
  1058. DRM_DEV_ERROR(dev, "Unable to get grf_clk: %d\n", ret);
  1059. return ret;
  1060. }
  1061. }
  1062. ret = clk_prepare_enable(dsi->pllref_clk);
  1063. if (ret) {
  1064. DRM_DEV_ERROR(dev, "Failed to enable pllref_clk\n");
  1065. return ret;
  1066. }
  1067. ret = dw_mipi_dsi_register(drm, dsi);
  1068. if (ret) {
  1069. DRM_DEV_ERROR(dev, "Failed to register mipi_dsi: %d\n", ret);
  1070. goto err_pllref;
  1071. }
  1072. dsi->dsi_host.ops = &dw_mipi_dsi_host_ops;
  1073. dsi->dsi_host.dev = dev;
  1074. ret = mipi_dsi_host_register(&dsi->dsi_host);
  1075. if (ret) {
  1076. DRM_DEV_ERROR(dev, "Failed to register MIPI host: %d\n", ret);
  1077. goto err_cleanup;
  1078. }
  1079. if (!dsi->panel) {
  1080. ret = -EPROBE_DEFER;
  1081. goto err_mipi_dsi_host;
  1082. }
  1083. dev_set_drvdata(dev, dsi);
  1084. pm_runtime_enable(dev);
  1085. return 0;
  1086. err_mipi_dsi_host:
  1087. mipi_dsi_host_unregister(&dsi->dsi_host);
  1088. err_cleanup:
  1089. dsi->connector.funcs->destroy(&dsi->connector);
  1090. dsi->encoder.funcs->destroy(&dsi->encoder);
  1091. err_pllref:
  1092. clk_disable_unprepare(dsi->pllref_clk);
  1093. return ret;
  1094. }
  1095. static void dw_mipi_dsi_unbind(struct device *dev, struct device *master,
  1096. void *data)
  1097. {
  1098. struct dw_mipi_dsi *dsi = dev_get_drvdata(dev);
  1099. mipi_dsi_host_unregister(&dsi->dsi_host);
  1100. pm_runtime_disable(dev);
  1101. dsi->connector.funcs->destroy(&dsi->connector);
  1102. dsi->encoder.funcs->destroy(&dsi->encoder);
  1103. clk_disable_unprepare(dsi->pllref_clk);
  1104. }
  1105. static const struct component_ops dw_mipi_dsi_ops = {
  1106. .bind = dw_mipi_dsi_bind,
  1107. .unbind = dw_mipi_dsi_unbind,
  1108. };
  1109. static int dw_mipi_dsi_probe(struct platform_device *pdev)
  1110. {
  1111. return component_add(&pdev->dev, &dw_mipi_dsi_ops);
  1112. }
  1113. static int dw_mipi_dsi_remove(struct platform_device *pdev)
  1114. {
  1115. component_del(&pdev->dev, &dw_mipi_dsi_ops);
  1116. return 0;
  1117. }
  1118. struct platform_driver dw_mipi_dsi_driver = {
  1119. .probe = dw_mipi_dsi_probe,
  1120. .remove = dw_mipi_dsi_remove,
  1121. .driver = {
  1122. .of_match_table = dw_mipi_dsi_dt_ids,
  1123. .name = DRIVER_NAME,
  1124. },
  1125. };