dw-mipi-dsi.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2016, Fuzhou Rockchip Electronics Co., Ltd
  4. * Copyright (C) STMicroelectronics SA 2017
  5. *
  6. * Modified by Philippe Cornu <[email protected]>
  7. * This generic Synopsys DesignWare MIPI DSI host driver is based on the
  8. * Rockchip version from rockchip/dw-mipi-dsi.c with phy & bridge APIs.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/component.h>
  12. #include <linux/debugfs.h>
  13. #include <linux/iopoll.h>
  14. #include <linux/module.h>
  15. #include <linux/of_device.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/reset.h>
  18. #include <video/mipi_display.h>
  19. #include <drm/bridge/dw_mipi_dsi.h>
  20. #include <drm/drm_atomic_helper.h>
  21. #include <drm/drm_bridge.h>
  22. #include <drm/drm_crtc.h>
  23. #include <drm/drm_mipi_dsi.h>
  24. #include <drm/drm_modes.h>
  25. #include <drm/drm_of.h>
  26. #include <drm/drm_print.h>
  27. #define HWVER_131 0x31333100 /* IP version 1.31 */
  28. #define DSI_VERSION 0x00
  29. #define VERSION GENMASK(31, 8)
  30. #define DSI_PWR_UP 0x04
  31. #define RESET 0
  32. #define POWERUP BIT(0)
  33. #define DSI_CLKMGR_CFG 0x08
  34. #define TO_CLK_DIVISION(div) (((div) & 0xff) << 8)
  35. #define TX_ESC_CLK_DIVISION(div) ((div) & 0xff)
  36. #define DSI_DPI_VCID 0x0c
  37. #define DPI_VCID(vcid) ((vcid) & 0x3)
  38. #define DSI_DPI_COLOR_CODING 0x10
  39. #define LOOSELY18_EN BIT(8)
  40. #define DPI_COLOR_CODING_16BIT_1 0x0
  41. #define DPI_COLOR_CODING_16BIT_2 0x1
  42. #define DPI_COLOR_CODING_16BIT_3 0x2
  43. #define DPI_COLOR_CODING_18BIT_1 0x3
  44. #define DPI_COLOR_CODING_18BIT_2 0x4
  45. #define DPI_COLOR_CODING_24BIT 0x5
  46. #define DSI_DPI_CFG_POL 0x14
  47. #define COLORM_ACTIVE_LOW BIT(4)
  48. #define SHUTD_ACTIVE_LOW BIT(3)
  49. #define HSYNC_ACTIVE_LOW BIT(2)
  50. #define VSYNC_ACTIVE_LOW BIT(1)
  51. #define DATAEN_ACTIVE_LOW BIT(0)
  52. #define DSI_DPI_LP_CMD_TIM 0x18
  53. #define OUTVACT_LPCMD_TIME(p) (((p) & 0xff) << 16)
  54. #define INVACT_LPCMD_TIME(p) ((p) & 0xff)
  55. #define DSI_DBI_VCID 0x1c
  56. #define DSI_DBI_CFG 0x20
  57. #define DSI_DBI_PARTITIONING_EN 0x24
  58. #define DSI_DBI_CMDSIZE 0x28
  59. #define DSI_PCKHDL_CFG 0x2c
  60. #define CRC_RX_EN BIT(4)
  61. #define ECC_RX_EN BIT(3)
  62. #define BTA_EN BIT(2)
  63. #define EOTP_RX_EN BIT(1)
  64. #define EOTP_TX_EN BIT(0)
  65. #define DSI_GEN_VCID 0x30
  66. #define DSI_MODE_CFG 0x34
  67. #define ENABLE_VIDEO_MODE 0
  68. #define ENABLE_CMD_MODE BIT(0)
  69. #define DSI_VID_MODE_CFG 0x38
  70. #define ENABLE_LOW_POWER (0x3f << 8)
  71. #define ENABLE_LOW_POWER_MASK (0x3f << 8)
  72. #define VID_MODE_TYPE_NON_BURST_SYNC_PULSES 0x0
  73. #define VID_MODE_TYPE_NON_BURST_SYNC_EVENTS 0x1
  74. #define VID_MODE_TYPE_BURST 0x2
  75. #define VID_MODE_TYPE_MASK 0x3
  76. #define ENABLE_LOW_POWER_CMD BIT(15)
  77. #define VID_MODE_VPG_ENABLE BIT(16)
  78. #define VID_MODE_VPG_MODE BIT(20)
  79. #define VID_MODE_VPG_HORIZONTAL BIT(24)
  80. #define DSI_VID_PKT_SIZE 0x3c
  81. #define VID_PKT_SIZE(p) ((p) & 0x3fff)
  82. #define DSI_VID_NUM_CHUNKS 0x40
  83. #define VID_NUM_CHUNKS(c) ((c) & 0x1fff)
  84. #define DSI_VID_NULL_SIZE 0x44
  85. #define VID_NULL_SIZE(b) ((b) & 0x1fff)
  86. #define DSI_VID_HSA_TIME 0x48
  87. #define DSI_VID_HBP_TIME 0x4c
  88. #define DSI_VID_HLINE_TIME 0x50
  89. #define DSI_VID_VSA_LINES 0x54
  90. #define DSI_VID_VBP_LINES 0x58
  91. #define DSI_VID_VFP_LINES 0x5c
  92. #define DSI_VID_VACTIVE_LINES 0x60
  93. #define DSI_EDPI_CMD_SIZE 0x64
  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 ACK_RQST_EN BIT(1)
  108. #define TEAR_FX_EN 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 DSI_GEN_PLD_DATA 0x70
  123. #define DSI_CMD_PKT_STATUS 0x74
  124. #define GEN_RD_CMD_BUSY BIT(6)
  125. #define GEN_PLD_R_FULL BIT(5)
  126. #define GEN_PLD_R_EMPTY BIT(4)
  127. #define GEN_PLD_W_FULL BIT(3)
  128. #define GEN_PLD_W_EMPTY BIT(2)
  129. #define GEN_CMD_FULL BIT(1)
  130. #define GEN_CMD_EMPTY BIT(0)
  131. #define DSI_TO_CNT_CFG 0x78
  132. #define HSTX_TO_CNT(p) (((p) & 0xffff) << 16)
  133. #define LPRX_TO_CNT(p) ((p) & 0xffff)
  134. #define DSI_HS_RD_TO_CNT 0x7c
  135. #define DSI_LP_RD_TO_CNT 0x80
  136. #define DSI_HS_WR_TO_CNT 0x84
  137. #define DSI_LP_WR_TO_CNT 0x88
  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 PHY_HS2LP_TIME_V131(lbcc) (((lbcc) & 0x3ff) << 16)
  150. #define PHY_LP2HS_TIME_V131(lbcc) ((lbcc) & 0x3ff)
  151. #define DSI_PHY_RSTZ 0xa0
  152. #define PHY_DISFORCEPLL 0
  153. #define PHY_ENFORCEPLL BIT(3)
  154. #define PHY_DISABLECLK 0
  155. #define PHY_ENABLECLK BIT(2)
  156. #define PHY_RSTZ 0
  157. #define PHY_UNRSTZ BIT(1)
  158. #define PHY_SHUTDOWNZ 0
  159. #define PHY_UNSHUTDOWNZ BIT(0)
  160. #define DSI_PHY_IF_CFG 0xa4
  161. #define PHY_STOP_WAIT_TIME(cycle) (((cycle) & 0xff) << 8)
  162. #define N_LANES(n) (((n) - 1) & 0x3)
  163. #define DSI_PHY_ULPS_CTRL 0xa8
  164. #define DSI_PHY_TX_TRIGGERS 0xac
  165. #define DSI_PHY_STATUS 0xb0
  166. #define PHY_STOP_STATE_CLK_LANE BIT(2)
  167. #define PHY_LOCK BIT(0)
  168. #define DSI_PHY_TST_CTRL0 0xb4
  169. #define PHY_TESTCLK BIT(1)
  170. #define PHY_UNTESTCLK 0
  171. #define PHY_TESTCLR BIT(0)
  172. #define PHY_UNTESTCLR 0
  173. #define DSI_PHY_TST_CTRL1 0xb8
  174. #define PHY_TESTEN BIT(16)
  175. #define PHY_UNTESTEN 0
  176. #define PHY_TESTDOUT(n) (((n) & 0xff) << 8)
  177. #define PHY_TESTDIN(n) ((n) & 0xff)
  178. #define DSI_INT_ST0 0xbc
  179. #define DSI_INT_ST1 0xc0
  180. #define DSI_INT_MSK0 0xc4
  181. #define DSI_INT_MSK1 0xc8
  182. #define DSI_PHY_TMR_RD_CFG 0xf4
  183. #define MAX_RD_TIME_V131(lbcc) ((lbcc) & 0x7fff)
  184. #define PHY_STATUS_TIMEOUT_US 10000
  185. #define CMD_PKT_STATUS_TIMEOUT_US 20000
  186. #ifdef CONFIG_DEBUG_FS
  187. #define VPG_DEFS(name, dsi) \
  188. ((void __force *)&((*dsi).vpg_defs.name))
  189. #define REGISTER(name, mask, dsi) \
  190. { #name, VPG_DEFS(name, dsi), mask, dsi }
  191. struct debugfs_entries {
  192. const char *name;
  193. bool *reg;
  194. u32 mask;
  195. struct dw_mipi_dsi *dsi;
  196. };
  197. #endif /* CONFIG_DEBUG_FS */
  198. struct dw_mipi_dsi {
  199. struct drm_bridge bridge;
  200. struct mipi_dsi_host dsi_host;
  201. struct drm_bridge *panel_bridge;
  202. struct device *dev;
  203. void __iomem *base;
  204. struct clk *pclk;
  205. unsigned int lane_mbps; /* per lane */
  206. u32 channel;
  207. u32 lanes;
  208. u32 format;
  209. unsigned long mode_flags;
  210. #ifdef CONFIG_DEBUG_FS
  211. struct dentry *debugfs;
  212. struct debugfs_entries *debugfs_vpg;
  213. struct {
  214. bool vpg;
  215. bool vpg_horizontal;
  216. bool vpg_ber_pattern;
  217. } vpg_defs;
  218. #endif /* CONFIG_DEBUG_FS */
  219. struct dw_mipi_dsi *master; /* dual-dsi master ptr */
  220. struct dw_mipi_dsi *slave; /* dual-dsi slave ptr */
  221. const struct dw_mipi_dsi_plat_data *plat_data;
  222. };
  223. /*
  224. * Check if either a link to a master or slave is present
  225. */
  226. static inline bool dw_mipi_is_dual_mode(struct dw_mipi_dsi *dsi)
  227. {
  228. return dsi->slave || dsi->master;
  229. }
  230. /*
  231. * The controller should generate 2 frames before
  232. * preparing the peripheral.
  233. */
  234. static void dw_mipi_dsi_wait_for_two_frames(const struct drm_display_mode *mode)
  235. {
  236. int refresh, two_frames;
  237. refresh = drm_mode_vrefresh(mode);
  238. two_frames = DIV_ROUND_UP(MSEC_PER_SEC, refresh) * 2;
  239. msleep(two_frames);
  240. }
  241. static inline struct dw_mipi_dsi *host_to_dsi(struct mipi_dsi_host *host)
  242. {
  243. return container_of(host, struct dw_mipi_dsi, dsi_host);
  244. }
  245. static inline struct dw_mipi_dsi *bridge_to_dsi(struct drm_bridge *bridge)
  246. {
  247. return container_of(bridge, struct dw_mipi_dsi, bridge);
  248. }
  249. static inline void dsi_write(struct dw_mipi_dsi *dsi, u32 reg, u32 val)
  250. {
  251. writel(val, dsi->base + reg);
  252. }
  253. static inline u32 dsi_read(struct dw_mipi_dsi *dsi, u32 reg)
  254. {
  255. return readl(dsi->base + reg);
  256. }
  257. static int dw_mipi_dsi_host_attach(struct mipi_dsi_host *host,
  258. struct mipi_dsi_device *device)
  259. {
  260. struct dw_mipi_dsi *dsi = host_to_dsi(host);
  261. const struct dw_mipi_dsi_plat_data *pdata = dsi->plat_data;
  262. struct drm_bridge *bridge;
  263. int ret;
  264. if (device->lanes > dsi->plat_data->max_data_lanes) {
  265. dev_err(dsi->dev, "the number of data lanes(%u) is too many\n",
  266. device->lanes);
  267. return -EINVAL;
  268. }
  269. dsi->lanes = device->lanes;
  270. dsi->channel = device->channel;
  271. dsi->format = device->format;
  272. dsi->mode_flags = device->mode_flags;
  273. bridge = devm_drm_of_get_bridge(dsi->dev, dsi->dev->of_node, 1, 0);
  274. if (IS_ERR(bridge))
  275. return PTR_ERR(bridge);
  276. dsi->panel_bridge = bridge;
  277. drm_bridge_add(&dsi->bridge);
  278. if (pdata->host_ops && pdata->host_ops->attach) {
  279. ret = pdata->host_ops->attach(pdata->priv_data, device);
  280. if (ret < 0)
  281. return ret;
  282. }
  283. return 0;
  284. }
  285. static int dw_mipi_dsi_host_detach(struct mipi_dsi_host *host,
  286. struct mipi_dsi_device *device)
  287. {
  288. struct dw_mipi_dsi *dsi = host_to_dsi(host);
  289. const struct dw_mipi_dsi_plat_data *pdata = dsi->plat_data;
  290. int ret;
  291. if (pdata->host_ops && pdata->host_ops->detach) {
  292. ret = pdata->host_ops->detach(pdata->priv_data, device);
  293. if (ret < 0)
  294. return ret;
  295. }
  296. drm_of_panel_bridge_remove(host->dev->of_node, 1, 0);
  297. drm_bridge_remove(&dsi->bridge);
  298. return 0;
  299. }
  300. static void dw_mipi_message_config(struct dw_mipi_dsi *dsi,
  301. const struct mipi_dsi_msg *msg)
  302. {
  303. bool lpm = msg->flags & MIPI_DSI_MSG_USE_LPM;
  304. u32 val = 0;
  305. /*
  306. * TODO dw drv improvements
  307. * largest packet sizes during hfp or during vsa/vpb/vfp
  308. * should be computed according to byte lane, lane number and only
  309. * if sending lp cmds in high speed is enable (PHY_TXREQUESTCLKHS)
  310. */
  311. dsi_write(dsi, DSI_DPI_LP_CMD_TIM, OUTVACT_LPCMD_TIME(16)
  312. | INVACT_LPCMD_TIME(4));
  313. if (msg->flags & MIPI_DSI_MSG_REQ_ACK)
  314. val |= ACK_RQST_EN;
  315. if (lpm)
  316. val |= CMD_MODE_ALL_LP;
  317. dsi_write(dsi, DSI_CMD_MODE_CFG, val);
  318. val = dsi_read(dsi, DSI_VID_MODE_CFG);
  319. if (lpm)
  320. val |= ENABLE_LOW_POWER_CMD;
  321. else
  322. val &= ~ENABLE_LOW_POWER_CMD;
  323. dsi_write(dsi, DSI_VID_MODE_CFG, val);
  324. }
  325. static int dw_mipi_dsi_gen_pkt_hdr_write(struct dw_mipi_dsi *dsi, u32 hdr_val)
  326. {
  327. int ret;
  328. u32 val, mask;
  329. ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
  330. val, !(val & GEN_CMD_FULL), 1000,
  331. CMD_PKT_STATUS_TIMEOUT_US);
  332. if (ret) {
  333. dev_err(dsi->dev, "failed to get available command FIFO\n");
  334. return ret;
  335. }
  336. dsi_write(dsi, DSI_GEN_HDR, hdr_val);
  337. mask = GEN_CMD_EMPTY | GEN_PLD_W_EMPTY;
  338. ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
  339. val, (val & mask) == mask,
  340. 1000, CMD_PKT_STATUS_TIMEOUT_US);
  341. if (ret) {
  342. dev_err(dsi->dev, "failed to write command FIFO\n");
  343. return ret;
  344. }
  345. return 0;
  346. }
  347. static int dw_mipi_dsi_write(struct dw_mipi_dsi *dsi,
  348. const struct mipi_dsi_packet *packet)
  349. {
  350. const u8 *tx_buf = packet->payload;
  351. int len = packet->payload_length, pld_data_bytes = sizeof(u32), ret;
  352. __le32 word;
  353. u32 val;
  354. while (len) {
  355. if (len < pld_data_bytes) {
  356. word = 0;
  357. memcpy(&word, tx_buf, len);
  358. dsi_write(dsi, DSI_GEN_PLD_DATA, le32_to_cpu(word));
  359. len = 0;
  360. } else {
  361. memcpy(&word, tx_buf, pld_data_bytes);
  362. dsi_write(dsi, DSI_GEN_PLD_DATA, le32_to_cpu(word));
  363. tx_buf += pld_data_bytes;
  364. len -= pld_data_bytes;
  365. }
  366. ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
  367. val, !(val & GEN_PLD_W_FULL), 1000,
  368. CMD_PKT_STATUS_TIMEOUT_US);
  369. if (ret) {
  370. dev_err(dsi->dev,
  371. "failed to get available write payload FIFO\n");
  372. return ret;
  373. }
  374. }
  375. word = 0;
  376. memcpy(&word, packet->header, sizeof(packet->header));
  377. return dw_mipi_dsi_gen_pkt_hdr_write(dsi, le32_to_cpu(word));
  378. }
  379. static int dw_mipi_dsi_read(struct dw_mipi_dsi *dsi,
  380. const struct mipi_dsi_msg *msg)
  381. {
  382. int i, j, ret, len = msg->rx_len;
  383. u8 *buf = msg->rx_buf;
  384. u32 val;
  385. /* Wait end of the read operation */
  386. ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
  387. val, !(val & GEN_RD_CMD_BUSY),
  388. 1000, CMD_PKT_STATUS_TIMEOUT_US);
  389. if (ret) {
  390. dev_err(dsi->dev, "Timeout during read operation\n");
  391. return ret;
  392. }
  393. for (i = 0; i < len; i += 4) {
  394. /* Read fifo must not be empty before all bytes are read */
  395. ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
  396. val, !(val & GEN_PLD_R_EMPTY),
  397. 1000, CMD_PKT_STATUS_TIMEOUT_US);
  398. if (ret) {
  399. dev_err(dsi->dev, "Read payload FIFO is empty\n");
  400. return ret;
  401. }
  402. val = dsi_read(dsi, DSI_GEN_PLD_DATA);
  403. for (j = 0; j < 4 && j + i < len; j++)
  404. buf[i + j] = val >> (8 * j);
  405. }
  406. return ret;
  407. }
  408. static ssize_t dw_mipi_dsi_host_transfer(struct mipi_dsi_host *host,
  409. const struct mipi_dsi_msg *msg)
  410. {
  411. struct dw_mipi_dsi *dsi = host_to_dsi(host);
  412. struct mipi_dsi_packet packet;
  413. int ret, nb_bytes;
  414. ret = mipi_dsi_create_packet(&packet, msg);
  415. if (ret) {
  416. dev_err(dsi->dev, "failed to create packet: %d\n", ret);
  417. return ret;
  418. }
  419. dw_mipi_message_config(dsi, msg);
  420. if (dsi->slave)
  421. dw_mipi_message_config(dsi->slave, msg);
  422. ret = dw_mipi_dsi_write(dsi, &packet);
  423. if (ret)
  424. return ret;
  425. if (dsi->slave) {
  426. ret = dw_mipi_dsi_write(dsi->slave, &packet);
  427. if (ret)
  428. return ret;
  429. }
  430. if (msg->rx_buf && msg->rx_len) {
  431. ret = dw_mipi_dsi_read(dsi, msg);
  432. if (ret)
  433. return ret;
  434. nb_bytes = msg->rx_len;
  435. } else {
  436. nb_bytes = packet.size;
  437. }
  438. return nb_bytes;
  439. }
  440. static const struct mipi_dsi_host_ops dw_mipi_dsi_host_ops = {
  441. .attach = dw_mipi_dsi_host_attach,
  442. .detach = dw_mipi_dsi_host_detach,
  443. .transfer = dw_mipi_dsi_host_transfer,
  444. };
  445. static void dw_mipi_dsi_video_mode_config(struct dw_mipi_dsi *dsi)
  446. {
  447. u32 val;
  448. /*
  449. * TODO dw drv improvements
  450. * enabling low power is panel-dependent, we should use the
  451. * panel configuration here...
  452. */
  453. val = ENABLE_LOW_POWER;
  454. if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
  455. val |= VID_MODE_TYPE_BURST;
  456. else if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
  457. val |= VID_MODE_TYPE_NON_BURST_SYNC_PULSES;
  458. else
  459. val |= VID_MODE_TYPE_NON_BURST_SYNC_EVENTS;
  460. #ifdef CONFIG_DEBUG_FS
  461. if (dsi->vpg_defs.vpg) {
  462. val |= VID_MODE_VPG_ENABLE;
  463. val |= dsi->vpg_defs.vpg_horizontal ?
  464. VID_MODE_VPG_HORIZONTAL : 0;
  465. val |= dsi->vpg_defs.vpg_ber_pattern ? VID_MODE_VPG_MODE : 0;
  466. }
  467. #endif /* CONFIG_DEBUG_FS */
  468. dsi_write(dsi, DSI_VID_MODE_CFG, val);
  469. }
  470. static void dw_mipi_dsi_set_mode(struct dw_mipi_dsi *dsi,
  471. unsigned long mode_flags)
  472. {
  473. u32 val;
  474. dsi_write(dsi, DSI_PWR_UP, RESET);
  475. if (mode_flags & MIPI_DSI_MODE_VIDEO) {
  476. dsi_write(dsi, DSI_MODE_CFG, ENABLE_VIDEO_MODE);
  477. dw_mipi_dsi_video_mode_config(dsi);
  478. } else {
  479. dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
  480. }
  481. val = PHY_TXREQUESTCLKHS;
  482. if (dsi->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS)
  483. val |= AUTO_CLKLANE_CTRL;
  484. dsi_write(dsi, DSI_LPCLK_CTRL, val);
  485. dsi_write(dsi, DSI_PWR_UP, POWERUP);
  486. }
  487. static void dw_mipi_dsi_disable(struct dw_mipi_dsi *dsi)
  488. {
  489. dsi_write(dsi, DSI_PWR_UP, RESET);
  490. dsi_write(dsi, DSI_PHY_RSTZ, PHY_RSTZ);
  491. }
  492. static void dw_mipi_dsi_init(struct dw_mipi_dsi *dsi)
  493. {
  494. const struct dw_mipi_dsi_phy_ops *phy_ops = dsi->plat_data->phy_ops;
  495. unsigned int esc_rate; /* in MHz */
  496. u32 esc_clk_division;
  497. int ret;
  498. /*
  499. * The maximum permitted escape clock is 20MHz and it is derived from
  500. * lanebyteclk, which is running at "lane_mbps / 8".
  501. */
  502. if (phy_ops->get_esc_clk_rate) {
  503. ret = phy_ops->get_esc_clk_rate(dsi->plat_data->priv_data,
  504. &esc_rate);
  505. if (ret)
  506. DRM_DEBUG_DRIVER("Phy get_esc_clk_rate() failed\n");
  507. } else
  508. esc_rate = 20; /* Default to 20MHz */
  509. /*
  510. * We want :
  511. * (lane_mbps >> 3) / esc_clk_division < X
  512. * which is:
  513. * (lane_mbps >> 3) / X > esc_clk_division
  514. */
  515. esc_clk_division = (dsi->lane_mbps >> 3) / esc_rate + 1;
  516. dsi_write(dsi, DSI_PWR_UP, RESET);
  517. /*
  518. * TODO dw drv improvements
  519. * timeout clock division should be computed with the
  520. * high speed transmission counter timeout and byte lane...
  521. */
  522. dsi_write(dsi, DSI_CLKMGR_CFG, TO_CLK_DIVISION(10) |
  523. TX_ESC_CLK_DIVISION(esc_clk_division));
  524. }
  525. static void dw_mipi_dsi_dpi_config(struct dw_mipi_dsi *dsi,
  526. const struct drm_display_mode *mode)
  527. {
  528. u32 val = 0, color = 0;
  529. switch (dsi->format) {
  530. case MIPI_DSI_FMT_RGB888:
  531. color = DPI_COLOR_CODING_24BIT;
  532. break;
  533. case MIPI_DSI_FMT_RGB666:
  534. color = DPI_COLOR_CODING_18BIT_2 | LOOSELY18_EN;
  535. break;
  536. case MIPI_DSI_FMT_RGB666_PACKED:
  537. color = DPI_COLOR_CODING_18BIT_1;
  538. break;
  539. case MIPI_DSI_FMT_RGB565:
  540. color = DPI_COLOR_CODING_16BIT_1;
  541. break;
  542. }
  543. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  544. val |= VSYNC_ACTIVE_LOW;
  545. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  546. val |= HSYNC_ACTIVE_LOW;
  547. dsi_write(dsi, DSI_DPI_VCID, DPI_VCID(dsi->channel));
  548. dsi_write(dsi, DSI_DPI_COLOR_CODING, color);
  549. dsi_write(dsi, DSI_DPI_CFG_POL, val);
  550. }
  551. static void dw_mipi_dsi_packet_handler_config(struct dw_mipi_dsi *dsi)
  552. {
  553. dsi_write(dsi, DSI_PCKHDL_CFG, CRC_RX_EN | ECC_RX_EN | BTA_EN);
  554. }
  555. static void dw_mipi_dsi_video_packet_config(struct dw_mipi_dsi *dsi,
  556. const struct drm_display_mode *mode)
  557. {
  558. /*
  559. * TODO dw drv improvements
  560. * only burst mode is supported here. For non-burst video modes,
  561. * we should compute DSI_VID_PKT_SIZE, DSI_VCCR.NUMC &
  562. * DSI_VNPCR.NPSIZE... especially because this driver supports
  563. * non-burst video modes, see dw_mipi_dsi_video_mode_config()...
  564. */
  565. dsi_write(dsi, DSI_VID_PKT_SIZE,
  566. dw_mipi_is_dual_mode(dsi) ?
  567. VID_PKT_SIZE(mode->hdisplay / 2) :
  568. VID_PKT_SIZE(mode->hdisplay));
  569. }
  570. static void dw_mipi_dsi_command_mode_config(struct dw_mipi_dsi *dsi)
  571. {
  572. /*
  573. * TODO dw drv improvements
  574. * compute high speed transmission counter timeout according
  575. * to the timeout clock division (TO_CLK_DIVISION) and byte lane...
  576. */
  577. dsi_write(dsi, DSI_TO_CNT_CFG, HSTX_TO_CNT(1000) | LPRX_TO_CNT(1000));
  578. /*
  579. * TODO dw drv improvements
  580. * the Bus-Turn-Around Timeout Counter should be computed
  581. * according to byte lane...
  582. */
  583. dsi_write(dsi, DSI_BTA_TO_CNT, 0xd00);
  584. dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
  585. }
  586. /* Get lane byte clock cycles. */
  587. static u32 dw_mipi_dsi_get_hcomponent_lbcc(struct dw_mipi_dsi *dsi,
  588. const struct drm_display_mode *mode,
  589. u32 hcomponent)
  590. {
  591. u32 frac, lbcc;
  592. lbcc = hcomponent * dsi->lane_mbps * MSEC_PER_SEC / 8;
  593. frac = lbcc % mode->clock;
  594. lbcc = lbcc / mode->clock;
  595. if (frac)
  596. lbcc++;
  597. return lbcc;
  598. }
  599. static void dw_mipi_dsi_line_timer_config(struct dw_mipi_dsi *dsi,
  600. const struct drm_display_mode *mode)
  601. {
  602. u32 htotal, hsa, hbp, lbcc;
  603. htotal = mode->htotal;
  604. hsa = mode->hsync_end - mode->hsync_start;
  605. hbp = mode->htotal - mode->hsync_end;
  606. /*
  607. * TODO dw drv improvements
  608. * computations below may be improved...
  609. */
  610. lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, htotal);
  611. dsi_write(dsi, DSI_VID_HLINE_TIME, lbcc);
  612. lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, hsa);
  613. dsi_write(dsi, DSI_VID_HSA_TIME, lbcc);
  614. lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, hbp);
  615. dsi_write(dsi, DSI_VID_HBP_TIME, lbcc);
  616. }
  617. static void dw_mipi_dsi_vertical_timing_config(struct dw_mipi_dsi *dsi,
  618. const struct drm_display_mode *mode)
  619. {
  620. u32 vactive, vsa, vfp, vbp;
  621. vactive = mode->vdisplay;
  622. vsa = mode->vsync_end - mode->vsync_start;
  623. vfp = mode->vsync_start - mode->vdisplay;
  624. vbp = mode->vtotal - mode->vsync_end;
  625. dsi_write(dsi, DSI_VID_VACTIVE_LINES, vactive);
  626. dsi_write(dsi, DSI_VID_VSA_LINES, vsa);
  627. dsi_write(dsi, DSI_VID_VFP_LINES, vfp);
  628. dsi_write(dsi, DSI_VID_VBP_LINES, vbp);
  629. }
  630. static void dw_mipi_dsi_dphy_timing_config(struct dw_mipi_dsi *dsi)
  631. {
  632. const struct dw_mipi_dsi_phy_ops *phy_ops = dsi->plat_data->phy_ops;
  633. struct dw_mipi_dsi_dphy_timing timing;
  634. u32 hw_version;
  635. int ret;
  636. ret = phy_ops->get_timing(dsi->plat_data->priv_data,
  637. dsi->lane_mbps, &timing);
  638. if (ret)
  639. DRM_DEV_ERROR(dsi->dev, "Retrieving phy timings failed\n");
  640. /*
  641. * TODO dw drv improvements
  642. * data & clock lane timers should be computed according to panel
  643. * blankings and to the automatic clock lane control mode...
  644. * note: DSI_PHY_TMR_CFG.MAX_RD_TIME should be in line with
  645. * DSI_CMD_MODE_CFG.MAX_RD_PKT_SIZE_LP (see CMD_MODE_ALL_LP)
  646. */
  647. hw_version = dsi_read(dsi, DSI_VERSION) & VERSION;
  648. if (hw_version >= HWVER_131) {
  649. dsi_write(dsi, DSI_PHY_TMR_CFG,
  650. PHY_HS2LP_TIME_V131(timing.data_hs2lp) |
  651. PHY_LP2HS_TIME_V131(timing.data_lp2hs));
  652. dsi_write(dsi, DSI_PHY_TMR_RD_CFG, MAX_RD_TIME_V131(10000));
  653. } else {
  654. dsi_write(dsi, DSI_PHY_TMR_CFG,
  655. PHY_HS2LP_TIME(timing.data_hs2lp) |
  656. PHY_LP2HS_TIME(timing.data_lp2hs) |
  657. MAX_RD_TIME(10000));
  658. }
  659. dsi_write(dsi, DSI_PHY_TMR_LPCLK_CFG,
  660. PHY_CLKHS2LP_TIME(timing.clk_hs2lp) |
  661. PHY_CLKLP2HS_TIME(timing.clk_lp2hs));
  662. }
  663. static void dw_mipi_dsi_dphy_interface_config(struct dw_mipi_dsi *dsi)
  664. {
  665. /*
  666. * TODO dw drv improvements
  667. * stop wait time should be the maximum between host dsi
  668. * and panel stop wait times
  669. */
  670. dsi_write(dsi, DSI_PHY_IF_CFG, PHY_STOP_WAIT_TIME(0x20) |
  671. N_LANES(dsi->lanes));
  672. }
  673. static void dw_mipi_dsi_dphy_init(struct dw_mipi_dsi *dsi)
  674. {
  675. /* Clear PHY state */
  676. dsi_write(dsi, DSI_PHY_RSTZ, PHY_DISFORCEPLL | PHY_DISABLECLK
  677. | PHY_RSTZ | PHY_SHUTDOWNZ);
  678. dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_UNTESTCLR);
  679. dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_TESTCLR);
  680. dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_UNTESTCLR);
  681. }
  682. static void dw_mipi_dsi_dphy_enable(struct dw_mipi_dsi *dsi)
  683. {
  684. u32 val;
  685. int ret;
  686. dsi_write(dsi, DSI_PHY_RSTZ, PHY_ENFORCEPLL | PHY_ENABLECLK |
  687. PHY_UNRSTZ | PHY_UNSHUTDOWNZ);
  688. ret = readl_poll_timeout(dsi->base + DSI_PHY_STATUS, val,
  689. val & PHY_LOCK, 1000, PHY_STATUS_TIMEOUT_US);
  690. if (ret)
  691. DRM_DEBUG_DRIVER("failed to wait phy lock state\n");
  692. ret = readl_poll_timeout(dsi->base + DSI_PHY_STATUS,
  693. val, val & PHY_STOP_STATE_CLK_LANE, 1000,
  694. PHY_STATUS_TIMEOUT_US);
  695. if (ret)
  696. DRM_DEBUG_DRIVER("failed to wait phy clk lane stop state\n");
  697. }
  698. static void dw_mipi_dsi_clear_err(struct dw_mipi_dsi *dsi)
  699. {
  700. dsi_read(dsi, DSI_INT_ST0);
  701. dsi_read(dsi, DSI_INT_ST1);
  702. dsi_write(dsi, DSI_INT_MSK0, 0);
  703. dsi_write(dsi, DSI_INT_MSK1, 0);
  704. }
  705. static void dw_mipi_dsi_bridge_post_atomic_disable(struct drm_bridge *bridge,
  706. struct drm_bridge_state *old_bridge_state)
  707. {
  708. struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
  709. const struct dw_mipi_dsi_phy_ops *phy_ops = dsi->plat_data->phy_ops;
  710. /*
  711. * Switch to command mode before panel-bridge post_disable &
  712. * panel unprepare.
  713. * Note: panel-bridge disable & panel disable has been called
  714. * before by the drm framework.
  715. */
  716. dw_mipi_dsi_set_mode(dsi, 0);
  717. /*
  718. * TODO Only way found to call panel-bridge post_disable &
  719. * panel unprepare before the dsi "final" disable...
  720. * This needs to be fixed in the drm_bridge framework and the API
  721. * needs to be updated to manage our own call chains...
  722. */
  723. if (dsi->panel_bridge->funcs->post_disable)
  724. dsi->panel_bridge->funcs->post_disable(dsi->panel_bridge);
  725. if (phy_ops->power_off)
  726. phy_ops->power_off(dsi->plat_data->priv_data);
  727. if (dsi->slave) {
  728. dw_mipi_dsi_disable(dsi->slave);
  729. clk_disable_unprepare(dsi->slave->pclk);
  730. pm_runtime_put(dsi->slave->dev);
  731. }
  732. dw_mipi_dsi_disable(dsi);
  733. clk_disable_unprepare(dsi->pclk);
  734. pm_runtime_put(dsi->dev);
  735. }
  736. static unsigned int dw_mipi_dsi_get_lanes(struct dw_mipi_dsi *dsi)
  737. {
  738. /* this instance is the slave, so add the master's lanes */
  739. if (dsi->master)
  740. return dsi->master->lanes + dsi->lanes;
  741. /* this instance is the master, so add the slave's lanes */
  742. if (dsi->slave)
  743. return dsi->lanes + dsi->slave->lanes;
  744. /* single-dsi, so no other instance to consider */
  745. return dsi->lanes;
  746. }
  747. static void dw_mipi_dsi_mode_set(struct dw_mipi_dsi *dsi,
  748. const struct drm_display_mode *adjusted_mode)
  749. {
  750. const struct dw_mipi_dsi_phy_ops *phy_ops = dsi->plat_data->phy_ops;
  751. void *priv_data = dsi->plat_data->priv_data;
  752. int ret;
  753. u32 lanes = dw_mipi_dsi_get_lanes(dsi);
  754. clk_prepare_enable(dsi->pclk);
  755. ret = phy_ops->get_lane_mbps(priv_data, adjusted_mode, dsi->mode_flags,
  756. lanes, dsi->format, &dsi->lane_mbps);
  757. if (ret)
  758. DRM_DEBUG_DRIVER("Phy get_lane_mbps() failed\n");
  759. pm_runtime_get_sync(dsi->dev);
  760. dw_mipi_dsi_init(dsi);
  761. dw_mipi_dsi_dpi_config(dsi, adjusted_mode);
  762. dw_mipi_dsi_packet_handler_config(dsi);
  763. dw_mipi_dsi_video_mode_config(dsi);
  764. dw_mipi_dsi_video_packet_config(dsi, adjusted_mode);
  765. dw_mipi_dsi_command_mode_config(dsi);
  766. dw_mipi_dsi_line_timer_config(dsi, adjusted_mode);
  767. dw_mipi_dsi_vertical_timing_config(dsi, adjusted_mode);
  768. dw_mipi_dsi_dphy_init(dsi);
  769. dw_mipi_dsi_dphy_timing_config(dsi);
  770. dw_mipi_dsi_dphy_interface_config(dsi);
  771. dw_mipi_dsi_clear_err(dsi);
  772. ret = phy_ops->init(priv_data);
  773. if (ret)
  774. DRM_DEBUG_DRIVER("Phy init() failed\n");
  775. dw_mipi_dsi_dphy_enable(dsi);
  776. dw_mipi_dsi_wait_for_two_frames(adjusted_mode);
  777. /* Switch to cmd mode for panel-bridge pre_enable & panel prepare */
  778. dw_mipi_dsi_set_mode(dsi, 0);
  779. if (phy_ops->power_on)
  780. phy_ops->power_on(dsi->plat_data->priv_data);
  781. }
  782. static void dw_mipi_dsi_bridge_mode_set(struct drm_bridge *bridge,
  783. const struct drm_display_mode *mode,
  784. const struct drm_display_mode *adjusted_mode)
  785. {
  786. struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
  787. dw_mipi_dsi_mode_set(dsi, adjusted_mode);
  788. if (dsi->slave)
  789. dw_mipi_dsi_mode_set(dsi->slave, adjusted_mode);
  790. }
  791. static void dw_mipi_dsi_bridge_atomic_enable(struct drm_bridge *bridge,
  792. struct drm_bridge_state *old_bridge_state)
  793. {
  794. struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
  795. /* Switch to video mode for panel-bridge enable & panel enable */
  796. dw_mipi_dsi_set_mode(dsi, MIPI_DSI_MODE_VIDEO);
  797. if (dsi->slave)
  798. dw_mipi_dsi_set_mode(dsi->slave, MIPI_DSI_MODE_VIDEO);
  799. }
  800. static enum drm_mode_status
  801. dw_mipi_dsi_bridge_mode_valid(struct drm_bridge *bridge,
  802. const struct drm_display_info *info,
  803. const struct drm_display_mode *mode)
  804. {
  805. struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
  806. const struct dw_mipi_dsi_plat_data *pdata = dsi->plat_data;
  807. enum drm_mode_status mode_status = MODE_OK;
  808. if (pdata->mode_valid)
  809. mode_status = pdata->mode_valid(pdata->priv_data, mode,
  810. dsi->mode_flags,
  811. dw_mipi_dsi_get_lanes(dsi),
  812. dsi->format);
  813. return mode_status;
  814. }
  815. static int dw_mipi_dsi_bridge_attach(struct drm_bridge *bridge,
  816. enum drm_bridge_attach_flags flags)
  817. {
  818. struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
  819. if (!bridge->encoder) {
  820. DRM_ERROR("Parent encoder object not found\n");
  821. return -ENODEV;
  822. }
  823. /* Set the encoder type as caller does not know it */
  824. bridge->encoder->encoder_type = DRM_MODE_ENCODER_DSI;
  825. /* Attach the panel-bridge to the dsi bridge */
  826. return drm_bridge_attach(bridge->encoder, dsi->panel_bridge, bridge,
  827. flags);
  828. }
  829. static const struct drm_bridge_funcs dw_mipi_dsi_bridge_funcs = {
  830. .atomic_duplicate_state = drm_atomic_helper_bridge_duplicate_state,
  831. .atomic_destroy_state = drm_atomic_helper_bridge_destroy_state,
  832. .atomic_reset = drm_atomic_helper_bridge_reset,
  833. .atomic_enable = dw_mipi_dsi_bridge_atomic_enable,
  834. .atomic_post_disable = dw_mipi_dsi_bridge_post_atomic_disable,
  835. .mode_set = dw_mipi_dsi_bridge_mode_set,
  836. .mode_valid = dw_mipi_dsi_bridge_mode_valid,
  837. .attach = dw_mipi_dsi_bridge_attach,
  838. };
  839. #ifdef CONFIG_DEBUG_FS
  840. static int dw_mipi_dsi_debugfs_write(void *data, u64 val)
  841. {
  842. struct debugfs_entries *vpg = data;
  843. struct dw_mipi_dsi *dsi;
  844. u32 mode_cfg;
  845. if (!vpg)
  846. return -ENODEV;
  847. dsi = vpg->dsi;
  848. *vpg->reg = (bool)val;
  849. mode_cfg = dsi_read(dsi, DSI_VID_MODE_CFG);
  850. if (*vpg->reg)
  851. mode_cfg |= vpg->mask;
  852. else
  853. mode_cfg &= ~vpg->mask;
  854. dsi_write(dsi, DSI_VID_MODE_CFG, mode_cfg);
  855. return 0;
  856. }
  857. static int dw_mipi_dsi_debugfs_show(void *data, u64 *val)
  858. {
  859. struct debugfs_entries *vpg = data;
  860. if (!vpg)
  861. return -ENODEV;
  862. *val = *vpg->reg;
  863. return 0;
  864. }
  865. DEFINE_DEBUGFS_ATTRIBUTE(fops_x32, dw_mipi_dsi_debugfs_show,
  866. dw_mipi_dsi_debugfs_write, "%llu\n");
  867. static void debugfs_create_files(void *data)
  868. {
  869. struct dw_mipi_dsi *dsi = data;
  870. struct debugfs_entries debugfs[] = {
  871. REGISTER(vpg, VID_MODE_VPG_ENABLE, dsi),
  872. REGISTER(vpg_horizontal, VID_MODE_VPG_HORIZONTAL, dsi),
  873. REGISTER(vpg_ber_pattern, VID_MODE_VPG_MODE, dsi),
  874. };
  875. int i;
  876. dsi->debugfs_vpg = kmemdup(debugfs, sizeof(debugfs), GFP_KERNEL);
  877. if (!dsi->debugfs_vpg)
  878. return;
  879. for (i = 0; i < ARRAY_SIZE(debugfs); i++)
  880. debugfs_create_file(dsi->debugfs_vpg[i].name, 0644,
  881. dsi->debugfs, &dsi->debugfs_vpg[i],
  882. &fops_x32);
  883. }
  884. static void dw_mipi_dsi_debugfs_init(struct dw_mipi_dsi *dsi)
  885. {
  886. dsi->debugfs = debugfs_create_dir(dev_name(dsi->dev), NULL);
  887. if (IS_ERR(dsi->debugfs)) {
  888. dev_err(dsi->dev, "failed to create debugfs root\n");
  889. return;
  890. }
  891. debugfs_create_files(dsi);
  892. }
  893. static void dw_mipi_dsi_debugfs_remove(struct dw_mipi_dsi *dsi)
  894. {
  895. debugfs_remove_recursive(dsi->debugfs);
  896. kfree(dsi->debugfs_vpg);
  897. }
  898. #else
  899. static void dw_mipi_dsi_debugfs_init(struct dw_mipi_dsi *dsi) { }
  900. static void dw_mipi_dsi_debugfs_remove(struct dw_mipi_dsi *dsi) { }
  901. #endif /* CONFIG_DEBUG_FS */
  902. static struct dw_mipi_dsi *
  903. __dw_mipi_dsi_probe(struct platform_device *pdev,
  904. const struct dw_mipi_dsi_plat_data *plat_data)
  905. {
  906. struct device *dev = &pdev->dev;
  907. struct reset_control *apb_rst;
  908. struct dw_mipi_dsi *dsi;
  909. int ret;
  910. dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
  911. if (!dsi)
  912. return ERR_PTR(-ENOMEM);
  913. dsi->dev = dev;
  914. dsi->plat_data = plat_data;
  915. if (!plat_data->phy_ops->init || !plat_data->phy_ops->get_lane_mbps ||
  916. !plat_data->phy_ops->get_timing) {
  917. DRM_ERROR("Phy not properly configured\n");
  918. return ERR_PTR(-ENODEV);
  919. }
  920. if (!plat_data->base) {
  921. dsi->base = devm_platform_ioremap_resource(pdev, 0);
  922. if (IS_ERR(dsi->base))
  923. return ERR_PTR(-ENODEV);
  924. } else {
  925. dsi->base = plat_data->base;
  926. }
  927. dsi->pclk = devm_clk_get(dev, "pclk");
  928. if (IS_ERR(dsi->pclk)) {
  929. ret = PTR_ERR(dsi->pclk);
  930. dev_err(dev, "Unable to get pclk: %d\n", ret);
  931. return ERR_PTR(ret);
  932. }
  933. /*
  934. * Note that the reset was not defined in the initial device tree, so
  935. * we have to be prepared for it not being found.
  936. */
  937. apb_rst = devm_reset_control_get_optional_exclusive(dev, "apb");
  938. if (IS_ERR(apb_rst)) {
  939. ret = PTR_ERR(apb_rst);
  940. if (ret != -EPROBE_DEFER)
  941. dev_err(dev, "Unable to get reset control: %d\n", ret);
  942. return ERR_PTR(ret);
  943. }
  944. if (apb_rst) {
  945. ret = clk_prepare_enable(dsi->pclk);
  946. if (ret) {
  947. dev_err(dev, "%s: Failed to enable pclk\n", __func__);
  948. return ERR_PTR(ret);
  949. }
  950. reset_control_assert(apb_rst);
  951. usleep_range(10, 20);
  952. reset_control_deassert(apb_rst);
  953. clk_disable_unprepare(dsi->pclk);
  954. }
  955. dw_mipi_dsi_debugfs_init(dsi);
  956. pm_runtime_enable(dev);
  957. dsi->dsi_host.ops = &dw_mipi_dsi_host_ops;
  958. dsi->dsi_host.dev = dev;
  959. ret = mipi_dsi_host_register(&dsi->dsi_host);
  960. if (ret) {
  961. dev_err(dev, "Failed to register MIPI host: %d\n", ret);
  962. pm_runtime_disable(dev);
  963. dw_mipi_dsi_debugfs_remove(dsi);
  964. return ERR_PTR(ret);
  965. }
  966. dsi->bridge.driver_private = dsi;
  967. dsi->bridge.funcs = &dw_mipi_dsi_bridge_funcs;
  968. #ifdef CONFIG_OF
  969. dsi->bridge.of_node = pdev->dev.of_node;
  970. #endif
  971. return dsi;
  972. }
  973. static void __dw_mipi_dsi_remove(struct dw_mipi_dsi *dsi)
  974. {
  975. mipi_dsi_host_unregister(&dsi->dsi_host);
  976. pm_runtime_disable(dsi->dev);
  977. dw_mipi_dsi_debugfs_remove(dsi);
  978. }
  979. void dw_mipi_dsi_set_slave(struct dw_mipi_dsi *dsi, struct dw_mipi_dsi *slave)
  980. {
  981. /* introduce controllers to each other */
  982. dsi->slave = slave;
  983. dsi->slave->master = dsi;
  984. /* migrate settings for already attached displays */
  985. dsi->slave->lanes = dsi->lanes;
  986. dsi->slave->channel = dsi->channel;
  987. dsi->slave->format = dsi->format;
  988. dsi->slave->mode_flags = dsi->mode_flags;
  989. }
  990. EXPORT_SYMBOL_GPL(dw_mipi_dsi_set_slave);
  991. /*
  992. * Probe/remove API, used from platforms based on the DRM bridge API.
  993. */
  994. struct dw_mipi_dsi *
  995. dw_mipi_dsi_probe(struct platform_device *pdev,
  996. const struct dw_mipi_dsi_plat_data *plat_data)
  997. {
  998. return __dw_mipi_dsi_probe(pdev, plat_data);
  999. }
  1000. EXPORT_SYMBOL_GPL(dw_mipi_dsi_probe);
  1001. void dw_mipi_dsi_remove(struct dw_mipi_dsi *dsi)
  1002. {
  1003. __dw_mipi_dsi_remove(dsi);
  1004. }
  1005. EXPORT_SYMBOL_GPL(dw_mipi_dsi_remove);
  1006. /*
  1007. * Bind/unbind API, used from platforms based on the component framework.
  1008. */
  1009. int dw_mipi_dsi_bind(struct dw_mipi_dsi *dsi, struct drm_encoder *encoder)
  1010. {
  1011. return drm_bridge_attach(encoder, &dsi->bridge, NULL, 0);
  1012. }
  1013. EXPORT_SYMBOL_GPL(dw_mipi_dsi_bind);
  1014. void dw_mipi_dsi_unbind(struct dw_mipi_dsi *dsi)
  1015. {
  1016. }
  1017. EXPORT_SYMBOL_GPL(dw_mipi_dsi_unbind);
  1018. MODULE_AUTHOR("Chris Zhong <[email protected]>");
  1019. MODULE_AUTHOR("Philippe Cornu <[email protected]>");
  1020. MODULE_DESCRIPTION("DW MIPI DSI host controller driver");
  1021. MODULE_LICENSE("GPL");
  1022. MODULE_ALIAS("platform:dw-mipi-dsi");