dw-mipi-dsi.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  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. .fill_modes = drm_helper_probe_single_connector_modes,
  900. .destroy = dw_mipi_dsi_drm_connector_destroy,
  901. .reset = drm_atomic_helper_connector_reset,
  902. .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
  903. .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
  904. };
  905. static int dw_mipi_dsi_register(struct drm_device *drm,
  906. struct dw_mipi_dsi *dsi)
  907. {
  908. struct drm_encoder *encoder = &dsi->encoder;
  909. struct drm_connector *connector = &dsi->connector;
  910. struct device *dev = dsi->dev;
  911. int ret;
  912. encoder->possible_crtcs = drm_of_find_possible_crtcs(drm,
  913. dev->of_node);
  914. /*
  915. * If we failed to find the CRTC(s) which this encoder is
  916. * supposed to be connected to, it's because the CRTC has
  917. * not been registered yet. Defer probing, and hope that
  918. * the required CRTC is added later.
  919. */
  920. if (encoder->possible_crtcs == 0)
  921. return -EPROBE_DEFER;
  922. drm_encoder_helper_add(&dsi->encoder,
  923. &dw_mipi_dsi_encoder_helper_funcs);
  924. ret = drm_encoder_init(drm, &dsi->encoder, &dw_mipi_dsi_encoder_funcs,
  925. DRM_MODE_ENCODER_DSI, NULL);
  926. if (ret) {
  927. dev_err(dev, "Failed to initialize encoder with drm\n");
  928. return ret;
  929. }
  930. drm_connector_helper_add(connector,
  931. &dw_mipi_dsi_connector_helper_funcs);
  932. drm_connector_init(drm, &dsi->connector,
  933. &dw_mipi_dsi_atomic_connector_funcs,
  934. DRM_MODE_CONNECTOR_DSI);
  935. drm_mode_connector_attach_encoder(connector, encoder);
  936. return 0;
  937. }
  938. static int rockchip_mipi_parse_dt(struct dw_mipi_dsi *dsi)
  939. {
  940. struct device_node *np = dsi->dev->of_node;
  941. dsi->grf_regmap = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
  942. if (IS_ERR(dsi->grf_regmap)) {
  943. dev_err(dsi->dev, "Unable to get rockchip,grf\n");
  944. return PTR_ERR(dsi->grf_regmap);
  945. }
  946. return 0;
  947. }
  948. static struct dw_mipi_dsi_plat_data rk3288_mipi_dsi_drv_data = {
  949. .dsi0_en_bit = RK3288_DSI0_SEL_VOP_LIT,
  950. .dsi1_en_bit = RK3288_DSI1_SEL_VOP_LIT,
  951. .grf_switch_reg = RK3288_GRF_SOC_CON6,
  952. .max_data_lanes = 4,
  953. };
  954. static struct dw_mipi_dsi_plat_data rk3399_mipi_dsi_drv_data = {
  955. .dsi0_en_bit = RK3399_DSI0_SEL_VOP_LIT,
  956. .dsi1_en_bit = RK3399_DSI1_SEL_VOP_LIT,
  957. .grf_switch_reg = RK3399_GRF_SOC_CON20,
  958. .grf_dsi0_mode = RK3399_GRF_DSI_MODE,
  959. .grf_dsi0_mode_reg = RK3399_GRF_SOC_CON22,
  960. .flags = DW_MIPI_NEEDS_PHY_CFG_CLK | DW_MIPI_NEEDS_GRF_CLK,
  961. .max_data_lanes = 4,
  962. };
  963. static const struct of_device_id dw_mipi_dsi_dt_ids[] = {
  964. {
  965. .compatible = "rockchip,rk3288-mipi-dsi",
  966. .data = &rk3288_mipi_dsi_drv_data,
  967. }, {
  968. .compatible = "rockchip,rk3399-mipi-dsi",
  969. .data = &rk3399_mipi_dsi_drv_data,
  970. },
  971. { /* sentinel */ }
  972. };
  973. MODULE_DEVICE_TABLE(of, dw_mipi_dsi_dt_ids);
  974. static int dw_mipi_dsi_bind(struct device *dev, struct device *master,
  975. void *data)
  976. {
  977. const struct of_device_id *of_id =
  978. of_match_device(dw_mipi_dsi_dt_ids, dev);
  979. const struct dw_mipi_dsi_plat_data *pdata = of_id->data;
  980. struct platform_device *pdev = to_platform_device(dev);
  981. struct reset_control *apb_rst;
  982. struct drm_device *drm = data;
  983. struct dw_mipi_dsi *dsi;
  984. struct resource *res;
  985. int ret;
  986. dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
  987. if (!dsi)
  988. return -ENOMEM;
  989. dsi->dev = dev;
  990. dsi->pdata = pdata;
  991. dsi->dpms_mode = DRM_MODE_DPMS_OFF;
  992. ret = rockchip_mipi_parse_dt(dsi);
  993. if (ret)
  994. return ret;
  995. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  996. if (!res)
  997. return -ENODEV;
  998. dsi->base = devm_ioremap_resource(dev, res);
  999. if (IS_ERR(dsi->base))
  1000. return PTR_ERR(dsi->base);
  1001. dsi->pllref_clk = devm_clk_get(dev, "ref");
  1002. if (IS_ERR(dsi->pllref_clk)) {
  1003. ret = PTR_ERR(dsi->pllref_clk);
  1004. dev_err(dev, "Unable to get pll reference clock: %d\n", ret);
  1005. return ret;
  1006. }
  1007. dsi->pclk = devm_clk_get(dev, "pclk");
  1008. if (IS_ERR(dsi->pclk)) {
  1009. ret = PTR_ERR(dsi->pclk);
  1010. dev_err(dev, "Unable to get pclk: %d\n", ret);
  1011. return ret;
  1012. }
  1013. /*
  1014. * Note that the reset was not defined in the initial device tree, so
  1015. * we have to be prepared for it not being found.
  1016. */
  1017. apb_rst = devm_reset_control_get(dev, "apb");
  1018. if (IS_ERR(apb_rst)) {
  1019. ret = PTR_ERR(apb_rst);
  1020. if (ret == -ENOENT) {
  1021. apb_rst = NULL;
  1022. } else {
  1023. dev_err(dev, "Unable to get reset control: %d\n", ret);
  1024. return ret;
  1025. }
  1026. }
  1027. if (apb_rst) {
  1028. ret = clk_prepare_enable(dsi->pclk);
  1029. if (ret) {
  1030. dev_err(dev, "%s: Failed to enable pclk\n", __func__);
  1031. return ret;
  1032. }
  1033. reset_control_assert(apb_rst);
  1034. usleep_range(10, 20);
  1035. reset_control_deassert(apb_rst);
  1036. clk_disable_unprepare(dsi->pclk);
  1037. }
  1038. if (pdata->flags & DW_MIPI_NEEDS_PHY_CFG_CLK) {
  1039. dsi->phy_cfg_clk = devm_clk_get(dev, "phy_cfg");
  1040. if (IS_ERR(dsi->phy_cfg_clk)) {
  1041. ret = PTR_ERR(dsi->phy_cfg_clk);
  1042. dev_err(dev, "Unable to get phy_cfg_clk: %d\n", ret);
  1043. return ret;
  1044. }
  1045. }
  1046. if (pdata->flags & DW_MIPI_NEEDS_GRF_CLK) {
  1047. dsi->grf_clk = devm_clk_get(dev, "grf");
  1048. if (IS_ERR(dsi->grf_clk)) {
  1049. ret = PTR_ERR(dsi->grf_clk);
  1050. dev_err(dev, "Unable to get grf_clk: %d\n", ret);
  1051. return ret;
  1052. }
  1053. }
  1054. ret = clk_prepare_enable(dsi->pllref_clk);
  1055. if (ret) {
  1056. dev_err(dev, "%s: Failed to enable pllref_clk\n", __func__);
  1057. return ret;
  1058. }
  1059. ret = dw_mipi_dsi_register(drm, dsi);
  1060. if (ret) {
  1061. dev_err(dev, "Failed to register mipi_dsi: %d\n", ret);
  1062. goto err_pllref;
  1063. }
  1064. pm_runtime_enable(dev);
  1065. dsi->dsi_host.ops = &dw_mipi_dsi_host_ops;
  1066. dsi->dsi_host.dev = dev;
  1067. ret = mipi_dsi_host_register(&dsi->dsi_host);
  1068. if (ret) {
  1069. dev_err(dev, "Failed to register MIPI host: %d\n", ret);
  1070. goto err_cleanup;
  1071. }
  1072. if (!dsi->panel) {
  1073. ret = -EPROBE_DEFER;
  1074. goto err_mipi_dsi_host;
  1075. }
  1076. dev_set_drvdata(dev, dsi);
  1077. return 0;
  1078. err_mipi_dsi_host:
  1079. mipi_dsi_host_unregister(&dsi->dsi_host);
  1080. err_cleanup:
  1081. drm_encoder_cleanup(&dsi->encoder);
  1082. drm_connector_cleanup(&dsi->connector);
  1083. err_pllref:
  1084. clk_disable_unprepare(dsi->pllref_clk);
  1085. return ret;
  1086. }
  1087. static void dw_mipi_dsi_unbind(struct device *dev, struct device *master,
  1088. void *data)
  1089. {
  1090. struct dw_mipi_dsi *dsi = dev_get_drvdata(dev);
  1091. mipi_dsi_host_unregister(&dsi->dsi_host);
  1092. pm_runtime_disable(dev);
  1093. clk_disable_unprepare(dsi->pllref_clk);
  1094. }
  1095. static const struct component_ops dw_mipi_dsi_ops = {
  1096. .bind = dw_mipi_dsi_bind,
  1097. .unbind = dw_mipi_dsi_unbind,
  1098. };
  1099. static int dw_mipi_dsi_probe(struct platform_device *pdev)
  1100. {
  1101. return component_add(&pdev->dev, &dw_mipi_dsi_ops);
  1102. }
  1103. static int dw_mipi_dsi_remove(struct platform_device *pdev)
  1104. {
  1105. component_del(&pdev->dev, &dw_mipi_dsi_ops);
  1106. return 0;
  1107. }
  1108. struct platform_driver dw_mipi_dsi_driver = {
  1109. .probe = dw_mipi_dsi_probe,
  1110. .remove = dw_mipi_dsi_remove,
  1111. .driver = {
  1112. .of_match_table = dw_mipi_dsi_dt_ids,
  1113. .name = DRIVER_NAME,
  1114. },
  1115. };