1 /* 2 * Copyright (C) 2016 Broadcom 3 * 4 * This program is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 as published by 6 * the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 11 * more details. 12 * 13 * You should have received a copy of the GNU General Public License along with 14 * this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16 17 /** 18 * DOC: VC4 DSI0/DSI1 module 19 * 20 * BCM2835 contains two DSI modules, DSI0 and DSI1. DSI0 is a 21 * single-lane DSI controller, while DSI1 is a more modern 4-lane DSI 22 * controller. 23 * 24 * Most Raspberry Pi boards expose DSI1 as their "DISPLAY" connector, 25 * while the compute module brings both DSI0 and DSI1 out. 26 * 27 * This driver has been tested for DSI1 video-mode display only 28 * currently, with most of the information necessary for DSI0 29 * hopefully present. 30 */ 31 32 #include <drm/drm_atomic_helper.h> 33 #include <drm/drm_crtc_helper.h> 34 #include <drm/drm_edid.h> 35 #include <drm/drm_mipi_dsi.h> 36 #include <drm/drm_of.h> 37 #include <drm/drm_panel.h> 38 #include <linux/clk.h> 39 #include <linux/clk-provider.h> 40 #include <linux/completion.h> 41 #include <linux/component.h> 42 #include <linux/dmaengine.h> 43 #include <linux/i2c.h> 44 #include <linux/of_address.h> 45 #include <linux/of_platform.h> 46 #include <linux/pm_runtime.h> 47 #include "vc4_drv.h" 48 #include "vc4_regs.h" 49 50 #define DSI_CMD_FIFO_DEPTH 16 51 #define DSI_PIX_FIFO_DEPTH 256 52 #define DSI_PIX_FIFO_WIDTH 4 53 54 #define DSI0_CTRL 0x00 55 56 /* Command packet control. */ 57 #define DSI0_TXPKT1C 0x04 /* AKA PKTC */ 58 #define DSI1_TXPKT1C 0x04 59 # define DSI_TXPKT1C_TRIG_CMD_MASK VC4_MASK(31, 24) 60 # define DSI_TXPKT1C_TRIG_CMD_SHIFT 24 61 # define DSI_TXPKT1C_CMD_REPEAT_MASK VC4_MASK(23, 10) 62 # define DSI_TXPKT1C_CMD_REPEAT_SHIFT 10 63 64 # define DSI_TXPKT1C_DISPLAY_NO_MASK VC4_MASK(9, 8) 65 # define DSI_TXPKT1C_DISPLAY_NO_SHIFT 8 66 /* Short, trigger, BTA, or a long packet that fits all in CMDFIFO. */ 67 # define DSI_TXPKT1C_DISPLAY_NO_SHORT 0 68 /* Primary display where cmdfifo provides part of the payload and 69 * pixelvalve the rest. 70 */ 71 # define DSI_TXPKT1C_DISPLAY_NO_PRIMARY 1 72 /* Secondary display where cmdfifo provides part of the payload and 73 * pixfifo the rest. 74 */ 75 # define DSI_TXPKT1C_DISPLAY_NO_SECONDARY 2 76 77 # define DSI_TXPKT1C_CMD_TX_TIME_MASK VC4_MASK(7, 6) 78 # define DSI_TXPKT1C_CMD_TX_TIME_SHIFT 6 79 80 # define DSI_TXPKT1C_CMD_CTRL_MASK VC4_MASK(5, 4) 81 # define DSI_TXPKT1C_CMD_CTRL_SHIFT 4 82 /* Command only. Uses TXPKT1H and DISPLAY_NO */ 83 # define DSI_TXPKT1C_CMD_CTRL_TX 0 84 /* Command with BTA for either ack or read data. */ 85 # define DSI_TXPKT1C_CMD_CTRL_RX 1 86 /* Trigger according to TRIG_CMD */ 87 # define DSI_TXPKT1C_CMD_CTRL_TRIG 2 88 /* BTA alone for getting error status after a command, or a TE trigger 89 * without a previous command. 90 */ 91 # define DSI_TXPKT1C_CMD_CTRL_BTA 3 92 93 # define DSI_TXPKT1C_CMD_MODE_LP BIT(3) 94 # define DSI_TXPKT1C_CMD_TYPE_LONG BIT(2) 95 # define DSI_TXPKT1C_CMD_TE_EN BIT(1) 96 # define DSI_TXPKT1C_CMD_EN BIT(0) 97 98 /* Command packet header. */ 99 #define DSI0_TXPKT1H 0x08 /* AKA PKTH */ 100 #define DSI1_TXPKT1H 0x08 101 # define DSI_TXPKT1H_BC_CMDFIFO_MASK VC4_MASK(31, 24) 102 # define DSI_TXPKT1H_BC_CMDFIFO_SHIFT 24 103 # define DSI_TXPKT1H_BC_PARAM_MASK VC4_MASK(23, 8) 104 # define DSI_TXPKT1H_BC_PARAM_SHIFT 8 105 # define DSI_TXPKT1H_BC_DT_MASK VC4_MASK(7, 0) 106 # define DSI_TXPKT1H_BC_DT_SHIFT 0 107 108 #define DSI0_RXPKT1H 0x0c /* AKA RX1_PKTH */ 109 #define DSI1_RXPKT1H 0x14 110 # define DSI_RXPKT1H_CRC_ERR BIT(31) 111 # define DSI_RXPKT1H_DET_ERR BIT(30) 112 # define DSI_RXPKT1H_ECC_ERR BIT(29) 113 # define DSI_RXPKT1H_COR_ERR BIT(28) 114 # define DSI_RXPKT1H_INCOMP_PKT BIT(25) 115 # define DSI_RXPKT1H_PKT_TYPE_LONG BIT(24) 116 /* Byte count if DSI_RXPKT1H_PKT_TYPE_LONG */ 117 # define DSI_RXPKT1H_BC_PARAM_MASK VC4_MASK(23, 8) 118 # define DSI_RXPKT1H_BC_PARAM_SHIFT 8 119 /* Short return bytes if !DSI_RXPKT1H_PKT_TYPE_LONG */ 120 # define DSI_RXPKT1H_SHORT_1_MASK VC4_MASK(23, 16) 121 # define DSI_RXPKT1H_SHORT_1_SHIFT 16 122 # define DSI_RXPKT1H_SHORT_0_MASK VC4_MASK(15, 8) 123 # define DSI_RXPKT1H_SHORT_0_SHIFT 8 124 # define DSI_RXPKT1H_DT_LP_CMD_MASK VC4_MASK(7, 0) 125 # define DSI_RXPKT1H_DT_LP_CMD_SHIFT 0 126 127 #define DSI0_RXPKT2H 0x10 /* AKA RX2_PKTH */ 128 #define DSI1_RXPKT2H 0x18 129 # define DSI_RXPKT1H_DET_ERR BIT(30) 130 # define DSI_RXPKT1H_ECC_ERR BIT(29) 131 # define DSI_RXPKT1H_COR_ERR BIT(28) 132 # define DSI_RXPKT1H_INCOMP_PKT BIT(25) 133 # define DSI_RXPKT1H_BC_PARAM_MASK VC4_MASK(23, 8) 134 # define DSI_RXPKT1H_BC_PARAM_SHIFT 8 135 # define DSI_RXPKT1H_DT_MASK VC4_MASK(7, 0) 136 # define DSI_RXPKT1H_DT_SHIFT 0 137 138 #define DSI0_TXPKT_CMD_FIFO 0x14 /* AKA CMD_DATAF */ 139 #define DSI1_TXPKT_CMD_FIFO 0x1c 140 141 #define DSI0_DISP0_CTRL 0x18 142 # define DSI_DISP0_PIX_CLK_DIV_MASK VC4_MASK(21, 13) 143 # define DSI_DISP0_PIX_CLK_DIV_SHIFT 13 144 # define DSI_DISP0_LP_STOP_CTRL_MASK VC4_MASK(12, 11) 145 # define DSI_DISP0_LP_STOP_CTRL_SHIFT 11 146 # define DSI_DISP0_LP_STOP_DISABLE 0 147 # define DSI_DISP0_LP_STOP_PERLINE 1 148 # define DSI_DISP0_LP_STOP_PERFRAME 2 149 150 /* Transmit RGB pixels and null packets only during HACTIVE, instead 151 * of going to LP-STOP. 152 */ 153 # define DSI_DISP_HACTIVE_NULL BIT(10) 154 /* Transmit blanking packet only during vblank, instead of allowing LP-STOP. */ 155 # define DSI_DISP_VBLP_CTRL BIT(9) 156 /* Transmit blanking packet only during HFP, instead of allowing LP-STOP. */ 157 # define DSI_DISP_HFP_CTRL BIT(8) 158 /* Transmit blanking packet only during HBP, instead of allowing LP-STOP. */ 159 # define DSI_DISP_HBP_CTRL BIT(7) 160 # define DSI_DISP0_CHANNEL_MASK VC4_MASK(6, 5) 161 # define DSI_DISP0_CHANNEL_SHIFT 5 162 /* Enables end events for HSYNC/VSYNC, not just start events. */ 163 # define DSI_DISP0_ST_END BIT(4) 164 # define DSI_DISP0_PFORMAT_MASK VC4_MASK(3, 2) 165 # define DSI_DISP0_PFORMAT_SHIFT 2 166 # define DSI_PFORMAT_RGB565 0 167 # define DSI_PFORMAT_RGB666_PACKED 1 168 # define DSI_PFORMAT_RGB666 2 169 # define DSI_PFORMAT_RGB888 3 170 /* Default is VIDEO mode. */ 171 # define DSI_DISP0_COMMAND_MODE BIT(1) 172 # define DSI_DISP0_ENABLE BIT(0) 173 174 #define DSI0_DISP1_CTRL 0x1c 175 #define DSI1_DISP1_CTRL 0x2c 176 /* Format of the data written to TXPKT_PIX_FIFO. */ 177 # define DSI_DISP1_PFORMAT_MASK VC4_MASK(2, 1) 178 # define DSI_DISP1_PFORMAT_SHIFT 1 179 # define DSI_DISP1_PFORMAT_16BIT 0 180 # define DSI_DISP1_PFORMAT_24BIT 1 181 # define DSI_DISP1_PFORMAT_32BIT_LE 2 182 # define DSI_DISP1_PFORMAT_32BIT_BE 3 183 184 /* DISP1 is always command mode. */ 185 # define DSI_DISP1_ENABLE BIT(0) 186 187 #define DSI0_TXPKT_PIX_FIFO 0x20 /* AKA PIX_FIFO */ 188 189 #define DSI0_INT_STAT 0x24 190 #define DSI0_INT_EN 0x28 191 # define DSI1_INT_PHY_D3_ULPS BIT(30) 192 # define DSI1_INT_PHY_D3_STOP BIT(29) 193 # define DSI1_INT_PHY_D2_ULPS BIT(28) 194 # define DSI1_INT_PHY_D2_STOP BIT(27) 195 # define DSI1_INT_PHY_D1_ULPS BIT(26) 196 # define DSI1_INT_PHY_D1_STOP BIT(25) 197 # define DSI1_INT_PHY_D0_ULPS BIT(24) 198 # define DSI1_INT_PHY_D0_STOP BIT(23) 199 # define DSI1_INT_FIFO_ERR BIT(22) 200 # define DSI1_INT_PHY_DIR_RTF BIT(21) 201 # define DSI1_INT_PHY_RXLPDT BIT(20) 202 # define DSI1_INT_PHY_RXTRIG BIT(19) 203 # define DSI1_INT_PHY_D0_LPDT BIT(18) 204 # define DSI1_INT_PHY_DIR_FTR BIT(17) 205 206 /* Signaled when the clock lane enters the given state. */ 207 # define DSI1_INT_PHY_CLOCK_ULPS BIT(16) 208 # define DSI1_INT_PHY_CLOCK_HS BIT(15) 209 # define DSI1_INT_PHY_CLOCK_STOP BIT(14) 210 211 /* Signaled on timeouts */ 212 # define DSI1_INT_PR_TO BIT(13) 213 # define DSI1_INT_TA_TO BIT(12) 214 # define DSI1_INT_LPRX_TO BIT(11) 215 # define DSI1_INT_HSTX_TO BIT(10) 216 217 /* Contention on a line when trying to drive the line low */ 218 # define DSI1_INT_ERR_CONT_LP1 BIT(9) 219 # define DSI1_INT_ERR_CONT_LP0 BIT(8) 220 221 /* Control error: incorrect line state sequence on data lane 0. */ 222 # define DSI1_INT_ERR_CONTROL BIT(7) 223 /* LPDT synchronization error (bits received not a multiple of 8. */ 224 225 # define DSI1_INT_ERR_SYNC_ESC BIT(6) 226 /* Signaled after receiving an error packet from the display in 227 * response to a read. 228 */ 229 # define DSI1_INT_RXPKT2 BIT(5) 230 /* Signaled after receiving a packet. The header and optional short 231 * response will be in RXPKT1H, and a long response will be in the 232 * RXPKT_FIFO. 233 */ 234 # define DSI1_INT_RXPKT1 BIT(4) 235 # define DSI1_INT_TXPKT2_DONE BIT(3) 236 # define DSI1_INT_TXPKT2_END BIT(2) 237 /* Signaled after all repeats of TXPKT1 are transferred. */ 238 # define DSI1_INT_TXPKT1_DONE BIT(1) 239 /* Signaled after each TXPKT1 repeat is scheduled. */ 240 # define DSI1_INT_TXPKT1_END BIT(0) 241 242 #define DSI1_INTERRUPTS_ALWAYS_ENABLED (DSI1_INT_ERR_SYNC_ESC | \ 243 DSI1_INT_ERR_CONTROL | \ 244 DSI1_INT_ERR_CONT_LP0 | \ 245 DSI1_INT_ERR_CONT_LP1 | \ 246 DSI1_INT_HSTX_TO | \ 247 DSI1_INT_LPRX_TO | \ 248 DSI1_INT_TA_TO | \ 249 DSI1_INT_PR_TO) 250 251 #define DSI0_STAT 0x2c 252 #define DSI0_HSTX_TO_CNT 0x30 253 #define DSI0_LPRX_TO_CNT 0x34 254 #define DSI0_TA_TO_CNT 0x38 255 #define DSI0_PR_TO_CNT 0x3c 256 #define DSI0_PHYC 0x40 257 # define DSI1_PHYC_ESC_CLK_LPDT_MASK VC4_MASK(25, 20) 258 # define DSI1_PHYC_ESC_CLK_LPDT_SHIFT 20 259 # define DSI1_PHYC_HS_CLK_CONTINUOUS BIT(18) 260 # define DSI0_PHYC_ESC_CLK_LPDT_MASK VC4_MASK(17, 12) 261 # define DSI0_PHYC_ESC_CLK_LPDT_SHIFT 12 262 # define DSI1_PHYC_CLANE_ULPS BIT(17) 263 # define DSI1_PHYC_CLANE_ENABLE BIT(16) 264 # define DSI_PHYC_DLANE3_ULPS BIT(13) 265 # define DSI_PHYC_DLANE3_ENABLE BIT(12) 266 # define DSI0_PHYC_HS_CLK_CONTINUOUS BIT(10) 267 # define DSI0_PHYC_CLANE_ULPS BIT(9) 268 # define DSI_PHYC_DLANE2_ULPS BIT(9) 269 # define DSI0_PHYC_CLANE_ENABLE BIT(8) 270 # define DSI_PHYC_DLANE2_ENABLE BIT(8) 271 # define DSI_PHYC_DLANE1_ULPS BIT(5) 272 # define DSI_PHYC_DLANE1_ENABLE BIT(4) 273 # define DSI_PHYC_DLANE0_FORCE_STOP BIT(2) 274 # define DSI_PHYC_DLANE0_ULPS BIT(1) 275 # define DSI_PHYC_DLANE0_ENABLE BIT(0) 276 277 #define DSI0_HS_CLT0 0x44 278 #define DSI0_HS_CLT1 0x48 279 #define DSI0_HS_CLT2 0x4c 280 #define DSI0_HS_DLT3 0x50 281 #define DSI0_HS_DLT4 0x54 282 #define DSI0_HS_DLT5 0x58 283 #define DSI0_HS_DLT6 0x5c 284 #define DSI0_HS_DLT7 0x60 285 286 #define DSI0_PHY_AFEC0 0x64 287 # define DSI0_PHY_AFEC0_DDR2CLK_EN BIT(26) 288 # define DSI0_PHY_AFEC0_DDRCLK_EN BIT(25) 289 # define DSI0_PHY_AFEC0_LATCH_ULPS BIT(24) 290 # define DSI1_PHY_AFEC0_IDR_DLANE3_MASK VC4_MASK(31, 29) 291 # define DSI1_PHY_AFEC0_IDR_DLANE3_SHIFT 29 292 # define DSI1_PHY_AFEC0_IDR_DLANE2_MASK VC4_MASK(28, 26) 293 # define DSI1_PHY_AFEC0_IDR_DLANE2_SHIFT 26 294 # define DSI1_PHY_AFEC0_IDR_DLANE1_MASK VC4_MASK(27, 23) 295 # define DSI1_PHY_AFEC0_IDR_DLANE1_SHIFT 23 296 # define DSI1_PHY_AFEC0_IDR_DLANE0_MASK VC4_MASK(22, 20) 297 # define DSI1_PHY_AFEC0_IDR_DLANE0_SHIFT 20 298 # define DSI1_PHY_AFEC0_IDR_CLANE_MASK VC4_MASK(19, 17) 299 # define DSI1_PHY_AFEC0_IDR_CLANE_SHIFT 17 300 # define DSI0_PHY_AFEC0_ACTRL_DLANE1_MASK VC4_MASK(23, 20) 301 # define DSI0_PHY_AFEC0_ACTRL_DLANE1_SHIFT 20 302 # define DSI0_PHY_AFEC0_ACTRL_DLANE0_MASK VC4_MASK(19, 16) 303 # define DSI0_PHY_AFEC0_ACTRL_DLANE0_SHIFT 16 304 # define DSI0_PHY_AFEC0_ACTRL_CLANE_MASK VC4_MASK(15, 12) 305 # define DSI0_PHY_AFEC0_ACTRL_CLANE_SHIFT 12 306 # define DSI1_PHY_AFEC0_DDR2CLK_EN BIT(16) 307 # define DSI1_PHY_AFEC0_DDRCLK_EN BIT(15) 308 # define DSI1_PHY_AFEC0_LATCH_ULPS BIT(14) 309 # define DSI1_PHY_AFEC0_RESET BIT(13) 310 # define DSI1_PHY_AFEC0_PD BIT(12) 311 # define DSI0_PHY_AFEC0_RESET BIT(11) 312 # define DSI1_PHY_AFEC0_PD_BG BIT(11) 313 # define DSI0_PHY_AFEC0_PD BIT(10) 314 # define DSI1_PHY_AFEC0_PD_DLANE3 BIT(10) 315 # define DSI0_PHY_AFEC0_PD_BG BIT(9) 316 # define DSI1_PHY_AFEC0_PD_DLANE2 BIT(9) 317 # define DSI0_PHY_AFEC0_PD_DLANE1 BIT(8) 318 # define DSI1_PHY_AFEC0_PD_DLANE1 BIT(8) 319 # define DSI_PHY_AFEC0_PTATADJ_MASK VC4_MASK(7, 4) 320 # define DSI_PHY_AFEC0_PTATADJ_SHIFT 4 321 # define DSI_PHY_AFEC0_CTATADJ_MASK VC4_MASK(3, 0) 322 # define DSI_PHY_AFEC0_CTATADJ_SHIFT 0 323 324 #define DSI0_PHY_AFEC1 0x68 325 # define DSI0_PHY_AFEC1_IDR_DLANE1_MASK VC4_MASK(10, 8) 326 # define DSI0_PHY_AFEC1_IDR_DLANE1_SHIFT 8 327 # define DSI0_PHY_AFEC1_IDR_DLANE0_MASK VC4_MASK(6, 4) 328 # define DSI0_PHY_AFEC1_IDR_DLANE0_SHIFT 4 329 # define DSI0_PHY_AFEC1_IDR_CLANE_MASK VC4_MASK(2, 0) 330 # define DSI0_PHY_AFEC1_IDR_CLANE_SHIFT 0 331 332 #define DSI0_TST_SEL 0x6c 333 #define DSI0_TST_MON 0x70 334 #define DSI0_ID 0x74 335 # define DSI_ID_VALUE 0x00647369 336 337 #define DSI1_CTRL 0x00 338 # define DSI_CTRL_HS_CLKC_MASK VC4_MASK(15, 14) 339 # define DSI_CTRL_HS_CLKC_SHIFT 14 340 # define DSI_CTRL_HS_CLKC_BYTE 0 341 # define DSI_CTRL_HS_CLKC_DDR2 1 342 # define DSI_CTRL_HS_CLKC_DDR 2 343 344 # define DSI_CTRL_RX_LPDT_EOT_DISABLE BIT(13) 345 # define DSI_CTRL_LPDT_EOT_DISABLE BIT(12) 346 # define DSI_CTRL_HSDT_EOT_DISABLE BIT(11) 347 # define DSI_CTRL_SOFT_RESET_CFG BIT(10) 348 # define DSI_CTRL_CAL_BYTE BIT(9) 349 # define DSI_CTRL_INV_BYTE BIT(8) 350 # define DSI_CTRL_CLR_LDF BIT(7) 351 # define DSI0_CTRL_CLR_PBCF BIT(6) 352 # define DSI1_CTRL_CLR_RXF BIT(6) 353 # define DSI0_CTRL_CLR_CPBCF BIT(5) 354 # define DSI1_CTRL_CLR_PDF BIT(5) 355 # define DSI0_CTRL_CLR_PDF BIT(4) 356 # define DSI1_CTRL_CLR_CDF BIT(4) 357 # define DSI0_CTRL_CLR_CDF BIT(3) 358 # define DSI0_CTRL_CTRL2 BIT(2) 359 # define DSI1_CTRL_DISABLE_DISP_CRCC BIT(2) 360 # define DSI0_CTRL_CTRL1 BIT(1) 361 # define DSI1_CTRL_DISABLE_DISP_ECCC BIT(1) 362 # define DSI0_CTRL_CTRL0 BIT(0) 363 # define DSI1_CTRL_EN BIT(0) 364 # define DSI0_CTRL_RESET_FIFOS (DSI_CTRL_CLR_LDF | \ 365 DSI0_CTRL_CLR_PBCF | \ 366 DSI0_CTRL_CLR_CPBCF | \ 367 DSI0_CTRL_CLR_PDF | \ 368 DSI0_CTRL_CLR_CDF) 369 # define DSI1_CTRL_RESET_FIFOS (DSI_CTRL_CLR_LDF | \ 370 DSI1_CTRL_CLR_RXF | \ 371 DSI1_CTRL_CLR_PDF | \ 372 DSI1_CTRL_CLR_CDF) 373 374 #define DSI1_TXPKT2C 0x0c 375 #define DSI1_TXPKT2H 0x10 376 #define DSI1_TXPKT_PIX_FIFO 0x20 377 #define DSI1_RXPKT_FIFO 0x24 378 #define DSI1_DISP0_CTRL 0x28 379 #define DSI1_INT_STAT 0x30 380 #define DSI1_INT_EN 0x34 381 /* State reporting bits. These mostly behave like INT_STAT, where 382 * writing a 1 clears the bit. 383 */ 384 #define DSI1_STAT 0x38 385 # define DSI1_STAT_PHY_D3_ULPS BIT(31) 386 # define DSI1_STAT_PHY_D3_STOP BIT(30) 387 # define DSI1_STAT_PHY_D2_ULPS BIT(29) 388 # define DSI1_STAT_PHY_D2_STOP BIT(28) 389 # define DSI1_STAT_PHY_D1_ULPS BIT(27) 390 # define DSI1_STAT_PHY_D1_STOP BIT(26) 391 # define DSI1_STAT_PHY_D0_ULPS BIT(25) 392 # define DSI1_STAT_PHY_D0_STOP BIT(24) 393 # define DSI1_STAT_FIFO_ERR BIT(23) 394 # define DSI1_STAT_PHY_RXLPDT BIT(22) 395 # define DSI1_STAT_PHY_RXTRIG BIT(21) 396 # define DSI1_STAT_PHY_D0_LPDT BIT(20) 397 /* Set when in forward direction */ 398 # define DSI1_STAT_PHY_DIR BIT(19) 399 # define DSI1_STAT_PHY_CLOCK_ULPS BIT(18) 400 # define DSI1_STAT_PHY_CLOCK_HS BIT(17) 401 # define DSI1_STAT_PHY_CLOCK_STOP BIT(16) 402 # define DSI1_STAT_PR_TO BIT(15) 403 # define DSI1_STAT_TA_TO BIT(14) 404 # define DSI1_STAT_LPRX_TO BIT(13) 405 # define DSI1_STAT_HSTX_TO BIT(12) 406 # define DSI1_STAT_ERR_CONT_LP1 BIT(11) 407 # define DSI1_STAT_ERR_CONT_LP0 BIT(10) 408 # define DSI1_STAT_ERR_CONTROL BIT(9) 409 # define DSI1_STAT_ERR_SYNC_ESC BIT(8) 410 # define DSI1_STAT_RXPKT2 BIT(7) 411 # define DSI1_STAT_RXPKT1 BIT(6) 412 # define DSI1_STAT_TXPKT2_BUSY BIT(5) 413 # define DSI1_STAT_TXPKT2_DONE BIT(4) 414 # define DSI1_STAT_TXPKT2_END BIT(3) 415 # define DSI1_STAT_TXPKT1_BUSY BIT(2) 416 # define DSI1_STAT_TXPKT1_DONE BIT(1) 417 # define DSI1_STAT_TXPKT1_END BIT(0) 418 419 #define DSI1_HSTX_TO_CNT 0x3c 420 #define DSI1_LPRX_TO_CNT 0x40 421 #define DSI1_TA_TO_CNT 0x44 422 #define DSI1_PR_TO_CNT 0x48 423 #define DSI1_PHYC 0x4c 424 425 #define DSI1_HS_CLT0 0x50 426 # define DSI_HS_CLT0_CZERO_MASK VC4_MASK(26, 18) 427 # define DSI_HS_CLT0_CZERO_SHIFT 18 428 # define DSI_HS_CLT0_CPRE_MASK VC4_MASK(17, 9) 429 # define DSI_HS_CLT0_CPRE_SHIFT 9 430 # define DSI_HS_CLT0_CPREP_MASK VC4_MASK(8, 0) 431 # define DSI_HS_CLT0_CPREP_SHIFT 0 432 433 #define DSI1_HS_CLT1 0x54 434 # define DSI_HS_CLT1_CTRAIL_MASK VC4_MASK(17, 9) 435 # define DSI_HS_CLT1_CTRAIL_SHIFT 9 436 # define DSI_HS_CLT1_CPOST_MASK VC4_MASK(8, 0) 437 # define DSI_HS_CLT1_CPOST_SHIFT 0 438 439 #define DSI1_HS_CLT2 0x58 440 # define DSI_HS_CLT2_WUP_MASK VC4_MASK(23, 0) 441 # define DSI_HS_CLT2_WUP_SHIFT 0 442 443 #define DSI1_HS_DLT3 0x5c 444 # define DSI_HS_DLT3_EXIT_MASK VC4_MASK(26, 18) 445 # define DSI_HS_DLT3_EXIT_SHIFT 18 446 # define DSI_HS_DLT3_ZERO_MASK VC4_MASK(17, 9) 447 # define DSI_HS_DLT3_ZERO_SHIFT 9 448 # define DSI_HS_DLT3_PRE_MASK VC4_MASK(8, 0) 449 # define DSI_HS_DLT3_PRE_SHIFT 0 450 451 #define DSI1_HS_DLT4 0x60 452 # define DSI_HS_DLT4_ANLAT_MASK VC4_MASK(22, 18) 453 # define DSI_HS_DLT4_ANLAT_SHIFT 18 454 # define DSI_HS_DLT4_TRAIL_MASK VC4_MASK(17, 9) 455 # define DSI_HS_DLT4_TRAIL_SHIFT 9 456 # define DSI_HS_DLT4_LPX_MASK VC4_MASK(8, 0) 457 # define DSI_HS_DLT4_LPX_SHIFT 0 458 459 #define DSI1_HS_DLT5 0x64 460 # define DSI_HS_DLT5_INIT_MASK VC4_MASK(23, 0) 461 # define DSI_HS_DLT5_INIT_SHIFT 0 462 463 #define DSI1_HS_DLT6 0x68 464 # define DSI_HS_DLT6_TA_GET_MASK VC4_MASK(31, 24) 465 # define DSI_HS_DLT6_TA_GET_SHIFT 24 466 # define DSI_HS_DLT6_TA_SURE_MASK VC4_MASK(23, 16) 467 # define DSI_HS_DLT6_TA_SURE_SHIFT 16 468 # define DSI_HS_DLT6_TA_GO_MASK VC4_MASK(15, 8) 469 # define DSI_HS_DLT6_TA_GO_SHIFT 8 470 # define DSI_HS_DLT6_LP_LPX_MASK VC4_MASK(7, 0) 471 # define DSI_HS_DLT6_LP_LPX_SHIFT 0 472 473 #define DSI1_HS_DLT7 0x6c 474 # define DSI_HS_DLT7_LP_WUP_MASK VC4_MASK(23, 0) 475 # define DSI_HS_DLT7_LP_WUP_SHIFT 0 476 477 #define DSI1_PHY_AFEC0 0x70 478 479 #define DSI1_PHY_AFEC1 0x74 480 # define DSI1_PHY_AFEC1_ACTRL_DLANE3_MASK VC4_MASK(19, 16) 481 # define DSI1_PHY_AFEC1_ACTRL_DLANE3_SHIFT 16 482 # define DSI1_PHY_AFEC1_ACTRL_DLANE2_MASK VC4_MASK(15, 12) 483 # define DSI1_PHY_AFEC1_ACTRL_DLANE2_SHIFT 12 484 # define DSI1_PHY_AFEC1_ACTRL_DLANE1_MASK VC4_MASK(11, 8) 485 # define DSI1_PHY_AFEC1_ACTRL_DLANE1_SHIFT 8 486 # define DSI1_PHY_AFEC1_ACTRL_DLANE0_MASK VC4_MASK(7, 4) 487 # define DSI1_PHY_AFEC1_ACTRL_DLANE0_SHIFT 4 488 # define DSI1_PHY_AFEC1_ACTRL_CLANE_MASK VC4_MASK(3, 0) 489 # define DSI1_PHY_AFEC1_ACTRL_CLANE_SHIFT 0 490 491 #define DSI1_TST_SEL 0x78 492 #define DSI1_TST_MON 0x7c 493 #define DSI1_PHY_TST1 0x80 494 #define DSI1_PHY_TST2 0x84 495 #define DSI1_PHY_FIFO_STAT 0x88 496 /* Actually, all registers in the range that aren't otherwise claimed 497 * will return the ID. 498 */ 499 #define DSI1_ID 0x8c 500 501 /* General DSI hardware state. */ 502 struct vc4_dsi { 503 struct platform_device *pdev; 504 505 struct mipi_dsi_host dsi_host; 506 struct drm_encoder *encoder; 507 struct drm_bridge *bridge; 508 509 void __iomem *regs; 510 511 struct dma_chan *reg_dma_chan; 512 dma_addr_t reg_dma_paddr; 513 u32 *reg_dma_mem; 514 dma_addr_t reg_paddr; 515 516 /* Whether we're on bcm2835's DSI0 or DSI1. */ 517 int port; 518 519 /* DSI channel for the panel we're connected to. */ 520 u32 channel; 521 u32 lanes; 522 u32 format; 523 u32 divider; 524 u32 mode_flags; 525 526 /* Input clock from CPRMAN to the digital PHY, for the DSI 527 * escape clock. 528 */ 529 struct clk *escape_clock; 530 531 /* Input clock to the analog PHY, used to generate the DSI bit 532 * clock. 533 */ 534 struct clk *pll_phy_clock; 535 536 /* HS Clocks generated within the DSI analog PHY. */ 537 struct clk_fixed_factor phy_clocks[3]; 538 539 struct clk_hw_onecell_data *clk_onecell; 540 541 /* Pixel clock output to the pixelvalve, generated from the HS 542 * clock. 543 */ 544 struct clk *pixel_clock; 545 546 struct completion xfer_completion; 547 int xfer_result; 548 }; 549 550 #define host_to_dsi(host) container_of(host, struct vc4_dsi, dsi_host) 551 552 static inline void 553 dsi_dma_workaround_write(struct vc4_dsi *dsi, u32 offset, u32 val) 554 { 555 struct dma_chan *chan = dsi->reg_dma_chan; 556 struct dma_async_tx_descriptor *tx; 557 dma_cookie_t cookie; 558 int ret; 559 560 /* DSI0 should be able to write normally. */ 561 if (!chan) { 562 writel(val, dsi->regs + offset); 563 return; 564 } 565 566 *dsi->reg_dma_mem = val; 567 568 tx = chan->device->device_prep_dma_memcpy(chan, 569 dsi->reg_paddr + offset, 570 dsi->reg_dma_paddr, 571 4, 0); 572 if (!tx) { 573 DRM_ERROR("Failed to set up DMA register write\n"); 574 return; 575 } 576 577 cookie = tx->tx_submit(tx); 578 ret = dma_submit_error(cookie); 579 if (ret) { 580 DRM_ERROR("Failed to submit DMA: %d\n", ret); 581 return; 582 } 583 ret = dma_sync_wait(chan, cookie); 584 if (ret) 585 DRM_ERROR("Failed to wait for DMA: %d\n", ret); 586 } 587 588 #define DSI_READ(offset) readl(dsi->regs + (offset)) 589 #define DSI_WRITE(offset, val) dsi_dma_workaround_write(dsi, offset, val) 590 #define DSI_PORT_READ(offset) \ 591 DSI_READ(dsi->port ? DSI1_##offset : DSI0_##offset) 592 #define DSI_PORT_WRITE(offset, val) \ 593 DSI_WRITE(dsi->port ? DSI1_##offset : DSI0_##offset, val) 594 #define DSI_PORT_BIT(bit) (dsi->port ? DSI1_##bit : DSI0_##bit) 595 596 /* VC4 DSI encoder KMS struct */ 597 struct vc4_dsi_encoder { 598 struct vc4_encoder base; 599 struct vc4_dsi *dsi; 600 }; 601 602 static inline struct vc4_dsi_encoder * 603 to_vc4_dsi_encoder(struct drm_encoder *encoder) 604 { 605 return container_of(encoder, struct vc4_dsi_encoder, base.base); 606 } 607 608 #define DSI_REG(reg) { reg, #reg } 609 static const struct { 610 u32 reg; 611 const char *name; 612 } dsi0_regs[] = { 613 DSI_REG(DSI0_CTRL), 614 DSI_REG(DSI0_STAT), 615 DSI_REG(DSI0_HSTX_TO_CNT), 616 DSI_REG(DSI0_LPRX_TO_CNT), 617 DSI_REG(DSI0_TA_TO_CNT), 618 DSI_REG(DSI0_PR_TO_CNT), 619 DSI_REG(DSI0_DISP0_CTRL), 620 DSI_REG(DSI0_DISP1_CTRL), 621 DSI_REG(DSI0_INT_STAT), 622 DSI_REG(DSI0_INT_EN), 623 DSI_REG(DSI0_PHYC), 624 DSI_REG(DSI0_HS_CLT0), 625 DSI_REG(DSI0_HS_CLT1), 626 DSI_REG(DSI0_HS_CLT2), 627 DSI_REG(DSI0_HS_DLT3), 628 DSI_REG(DSI0_HS_DLT4), 629 DSI_REG(DSI0_HS_DLT5), 630 DSI_REG(DSI0_HS_DLT6), 631 DSI_REG(DSI0_HS_DLT7), 632 DSI_REG(DSI0_PHY_AFEC0), 633 DSI_REG(DSI0_PHY_AFEC1), 634 DSI_REG(DSI0_ID), 635 }; 636 637 static const struct { 638 u32 reg; 639 const char *name; 640 } dsi1_regs[] = { 641 DSI_REG(DSI1_CTRL), 642 DSI_REG(DSI1_STAT), 643 DSI_REG(DSI1_HSTX_TO_CNT), 644 DSI_REG(DSI1_LPRX_TO_CNT), 645 DSI_REG(DSI1_TA_TO_CNT), 646 DSI_REG(DSI1_PR_TO_CNT), 647 DSI_REG(DSI1_DISP0_CTRL), 648 DSI_REG(DSI1_DISP1_CTRL), 649 DSI_REG(DSI1_INT_STAT), 650 DSI_REG(DSI1_INT_EN), 651 DSI_REG(DSI1_PHYC), 652 DSI_REG(DSI1_HS_CLT0), 653 DSI_REG(DSI1_HS_CLT1), 654 DSI_REG(DSI1_HS_CLT2), 655 DSI_REG(DSI1_HS_DLT3), 656 DSI_REG(DSI1_HS_DLT4), 657 DSI_REG(DSI1_HS_DLT5), 658 DSI_REG(DSI1_HS_DLT6), 659 DSI_REG(DSI1_HS_DLT7), 660 DSI_REG(DSI1_PHY_AFEC0), 661 DSI_REG(DSI1_PHY_AFEC1), 662 DSI_REG(DSI1_ID), 663 }; 664 665 static void vc4_dsi_dump_regs(struct vc4_dsi *dsi) 666 { 667 int i; 668 669 if (dsi->port == 0) { 670 for (i = 0; i < ARRAY_SIZE(dsi0_regs); i++) { 671 DRM_INFO("0x%04x (%s): 0x%08x\n", 672 dsi0_regs[i].reg, dsi0_regs[i].name, 673 DSI_READ(dsi0_regs[i].reg)); 674 } 675 } else { 676 for (i = 0; i < ARRAY_SIZE(dsi1_regs); i++) { 677 DRM_INFO("0x%04x (%s): 0x%08x\n", 678 dsi1_regs[i].reg, dsi1_regs[i].name, 679 DSI_READ(dsi1_regs[i].reg)); 680 } 681 } 682 } 683 684 #ifdef CONFIG_DEBUG_FS 685 int vc4_dsi_debugfs_regs(struct seq_file *m, void *unused) 686 { 687 struct drm_info_node *node = (struct drm_info_node *)m->private; 688 struct drm_device *drm = node->minor->dev; 689 struct vc4_dev *vc4 = to_vc4_dev(drm); 690 int dsi_index = (uintptr_t)node->info_ent->data; 691 struct vc4_dsi *dsi = (dsi_index == 1 ? vc4->dsi1 : NULL); 692 int i; 693 694 if (!dsi) 695 return 0; 696 697 if (dsi->port == 0) { 698 for (i = 0; i < ARRAY_SIZE(dsi0_regs); i++) { 699 seq_printf(m, "0x%04x (%s): 0x%08x\n", 700 dsi0_regs[i].reg, dsi0_regs[i].name, 701 DSI_READ(dsi0_regs[i].reg)); 702 } 703 } else { 704 for (i = 0; i < ARRAY_SIZE(dsi1_regs); i++) { 705 seq_printf(m, "0x%04x (%s): 0x%08x\n", 706 dsi1_regs[i].reg, dsi1_regs[i].name, 707 DSI_READ(dsi1_regs[i].reg)); 708 } 709 } 710 711 return 0; 712 } 713 #endif 714 715 static void vc4_dsi_encoder_destroy(struct drm_encoder *encoder) 716 { 717 drm_encoder_cleanup(encoder); 718 } 719 720 static const struct drm_encoder_funcs vc4_dsi_encoder_funcs = { 721 .destroy = vc4_dsi_encoder_destroy, 722 }; 723 724 static void vc4_dsi_latch_ulps(struct vc4_dsi *dsi, bool latch) 725 { 726 u32 afec0 = DSI_PORT_READ(PHY_AFEC0); 727 728 if (latch) 729 afec0 |= DSI_PORT_BIT(PHY_AFEC0_LATCH_ULPS); 730 else 731 afec0 &= ~DSI_PORT_BIT(PHY_AFEC0_LATCH_ULPS); 732 733 DSI_PORT_WRITE(PHY_AFEC0, afec0); 734 } 735 736 /* Enters or exits Ultra Low Power State. */ 737 static void vc4_dsi_ulps(struct vc4_dsi *dsi, bool ulps) 738 { 739 bool non_continuous = dsi->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS; 740 u32 phyc_ulps = ((non_continuous ? DSI_PORT_BIT(PHYC_CLANE_ULPS) : 0) | 741 DSI_PHYC_DLANE0_ULPS | 742 (dsi->lanes > 1 ? DSI_PHYC_DLANE1_ULPS : 0) | 743 (dsi->lanes > 2 ? DSI_PHYC_DLANE2_ULPS : 0) | 744 (dsi->lanes > 3 ? DSI_PHYC_DLANE3_ULPS : 0)); 745 u32 stat_ulps = ((non_continuous ? DSI1_STAT_PHY_CLOCK_ULPS : 0) | 746 DSI1_STAT_PHY_D0_ULPS | 747 (dsi->lanes > 1 ? DSI1_STAT_PHY_D1_ULPS : 0) | 748 (dsi->lanes > 2 ? DSI1_STAT_PHY_D2_ULPS : 0) | 749 (dsi->lanes > 3 ? DSI1_STAT_PHY_D3_ULPS : 0)); 750 u32 stat_stop = ((non_continuous ? DSI1_STAT_PHY_CLOCK_STOP : 0) | 751 DSI1_STAT_PHY_D0_STOP | 752 (dsi->lanes > 1 ? DSI1_STAT_PHY_D1_STOP : 0) | 753 (dsi->lanes > 2 ? DSI1_STAT_PHY_D2_STOP : 0) | 754 (dsi->lanes > 3 ? DSI1_STAT_PHY_D3_STOP : 0)); 755 int ret; 756 bool ulps_currently_enabled = (DSI_PORT_READ(PHY_AFEC0) & 757 DSI_PORT_BIT(PHY_AFEC0_LATCH_ULPS)); 758 759 if (ulps == ulps_currently_enabled) 760 return; 761 762 DSI_PORT_WRITE(STAT, stat_ulps); 763 DSI_PORT_WRITE(PHYC, DSI_PORT_READ(PHYC) | phyc_ulps); 764 ret = wait_for((DSI_PORT_READ(STAT) & stat_ulps) == stat_ulps, 200); 765 if (ret) { 766 dev_warn(&dsi->pdev->dev, 767 "Timeout waiting for DSI ULPS entry: STAT 0x%08x", 768 DSI_PORT_READ(STAT)); 769 DSI_PORT_WRITE(PHYC, DSI_PORT_READ(PHYC) & ~phyc_ulps); 770 vc4_dsi_latch_ulps(dsi, false); 771 return; 772 } 773 774 /* The DSI module can't be disabled while the module is 775 * generating ULPS state. So, to be able to disable the 776 * module, we have the AFE latch the ULPS state and continue 777 * on to having the module enter STOP. 778 */ 779 vc4_dsi_latch_ulps(dsi, ulps); 780 781 DSI_PORT_WRITE(STAT, stat_stop); 782 DSI_PORT_WRITE(PHYC, DSI_PORT_READ(PHYC) & ~phyc_ulps); 783 ret = wait_for((DSI_PORT_READ(STAT) & stat_stop) == stat_stop, 200); 784 if (ret) { 785 dev_warn(&dsi->pdev->dev, 786 "Timeout waiting for DSI STOP entry: STAT 0x%08x", 787 DSI_PORT_READ(STAT)); 788 DSI_PORT_WRITE(PHYC, DSI_PORT_READ(PHYC) & ~phyc_ulps); 789 return; 790 } 791 } 792 793 static u32 794 dsi_hs_timing(u32 ui_ns, u32 ns, u32 ui) 795 { 796 /* The HS timings have to be rounded up to a multiple of 8 797 * because we're using the byte clock. 798 */ 799 return roundup(ui + DIV_ROUND_UP(ns, ui_ns), 8); 800 } 801 802 /* ESC always runs at 100Mhz. */ 803 #define ESC_TIME_NS 10 804 805 static u32 806 dsi_esc_timing(u32 ns) 807 { 808 return DIV_ROUND_UP(ns, ESC_TIME_NS); 809 } 810 811 static void vc4_dsi_encoder_disable(struct drm_encoder *encoder) 812 { 813 struct vc4_dsi_encoder *vc4_encoder = to_vc4_dsi_encoder(encoder); 814 struct vc4_dsi *dsi = vc4_encoder->dsi; 815 struct device *dev = &dsi->pdev->dev; 816 817 drm_bridge_disable(dsi->bridge); 818 vc4_dsi_ulps(dsi, true); 819 drm_bridge_post_disable(dsi->bridge); 820 821 clk_disable_unprepare(dsi->pll_phy_clock); 822 clk_disable_unprepare(dsi->escape_clock); 823 clk_disable_unprepare(dsi->pixel_clock); 824 825 pm_runtime_put(dev); 826 } 827 828 /* Extends the mode's blank intervals to handle BCM2835's integer-only 829 * DSI PLL divider. 830 * 831 * On 2835, PLLD is set to 2Ghz, and may not be changed by the display 832 * driver since most peripherals are hanging off of the PLLD_PER 833 * divider. PLLD_DSI1, which drives our DSI bit clock (and therefore 834 * the pixel clock), only has an integer divider off of DSI. 835 * 836 * To get our panel mode to refresh at the expected 60Hz, we need to 837 * extend the horizontal blank time. This means we drive a 838 * higher-than-expected clock rate to the panel, but that's what the 839 * firmware does too. 840 */ 841 static bool vc4_dsi_encoder_mode_fixup(struct drm_encoder *encoder, 842 const struct drm_display_mode *mode, 843 struct drm_display_mode *adjusted_mode) 844 { 845 struct vc4_dsi_encoder *vc4_encoder = to_vc4_dsi_encoder(encoder); 846 struct vc4_dsi *dsi = vc4_encoder->dsi; 847 struct clk *phy_parent = clk_get_parent(dsi->pll_phy_clock); 848 unsigned long parent_rate = clk_get_rate(phy_parent); 849 unsigned long pixel_clock_hz = mode->clock * 1000; 850 unsigned long pll_clock = pixel_clock_hz * dsi->divider; 851 int divider; 852 853 /* Find what divider gets us a faster clock than the requested 854 * pixel clock. 855 */ 856 for (divider = 1; divider < 8; divider++) { 857 if (parent_rate / divider < pll_clock) { 858 divider--; 859 break; 860 } 861 } 862 863 /* Now that we've picked a PLL divider, calculate back to its 864 * pixel clock. 865 */ 866 pll_clock = parent_rate / divider; 867 pixel_clock_hz = pll_clock / dsi->divider; 868 869 adjusted_mode->clock = pixel_clock_hz / 1000; 870 871 /* Given the new pixel clock, adjust HFP to keep vrefresh the same. */ 872 adjusted_mode->htotal = adjusted_mode->clock * mode->htotal / 873 mode->clock; 874 adjusted_mode->hsync_end += adjusted_mode->htotal - mode->htotal; 875 adjusted_mode->hsync_start += adjusted_mode->htotal - mode->htotal; 876 877 return true; 878 } 879 880 static void vc4_dsi_encoder_enable(struct drm_encoder *encoder) 881 { 882 struct drm_display_mode *mode = &encoder->crtc->state->adjusted_mode; 883 struct vc4_dsi_encoder *vc4_encoder = to_vc4_dsi_encoder(encoder); 884 struct vc4_dsi *dsi = vc4_encoder->dsi; 885 struct device *dev = &dsi->pdev->dev; 886 bool debug_dump_regs = false; 887 unsigned long hs_clock; 888 u32 ui_ns; 889 /* Minimum LP state duration in escape clock cycles. */ 890 u32 lpx = dsi_esc_timing(60); 891 unsigned long pixel_clock_hz = mode->clock * 1000; 892 unsigned long dsip_clock; 893 unsigned long phy_clock; 894 int ret; 895 896 ret = pm_runtime_get_sync(dev); 897 if (ret) { 898 DRM_ERROR("Failed to runtime PM enable on DSI%d\n", dsi->port); 899 return; 900 } 901 902 if (debug_dump_regs) { 903 DRM_INFO("DSI regs before:\n"); 904 vc4_dsi_dump_regs(dsi); 905 } 906 907 /* Round up the clk_set_rate() request slightly, since 908 * PLLD_DSI1 is an integer divider and its rate selection will 909 * never round up. 910 */ 911 phy_clock = (pixel_clock_hz + 1000) * dsi->divider; 912 ret = clk_set_rate(dsi->pll_phy_clock, phy_clock); 913 if (ret) { 914 dev_err(&dsi->pdev->dev, 915 "Failed to set phy clock to %ld: %d\n", phy_clock, ret); 916 } 917 918 /* Reset the DSI and all its fifos. */ 919 DSI_PORT_WRITE(CTRL, 920 DSI_CTRL_SOFT_RESET_CFG | 921 DSI_PORT_BIT(CTRL_RESET_FIFOS)); 922 923 DSI_PORT_WRITE(CTRL, 924 DSI_CTRL_HSDT_EOT_DISABLE | 925 DSI_CTRL_RX_LPDT_EOT_DISABLE); 926 927 /* Clear all stat bits so we see what has happened during enable. */ 928 DSI_PORT_WRITE(STAT, DSI_PORT_READ(STAT)); 929 930 /* Set AFE CTR00/CTR1 to release powerdown of analog. */ 931 if (dsi->port == 0) { 932 u32 afec0 = (VC4_SET_FIELD(7, DSI_PHY_AFEC0_PTATADJ) | 933 VC4_SET_FIELD(7, DSI_PHY_AFEC0_CTATADJ)); 934 935 if (dsi->lanes < 2) 936 afec0 |= DSI0_PHY_AFEC0_PD_DLANE1; 937 938 if (!(dsi->mode_flags & MIPI_DSI_MODE_VIDEO)) 939 afec0 |= DSI0_PHY_AFEC0_RESET; 940 941 DSI_PORT_WRITE(PHY_AFEC0, afec0); 942 943 DSI_PORT_WRITE(PHY_AFEC1, 944 VC4_SET_FIELD(6, DSI0_PHY_AFEC1_IDR_DLANE1) | 945 VC4_SET_FIELD(6, DSI0_PHY_AFEC1_IDR_DLANE0) | 946 VC4_SET_FIELD(6, DSI0_PHY_AFEC1_IDR_CLANE)); 947 } else { 948 u32 afec0 = (VC4_SET_FIELD(7, DSI_PHY_AFEC0_PTATADJ) | 949 VC4_SET_FIELD(7, DSI_PHY_AFEC0_CTATADJ) | 950 VC4_SET_FIELD(6, DSI1_PHY_AFEC0_IDR_CLANE) | 951 VC4_SET_FIELD(6, DSI1_PHY_AFEC0_IDR_DLANE0) | 952 VC4_SET_FIELD(6, DSI1_PHY_AFEC0_IDR_DLANE1) | 953 VC4_SET_FIELD(6, DSI1_PHY_AFEC0_IDR_DLANE2) | 954 VC4_SET_FIELD(6, DSI1_PHY_AFEC0_IDR_DLANE3)); 955 956 if (dsi->lanes < 4) 957 afec0 |= DSI1_PHY_AFEC0_PD_DLANE3; 958 if (dsi->lanes < 3) 959 afec0 |= DSI1_PHY_AFEC0_PD_DLANE2; 960 if (dsi->lanes < 2) 961 afec0 |= DSI1_PHY_AFEC0_PD_DLANE1; 962 963 afec0 |= DSI1_PHY_AFEC0_RESET; 964 965 DSI_PORT_WRITE(PHY_AFEC0, afec0); 966 967 DSI_PORT_WRITE(PHY_AFEC1, 0); 968 969 /* AFEC reset hold time */ 970 mdelay(1); 971 } 972 973 ret = clk_prepare_enable(dsi->escape_clock); 974 if (ret) { 975 DRM_ERROR("Failed to turn on DSI escape clock: %d\n", ret); 976 return; 977 } 978 979 ret = clk_prepare_enable(dsi->pll_phy_clock); 980 if (ret) { 981 DRM_ERROR("Failed to turn on DSI PLL: %d\n", ret); 982 return; 983 } 984 985 hs_clock = clk_get_rate(dsi->pll_phy_clock); 986 987 /* Yes, we set the DSI0P/DSI1P pixel clock to the byte rate, 988 * not the pixel clock rate. DSIxP take from the APHY's byte, 989 * DDR2, or DDR4 clock (we use byte) and feed into the PV at 990 * that rate. Separately, a value derived from PIX_CLK_DIV 991 * and HS_CLKC is fed into the PV to divide down to the actual 992 * pixel clock for pushing pixels into DSI. 993 */ 994 dsip_clock = phy_clock / 8; 995 ret = clk_set_rate(dsi->pixel_clock, dsip_clock); 996 if (ret) { 997 dev_err(dev, "Failed to set pixel clock to %ldHz: %d\n", 998 dsip_clock, ret); 999 } 1000 1001 ret = clk_prepare_enable(dsi->pixel_clock); 1002 if (ret) { 1003 DRM_ERROR("Failed to turn on DSI pixel clock: %d\n", ret); 1004 return; 1005 } 1006 1007 /* How many ns one DSI unit interval is. Note that the clock 1008 * is DDR, so there's an extra divide by 2. 1009 */ 1010 ui_ns = DIV_ROUND_UP(500000000, hs_clock); 1011 1012 DSI_PORT_WRITE(HS_CLT0, 1013 VC4_SET_FIELD(dsi_hs_timing(ui_ns, 262, 0), 1014 DSI_HS_CLT0_CZERO) | 1015 VC4_SET_FIELD(dsi_hs_timing(ui_ns, 0, 8), 1016 DSI_HS_CLT0_CPRE) | 1017 VC4_SET_FIELD(dsi_hs_timing(ui_ns, 38, 0), 1018 DSI_HS_CLT0_CPREP)); 1019 1020 DSI_PORT_WRITE(HS_CLT1, 1021 VC4_SET_FIELD(dsi_hs_timing(ui_ns, 60, 0), 1022 DSI_HS_CLT1_CTRAIL) | 1023 VC4_SET_FIELD(dsi_hs_timing(ui_ns, 60, 52), 1024 DSI_HS_CLT1_CPOST)); 1025 1026 DSI_PORT_WRITE(HS_CLT2, 1027 VC4_SET_FIELD(dsi_hs_timing(ui_ns, 1000000, 0), 1028 DSI_HS_CLT2_WUP)); 1029 1030 DSI_PORT_WRITE(HS_DLT3, 1031 VC4_SET_FIELD(dsi_hs_timing(ui_ns, 100, 0), 1032 DSI_HS_DLT3_EXIT) | 1033 VC4_SET_FIELD(dsi_hs_timing(ui_ns, 105, 6), 1034 DSI_HS_DLT3_ZERO) | 1035 VC4_SET_FIELD(dsi_hs_timing(ui_ns, 40, 4), 1036 DSI_HS_DLT3_PRE)); 1037 1038 DSI_PORT_WRITE(HS_DLT4, 1039 VC4_SET_FIELD(dsi_hs_timing(ui_ns, lpx * ESC_TIME_NS, 0), 1040 DSI_HS_DLT4_LPX) | 1041 VC4_SET_FIELD(max(dsi_hs_timing(ui_ns, 0, 8), 1042 dsi_hs_timing(ui_ns, 60, 4)), 1043 DSI_HS_DLT4_TRAIL) | 1044 VC4_SET_FIELD(0, DSI_HS_DLT4_ANLAT)); 1045 1046 /* T_INIT is how long STOP is driven after power-up to 1047 * indicate to the slave (also coming out of power-up) that 1048 * master init is complete, and should be greater than the 1049 * maximum of two value: T_INIT,MASTER and T_INIT,SLAVE. The 1050 * D-PHY spec gives a minimum 100us for T_INIT,MASTER and 1051 * T_INIT,SLAVE, while allowing protocols on top of it to give 1052 * greater minimums. The vc4 firmware uses an extremely 1053 * conservative 5ms, and we maintain that here. 1054 */ 1055 DSI_PORT_WRITE(HS_DLT5, VC4_SET_FIELD(dsi_hs_timing(ui_ns, 1056 5 * 1000 * 1000, 0), 1057 DSI_HS_DLT5_INIT)); 1058 1059 DSI_PORT_WRITE(HS_DLT6, 1060 VC4_SET_FIELD(lpx * 5, DSI_HS_DLT6_TA_GET) | 1061 VC4_SET_FIELD(lpx, DSI_HS_DLT6_TA_SURE) | 1062 VC4_SET_FIELD(lpx * 4, DSI_HS_DLT6_TA_GO) | 1063 VC4_SET_FIELD(lpx, DSI_HS_DLT6_LP_LPX)); 1064 1065 DSI_PORT_WRITE(HS_DLT7, 1066 VC4_SET_FIELD(dsi_esc_timing(1000000), 1067 DSI_HS_DLT7_LP_WUP)); 1068 1069 DSI_PORT_WRITE(PHYC, 1070 DSI_PHYC_DLANE0_ENABLE | 1071 (dsi->lanes >= 2 ? DSI_PHYC_DLANE1_ENABLE : 0) | 1072 (dsi->lanes >= 3 ? DSI_PHYC_DLANE2_ENABLE : 0) | 1073 (dsi->lanes >= 4 ? DSI_PHYC_DLANE3_ENABLE : 0) | 1074 DSI_PORT_BIT(PHYC_CLANE_ENABLE) | 1075 ((dsi->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS) ? 1076 0 : DSI_PORT_BIT(PHYC_HS_CLK_CONTINUOUS)) | 1077 (dsi->port == 0 ? 1078 VC4_SET_FIELD(lpx - 1, DSI0_PHYC_ESC_CLK_LPDT) : 1079 VC4_SET_FIELD(lpx - 1, DSI1_PHYC_ESC_CLK_LPDT))); 1080 1081 DSI_PORT_WRITE(CTRL, 1082 DSI_PORT_READ(CTRL) | 1083 DSI_CTRL_CAL_BYTE); 1084 1085 /* HS timeout in HS clock cycles: disabled. */ 1086 DSI_PORT_WRITE(HSTX_TO_CNT, 0); 1087 /* LP receive timeout in HS clocks. */ 1088 DSI_PORT_WRITE(LPRX_TO_CNT, 0xffffff); 1089 /* Bus turnaround timeout */ 1090 DSI_PORT_WRITE(TA_TO_CNT, 100000); 1091 /* Display reset sequence timeout */ 1092 DSI_PORT_WRITE(PR_TO_CNT, 100000); 1093 1094 /* Set up DISP1 for transferring long command payloads through 1095 * the pixfifo. 1096 */ 1097 DSI_PORT_WRITE(DISP1_CTRL, 1098 VC4_SET_FIELD(DSI_DISP1_PFORMAT_32BIT_LE, 1099 DSI_DISP1_PFORMAT) | 1100 DSI_DISP1_ENABLE); 1101 1102 /* Ungate the block. */ 1103 if (dsi->port == 0) 1104 DSI_PORT_WRITE(CTRL, DSI_PORT_READ(CTRL) | DSI0_CTRL_CTRL0); 1105 else 1106 DSI_PORT_WRITE(CTRL, DSI_PORT_READ(CTRL) | DSI1_CTRL_EN); 1107 1108 /* Bring AFE out of reset. */ 1109 if (dsi->port == 0) { 1110 } else { 1111 DSI_PORT_WRITE(PHY_AFEC0, 1112 DSI_PORT_READ(PHY_AFEC0) & 1113 ~DSI1_PHY_AFEC0_RESET); 1114 } 1115 1116 vc4_dsi_ulps(dsi, false); 1117 1118 drm_bridge_pre_enable(dsi->bridge); 1119 1120 if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO) { 1121 DSI_PORT_WRITE(DISP0_CTRL, 1122 VC4_SET_FIELD(dsi->divider, 1123 DSI_DISP0_PIX_CLK_DIV) | 1124 VC4_SET_FIELD(dsi->format, DSI_DISP0_PFORMAT) | 1125 VC4_SET_FIELD(DSI_DISP0_LP_STOP_PERFRAME, 1126 DSI_DISP0_LP_STOP_CTRL) | 1127 DSI_DISP0_ST_END | 1128 DSI_DISP0_ENABLE); 1129 } else { 1130 DSI_PORT_WRITE(DISP0_CTRL, 1131 DSI_DISP0_COMMAND_MODE | 1132 DSI_DISP0_ENABLE); 1133 } 1134 1135 drm_bridge_enable(dsi->bridge); 1136 1137 if (debug_dump_regs) { 1138 DRM_INFO("DSI regs after:\n"); 1139 vc4_dsi_dump_regs(dsi); 1140 } 1141 } 1142 1143 static ssize_t vc4_dsi_host_transfer(struct mipi_dsi_host *host, 1144 const struct mipi_dsi_msg *msg) 1145 { 1146 struct vc4_dsi *dsi = host_to_dsi(host); 1147 struct mipi_dsi_packet packet; 1148 u32 pkth = 0, pktc = 0; 1149 int i, ret; 1150 bool is_long = mipi_dsi_packet_format_is_long(msg->type); 1151 u32 cmd_fifo_len = 0, pix_fifo_len = 0; 1152 1153 mipi_dsi_create_packet(&packet, msg); 1154 1155 pkth |= VC4_SET_FIELD(packet.header[0], DSI_TXPKT1H_BC_DT); 1156 pkth |= VC4_SET_FIELD(packet.header[1] | 1157 (packet.header[2] << 8), 1158 DSI_TXPKT1H_BC_PARAM); 1159 if (is_long) { 1160 /* Divide data across the various FIFOs we have available. 1161 * The command FIFO takes byte-oriented data, but is of 1162 * limited size. The pixel FIFO (never actually used for 1163 * pixel data in reality) is word oriented, and substantially 1164 * larger. So, we use the pixel FIFO for most of the data, 1165 * sending the residual bytes in the command FIFO at the start. 1166 * 1167 * With this arrangement, the command FIFO will never get full. 1168 */ 1169 if (packet.payload_length <= 16) { 1170 cmd_fifo_len = packet.payload_length; 1171 pix_fifo_len = 0; 1172 } else { 1173 cmd_fifo_len = (packet.payload_length % 1174 DSI_PIX_FIFO_WIDTH); 1175 pix_fifo_len = ((packet.payload_length - cmd_fifo_len) / 1176 DSI_PIX_FIFO_WIDTH); 1177 } 1178 1179 WARN_ON_ONCE(pix_fifo_len >= DSI_PIX_FIFO_DEPTH); 1180 1181 pkth |= VC4_SET_FIELD(cmd_fifo_len, DSI_TXPKT1H_BC_CMDFIFO); 1182 } 1183 1184 if (msg->rx_len) { 1185 pktc |= VC4_SET_FIELD(DSI_TXPKT1C_CMD_CTRL_RX, 1186 DSI_TXPKT1C_CMD_CTRL); 1187 } else { 1188 pktc |= VC4_SET_FIELD(DSI_TXPKT1C_CMD_CTRL_TX, 1189 DSI_TXPKT1C_CMD_CTRL); 1190 } 1191 1192 for (i = 0; i < cmd_fifo_len; i++) 1193 DSI_PORT_WRITE(TXPKT_CMD_FIFO, packet.payload[i]); 1194 for (i = 0; i < pix_fifo_len; i++) { 1195 const u8 *pix = packet.payload + cmd_fifo_len + i * 4; 1196 1197 DSI_PORT_WRITE(TXPKT_PIX_FIFO, 1198 pix[0] | 1199 pix[1] << 8 | 1200 pix[2] << 16 | 1201 pix[3] << 24); 1202 } 1203 1204 if (msg->flags & MIPI_DSI_MSG_USE_LPM) 1205 pktc |= DSI_TXPKT1C_CMD_MODE_LP; 1206 if (is_long) 1207 pktc |= DSI_TXPKT1C_CMD_TYPE_LONG; 1208 1209 /* Send one copy of the packet. Larger repeats are used for pixel 1210 * data in command mode. 1211 */ 1212 pktc |= VC4_SET_FIELD(1, DSI_TXPKT1C_CMD_REPEAT); 1213 1214 pktc |= DSI_TXPKT1C_CMD_EN; 1215 if (pix_fifo_len) { 1216 pktc |= VC4_SET_FIELD(DSI_TXPKT1C_DISPLAY_NO_SECONDARY, 1217 DSI_TXPKT1C_DISPLAY_NO); 1218 } else { 1219 pktc |= VC4_SET_FIELD(DSI_TXPKT1C_DISPLAY_NO_SHORT, 1220 DSI_TXPKT1C_DISPLAY_NO); 1221 } 1222 1223 /* Enable the appropriate interrupt for the transfer completion. */ 1224 dsi->xfer_result = 0; 1225 reinit_completion(&dsi->xfer_completion); 1226 DSI_PORT_WRITE(INT_STAT, DSI1_INT_TXPKT1_DONE | DSI1_INT_PHY_DIR_RTF); 1227 if (msg->rx_len) { 1228 DSI_PORT_WRITE(INT_EN, (DSI1_INTERRUPTS_ALWAYS_ENABLED | 1229 DSI1_INT_PHY_DIR_RTF)); 1230 } else { 1231 DSI_PORT_WRITE(INT_EN, (DSI1_INTERRUPTS_ALWAYS_ENABLED | 1232 DSI1_INT_TXPKT1_DONE)); 1233 } 1234 1235 /* Send the packet. */ 1236 DSI_PORT_WRITE(TXPKT1H, pkth); 1237 DSI_PORT_WRITE(TXPKT1C, pktc); 1238 1239 if (!wait_for_completion_timeout(&dsi->xfer_completion, 1240 msecs_to_jiffies(1000))) { 1241 dev_err(&dsi->pdev->dev, "transfer interrupt wait timeout"); 1242 dev_err(&dsi->pdev->dev, "instat: 0x%08x\n", 1243 DSI_PORT_READ(INT_STAT)); 1244 ret = -ETIMEDOUT; 1245 } else { 1246 ret = dsi->xfer_result; 1247 } 1248 1249 DSI_PORT_WRITE(INT_EN, DSI1_INTERRUPTS_ALWAYS_ENABLED); 1250 1251 if (ret) 1252 goto reset_fifo_and_return; 1253 1254 if (ret == 0 && msg->rx_len) { 1255 u32 rxpkt1h = DSI_PORT_READ(RXPKT1H); 1256 u8 *msg_rx = msg->rx_buf; 1257 1258 if (rxpkt1h & DSI_RXPKT1H_PKT_TYPE_LONG) { 1259 u32 rxlen = VC4_GET_FIELD(rxpkt1h, 1260 DSI_RXPKT1H_BC_PARAM); 1261 1262 if (rxlen != msg->rx_len) { 1263 DRM_ERROR("DSI returned %db, expecting %db\n", 1264 rxlen, (int)msg->rx_len); 1265 ret = -ENXIO; 1266 goto reset_fifo_and_return; 1267 } 1268 1269 for (i = 0; i < msg->rx_len; i++) 1270 msg_rx[i] = DSI_READ(DSI1_RXPKT_FIFO); 1271 } else { 1272 /* FINISHME: Handle AWER */ 1273 1274 msg_rx[0] = VC4_GET_FIELD(rxpkt1h, 1275 DSI_RXPKT1H_SHORT_0); 1276 if (msg->rx_len > 1) { 1277 msg_rx[1] = VC4_GET_FIELD(rxpkt1h, 1278 DSI_RXPKT1H_SHORT_1); 1279 } 1280 } 1281 } 1282 1283 return ret; 1284 1285 reset_fifo_and_return: 1286 DRM_ERROR("DSI transfer failed, resetting: %d\n", ret); 1287 1288 DSI_PORT_WRITE(TXPKT1C, DSI_PORT_READ(TXPKT1C) & ~DSI_TXPKT1C_CMD_EN); 1289 udelay(1); 1290 DSI_PORT_WRITE(CTRL, 1291 DSI_PORT_READ(CTRL) | 1292 DSI_PORT_BIT(CTRL_RESET_FIFOS)); 1293 1294 DSI_PORT_WRITE(TXPKT1C, 0); 1295 DSI_PORT_WRITE(INT_EN, DSI1_INTERRUPTS_ALWAYS_ENABLED); 1296 return ret; 1297 } 1298 1299 static int vc4_dsi_host_attach(struct mipi_dsi_host *host, 1300 struct mipi_dsi_device *device) 1301 { 1302 struct vc4_dsi *dsi = host_to_dsi(host); 1303 1304 dsi->lanes = device->lanes; 1305 dsi->channel = device->channel; 1306 dsi->mode_flags = device->mode_flags; 1307 1308 switch (device->format) { 1309 case MIPI_DSI_FMT_RGB888: 1310 dsi->format = DSI_PFORMAT_RGB888; 1311 dsi->divider = 24 / dsi->lanes; 1312 break; 1313 case MIPI_DSI_FMT_RGB666: 1314 dsi->format = DSI_PFORMAT_RGB666; 1315 dsi->divider = 24 / dsi->lanes; 1316 break; 1317 case MIPI_DSI_FMT_RGB666_PACKED: 1318 dsi->format = DSI_PFORMAT_RGB666_PACKED; 1319 dsi->divider = 18 / dsi->lanes; 1320 break; 1321 case MIPI_DSI_FMT_RGB565: 1322 dsi->format = DSI_PFORMAT_RGB565; 1323 dsi->divider = 16 / dsi->lanes; 1324 break; 1325 default: 1326 dev_err(&dsi->pdev->dev, "Unknown DSI format: %d.\n", 1327 dsi->format); 1328 return 0; 1329 } 1330 1331 if (!(dsi->mode_flags & MIPI_DSI_MODE_VIDEO)) { 1332 dev_err(&dsi->pdev->dev, 1333 "Only VIDEO mode panels supported currently.\n"); 1334 return 0; 1335 } 1336 1337 return 0; 1338 } 1339 1340 static int vc4_dsi_host_detach(struct mipi_dsi_host *host, 1341 struct mipi_dsi_device *device) 1342 { 1343 return 0; 1344 } 1345 1346 static const struct mipi_dsi_host_ops vc4_dsi_host_ops = { 1347 .attach = vc4_dsi_host_attach, 1348 .detach = vc4_dsi_host_detach, 1349 .transfer = vc4_dsi_host_transfer, 1350 }; 1351 1352 static const struct drm_encoder_helper_funcs vc4_dsi_encoder_helper_funcs = { 1353 .disable = vc4_dsi_encoder_disable, 1354 .enable = vc4_dsi_encoder_enable, 1355 .mode_fixup = vc4_dsi_encoder_mode_fixup, 1356 }; 1357 1358 static const struct of_device_id vc4_dsi_dt_match[] = { 1359 { .compatible = "brcm,bcm2835-dsi1", (void *)(uintptr_t)1 }, 1360 {} 1361 }; 1362 1363 static void dsi_handle_error(struct vc4_dsi *dsi, 1364 irqreturn_t *ret, u32 stat, u32 bit, 1365 const char *type) 1366 { 1367 if (!(stat & bit)) 1368 return; 1369 1370 DRM_ERROR("DSI%d: %s error\n", dsi->port, type); 1371 *ret = IRQ_HANDLED; 1372 } 1373 1374 /* 1375 * Initial handler for port 1 where we need the reg_dma workaround. 1376 * The register DMA writes sleep, so we can't do it in the top half. 1377 * Instead we use IRQF_ONESHOT so that the IRQ gets disabled in the 1378 * parent interrupt contrller until our interrupt thread is done. 1379 */ 1380 static irqreturn_t vc4_dsi_irq_defer_to_thread_handler(int irq, void *data) 1381 { 1382 struct vc4_dsi *dsi = data; 1383 u32 stat = DSI_PORT_READ(INT_STAT); 1384 1385 if (!stat) 1386 return IRQ_NONE; 1387 1388 return IRQ_WAKE_THREAD; 1389 } 1390 1391 /* 1392 * Normal IRQ handler for port 0, or the threaded IRQ handler for port 1393 * 1 where we need the reg_dma workaround. 1394 */ 1395 static irqreturn_t vc4_dsi_irq_handler(int irq, void *data) 1396 { 1397 struct vc4_dsi *dsi = data; 1398 u32 stat = DSI_PORT_READ(INT_STAT); 1399 irqreturn_t ret = IRQ_NONE; 1400 1401 DSI_PORT_WRITE(INT_STAT, stat); 1402 1403 dsi_handle_error(dsi, &ret, stat, 1404 DSI1_INT_ERR_SYNC_ESC, "LPDT sync"); 1405 dsi_handle_error(dsi, &ret, stat, 1406 DSI1_INT_ERR_CONTROL, "data lane 0 sequence"); 1407 dsi_handle_error(dsi, &ret, stat, 1408 DSI1_INT_ERR_CONT_LP0, "LP0 contention"); 1409 dsi_handle_error(dsi, &ret, stat, 1410 DSI1_INT_ERR_CONT_LP1, "LP1 contention"); 1411 dsi_handle_error(dsi, &ret, stat, 1412 DSI1_INT_HSTX_TO, "HSTX timeout"); 1413 dsi_handle_error(dsi, &ret, stat, 1414 DSI1_INT_LPRX_TO, "LPRX timeout"); 1415 dsi_handle_error(dsi, &ret, stat, 1416 DSI1_INT_TA_TO, "turnaround timeout"); 1417 dsi_handle_error(dsi, &ret, stat, 1418 DSI1_INT_PR_TO, "peripheral reset timeout"); 1419 1420 if (stat & (DSI1_INT_TXPKT1_DONE | DSI1_INT_PHY_DIR_RTF)) { 1421 complete(&dsi->xfer_completion); 1422 ret = IRQ_HANDLED; 1423 } else if (stat & DSI1_INT_HSTX_TO) { 1424 complete(&dsi->xfer_completion); 1425 dsi->xfer_result = -ETIMEDOUT; 1426 ret = IRQ_HANDLED; 1427 } 1428 1429 return ret; 1430 } 1431 1432 /** 1433 * vc4_dsi_init_phy_clocks - Exposes clocks generated by the analog 1434 * PHY that are consumed by CPRMAN (clk-bcm2835.c). 1435 * @dsi: DSI encoder 1436 */ 1437 static int 1438 vc4_dsi_init_phy_clocks(struct vc4_dsi *dsi) 1439 { 1440 struct device *dev = &dsi->pdev->dev; 1441 const char *parent_name = __clk_get_name(dsi->pll_phy_clock); 1442 static const struct { 1443 const char *dsi0_name, *dsi1_name; 1444 int div; 1445 } phy_clocks[] = { 1446 { "dsi0_byte", "dsi1_byte", 8 }, 1447 { "dsi0_ddr2", "dsi1_ddr2", 4 }, 1448 { "dsi0_ddr", "dsi1_ddr", 2 }, 1449 }; 1450 int i; 1451 1452 dsi->clk_onecell = devm_kzalloc(dev, 1453 sizeof(*dsi->clk_onecell) + 1454 ARRAY_SIZE(phy_clocks) * 1455 sizeof(struct clk_hw *), 1456 GFP_KERNEL); 1457 if (!dsi->clk_onecell) 1458 return -ENOMEM; 1459 dsi->clk_onecell->num = ARRAY_SIZE(phy_clocks); 1460 1461 for (i = 0; i < ARRAY_SIZE(phy_clocks); i++) { 1462 struct clk_fixed_factor *fix = &dsi->phy_clocks[i]; 1463 struct clk_init_data init; 1464 int ret; 1465 1466 /* We just use core fixed factor clock ops for the PHY 1467 * clocks. The clocks are actually gated by the 1468 * PHY_AFEC0_DDRCLK_EN bits, which we should be 1469 * setting if we use the DDR/DDR2 clocks. However, 1470 * vc4_dsi_encoder_enable() is setting up both AFEC0, 1471 * setting both our parent DSI PLL's rate and this 1472 * clock's rate, so it knows if DDR/DDR2 are going to 1473 * be used and could enable the gates itself. 1474 */ 1475 fix->mult = 1; 1476 fix->div = phy_clocks[i].div; 1477 fix->hw.init = &init; 1478 1479 memset(&init, 0, sizeof(init)); 1480 init.parent_names = &parent_name; 1481 init.num_parents = 1; 1482 if (dsi->port == 1) 1483 init.name = phy_clocks[i].dsi1_name; 1484 else 1485 init.name = phy_clocks[i].dsi0_name; 1486 init.ops = &clk_fixed_factor_ops; 1487 1488 ret = devm_clk_hw_register(dev, &fix->hw); 1489 if (ret) 1490 return ret; 1491 1492 dsi->clk_onecell->hws[i] = &fix->hw; 1493 } 1494 1495 return of_clk_add_hw_provider(dev->of_node, 1496 of_clk_hw_onecell_get, 1497 dsi->clk_onecell); 1498 } 1499 1500 static int vc4_dsi_bind(struct device *dev, struct device *master, void *data) 1501 { 1502 struct platform_device *pdev = to_platform_device(dev); 1503 struct drm_device *drm = dev_get_drvdata(master); 1504 struct vc4_dev *vc4 = to_vc4_dev(drm); 1505 struct vc4_dsi *dsi = dev_get_drvdata(dev); 1506 struct vc4_dsi_encoder *vc4_dsi_encoder; 1507 struct drm_panel *panel; 1508 const struct of_device_id *match; 1509 dma_cap_mask_t dma_mask; 1510 int ret; 1511 1512 match = of_match_device(vc4_dsi_dt_match, dev); 1513 if (!match) 1514 return -ENODEV; 1515 1516 dsi->port = (uintptr_t)match->data; 1517 1518 vc4_dsi_encoder = devm_kzalloc(dev, sizeof(*vc4_dsi_encoder), 1519 GFP_KERNEL); 1520 if (!vc4_dsi_encoder) 1521 return -ENOMEM; 1522 vc4_dsi_encoder->base.type = VC4_ENCODER_TYPE_DSI1; 1523 vc4_dsi_encoder->dsi = dsi; 1524 dsi->encoder = &vc4_dsi_encoder->base.base; 1525 1526 dsi->regs = vc4_ioremap_regs(pdev, 0); 1527 if (IS_ERR(dsi->regs)) 1528 return PTR_ERR(dsi->regs); 1529 1530 if (DSI_PORT_READ(ID) != DSI_ID_VALUE) { 1531 dev_err(dev, "Port returned 0x%08x for ID instead of 0x%08x\n", 1532 DSI_PORT_READ(ID), DSI_ID_VALUE); 1533 return -ENODEV; 1534 } 1535 1536 /* DSI1 has a broken AXI slave that doesn't respond to writes 1537 * from the ARM. It does handle writes from the DMA engine, 1538 * so set up a channel for talking to it. 1539 */ 1540 if (dsi->port == 1) { 1541 dsi->reg_dma_mem = dma_alloc_coherent(dev, 4, 1542 &dsi->reg_dma_paddr, 1543 GFP_KERNEL); 1544 if (!dsi->reg_dma_mem) { 1545 DRM_ERROR("Failed to get DMA memory\n"); 1546 return -ENOMEM; 1547 } 1548 1549 dma_cap_zero(dma_mask); 1550 dma_cap_set(DMA_MEMCPY, dma_mask); 1551 dsi->reg_dma_chan = dma_request_chan_by_mask(&dma_mask); 1552 if (IS_ERR(dsi->reg_dma_chan)) { 1553 ret = PTR_ERR(dsi->reg_dma_chan); 1554 if (ret != -EPROBE_DEFER) 1555 DRM_ERROR("Failed to get DMA channel: %d\n", 1556 ret); 1557 return ret; 1558 } 1559 1560 /* Get the physical address of the device's registers. The 1561 * struct resource for the regs gives us the bus address 1562 * instead. 1563 */ 1564 dsi->reg_paddr = be32_to_cpup(of_get_address(dev->of_node, 1565 0, NULL, NULL)); 1566 } 1567 1568 init_completion(&dsi->xfer_completion); 1569 /* At startup enable error-reporting interrupts and nothing else. */ 1570 DSI_PORT_WRITE(INT_EN, DSI1_INTERRUPTS_ALWAYS_ENABLED); 1571 /* Clear any existing interrupt state. */ 1572 DSI_PORT_WRITE(INT_STAT, DSI_PORT_READ(INT_STAT)); 1573 1574 if (dsi->reg_dma_mem) 1575 ret = devm_request_threaded_irq(dev, platform_get_irq(pdev, 0), 1576 vc4_dsi_irq_defer_to_thread_handler, 1577 vc4_dsi_irq_handler, 1578 IRQF_ONESHOT, 1579 "vc4 dsi", dsi); 1580 else 1581 ret = devm_request_irq(dev, platform_get_irq(pdev, 0), 1582 vc4_dsi_irq_handler, 0, "vc4 dsi", dsi); 1583 if (ret) { 1584 if (ret != -EPROBE_DEFER) 1585 dev_err(dev, "Failed to get interrupt: %d\n", ret); 1586 return ret; 1587 } 1588 1589 dsi->escape_clock = devm_clk_get(dev, "escape"); 1590 if (IS_ERR(dsi->escape_clock)) { 1591 ret = PTR_ERR(dsi->escape_clock); 1592 if (ret != -EPROBE_DEFER) 1593 dev_err(dev, "Failed to get escape clock: %d\n", ret); 1594 return ret; 1595 } 1596 1597 dsi->pll_phy_clock = devm_clk_get(dev, "phy"); 1598 if (IS_ERR(dsi->pll_phy_clock)) { 1599 ret = PTR_ERR(dsi->pll_phy_clock); 1600 if (ret != -EPROBE_DEFER) 1601 dev_err(dev, "Failed to get phy clock: %d\n", ret); 1602 return ret; 1603 } 1604 1605 dsi->pixel_clock = devm_clk_get(dev, "pixel"); 1606 if (IS_ERR(dsi->pixel_clock)) { 1607 ret = PTR_ERR(dsi->pixel_clock); 1608 if (ret != -EPROBE_DEFER) 1609 dev_err(dev, "Failed to get pixel clock: %d\n", ret); 1610 return ret; 1611 } 1612 1613 ret = drm_of_find_panel_or_bridge(dev->of_node, 0, 0, 1614 &panel, &dsi->bridge); 1615 if (ret) { 1616 /* If the bridge or panel pointed by dev->of_node is not 1617 * enabled, just return 0 here so that we don't prevent the DRM 1618 * dev from being registered. Of course that means the DSI 1619 * encoder won't be exposed, but that's not a problem since 1620 * nothing is connected to it. 1621 */ 1622 if (ret == -ENODEV) 1623 return 0; 1624 1625 return ret; 1626 } 1627 1628 if (panel) { 1629 dsi->bridge = devm_drm_panel_bridge_add(dev, panel, 1630 DRM_MODE_CONNECTOR_DSI); 1631 if (IS_ERR(dsi->bridge)) 1632 return PTR_ERR(dsi->bridge); 1633 } 1634 1635 /* The esc clock rate is supposed to always be 100Mhz. */ 1636 ret = clk_set_rate(dsi->escape_clock, 100 * 1000000); 1637 if (ret) { 1638 dev_err(dev, "Failed to set esc clock: %d\n", ret); 1639 return ret; 1640 } 1641 1642 ret = vc4_dsi_init_phy_clocks(dsi); 1643 if (ret) 1644 return ret; 1645 1646 if (dsi->port == 1) 1647 vc4->dsi1 = dsi; 1648 1649 drm_encoder_init(drm, dsi->encoder, &vc4_dsi_encoder_funcs, 1650 DRM_MODE_ENCODER_DSI, NULL); 1651 drm_encoder_helper_add(dsi->encoder, &vc4_dsi_encoder_helper_funcs); 1652 1653 ret = drm_bridge_attach(dsi->encoder, dsi->bridge, NULL); 1654 if (ret) { 1655 dev_err(dev, "bridge attach failed: %d\n", ret); 1656 return ret; 1657 } 1658 /* Disable the atomic helper calls into the bridge. We 1659 * manually call the bridge pre_enable / enable / etc. calls 1660 * from our driver, since we need to sequence them within the 1661 * encoder's enable/disable paths. 1662 */ 1663 dsi->encoder->bridge = NULL; 1664 1665 pm_runtime_enable(dev); 1666 1667 return 0; 1668 } 1669 1670 static void vc4_dsi_unbind(struct device *dev, struct device *master, 1671 void *data) 1672 { 1673 struct drm_device *drm = dev_get_drvdata(master); 1674 struct vc4_dev *vc4 = to_vc4_dev(drm); 1675 struct vc4_dsi *dsi = dev_get_drvdata(dev); 1676 1677 if (dsi->bridge) 1678 pm_runtime_disable(dev); 1679 1680 vc4_dsi_encoder_destroy(dsi->encoder); 1681 1682 if (dsi->port == 1) 1683 vc4->dsi1 = NULL; 1684 } 1685 1686 static const struct component_ops vc4_dsi_ops = { 1687 .bind = vc4_dsi_bind, 1688 .unbind = vc4_dsi_unbind, 1689 }; 1690 1691 static int vc4_dsi_dev_probe(struct platform_device *pdev) 1692 { 1693 struct device *dev = &pdev->dev; 1694 struct vc4_dsi *dsi; 1695 int ret; 1696 1697 dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL); 1698 if (!dsi) 1699 return -ENOMEM; 1700 dev_set_drvdata(dev, dsi); 1701 1702 dsi->pdev = pdev; 1703 1704 /* Note, the initialization sequence for DSI and panels is 1705 * tricky. The component bind above won't get past its 1706 * -EPROBE_DEFER until the panel/bridge probes. The 1707 * panel/bridge will return -EPROBE_DEFER until it has a 1708 * mipi_dsi_host to register its device to. So, we register 1709 * the host during pdev probe time, so vc4 as a whole can then 1710 * -EPROBE_DEFER its component bind process until the panel 1711 * successfully attaches. 1712 */ 1713 dsi->dsi_host.ops = &vc4_dsi_host_ops; 1714 dsi->dsi_host.dev = dev; 1715 mipi_dsi_host_register(&dsi->dsi_host); 1716 1717 ret = component_add(&pdev->dev, &vc4_dsi_ops); 1718 if (ret) { 1719 mipi_dsi_host_unregister(&dsi->dsi_host); 1720 return ret; 1721 } 1722 1723 return 0; 1724 } 1725 1726 static int vc4_dsi_dev_remove(struct platform_device *pdev) 1727 { 1728 struct device *dev = &pdev->dev; 1729 struct vc4_dsi *dsi = dev_get_drvdata(dev); 1730 1731 component_del(&pdev->dev, &vc4_dsi_ops); 1732 mipi_dsi_host_unregister(&dsi->dsi_host); 1733 1734 return 0; 1735 } 1736 1737 struct platform_driver vc4_dsi_driver = { 1738 .probe = vc4_dsi_dev_probe, 1739 .remove = vc4_dsi_dev_remove, 1740 .driver = { 1741 .name = "vc4_dsi", 1742 .of_match_table = vc4_dsi_dt_match, 1743 }, 1744 }; 1745