dw-mipi-dsi.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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 <philippe.cornu@st.com>
  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/iopoll.h>
  13. #include <linux/module.h>
  14. #include <linux/of_device.h>
  15. #include <linux/pm_runtime.h>
  16. #include <linux/reset.h>
  17. #include <drm/drmP.h>
  18. #include <drm/drm_atomic_helper.h>
  19. #include <drm/drm_bridge.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/bridge/dw_mipi_dsi.h>
  25. #include <video/mipi_display.h>
  26. #define HWVER_131 0x31333100 /* IP version 1.31 */
  27. #define DSI_VERSION 0x00
  28. #define VERSION GENMASK(31, 8)
  29. #define DSI_PWR_UP 0x04
  30. #define RESET 0
  31. #define POWERUP BIT(0)
  32. #define DSI_CLKMGR_CFG 0x08
  33. #define TO_CLK_DIVISION(div) (((div) & 0xff) << 8)
  34. #define TX_ESC_CLK_DIVISION(div) ((div) & 0xff)
  35. #define DSI_DPI_VCID 0x0c
  36. #define DPI_VCID(vcid) ((vcid) & 0x3)
  37. #define DSI_DPI_COLOR_CODING 0x10
  38. #define LOOSELY18_EN BIT(8)
  39. #define DPI_COLOR_CODING_16BIT_1 0x0
  40. #define DPI_COLOR_CODING_16BIT_2 0x1
  41. #define DPI_COLOR_CODING_16BIT_3 0x2
  42. #define DPI_COLOR_CODING_18BIT_1 0x3
  43. #define DPI_COLOR_CODING_18BIT_2 0x4
  44. #define DPI_COLOR_CODING_24BIT 0x5
  45. #define DSI_DPI_CFG_POL 0x14
  46. #define COLORM_ACTIVE_LOW BIT(4)
  47. #define SHUTD_ACTIVE_LOW BIT(3)
  48. #define HSYNC_ACTIVE_LOW BIT(2)
  49. #define VSYNC_ACTIVE_LOW BIT(1)
  50. #define DATAEN_ACTIVE_LOW BIT(0)
  51. #define DSI_DPI_LP_CMD_TIM 0x18
  52. #define OUTVACT_LPCMD_TIME(p) (((p) & 0xff) << 16)
  53. #define INVACT_LPCMD_TIME(p) ((p) & 0xff)
  54. #define DSI_DBI_VCID 0x1c
  55. #define DSI_DBI_CFG 0x20
  56. #define DSI_DBI_PARTITIONING_EN 0x24
  57. #define DSI_DBI_CMDSIZE 0x28
  58. #define DSI_PCKHDL_CFG 0x2c
  59. #define CRC_RX_EN BIT(4)
  60. #define ECC_RX_EN BIT(3)
  61. #define BTA_EN BIT(2)
  62. #define EOTP_RX_EN BIT(1)
  63. #define EOTP_TX_EN BIT(0)
  64. #define DSI_GEN_VCID 0x30
  65. #define DSI_MODE_CFG 0x34
  66. #define ENABLE_VIDEO_MODE 0
  67. #define ENABLE_CMD_MODE BIT(0)
  68. #define DSI_VID_MODE_CFG 0x38
  69. #define ENABLE_LOW_POWER (0x3f << 8)
  70. #define ENABLE_LOW_POWER_MASK (0x3f << 8)
  71. #define VID_MODE_TYPE_NON_BURST_SYNC_PULSES 0x0
  72. #define VID_MODE_TYPE_NON_BURST_SYNC_EVENTS 0x1
  73. #define VID_MODE_TYPE_BURST 0x2
  74. #define VID_MODE_TYPE_MASK 0x3
  75. #define DSI_VID_PKT_SIZE 0x3c
  76. #define VID_PKT_SIZE(p) ((p) & 0x3fff)
  77. #define DSI_VID_NUM_CHUNKS 0x40
  78. #define VID_NUM_CHUNKS(c) ((c) & 0x1fff)
  79. #define DSI_VID_NULL_SIZE 0x44
  80. #define VID_NULL_SIZE(b) ((b) & 0x1fff)
  81. #define DSI_VID_HSA_TIME 0x48
  82. #define DSI_VID_HBP_TIME 0x4c
  83. #define DSI_VID_HLINE_TIME 0x50
  84. #define DSI_VID_VSA_LINES 0x54
  85. #define DSI_VID_VBP_LINES 0x58
  86. #define DSI_VID_VFP_LINES 0x5c
  87. #define DSI_VID_VACTIVE_LINES 0x60
  88. #define DSI_EDPI_CMD_SIZE 0x64
  89. #define DSI_CMD_MODE_CFG 0x68
  90. #define MAX_RD_PKT_SIZE_LP BIT(24)
  91. #define DCS_LW_TX_LP BIT(19)
  92. #define DCS_SR_0P_TX_LP BIT(18)
  93. #define DCS_SW_1P_TX_LP BIT(17)
  94. #define DCS_SW_0P_TX_LP BIT(16)
  95. #define GEN_LW_TX_LP BIT(14)
  96. #define GEN_SR_2P_TX_LP BIT(13)
  97. #define GEN_SR_1P_TX_LP BIT(12)
  98. #define GEN_SR_0P_TX_LP BIT(11)
  99. #define GEN_SW_2P_TX_LP BIT(10)
  100. #define GEN_SW_1P_TX_LP BIT(9)
  101. #define GEN_SW_0P_TX_LP BIT(8)
  102. #define ACK_RQST_EN BIT(1)
  103. #define TEAR_FX_EN BIT(0)
  104. #define CMD_MODE_ALL_LP (MAX_RD_PKT_SIZE_LP | \
  105. DCS_LW_TX_LP | \
  106. DCS_SR_0P_TX_LP | \
  107. DCS_SW_1P_TX_LP | \
  108. DCS_SW_0P_TX_LP | \
  109. GEN_LW_TX_LP | \
  110. GEN_SR_2P_TX_LP | \
  111. GEN_SR_1P_TX_LP | \
  112. GEN_SR_0P_TX_LP | \
  113. GEN_SW_2P_TX_LP | \
  114. GEN_SW_1P_TX_LP | \
  115. GEN_SW_0P_TX_LP)
  116. #define DSI_GEN_HDR 0x6c
  117. #define DSI_GEN_PLD_DATA 0x70
  118. #define DSI_CMD_PKT_STATUS 0x74
  119. #define GEN_RD_CMD_BUSY BIT(6)
  120. #define GEN_PLD_R_FULL BIT(5)
  121. #define GEN_PLD_R_EMPTY BIT(4)
  122. #define GEN_PLD_W_FULL BIT(3)
  123. #define GEN_PLD_W_EMPTY BIT(2)
  124. #define GEN_CMD_FULL BIT(1)
  125. #define GEN_CMD_EMPTY BIT(0)
  126. #define DSI_TO_CNT_CFG 0x78
  127. #define HSTX_TO_CNT(p) (((p) & 0xffff) << 16)
  128. #define LPRX_TO_CNT(p) ((p) & 0xffff)
  129. #define DSI_HS_RD_TO_CNT 0x7c
  130. #define DSI_LP_RD_TO_CNT 0x80
  131. #define DSI_HS_WR_TO_CNT 0x84
  132. #define DSI_LP_WR_TO_CNT 0x88
  133. #define DSI_BTA_TO_CNT 0x8c
  134. #define DSI_LPCLK_CTRL 0x94
  135. #define AUTO_CLKLANE_CTRL BIT(1)
  136. #define PHY_TXREQUESTCLKHS BIT(0)
  137. #define DSI_PHY_TMR_LPCLK_CFG 0x98
  138. #define PHY_CLKHS2LP_TIME(lbcc) (((lbcc) & 0x3ff) << 16)
  139. #define PHY_CLKLP2HS_TIME(lbcc) ((lbcc) & 0x3ff)
  140. #define DSI_PHY_TMR_CFG 0x9c
  141. #define PHY_HS2LP_TIME(lbcc) (((lbcc) & 0xff) << 24)
  142. #define PHY_LP2HS_TIME(lbcc) (((lbcc) & 0xff) << 16)
  143. #define MAX_RD_TIME(lbcc) ((lbcc) & 0x7fff)
  144. #define PHY_HS2LP_TIME_V131(lbcc) (((lbcc) & 0x3ff) << 16)
  145. #define PHY_LP2HS_TIME_V131(lbcc) ((lbcc) & 0x3ff)
  146. #define DSI_PHY_RSTZ 0xa0
  147. #define PHY_DISFORCEPLL 0
  148. #define PHY_ENFORCEPLL BIT(3)
  149. #define PHY_DISABLECLK 0
  150. #define PHY_ENABLECLK BIT(2)
  151. #define PHY_RSTZ 0
  152. #define PHY_UNRSTZ BIT(1)
  153. #define PHY_SHUTDOWNZ 0
  154. #define PHY_UNSHUTDOWNZ BIT(0)
  155. #define DSI_PHY_IF_CFG 0xa4
  156. #define PHY_STOP_WAIT_TIME(cycle) (((cycle) & 0xff) << 8)
  157. #define N_LANES(n) (((n) - 1) & 0x3)
  158. #define DSI_PHY_ULPS_CTRL 0xa8
  159. #define DSI_PHY_TX_TRIGGERS 0xac
  160. #define DSI_PHY_STATUS 0xb0
  161. #define PHY_STOP_STATE_CLK_LANE BIT(2)
  162. #define PHY_LOCK BIT(0)
  163. #define DSI_PHY_TST_CTRL0 0xb4
  164. #define PHY_TESTCLK BIT(1)
  165. #define PHY_UNTESTCLK 0
  166. #define PHY_TESTCLR BIT(0)
  167. #define PHY_UNTESTCLR 0
  168. #define DSI_PHY_TST_CTRL1 0xb8
  169. #define PHY_TESTEN BIT(16)
  170. #define PHY_UNTESTEN 0
  171. #define PHY_TESTDOUT(n) (((n) & 0xff) << 8)
  172. #define PHY_TESTDIN(n) ((n) & 0xff)
  173. #define DSI_INT_ST0 0xbc
  174. #define DSI_INT_ST1 0xc0
  175. #define DSI_INT_MSK0 0xc4
  176. #define DSI_INT_MSK1 0xc8
  177. #define DSI_PHY_TMR_RD_CFG 0xf4
  178. #define MAX_RD_TIME_V131(lbcc) ((lbcc) & 0x7fff)
  179. #define PHY_STATUS_TIMEOUT_US 10000
  180. #define CMD_PKT_STATUS_TIMEOUT_US 20000
  181. struct dw_mipi_dsi {
  182. struct drm_bridge bridge;
  183. struct mipi_dsi_host dsi_host;
  184. struct drm_bridge *panel_bridge;
  185. struct device *dev;
  186. void __iomem *base;
  187. struct clk *pclk;
  188. unsigned int lane_mbps; /* per lane */
  189. u32 channel;
  190. u32 lanes;
  191. u32 format;
  192. unsigned long mode_flags;
  193. const struct dw_mipi_dsi_plat_data *plat_data;
  194. };
  195. /*
  196. * The controller should generate 2 frames before
  197. * preparing the peripheral.
  198. */
  199. static void dw_mipi_dsi_wait_for_two_frames(struct drm_display_mode *mode)
  200. {
  201. int refresh, two_frames;
  202. refresh = drm_mode_vrefresh(mode);
  203. two_frames = DIV_ROUND_UP(MSEC_PER_SEC, refresh) * 2;
  204. msleep(two_frames);
  205. }
  206. static inline struct dw_mipi_dsi *host_to_dsi(struct mipi_dsi_host *host)
  207. {
  208. return container_of(host, struct dw_mipi_dsi, dsi_host);
  209. }
  210. static inline struct dw_mipi_dsi *bridge_to_dsi(struct drm_bridge *bridge)
  211. {
  212. return container_of(bridge, struct dw_mipi_dsi, bridge);
  213. }
  214. static inline void dsi_write(struct dw_mipi_dsi *dsi, u32 reg, u32 val)
  215. {
  216. writel(val, dsi->base + reg);
  217. }
  218. static inline u32 dsi_read(struct dw_mipi_dsi *dsi, u32 reg)
  219. {
  220. return readl(dsi->base + reg);
  221. }
  222. static int dw_mipi_dsi_host_attach(struct mipi_dsi_host *host,
  223. struct mipi_dsi_device *device)
  224. {
  225. struct dw_mipi_dsi *dsi = host_to_dsi(host);
  226. struct drm_bridge *bridge;
  227. struct drm_panel *panel;
  228. int ret;
  229. if (device->lanes > dsi->plat_data->max_data_lanes) {
  230. dev_err(dsi->dev, "the number of data lanes(%u) is too many\n",
  231. device->lanes);
  232. return -EINVAL;
  233. }
  234. dsi->lanes = device->lanes;
  235. dsi->channel = device->channel;
  236. dsi->format = device->format;
  237. dsi->mode_flags = device->mode_flags;
  238. ret = drm_of_find_panel_or_bridge(host->dev->of_node, 1, 0,
  239. &panel, &bridge);
  240. if (ret)
  241. return ret;
  242. if (panel) {
  243. bridge = drm_panel_bridge_add(panel, DRM_MODE_CONNECTOR_DSI);
  244. if (IS_ERR(bridge))
  245. return PTR_ERR(bridge);
  246. }
  247. dsi->panel_bridge = bridge;
  248. drm_bridge_add(&dsi->bridge);
  249. return 0;
  250. }
  251. static int dw_mipi_dsi_host_detach(struct mipi_dsi_host *host,
  252. struct mipi_dsi_device *device)
  253. {
  254. struct dw_mipi_dsi *dsi = host_to_dsi(host);
  255. drm_of_panel_bridge_remove(host->dev->of_node, 1, 0);
  256. drm_bridge_remove(&dsi->bridge);
  257. return 0;
  258. }
  259. static void dw_mipi_message_config(struct dw_mipi_dsi *dsi,
  260. const struct mipi_dsi_msg *msg)
  261. {
  262. bool lpm = msg->flags & MIPI_DSI_MSG_USE_LPM;
  263. u32 val = 0;
  264. if (msg->flags & MIPI_DSI_MSG_REQ_ACK)
  265. val |= ACK_RQST_EN;
  266. if (lpm)
  267. val |= CMD_MODE_ALL_LP;
  268. dsi_write(dsi, DSI_LPCLK_CTRL, lpm ? 0 : PHY_TXREQUESTCLKHS);
  269. dsi_write(dsi, DSI_CMD_MODE_CFG, val);
  270. }
  271. static int dw_mipi_dsi_gen_pkt_hdr_write(struct dw_mipi_dsi *dsi, u32 hdr_val)
  272. {
  273. int ret;
  274. u32 val, mask;
  275. ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
  276. val, !(val & GEN_CMD_FULL), 1000,
  277. CMD_PKT_STATUS_TIMEOUT_US);
  278. if (ret) {
  279. dev_err(dsi->dev, "failed to get available command FIFO\n");
  280. return ret;
  281. }
  282. dsi_write(dsi, DSI_GEN_HDR, hdr_val);
  283. mask = GEN_CMD_EMPTY | GEN_PLD_W_EMPTY;
  284. ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
  285. val, (val & mask) == mask,
  286. 1000, CMD_PKT_STATUS_TIMEOUT_US);
  287. if (ret) {
  288. dev_err(dsi->dev, "failed to write command FIFO\n");
  289. return ret;
  290. }
  291. return 0;
  292. }
  293. static int dw_mipi_dsi_write(struct dw_mipi_dsi *dsi,
  294. const struct mipi_dsi_packet *packet)
  295. {
  296. const u8 *tx_buf = packet->payload;
  297. int len = packet->payload_length, pld_data_bytes = sizeof(u32), ret;
  298. __le32 word;
  299. u32 val;
  300. while (len) {
  301. if (len < pld_data_bytes) {
  302. word = 0;
  303. memcpy(&word, tx_buf, len);
  304. dsi_write(dsi, DSI_GEN_PLD_DATA, le32_to_cpu(word));
  305. len = 0;
  306. } else {
  307. memcpy(&word, tx_buf, pld_data_bytes);
  308. dsi_write(dsi, DSI_GEN_PLD_DATA, le32_to_cpu(word));
  309. tx_buf += pld_data_bytes;
  310. len -= pld_data_bytes;
  311. }
  312. ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
  313. val, !(val & GEN_PLD_W_FULL), 1000,
  314. CMD_PKT_STATUS_TIMEOUT_US);
  315. if (ret) {
  316. dev_err(dsi->dev,
  317. "failed to get available write payload FIFO\n");
  318. return ret;
  319. }
  320. }
  321. word = 0;
  322. memcpy(&word, packet->header, sizeof(packet->header));
  323. return dw_mipi_dsi_gen_pkt_hdr_write(dsi, le32_to_cpu(word));
  324. }
  325. static int dw_mipi_dsi_read(struct dw_mipi_dsi *dsi,
  326. const struct mipi_dsi_msg *msg)
  327. {
  328. int i, j, ret, len = msg->rx_len;
  329. u8 *buf = msg->rx_buf;
  330. u32 val;
  331. /* Wait end of the read operation */
  332. ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
  333. val, !(val & GEN_RD_CMD_BUSY),
  334. 1000, CMD_PKT_STATUS_TIMEOUT_US);
  335. if (ret) {
  336. dev_err(dsi->dev, "Timeout during read operation\n");
  337. return ret;
  338. }
  339. for (i = 0; i < len; i += 4) {
  340. /* Read fifo must not be empty before all bytes are read */
  341. ret = readl_poll_timeout(dsi->base + DSI_CMD_PKT_STATUS,
  342. val, !(val & GEN_PLD_R_EMPTY),
  343. 1000, CMD_PKT_STATUS_TIMEOUT_US);
  344. if (ret) {
  345. dev_err(dsi->dev, "Read payload FIFO is empty\n");
  346. return ret;
  347. }
  348. val = dsi_read(dsi, DSI_GEN_PLD_DATA);
  349. for (j = 0; j < 4 && j + i < len; j++)
  350. buf[i + j] = val >> (8 * j);
  351. }
  352. return ret;
  353. }
  354. static ssize_t dw_mipi_dsi_host_transfer(struct mipi_dsi_host *host,
  355. const struct mipi_dsi_msg *msg)
  356. {
  357. struct dw_mipi_dsi *dsi = host_to_dsi(host);
  358. struct mipi_dsi_packet packet;
  359. int ret, nb_bytes;
  360. ret = mipi_dsi_create_packet(&packet, msg);
  361. if (ret) {
  362. dev_err(dsi->dev, "failed to create packet: %d\n", ret);
  363. return ret;
  364. }
  365. dw_mipi_message_config(dsi, msg);
  366. ret = dw_mipi_dsi_write(dsi, &packet);
  367. if (ret)
  368. return ret;
  369. if (msg->rx_buf && msg->rx_len) {
  370. ret = dw_mipi_dsi_read(dsi, msg);
  371. if (ret)
  372. return ret;
  373. nb_bytes = msg->rx_len;
  374. } else {
  375. nb_bytes = packet.size;
  376. }
  377. return nb_bytes;
  378. }
  379. static const struct mipi_dsi_host_ops dw_mipi_dsi_host_ops = {
  380. .attach = dw_mipi_dsi_host_attach,
  381. .detach = dw_mipi_dsi_host_detach,
  382. .transfer = dw_mipi_dsi_host_transfer,
  383. };
  384. static void dw_mipi_dsi_video_mode_config(struct dw_mipi_dsi *dsi)
  385. {
  386. u32 val;
  387. /*
  388. * TODO dw drv improvements
  389. * enabling low power is panel-dependent, we should use the
  390. * panel configuration here...
  391. */
  392. val = ENABLE_LOW_POWER;
  393. if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
  394. val |= VID_MODE_TYPE_BURST;
  395. else if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
  396. val |= VID_MODE_TYPE_NON_BURST_SYNC_PULSES;
  397. else
  398. val |= VID_MODE_TYPE_NON_BURST_SYNC_EVENTS;
  399. dsi_write(dsi, DSI_VID_MODE_CFG, val);
  400. }
  401. static void dw_mipi_dsi_set_mode(struct dw_mipi_dsi *dsi,
  402. unsigned long mode_flags)
  403. {
  404. dsi_write(dsi, DSI_PWR_UP, RESET);
  405. if (mode_flags & MIPI_DSI_MODE_VIDEO) {
  406. dsi_write(dsi, DSI_MODE_CFG, ENABLE_VIDEO_MODE);
  407. dw_mipi_dsi_video_mode_config(dsi);
  408. dsi_write(dsi, DSI_LPCLK_CTRL, PHY_TXREQUESTCLKHS);
  409. } else {
  410. dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
  411. }
  412. dsi_write(dsi, DSI_PWR_UP, POWERUP);
  413. }
  414. static void dw_mipi_dsi_disable(struct dw_mipi_dsi *dsi)
  415. {
  416. dsi_write(dsi, DSI_PWR_UP, RESET);
  417. dsi_write(dsi, DSI_PHY_RSTZ, PHY_RSTZ);
  418. }
  419. static void dw_mipi_dsi_init(struct dw_mipi_dsi *dsi)
  420. {
  421. /*
  422. * The maximum permitted escape clock is 20MHz and it is derived from
  423. * lanebyteclk, which is running at "lane_mbps / 8". Thus we want:
  424. *
  425. * (lane_mbps >> 3) / esc_clk_division < 20
  426. * which is:
  427. * (lane_mbps >> 3) / 20 > esc_clk_division
  428. */
  429. u32 esc_clk_division = (dsi->lane_mbps >> 3) / 20 + 1;
  430. dsi_write(dsi, DSI_PWR_UP, RESET);
  431. /*
  432. * TODO dw drv improvements
  433. * timeout clock division should be computed with the
  434. * high speed transmission counter timeout and byte lane...
  435. */
  436. dsi_write(dsi, DSI_CLKMGR_CFG, TO_CLK_DIVISION(10) |
  437. TX_ESC_CLK_DIVISION(esc_clk_division));
  438. }
  439. static void dw_mipi_dsi_dpi_config(struct dw_mipi_dsi *dsi,
  440. struct drm_display_mode *mode)
  441. {
  442. u32 val = 0, color = 0;
  443. switch (dsi->format) {
  444. case MIPI_DSI_FMT_RGB888:
  445. color = DPI_COLOR_CODING_24BIT;
  446. break;
  447. case MIPI_DSI_FMT_RGB666:
  448. color = DPI_COLOR_CODING_18BIT_2 | LOOSELY18_EN;
  449. break;
  450. case MIPI_DSI_FMT_RGB666_PACKED:
  451. color = DPI_COLOR_CODING_18BIT_1;
  452. break;
  453. case MIPI_DSI_FMT_RGB565:
  454. color = DPI_COLOR_CODING_16BIT_1;
  455. break;
  456. }
  457. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  458. val |= VSYNC_ACTIVE_LOW;
  459. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  460. val |= HSYNC_ACTIVE_LOW;
  461. dsi_write(dsi, DSI_DPI_VCID, DPI_VCID(dsi->channel));
  462. dsi_write(dsi, DSI_DPI_COLOR_CODING, color);
  463. dsi_write(dsi, DSI_DPI_CFG_POL, val);
  464. /*
  465. * TODO dw drv improvements
  466. * largest packet sizes during hfp or during vsa/vpb/vfp
  467. * should be computed according to byte lane, lane number and only
  468. * if sending lp cmds in high speed is enable (PHY_TXREQUESTCLKHS)
  469. */
  470. dsi_write(dsi, DSI_DPI_LP_CMD_TIM, OUTVACT_LPCMD_TIME(4)
  471. | INVACT_LPCMD_TIME(4));
  472. }
  473. static void dw_mipi_dsi_packet_handler_config(struct dw_mipi_dsi *dsi)
  474. {
  475. dsi_write(dsi, DSI_PCKHDL_CFG, CRC_RX_EN | ECC_RX_EN | BTA_EN);
  476. }
  477. static void dw_mipi_dsi_video_packet_config(struct dw_mipi_dsi *dsi,
  478. struct drm_display_mode *mode)
  479. {
  480. /*
  481. * TODO dw drv improvements
  482. * only burst mode is supported here. For non-burst video modes,
  483. * we should compute DSI_VID_PKT_SIZE, DSI_VCCR.NUMC &
  484. * DSI_VNPCR.NPSIZE... especially because this driver supports
  485. * non-burst video modes, see dw_mipi_dsi_video_mode_config()...
  486. */
  487. dsi_write(dsi, DSI_VID_PKT_SIZE, VID_PKT_SIZE(mode->hdisplay));
  488. }
  489. static void dw_mipi_dsi_command_mode_config(struct dw_mipi_dsi *dsi)
  490. {
  491. /*
  492. * TODO dw drv improvements
  493. * compute high speed transmission counter timeout according
  494. * to the timeout clock division (TO_CLK_DIVISION) and byte lane...
  495. */
  496. dsi_write(dsi, DSI_TO_CNT_CFG, HSTX_TO_CNT(1000) | LPRX_TO_CNT(1000));
  497. /*
  498. * TODO dw drv improvements
  499. * the Bus-Turn-Around Timeout Counter should be computed
  500. * according to byte lane...
  501. */
  502. dsi_write(dsi, DSI_BTA_TO_CNT, 0xd00);
  503. dsi_write(dsi, DSI_MODE_CFG, ENABLE_CMD_MODE);
  504. }
  505. /* Get lane byte clock cycles. */
  506. static u32 dw_mipi_dsi_get_hcomponent_lbcc(struct dw_mipi_dsi *dsi,
  507. struct drm_display_mode *mode,
  508. u32 hcomponent)
  509. {
  510. u32 frac, lbcc;
  511. lbcc = hcomponent * dsi->lane_mbps * MSEC_PER_SEC / 8;
  512. frac = lbcc % mode->clock;
  513. lbcc = lbcc / mode->clock;
  514. if (frac)
  515. lbcc++;
  516. return lbcc;
  517. }
  518. static void dw_mipi_dsi_line_timer_config(struct dw_mipi_dsi *dsi,
  519. struct drm_display_mode *mode)
  520. {
  521. u32 htotal, hsa, hbp, lbcc;
  522. htotal = mode->htotal;
  523. hsa = mode->hsync_end - mode->hsync_start;
  524. hbp = mode->htotal - mode->hsync_end;
  525. /*
  526. * TODO dw drv improvements
  527. * computations below may be improved...
  528. */
  529. lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, htotal);
  530. dsi_write(dsi, DSI_VID_HLINE_TIME, lbcc);
  531. lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, hsa);
  532. dsi_write(dsi, DSI_VID_HSA_TIME, lbcc);
  533. lbcc = dw_mipi_dsi_get_hcomponent_lbcc(dsi, mode, hbp);
  534. dsi_write(dsi, DSI_VID_HBP_TIME, lbcc);
  535. }
  536. static void dw_mipi_dsi_vertical_timing_config(struct dw_mipi_dsi *dsi,
  537. struct drm_display_mode *mode)
  538. {
  539. u32 vactive, vsa, vfp, vbp;
  540. vactive = mode->vdisplay;
  541. vsa = mode->vsync_end - mode->vsync_start;
  542. vfp = mode->vsync_start - mode->vdisplay;
  543. vbp = mode->vtotal - mode->vsync_end;
  544. dsi_write(dsi, DSI_VID_VACTIVE_LINES, vactive);
  545. dsi_write(dsi, DSI_VID_VSA_LINES, vsa);
  546. dsi_write(dsi, DSI_VID_VFP_LINES, vfp);
  547. dsi_write(dsi, DSI_VID_VBP_LINES, vbp);
  548. }
  549. static void dw_mipi_dsi_dphy_timing_config(struct dw_mipi_dsi *dsi)
  550. {
  551. u32 hw_version;
  552. /*
  553. * TODO dw drv improvements
  554. * data & clock lane timers should be computed according to panel
  555. * blankings and to the automatic clock lane control mode...
  556. * note: DSI_PHY_TMR_CFG.MAX_RD_TIME should be in line with
  557. * DSI_CMD_MODE_CFG.MAX_RD_PKT_SIZE_LP (see CMD_MODE_ALL_LP)
  558. */
  559. hw_version = dsi_read(dsi, DSI_VERSION) & VERSION;
  560. if (hw_version >= HWVER_131) {
  561. dsi_write(dsi, DSI_PHY_TMR_CFG, PHY_HS2LP_TIME_V131(0x40) |
  562. PHY_LP2HS_TIME_V131(0x40));
  563. dsi_write(dsi, DSI_PHY_TMR_RD_CFG, MAX_RD_TIME_V131(10000));
  564. } else {
  565. dsi_write(dsi, DSI_PHY_TMR_CFG, PHY_HS2LP_TIME(0x40) |
  566. PHY_LP2HS_TIME(0x40) | MAX_RD_TIME(10000));
  567. }
  568. dsi_write(dsi, DSI_PHY_TMR_LPCLK_CFG, PHY_CLKHS2LP_TIME(0x40)
  569. | PHY_CLKLP2HS_TIME(0x40));
  570. }
  571. static void dw_mipi_dsi_dphy_interface_config(struct dw_mipi_dsi *dsi)
  572. {
  573. /*
  574. * TODO dw drv improvements
  575. * stop wait time should be the maximum between host dsi
  576. * and panel stop wait times
  577. */
  578. dsi_write(dsi, DSI_PHY_IF_CFG, PHY_STOP_WAIT_TIME(0x20) |
  579. N_LANES(dsi->lanes));
  580. }
  581. static void dw_mipi_dsi_dphy_init(struct dw_mipi_dsi *dsi)
  582. {
  583. /* Clear PHY state */
  584. dsi_write(dsi, DSI_PHY_RSTZ, PHY_DISFORCEPLL | PHY_DISABLECLK
  585. | PHY_RSTZ | PHY_SHUTDOWNZ);
  586. dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_UNTESTCLR);
  587. dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_TESTCLR);
  588. dsi_write(dsi, DSI_PHY_TST_CTRL0, PHY_UNTESTCLR);
  589. }
  590. static void dw_mipi_dsi_dphy_enable(struct dw_mipi_dsi *dsi)
  591. {
  592. u32 val;
  593. int ret;
  594. dsi_write(dsi, DSI_PHY_RSTZ, PHY_ENFORCEPLL | PHY_ENABLECLK |
  595. PHY_UNRSTZ | PHY_UNSHUTDOWNZ);
  596. ret = readl_poll_timeout(dsi->base + DSI_PHY_STATUS, val,
  597. val & PHY_LOCK, 1000, PHY_STATUS_TIMEOUT_US);
  598. if (ret)
  599. DRM_DEBUG_DRIVER("failed to wait phy lock state\n");
  600. ret = readl_poll_timeout(dsi->base + DSI_PHY_STATUS,
  601. val, val & PHY_STOP_STATE_CLK_LANE, 1000,
  602. PHY_STATUS_TIMEOUT_US);
  603. if (ret)
  604. DRM_DEBUG_DRIVER("failed to wait phy clk lane stop state\n");
  605. }
  606. static void dw_mipi_dsi_clear_err(struct dw_mipi_dsi *dsi)
  607. {
  608. dsi_read(dsi, DSI_INT_ST0);
  609. dsi_read(dsi, DSI_INT_ST1);
  610. dsi_write(dsi, DSI_INT_MSK0, 0);
  611. dsi_write(dsi, DSI_INT_MSK1, 0);
  612. }
  613. static void dw_mipi_dsi_bridge_post_disable(struct drm_bridge *bridge)
  614. {
  615. struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
  616. /*
  617. * Switch to command mode before panel-bridge post_disable &
  618. * panel unprepare.
  619. * Note: panel-bridge disable & panel disable has been called
  620. * before by the drm framework.
  621. */
  622. dw_mipi_dsi_set_mode(dsi, 0);
  623. /*
  624. * TODO Only way found to call panel-bridge post_disable &
  625. * panel unprepare before the dsi "final" disable...
  626. * This needs to be fixed in the drm_bridge framework and the API
  627. * needs to be updated to manage our own call chains...
  628. */
  629. dsi->panel_bridge->funcs->post_disable(dsi->panel_bridge);
  630. dw_mipi_dsi_disable(dsi);
  631. clk_disable_unprepare(dsi->pclk);
  632. pm_runtime_put(dsi->dev);
  633. }
  634. static void dw_mipi_dsi_bridge_mode_set(struct drm_bridge *bridge,
  635. struct drm_display_mode *mode,
  636. struct drm_display_mode *adjusted_mode)
  637. {
  638. struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
  639. const struct dw_mipi_dsi_phy_ops *phy_ops = dsi->plat_data->phy_ops;
  640. void *priv_data = dsi->plat_data->priv_data;
  641. int ret;
  642. clk_prepare_enable(dsi->pclk);
  643. ret = phy_ops->get_lane_mbps(priv_data, adjusted_mode, dsi->mode_flags,
  644. dsi->lanes, dsi->format, &dsi->lane_mbps);
  645. if (ret)
  646. DRM_DEBUG_DRIVER("Phy get_lane_mbps() failed\n");
  647. pm_runtime_get_sync(dsi->dev);
  648. dw_mipi_dsi_init(dsi);
  649. dw_mipi_dsi_dpi_config(dsi, adjusted_mode);
  650. dw_mipi_dsi_packet_handler_config(dsi);
  651. dw_mipi_dsi_video_mode_config(dsi);
  652. dw_mipi_dsi_video_packet_config(dsi, adjusted_mode);
  653. dw_mipi_dsi_command_mode_config(dsi);
  654. dw_mipi_dsi_line_timer_config(dsi, adjusted_mode);
  655. dw_mipi_dsi_vertical_timing_config(dsi, adjusted_mode);
  656. dw_mipi_dsi_dphy_init(dsi);
  657. dw_mipi_dsi_dphy_timing_config(dsi);
  658. dw_mipi_dsi_dphy_interface_config(dsi);
  659. dw_mipi_dsi_clear_err(dsi);
  660. ret = phy_ops->init(priv_data);
  661. if (ret)
  662. DRM_DEBUG_DRIVER("Phy init() failed\n");
  663. dw_mipi_dsi_dphy_enable(dsi);
  664. dw_mipi_dsi_wait_for_two_frames(adjusted_mode);
  665. /* Switch to cmd mode for panel-bridge pre_enable & panel prepare */
  666. dw_mipi_dsi_set_mode(dsi, 0);
  667. }
  668. static void dw_mipi_dsi_bridge_enable(struct drm_bridge *bridge)
  669. {
  670. struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
  671. /* Switch to video mode for panel-bridge enable & panel enable */
  672. dw_mipi_dsi_set_mode(dsi, MIPI_DSI_MODE_VIDEO);
  673. }
  674. static enum drm_mode_status
  675. dw_mipi_dsi_bridge_mode_valid(struct drm_bridge *bridge,
  676. const struct drm_display_mode *mode)
  677. {
  678. struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
  679. const struct dw_mipi_dsi_plat_data *pdata = dsi->plat_data;
  680. enum drm_mode_status mode_status = MODE_OK;
  681. if (pdata->mode_valid)
  682. mode_status = pdata->mode_valid(pdata->priv_data, mode);
  683. return mode_status;
  684. }
  685. static int dw_mipi_dsi_bridge_attach(struct drm_bridge *bridge)
  686. {
  687. struct dw_mipi_dsi *dsi = bridge_to_dsi(bridge);
  688. if (!bridge->encoder) {
  689. DRM_ERROR("Parent encoder object not found\n");
  690. return -ENODEV;
  691. }
  692. /* Set the encoder type as caller does not know it */
  693. bridge->encoder->encoder_type = DRM_MODE_ENCODER_DSI;
  694. /* Attach the panel-bridge to the dsi bridge */
  695. return drm_bridge_attach(bridge->encoder, dsi->panel_bridge, bridge);
  696. }
  697. static const struct drm_bridge_funcs dw_mipi_dsi_bridge_funcs = {
  698. .mode_set = dw_mipi_dsi_bridge_mode_set,
  699. .enable = dw_mipi_dsi_bridge_enable,
  700. .post_disable = dw_mipi_dsi_bridge_post_disable,
  701. .mode_valid = dw_mipi_dsi_bridge_mode_valid,
  702. .attach = dw_mipi_dsi_bridge_attach,
  703. };
  704. static struct dw_mipi_dsi *
  705. __dw_mipi_dsi_probe(struct platform_device *pdev,
  706. const struct dw_mipi_dsi_plat_data *plat_data)
  707. {
  708. struct device *dev = &pdev->dev;
  709. struct reset_control *apb_rst;
  710. struct dw_mipi_dsi *dsi;
  711. struct resource *res;
  712. int ret;
  713. dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
  714. if (!dsi)
  715. return ERR_PTR(-ENOMEM);
  716. dsi->dev = dev;
  717. dsi->plat_data = plat_data;
  718. if (!plat_data->phy_ops->init || !plat_data->phy_ops->get_lane_mbps) {
  719. DRM_ERROR("Phy not properly configured\n");
  720. return ERR_PTR(-ENODEV);
  721. }
  722. if (!plat_data->base) {
  723. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  724. if (!res)
  725. return ERR_PTR(-ENODEV);
  726. dsi->base = devm_ioremap_resource(dev, res);
  727. if (IS_ERR(dsi->base))
  728. return ERR_PTR(-ENODEV);
  729. } else {
  730. dsi->base = plat_data->base;
  731. }
  732. dsi->pclk = devm_clk_get(dev, "pclk");
  733. if (IS_ERR(dsi->pclk)) {
  734. ret = PTR_ERR(dsi->pclk);
  735. dev_err(dev, "Unable to get pclk: %d\n", ret);
  736. return ERR_PTR(ret);
  737. }
  738. /*
  739. * Note that the reset was not defined in the initial device tree, so
  740. * we have to be prepared for it not being found.
  741. */
  742. apb_rst = devm_reset_control_get_optional_exclusive(dev, "apb");
  743. if (IS_ERR(apb_rst)) {
  744. ret = PTR_ERR(apb_rst);
  745. if (ret != -EPROBE_DEFER)
  746. dev_err(dev, "Unable to get reset control: %d\n", ret);
  747. return ERR_PTR(ret);
  748. }
  749. if (apb_rst) {
  750. ret = clk_prepare_enable(dsi->pclk);
  751. if (ret) {
  752. dev_err(dev, "%s: Failed to enable pclk\n", __func__);
  753. return ERR_PTR(ret);
  754. }
  755. reset_control_assert(apb_rst);
  756. usleep_range(10, 20);
  757. reset_control_deassert(apb_rst);
  758. clk_disable_unprepare(dsi->pclk);
  759. }
  760. pm_runtime_enable(dev);
  761. dsi->dsi_host.ops = &dw_mipi_dsi_host_ops;
  762. dsi->dsi_host.dev = dev;
  763. ret = mipi_dsi_host_register(&dsi->dsi_host);
  764. if (ret) {
  765. dev_err(dev, "Failed to register MIPI host: %d\n", ret);
  766. return ERR_PTR(ret);
  767. }
  768. dsi->bridge.driver_private = dsi;
  769. dsi->bridge.funcs = &dw_mipi_dsi_bridge_funcs;
  770. #ifdef CONFIG_OF
  771. dsi->bridge.of_node = pdev->dev.of_node;
  772. #endif
  773. return dsi;
  774. }
  775. static void __dw_mipi_dsi_remove(struct dw_mipi_dsi *dsi)
  776. {
  777. pm_runtime_disable(dsi->dev);
  778. }
  779. /*
  780. * Probe/remove API, used from platforms based on the DRM bridge API.
  781. */
  782. struct dw_mipi_dsi *
  783. dw_mipi_dsi_probe(struct platform_device *pdev,
  784. const struct dw_mipi_dsi_plat_data *plat_data)
  785. {
  786. return __dw_mipi_dsi_probe(pdev, plat_data);
  787. }
  788. EXPORT_SYMBOL_GPL(dw_mipi_dsi_probe);
  789. void dw_mipi_dsi_remove(struct dw_mipi_dsi *dsi)
  790. {
  791. mipi_dsi_host_unregister(&dsi->dsi_host);
  792. __dw_mipi_dsi_remove(dsi);
  793. }
  794. EXPORT_SYMBOL_GPL(dw_mipi_dsi_remove);
  795. /*
  796. * Bind/unbind API, used from platforms based on the component framework.
  797. */
  798. struct dw_mipi_dsi *
  799. dw_mipi_dsi_bind(struct platform_device *pdev, struct drm_encoder *encoder,
  800. const struct dw_mipi_dsi_plat_data *plat_data)
  801. {
  802. struct dw_mipi_dsi *dsi;
  803. int ret;
  804. dsi = __dw_mipi_dsi_probe(pdev, plat_data);
  805. if (IS_ERR(dsi))
  806. return dsi;
  807. ret = drm_bridge_attach(encoder, &dsi->bridge, NULL);
  808. if (ret) {
  809. dw_mipi_dsi_remove(dsi);
  810. DRM_ERROR("Failed to initialize bridge with drm\n");
  811. return ERR_PTR(ret);
  812. }
  813. return dsi;
  814. }
  815. EXPORT_SYMBOL_GPL(dw_mipi_dsi_bind);
  816. void dw_mipi_dsi_unbind(struct dw_mipi_dsi *dsi)
  817. {
  818. __dw_mipi_dsi_remove(dsi);
  819. }
  820. EXPORT_SYMBOL_GPL(dw_mipi_dsi_unbind);
  821. MODULE_AUTHOR("Chris Zhong <zyw@rock-chips.com>");
  822. MODULE_AUTHOR("Philippe Cornu <philippe.cornu@st.com>");
  823. MODULE_DESCRIPTION("DW MIPI DSI host controller driver");
  824. MODULE_LICENSE("GPL");
  825. MODULE_ALIAS("platform:dw-mipi-dsi");