dw-mipi-dsi.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  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. dev_err(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. dev_err(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. dev_err(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. dev_err(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. dev_err(dsi->dev, "failed to get bpp for pixel format %d\n",
  445. dsi->format);
  446. return bpp;
  447. }
  448. mpclk = DIV_ROUND_UP(mode->clock, MSEC_PER_SEC);
  449. if (mpclk) {
  450. /* take 1 / 0.8, since mbps must big than bandwidth of RGB */
  451. tmp = mpclk * (bpp / dsi->lanes) * 10 / 8;
  452. if (tmp < max_mbps)
  453. target_mbps = tmp;
  454. else
  455. dev_err(dsi->dev, "DPHY clock frequency is out of range\n");
  456. }
  457. pllref = DIV_ROUND_UP(clk_get_rate(dsi->pllref_clk), USEC_PER_SEC);
  458. tmp = pllref;
  459. /*
  460. * The limits on the PLL divisor are:
  461. *
  462. * 5MHz <= (pllref / n) <= 40MHz
  463. *
  464. * we walk over these values in descreasing order so that if we hit
  465. * an exact match for target_mbps it is more likely that "m" will be
  466. * even.
  467. *
  468. * TODO: ensure that "m" is even after this loop.
  469. */
  470. for (i = pllref / 5; i > (pllref / 40); i--) {
  471. pre = pllref / i;
  472. if ((tmp > (target_mbps % pre)) && (target_mbps / pre < 512)) {
  473. tmp = target_mbps % pre;
  474. n = i;
  475. m = target_mbps / pre;
  476. }
  477. if (tmp == 0)
  478. break;
  479. }
  480. dsi->lane_mbps = pllref / n * m;
  481. dsi->input_div = n;
  482. dsi->feedback_div = m;
  483. return 0;
  484. }
  485. static int dw_mipi_dsi_host_attach(struct mipi_dsi_host *host,
  486. struct mipi_dsi_device *device)
  487. {
  488. struct dw_mipi_dsi *dsi = host_to_dsi(host);
  489. if (device->lanes > dsi->pdata->max_data_lanes) {
  490. dev_err(dsi->dev, "the number of data lanes(%u) is too many\n",
  491. device->lanes);
  492. return -EINVAL;
  493. }
  494. dsi->lanes = device->lanes;
  495. dsi->channel = device->channel;
  496. dsi->format = device->format;
  497. dsi->mode_flags = device->mode_flags;
  498. dsi->panel = of_drm_find_panel(device->dev.of_node);
  499. if (dsi->panel)
  500. return drm_panel_attach(dsi->panel, &dsi->connector);
  501. return -EINVAL;
  502. }
  503. static int dw_mipi_dsi_host_detach(struct mipi_dsi_host *host,
  504. struct mipi_dsi_device *device)
  505. {
  506. struct dw_mipi_dsi *dsi = host_to_dsi(host);
  507. drm_panel_detach(dsi->panel);
  508. return 0;
  509. }
  510. static void dw_mipi_message_config(struct dw_mipi_dsi *dsi,
  511. const struct mipi_dsi_msg *msg)
  512. {
  513. bool lpm = msg->flags & MIPI_DSI_MSG_USE_LPM;
  514. u32 val = 0;
  515. if (msg->flags & MIPI_DSI_MSG_REQ_ACK)
  516. val |= EN_ACK_RQST;
  517. if (lpm)
  518. val |= CMD_MODE_ALL_LP;
  519. dsi_write(dsi, DSI_LPCLK_CTRL, lpm ? 0 : PHY_TXREQUESTCLKHS);
  520. dsi_write(dsi, DSI_CMD_MODE_CFG, val);
  521. }
  522. static int dw_mipi_dsi_gen_pkt_hdr_write(struct dw_mipi_dsi *dsi, u32 hdr_val)
  523. {
  524. int ret;
  525. u32 val, mask;
  526. ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
  527. val, !(val & GEN_CMD_FULL), 1000,
  528. CMD_PKT_STATUS_TIMEOUT_US);
  529. if (ret < 0) {
  530. dev_err(dsi->dev, "failed to get available command FIFO\n");
  531. return ret;
  532. }
  533. dsi_write(dsi, DSI_GEN_HDR, hdr_val);
  534. mask = GEN_CMD_EMPTY | GEN_PLD_W_EMPTY;
  535. ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
  536. val, (val & mask) == mask,
  537. 1000, CMD_PKT_STATUS_TIMEOUT_US);
  538. if (ret < 0) {
  539. dev_err(dsi->dev, "failed to write command FIFO\n");
  540. return ret;
  541. }
  542. return 0;
  543. }
  544. static int dw_mipi_dsi_dcs_short_write(struct dw_mipi_dsi *dsi,
  545. const struct mipi_dsi_msg *msg)
  546. {
  547. const u8 *tx_buf = msg->tx_buf;
  548. u16 data = 0;
  549. u32 val;
  550. if (msg->tx_len > 0)
  551. data |= tx_buf[0];
  552. if (msg->tx_len > 1)
  553. data |= tx_buf[1] << 8;
  554. if (msg->tx_len > 2) {
  555. dev_err(dsi->dev, "too long tx buf length %zu for short write\n",
  556. msg->tx_len);
  557. return -EINVAL;
  558. }
  559. val = GEN_HDATA(data) | GEN_HTYPE(msg->type);
  560. return dw_mipi_dsi_gen_pkt_hdr_write(dsi, val);
  561. }
  562. static int dw_mipi_dsi_dcs_long_write(struct dw_mipi_dsi *dsi,
  563. const struct mipi_dsi_msg *msg)
  564. {
  565. const u8 *tx_buf = msg->tx_buf;
  566. int len = msg->tx_len, pld_data_bytes = sizeof(u32), ret;
  567. u32 hdr_val = GEN_HDATA(msg->tx_len) | GEN_HTYPE(msg->type);
  568. u32 remainder;
  569. u32 val;
  570. if (msg->tx_len < 3) {
  571. dev_err(dsi->dev, "wrong tx buf length %zu for long write\n",
  572. msg->tx_len);
  573. return -EINVAL;
  574. }
  575. while (DIV_ROUND_UP(len, pld_data_bytes)) {
  576. if (len < pld_data_bytes) {
  577. remainder = 0;
  578. memcpy(&remainder, tx_buf, len);
  579. dsi_write(dsi, DSI_GEN_PLD_DATA, remainder);
  580. len = 0;
  581. } else {
  582. memcpy(&remainder, tx_buf, pld_data_bytes);
  583. dsi_write(dsi, DSI_GEN_PLD_DATA, remainder);
  584. tx_buf += pld_data_bytes;
  585. len -= pld_data_bytes;
  586. }
  587. ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
  588. val, !(val & GEN_PLD_W_FULL), 1000,
  589. CMD_PKT_STATUS_TIMEOUT_US);
  590. if (ret < 0) {
  591. dev_err(dsi->dev,
  592. "failed to get available write payload FIFO\n");
  593. return ret;
  594. }
  595. }
  596. return dw_mipi_dsi_gen_pkt_hdr_write(dsi, hdr_val);
  597. }
  598. static ssize_t dw_mipi_dsi_host_transfer(struct mipi_dsi_host *host,
  599. const struct mipi_dsi_msg *msg)
  600. {
  601. struct dw_mipi_dsi *dsi = host_to_dsi(host);
  602. int ret;
  603. dw_mipi_message_config(dsi, msg);
  604. switch (msg->type) {
  605. case MIPI_DSI_DCS_SHORT_WRITE:
  606. case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
  607. case MIPI_DSI_SET_MAXIMUM_RETURN_PACKET_SIZE:
  608. ret = dw_mipi_dsi_dcs_short_write(dsi, msg);
  609. break;
  610. case MIPI_DSI_DCS_LONG_WRITE:
  611. ret = dw_mipi_dsi_dcs_long_write(dsi, msg);
  612. break;
  613. default:
  614. dev_err(dsi->dev, "unsupported message type 0x%02x\n",
  615. msg->type);
  616. ret = -EINVAL;
  617. }
  618. return ret;
  619. }
  620. static const struct mipi_dsi_host_ops dw_mipi_dsi_host_ops = {
  621. .attach = dw_mipi_dsi_host_attach,
  622. .detach = dw_mipi_dsi_host_detach,
  623. .transfer = dw_mipi_dsi_host_transfer,
  624. };
  625. static void dw_mipi_dsi_video_mode_config(struct dw_mipi_dsi *dsi)
  626. {
  627. u32 val;
  628. val = ENABLE_LOW_POWER;
  629. if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
  630. val |= VID_MODE_TYPE_BURST;
  631. else if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
  632. val |= VID_MODE_TYPE_NON_BURST_SYNC_PULSES;
  633. else
  634. val |= VID_MODE_TYPE_NON_BURST_SYNC_EVENTS;
  635. dsi_write(dsi, DSI_VID_MODE_CFG, val);
  636. }
  637. static void dw_mipi_dsi_set_mode(struct dw_mipi_dsi *dsi,
  638. enum dw_mipi_dsi_mode mode)
  639. {
  640. if (mode == DW_MIPI_DSI_CMD_MODE) {
  641. dsi_write(dsi, DSI_PWR_UP, RESET);
  642. dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
  643. dsi_write(dsi, DSI_PWR_UP, POWERUP);
  644. } else {
  645. dsi_write(dsi, DSI_PWR_UP, RESET);
  646. dsi_write(dsi, DSI_MODE_CFG, ENABLE_VIDEO_MODE);
  647. dw_mipi_dsi_video_mode_config(dsi);
  648. dsi_write(dsi, DSI_LPCLK_CTRL, PHY_TXREQUESTCLKHS);
  649. dsi_write(dsi, DSI_PWR_UP, POWERUP);
  650. }
  651. }
  652. static void dw_mipi_dsi_disable(struct dw_mipi_dsi *dsi)
  653. {
  654. dsi_write(dsi, DSI_PWR_UP, RESET);
  655. dsi_write(dsi, DSI_PHY_RSTZ, PHY_RSTZ);
  656. }
  657. static void dw_mipi_dsi_init(struct dw_mipi_dsi *dsi)
  658. {
  659. /*
  660. * The maximum permitted escape clock is 20MHz and it is derived from
  661. * lanebyteclk, which is running at "lane_mbps / 8". Thus we want:
  662. *
  663. * (lane_mbps >> 3) / esc_clk_division < 20
  664. * which is:
  665. * (lane_mbps >> 3) / 20 > esc_clk_division
  666. */
  667. u32 esc_clk_division = (dsi->lane_mbps >> 3) / 20 + 1;
  668. dsi_write(dsi, DSI_PWR_UP, RESET);
  669. dsi_write(dsi, DSI_PHY_RSTZ, PHY_DISFORCEPLL | PHY_DISABLECLK
  670. | PHY_RSTZ | PHY_SHUTDOWNZ);
  671. dsi_write(dsi, DSI_CLKMGR_CFG, TO_CLK_DIVIDSION(10) |
  672. TX_ESC_CLK_DIVIDSION(esc_clk_division));
  673. }
  674. static void dw_mipi_dsi_dpi_config(struct dw_mipi_dsi *dsi,
  675. struct drm_display_mode *mode)
  676. {
  677. u32 val = 0, color = 0;
  678. switch (dsi->format) {
  679. case MIPI_DSI_FMT_RGB888:
  680. color = DPI_COLOR_CODING_24BIT;
  681. break;
  682. case MIPI_DSI_FMT_RGB666:
  683. color = DPI_COLOR_CODING_18BIT_2 | EN18_LOOSELY;
  684. break;
  685. case MIPI_DSI_FMT_RGB666_PACKED:
  686. color = DPI_COLOR_CODING_18BIT_1;
  687. break;
  688. case MIPI_DSI_FMT_RGB565:
  689. color = DPI_COLOR_CODING_16BIT_1;
  690. break;
  691. }
  692. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  693. val |= VSYNC_ACTIVE_LOW;
  694. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  695. val |= HSYNC_ACTIVE_LOW;
  696. dsi_write(dsi, DSI_DPI_VCID, DPI_VID(dsi->channel));
  697. dsi_write(dsi, DSI_DPI_COLOR_CODING, color);
  698. dsi_write(dsi, DSI_DPI_CFG_POL, val);
  699. dsi_write(dsi, DSI_DPI_LP_CMD_TIM, OUTVACT_LPCMD_TIME(4)
  700. | INVACT_LPCMD_TIME(4));
  701. }
  702. static void dw_mipi_dsi_packet_handler_config(struct dw_mipi_dsi *dsi)
  703. {
  704. dsi_write(dsi, DSI_PCKHDL_CFG, EN_CRC_RX | EN_ECC_RX | EN_BTA);
  705. }
  706. static void dw_mipi_dsi_video_packet_config(struct dw_mipi_dsi *dsi,
  707. struct drm_display_mode *mode)
  708. {
  709. dsi_write(dsi, DSI_VID_PKT_SIZE, VID_PKT_SIZE(mode->hdisplay));
  710. }
  711. static void dw_mipi_dsi_command_mode_config(struct dw_mipi_dsi *dsi)
  712. {
  713. dsi_write(dsi, DSI_TO_CNT_CFG, HSTX_TO_CNT(1000) | LPRX_TO_CNT(1000));
  714. dsi_write(dsi, DSI_BTA_TO_CNT, 0xd00);
  715. dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
  716. }
  717. /* Get lane byte clock cycles. */
  718. static u32 dw_mipi_dsi_get_hcomponent_lbcc(struct dw_mipi_dsi *dsi,
  719. struct drm_display_mode *mode,
  720. u32 hcomponent)
  721. {
  722. u32 frac, lbcc;
  723. lbcc = hcomponent * dsi->lane_mbps * MSEC_PER_SEC / 8;
  724. frac = lbcc % mode->clock;
  725. lbcc = lbcc / mode->clock;
  726. if (frac)
  727. lbcc++;
  728. return lbcc;
  729. }
  730. static void dw_mipi_dsi_line_timer_config(struct dw_mipi_dsi *dsi,
  731. struct drm_display_mode *mode)
  732. {
  733. u32 htotal, hsa, hbp, lbcc;
  734. htotal = mode->htotal;
  735. hsa = mode->hsync_end - mode->hsync_start;
  736. hbp = mode->htotal - mode->hsync_end;
  737. lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, htotal);
  738. dsi_write(dsi, DSI_VID_HLINE_TIME, lbcc);
  739. lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, hsa);
  740. dsi_write(dsi, DSI_VID_HSA_TIME, lbcc);
  741. lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, hbp);
  742. dsi_write(dsi, DSI_VID_HBP_TIME, lbcc);
  743. }
  744. static void dw_mipi_dsi_vertical_timing_config(struct dw_mipi_dsi *dsi,
  745. struct drm_display_mode *mode)
  746. {
  747. u32 vactive, vsa, vfp, vbp;
  748. vactive = mode->vdisplay;
  749. vsa = mode->vsync_end - mode->vsync_start;
  750. vfp = mode->vsync_start - mode->vdisplay;
  751. vbp = mode->vtotal - mode->vsync_end;
  752. dsi_write(dsi, DSI_VID_VACTIVE_LINES, vactive);
  753. dsi_write(dsi, DSI_VID_VSA_LINES, vsa);
  754. dsi_write(dsi, DSI_VID_VFP_LINES, vfp);
  755. dsi_write(dsi, DSI_VID_VBP_LINES, vbp);
  756. }
  757. static void dw_mipi_dsi_dphy_timing_config(struct dw_mipi_dsi *dsi)
  758. {
  759. dsi_write(dsi, DSI_PHY_TMR_CFG, PHY_HS2LP_TIME(0x40)
  760. | PHY_LP2HS_TIME(0x40) | MAX_RD_TIME(10000));
  761. dsi_write(dsi, DSI_PHY_TMR_LPCLK_CFG, PHY_CLKHS2LP_TIME(0x40)
  762. | PHY_CLKLP2HS_TIME(0x40));
  763. }
  764. static void dw_mipi_dsi_dphy_interface_config(struct dw_mipi_dsi *dsi)
  765. {
  766. dsi_write(dsi, DSI_PHY_IF_CFG, PHY_STOP_WAIT_TIME(0x20) |
  767. N_LANES(dsi->lanes));
  768. }
  769. static void dw_mipi_dsi_clear_err(struct dw_mipi_dsi *dsi)
  770. {
  771. dsi_read(dsi, DSI_INT_ST0);
  772. dsi_read(dsi, DSI_INT_ST1);
  773. dsi_write(dsi, DSI_INT_MSK0, 0);
  774. dsi_write(dsi, DSI_INT_MSK1, 0);
  775. }
  776. static void dw_mipi_dsi_encoder_disable(struct drm_encoder *encoder)
  777. {
  778. struct dw_mipi_dsi *dsi = encoder_to_dsi(encoder);
  779. if (dsi->dpms_mode != DRM_MODE_DPMS_ON)
  780. return;
  781. if (clk_prepare_enable(dsi->pclk)) {
  782. dev_err(dsi->dev, "%s: Failed to enable pclk\n", __func__);
  783. return;
  784. }
  785. drm_panel_disable(dsi->panel);
  786. dw_mipi_dsi_set_mode(dsi, DW_MIPI_DSI_CMD_MODE);
  787. drm_panel_unprepare(dsi->panel);
  788. dw_mipi_dsi_disable(dsi);
  789. pm_runtime_put(dsi->dev);
  790. clk_disable_unprepare(dsi->pclk);
  791. dsi->dpms_mode = DRM_MODE_DPMS_OFF;
  792. }
  793. static void dw_mipi_dsi_encoder_enable(struct drm_encoder *encoder)
  794. {
  795. struct dw_mipi_dsi *dsi = encoder_to_dsi(encoder);
  796. struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode;
  797. const struct dw_mipi_dsi_plat_data *pdata = dsi->pdata;
  798. int mux = drm_of_encoder_active_endpoint_id(dsi->dev->of_node, encoder);
  799. u32 val;
  800. int ret;
  801. ret = dw_mipi_dsi_get_lane_bps(dsi, mode);
  802. if (ret < 0)
  803. return;
  804. if (dsi->dpms_mode == DRM_MODE_DPMS_ON)
  805. return;
  806. if (clk_prepare_enable(dsi->pclk)) {
  807. dev_err(dsi->dev, "%s: Failed to enable pclk\n", __func__);
  808. return;
  809. }
  810. pm_runtime_get_sync(dsi->dev);
  811. dw_mipi_dsi_init(dsi);
  812. dw_mipi_dsi_dpi_config(dsi, mode);
  813. dw_mipi_dsi_packet_handler_config(dsi);
  814. dw_mipi_dsi_video_mode_config(dsi);
  815. dw_mipi_dsi_video_packet_config(dsi, mode);
  816. dw_mipi_dsi_command_mode_config(dsi);
  817. dw_mipi_dsi_line_timer_config(dsi, mode);
  818. dw_mipi_dsi_vertical_timing_config(dsi, mode);
  819. dw_mipi_dsi_dphy_timing_config(dsi);
  820. dw_mipi_dsi_dphy_interface_config(dsi);
  821. dw_mipi_dsi_clear_err(dsi);
  822. /*
  823. * For the RK3399, the clk of grf must be enabled before writing grf
  824. * register. And for RK3288 or other soc, this grf_clk must be NULL,
  825. * the clk_prepare_enable return true directly.
  826. */
  827. ret = clk_prepare_enable(dsi->grf_clk);
  828. if (ret) {
  829. dev_err(dsi->dev, "Failed to enable grf_clk: %d\n", ret);
  830. return;
  831. }
  832. if (pdata->grf_dsi0_mode_reg)
  833. regmap_write(dsi->grf_regmap, pdata->grf_dsi0_mode_reg,
  834. pdata->grf_dsi0_mode);
  835. dw_mipi_dsi_phy_init(dsi);
  836. dw_mipi_dsi_wait_for_two_frames(mode);
  837. dw_mipi_dsi_set_mode(dsi, DW_MIPI_DSI_CMD_MODE);
  838. if (drm_panel_prepare(dsi->panel))
  839. dev_err(dsi->dev, "failed to prepare panel\n");
  840. dw_mipi_dsi_set_mode(dsi, DW_MIPI_DSI_VID_MODE);
  841. drm_panel_enable(dsi->panel);
  842. clk_disable_unprepare(dsi->pclk);
  843. if (mux)
  844. val = pdata->dsi0_en_bit | (pdata->dsi0_en_bit << 16);
  845. else
  846. val = pdata->dsi0_en_bit << 16;
  847. regmap_write(dsi->grf_regmap, pdata->grf_switch_reg, val);
  848. dev_dbg(dsi->dev, "vop %s output to dsi0\n", (mux) ? "LIT" : "BIG");
  849. dsi->dpms_mode = DRM_MODE_DPMS_ON;
  850. clk_disable_unprepare(dsi->grf_clk);
  851. }
  852. static int
  853. dw_mipi_dsi_encoder_atomic_check(struct drm_encoder *encoder,
  854. struct drm_crtc_state *crtc_state,
  855. struct drm_connector_state *conn_state)
  856. {
  857. struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
  858. struct dw_mipi_dsi *dsi = encoder_to_dsi(encoder);
  859. switch (dsi->format) {
  860. case MIPI_DSI_FMT_RGB888:
  861. s->output_mode = ROCKCHIP_OUT_MODE_P888;
  862. break;
  863. case MIPI_DSI_FMT_RGB666:
  864. s->output_mode = ROCKCHIP_OUT_MODE_P666;
  865. break;
  866. case MIPI_DSI_FMT_RGB565:
  867. s->output_mode = ROCKCHIP_OUT_MODE_P565;
  868. break;
  869. default:
  870. WARN_ON(1);
  871. return -EINVAL;
  872. }
  873. s->output_type = DRM_MODE_CONNECTOR_DSI;
  874. return 0;
  875. }
  876. static const struct drm_encoder_helper_funcs
  877. dw_mipi_dsi_encoder_helper_funcs = {
  878. .enable = dw_mipi_dsi_encoder_enable,
  879. .disable = dw_mipi_dsi_encoder_disable,
  880. .atomic_check = dw_mipi_dsi_encoder_atomic_check,
  881. };
  882. static const struct drm_encoder_funcs dw_mipi_dsi_encoder_funcs = {
  883. .destroy = drm_encoder_cleanup,
  884. };
  885. static int dw_mipi_dsi_connector_get_modes(struct drm_connector *connector)
  886. {
  887. struct dw_mipi_dsi *dsi = con_to_dsi(connector);
  888. return drm_panel_get_modes(dsi->panel);
  889. }
  890. static struct drm_connector_helper_funcs dw_mipi_dsi_connector_helper_funcs = {
  891. .get_modes = dw_mipi_dsi_connector_get_modes,
  892. };
  893. static void dw_mipi_dsi_drm_connector_destroy(struct drm_connector *connector)
  894. {
  895. drm_connector_unregister(connector);
  896. drm_connector_cleanup(connector);
  897. }
  898. static const struct drm_connector_funcs dw_mipi_dsi_atomic_connector_funcs = {
  899. .dpms = drm_atomic_helper_connector_dpms,
  900. .fill_modes = drm_helper_probe_single_connector_modes,
  901. .destroy = dw_mipi_dsi_drm_connector_destroy,
  902. .reset = drm_atomic_helper_connector_reset,
  903. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  904. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  905. };
  906. static int dw_mipi_dsi_register(struct drm_device *drm,
  907. struct dw_mipi_dsi *dsi)
  908. {
  909. struct drm_encoder *encoder = &dsi->encoder;
  910. struct drm_connector *connector = &dsi->connector;
  911. struct device *dev = dsi->dev;
  912. int ret;
  913. encoder->possible_crtcs = drm_of_find_possible_crtcs(drm,
  914. dev->of_node);
  915. /*
  916. * If we failed to find the CRTC(s) which this encoder is
  917. * supposed to be connected to, it's because the CRTC has
  918. * not been registered yet. Defer probing, and hope that
  919. * the required CRTC is added later.
  920. */
  921. if (encoder->possible_crtcs == 0)
  922. return -EPROBE_DEFER;
  923. drm_encoder_helper_add(&dsi->encoder,
  924. &dw_mipi_dsi_encoder_helper_funcs);
  925. ret = drm_encoder_init(drm, &dsi->encoder, &dw_mipi_dsi_encoder_funcs,
  926. DRM_MODE_ENCODER_DSI, NULL);
  927. if (ret) {
  928. dev_err(dev, "Failed to initialize encoder with drm\n");
  929. return ret;
  930. }
  931. drm_connector_helper_add(connector,
  932. &dw_mipi_dsi_connector_helper_funcs);
  933. drm_connector_init(drm, &dsi->connector,
  934. &dw_mipi_dsi_atomic_connector_funcs,
  935. DRM_MODE_CONNECTOR_DSI);
  936. drm_mode_connector_attach_encoder(connector, encoder);
  937. return 0;
  938. }
  939. static int rockchip_mipi_parse_dt(struct dw_mipi_dsi *dsi)
  940. {
  941. struct device_node *np = dsi->dev->of_node;
  942. dsi->grf_regmap = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
  943. if (IS_ERR(dsi->grf_regmap)) {
  944. dev_err(dsi->dev, "Unable to get rockchip,grf\n");
  945. return PTR_ERR(dsi->grf_regmap);
  946. }
  947. return 0;
  948. }
  949. static struct dw_mipi_dsi_plat_data rk3288_mipi_dsi_drv_data = {
  950. .dsi0_en_bit = RK3288_DSI0_SEL_VOP_LIT,
  951. .dsi1_en_bit = RK3288_DSI1_SEL_VOP_LIT,
  952. .grf_switch_reg = RK3288_GRF_SOC_CON6,
  953. .max_data_lanes = 4,
  954. };
  955. static struct dw_mipi_dsi_plat_data rk3399_mipi_dsi_drv_data = {
  956. .dsi0_en_bit = RK3399_DSI0_SEL_VOP_LIT,
  957. .dsi1_en_bit = RK3399_DSI1_SEL_VOP_LIT,
  958. .grf_switch_reg = RK3399_GRF_SOC_CON20,
  959. .grf_dsi0_mode = RK3399_GRF_DSI_MODE,
  960. .grf_dsi0_mode_reg = RK3399_GRF_SOC_CON22,
  961. .flags = DW_MIPI_NEEDS_PHY_CFG_CLK | DW_MIPI_NEEDS_GRF_CLK,
  962. .max_data_lanes = 4,
  963. };
  964. static const struct of_device_id dw_mipi_dsi_dt_ids[] = {
  965. {
  966. .compatible = "rockchip,rk3288-mipi-dsi",
  967. .data = &rk3288_mipi_dsi_drv_data,
  968. }, {
  969. .compatible = "rockchip,rk3399-mipi-dsi",
  970. .data = &rk3399_mipi_dsi_drv_data,
  971. },
  972. { /* sentinel */ }
  973. };
  974. MODULE_DEVICE_TABLE(of, dw_mipi_dsi_dt_ids);
  975. static int dw_mipi_dsi_bind(struct device *dev, struct device *master,
  976. void *data)
  977. {
  978. const struct of_device_id *of_id =
  979. of_match_device(dw_mipi_dsi_dt_ids, dev);
  980. const struct dw_mipi_dsi_plat_data *pdata = of_id->data;
  981. struct platform_device *pdev = to_platform_device(dev);
  982. struct reset_control *apb_rst;
  983. struct drm_device *drm = data;
  984. struct dw_mipi_dsi *dsi;
  985. struct resource *res;
  986. int ret;
  987. dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
  988. if (!dsi)
  989. return -ENOMEM;
  990. dsi->dev = dev;
  991. dsi->pdata = pdata;
  992. dsi->dpms_mode = DRM_MODE_DPMS_OFF;
  993. ret = rockchip_mipi_parse_dt(dsi);
  994. if (ret)
  995. return ret;
  996. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  997. if (!res)
  998. return -ENODEV;
  999. dsi->base = devm_ioremap_resource(dev, res);
  1000. if (IS_ERR(dsi->base))
  1001. return PTR_ERR(dsi->base);
  1002. dsi->pllref_clk = devm_clk_get(dev, "ref");
  1003. if (IS_ERR(dsi->pllref_clk)) {
  1004. ret = PTR_ERR(dsi->pllref_clk);
  1005. dev_err(dev, "Unable to get pll reference clock: %d\n", ret);
  1006. return ret;
  1007. }
  1008. dsi->pclk = devm_clk_get(dev, "pclk");
  1009. if (IS_ERR(dsi->pclk)) {
  1010. ret = PTR_ERR(dsi->pclk);
  1011. dev_err(dev, "Unable to get pclk: %d\n", ret);
  1012. return ret;
  1013. }
  1014. /*
  1015. * Note that the reset was not defined in the initial device tree, so
  1016. * we have to be prepared for it not being found.
  1017. */
  1018. apb_rst = devm_reset_control_get(dev, "apb");
  1019. if (IS_ERR(apb_rst)) {
  1020. ret = PTR_ERR(apb_rst);
  1021. if (ret == -ENOENT) {
  1022. apb_rst = NULL;
  1023. } else {
  1024. dev_err(dev, "Unable to get reset control: %d\n", ret);
  1025. return ret;
  1026. }
  1027. }
  1028. if (apb_rst) {
  1029. ret = clk_prepare_enable(dsi->pclk);
  1030. if (ret) {
  1031. dev_err(dev, "%s: Failed to enable pclk\n", __func__);
  1032. return ret;
  1033. }
  1034. reset_control_assert(apb_rst);
  1035. usleep_range(10, 20);
  1036. reset_control_deassert(apb_rst);
  1037. clk_disable_unprepare(dsi->pclk);
  1038. }
  1039. if (pdata->flags & DW_MIPI_NEEDS_PHY_CFG_CLK) {
  1040. dsi->phy_cfg_clk = devm_clk_get(dev, "phy_cfg");
  1041. if (IS_ERR(dsi->phy_cfg_clk)) {
  1042. ret = PTR_ERR(dsi->phy_cfg_clk);
  1043. dev_err(dev, "Unable to get phy_cfg_clk: %d\n", ret);
  1044. return ret;
  1045. }
  1046. }
  1047. if (pdata->flags & DW_MIPI_NEEDS_GRF_CLK) {
  1048. dsi->grf_clk = devm_clk_get(dev, "grf");
  1049. if (IS_ERR(dsi->grf_clk)) {
  1050. ret = PTR_ERR(dsi->grf_clk);
  1051. dev_err(dev, "Unable to get grf_clk: %d\n", ret);
  1052. return ret;
  1053. }
  1054. }
  1055. ret = clk_prepare_enable(dsi->pllref_clk);
  1056. if (ret) {
  1057. dev_err(dev, "%s: Failed to enable pllref_clk\n", __func__);
  1058. return ret;
  1059. }
  1060. ret = dw_mipi_dsi_register(drm, dsi);
  1061. if (ret) {
  1062. dev_err(dev, "Failed to register mipi_dsi: %d\n", ret);
  1063. goto err_pllref;
  1064. }
  1065. pm_runtime_enable(dev);
  1066. dsi->dsi_host.ops = &dw_mipi_dsi_host_ops;
  1067. dsi->dsi_host.dev = dev;
  1068. ret = mipi_dsi_host_register(&dsi->dsi_host);
  1069. if (ret) {
  1070. dev_err(dev, "Failed to register MIPI host: %d\n", ret);
  1071. goto err_cleanup;
  1072. }
  1073. if (!dsi->panel) {
  1074. ret = -EPROBE_DEFER;
  1075. goto err_mipi_dsi_host;
  1076. }
  1077. dev_set_drvdata(dev, dsi);
  1078. return 0;
  1079. err_mipi_dsi_host:
  1080. mipi_dsi_host_unregister(&dsi->dsi_host);
  1081. err_cleanup:
  1082. drm_encoder_cleanup(&dsi->encoder);
  1083. drm_connector_cleanup(&dsi->connector);
  1084. err_pllref:
  1085. clk_disable_unprepare(dsi->pllref_clk);
  1086. return ret;
  1087. }
  1088. static void dw_mipi_dsi_unbind(struct device *dev, struct device *master,
  1089. void *data)
  1090. {
  1091. struct dw_mipi_dsi *dsi = dev_get_drvdata(dev);
  1092. mipi_dsi_host_unregister(&dsi->dsi_host);
  1093. pm_runtime_disable(dev);
  1094. clk_disable_unprepare(dsi->pllref_clk);
  1095. }
  1096. static const struct component_ops dw_mipi_dsi_ops = {
  1097. .bind = dw_mipi_dsi_bind,
  1098. .unbind = dw_mipi_dsi_unbind,
  1099. };
  1100. static int dw_mipi_dsi_probe(struct platform_device *pdev)
  1101. {
  1102. return component_add(&pdev->dev, &dw_mipi_dsi_ops);
  1103. }
  1104. static int dw_mipi_dsi_remove(struct platform_device *pdev)
  1105. {
  1106. component_del(&pdev->dev, &dw_mipi_dsi_ops);
  1107. return 0;
  1108. }
  1109. struct platform_driver dw_mipi_dsi_driver = {
  1110. .probe = dw_mipi_dsi_probe,
  1111. .remove = dw_mipi_dsi_remove,
  1112. .driver = {
  1113. .of_match_table = dw_mipi_dsi_dt_ids,
  1114. .name = DRIVER_NAME,
  1115. },
  1116. };