0001
0002
0003
0004
0005
0006 #include <linux/component.h>
0007 #include <linux/module.h>
0008 #include <linux/of_address.h>
0009 #include <linux/of_device.h>
0010 #include <linux/of_irq.h>
0011 #include <linux/of_graph.h>
0012 #include <video/mipi_display.h>
0013
0014 #include <drm/drm_atomic_helper.h>
0015 #include <drm/drm_bridge.h>
0016 #include <drm/drm_crtc_helper.h>
0017 #include <drm/drm_of.h>
0018 #include <drm/drm_probe_helper.h>
0019
0020 #include "sprd_drm.h"
0021 #include "sprd_dpu.h"
0022 #include "sprd_dsi.h"
0023
0024 #define SOFT_RESET 0x04
0025 #define MASK_PROTOCOL_INT 0x0C
0026 #define MASK_INTERNAL_INT 0x14
0027 #define DSI_MODE_CFG 0x18
0028
0029 #define VIRTUAL_CHANNEL_ID 0x1C
0030 #define GEN_RX_VCID GENMASK(1, 0)
0031 #define VIDEO_PKT_VCID GENMASK(3, 2)
0032
0033 #define DPI_VIDEO_FORMAT 0x20
0034 #define DPI_VIDEO_MODE_FORMAT GENMASK(5, 0)
0035 #define LOOSELY18_EN BIT(6)
0036
0037 #define VIDEO_PKT_CONFIG 0x24
0038 #define VIDEO_PKT_SIZE GENMASK(15, 0)
0039 #define VIDEO_LINE_CHUNK_NUM GENMASK(31, 16)
0040
0041 #define VIDEO_LINE_HBLK_TIME 0x28
0042 #define VIDEO_LINE_HBP_TIME GENMASK(15, 0)
0043 #define VIDEO_LINE_HSA_TIME GENMASK(31, 16)
0044
0045 #define VIDEO_LINE_TIME 0x2C
0046
0047 #define VIDEO_VBLK_LINES 0x30
0048 #define VFP_LINES GENMASK(9, 0)
0049 #define VBP_LINES GENMASK(19, 10)
0050 #define VSA_LINES GENMASK(29, 20)
0051
0052 #define VIDEO_VACTIVE_LINES 0x34
0053
0054 #define VID_MODE_CFG 0x38
0055 #define VID_MODE_TYPE GENMASK(1, 0)
0056 #define LP_VSA_EN BIT(8)
0057 #define LP_VBP_EN BIT(9)
0058 #define LP_VFP_EN BIT(10)
0059 #define LP_VACT_EN BIT(11)
0060 #define LP_HBP_EN BIT(12)
0061 #define LP_HFP_EN BIT(13)
0062 #define FRAME_BTA_ACK_EN BIT(14)
0063
0064 #define TIMEOUT_CNT_CLK_CONFIG 0x40
0065 #define HTX_TO_CONFIG 0x44
0066 #define LRX_H_TO_CONFIG 0x48
0067
0068 #define TX_ESC_CLK_CONFIG 0x5C
0069
0070 #define CMD_MODE_CFG 0x68
0071 #define TEAR_FX_EN BIT(0)
0072
0073 #define GEN_HDR 0x6C
0074 #define GEN_DT GENMASK(5, 0)
0075 #define GEN_VC GENMASK(7, 6)
0076
0077 #define GEN_PLD_DATA 0x70
0078
0079 #define PHY_CLK_LANE_LP_CTRL 0x74
0080 #define PHY_CLKLANE_TX_REQ_HS BIT(0)
0081 #define AUTO_CLKLANE_CTRL_EN BIT(1)
0082
0083 #define PHY_INTERFACE_CTRL 0x78
0084 #define RF_PHY_SHUTDOWN BIT(0)
0085 #define RF_PHY_RESET_N BIT(1)
0086 #define RF_PHY_CLK_EN BIT(2)
0087
0088 #define CMD_MODE_STATUS 0x98
0089 #define GEN_CMD_RDATA_FIFO_EMPTY BIT(1)
0090 #define GEN_CMD_WDATA_FIFO_EMPTY BIT(3)
0091 #define GEN_CMD_CMD_FIFO_EMPTY BIT(5)
0092 #define GEN_CMD_RDCMD_DONE BIT(7)
0093
0094 #define PHY_STATUS 0x9C
0095 #define PHY_LOCK BIT(1)
0096
0097 #define PHY_MIN_STOP_TIME 0xA0
0098 #define PHY_LANE_NUM_CONFIG 0xA4
0099
0100 #define PHY_CLKLANE_TIME_CONFIG 0xA8
0101 #define PHY_CLKLANE_LP_TO_HS_TIME GENMASK(15, 0)
0102 #define PHY_CLKLANE_HS_TO_LP_TIME GENMASK(31, 16)
0103
0104 #define PHY_DATALANE_TIME_CONFIG 0xAC
0105 #define PHY_DATALANE_LP_TO_HS_TIME GENMASK(15, 0)
0106 #define PHY_DATALANE_HS_TO_LP_TIME GENMASK(31, 16)
0107
0108 #define MAX_READ_TIME 0xB0
0109
0110 #define RX_PKT_CHECK_CONFIG 0xB4
0111 #define RX_PKT_ECC_EN BIT(0)
0112 #define RX_PKT_CRC_EN BIT(1)
0113
0114 #define TA_EN 0xB8
0115
0116 #define EOTP_EN 0xBC
0117 #define TX_EOTP_EN BIT(0)
0118 #define RX_EOTP_EN BIT(1)
0119
0120 #define VIDEO_NULLPKT_SIZE 0xC0
0121 #define DCS_WM_PKT_SIZE 0xC4
0122
0123 #define VIDEO_SIG_DELAY_CONFIG 0xD0
0124 #define VIDEO_SIG_DELAY GENMASK(23, 0)
0125
0126 #define PHY_TST_CTRL0 0xF0
0127 #define PHY_TESTCLR BIT(0)
0128 #define PHY_TESTCLK BIT(1)
0129
0130 #define PHY_TST_CTRL1 0xF4
0131 #define PHY_TESTDIN GENMASK(7, 0)
0132 #define PHY_TESTDOUT GENMASK(15, 8)
0133 #define PHY_TESTEN BIT(16)
0134
0135 #define host_to_dsi(host) \
0136 container_of(host, struct sprd_dsi, host)
0137
0138 static inline u32
0139 dsi_reg_rd(struct dsi_context *ctx, u32 offset, u32 mask,
0140 u32 shift)
0141 {
0142 return (readl(ctx->base + offset) & mask) >> shift;
0143 }
0144
0145 static inline void
0146 dsi_reg_wr(struct dsi_context *ctx, u32 offset, u32 mask,
0147 u32 shift, u32 val)
0148 {
0149 u32 ret;
0150
0151 ret = readl(ctx->base + offset);
0152 ret &= ~mask;
0153 ret |= (val << shift) & mask;
0154 writel(ret, ctx->base + offset);
0155 }
0156
0157 static inline void
0158 dsi_reg_up(struct dsi_context *ctx, u32 offset, u32 mask,
0159 u32 val)
0160 {
0161 u32 ret = readl(ctx->base + offset);
0162
0163 writel((ret & ~mask) | (val & mask), ctx->base + offset);
0164 }
0165
0166 static int regmap_tst_io_write(void *context, u32 reg, u32 val)
0167 {
0168 struct sprd_dsi *dsi = context;
0169 struct dsi_context *ctx = &dsi->ctx;
0170
0171 if (val > 0xff || reg > 0xff)
0172 return -EINVAL;
0173
0174 drm_dbg(dsi->drm, "reg = 0x%02x, val = 0x%02x\n", reg, val);
0175
0176 dsi_reg_up(ctx, PHY_TST_CTRL1, PHY_TESTEN, PHY_TESTEN);
0177 dsi_reg_wr(ctx, PHY_TST_CTRL1, PHY_TESTDIN, 0, reg);
0178 dsi_reg_up(ctx, PHY_TST_CTRL0, PHY_TESTCLK, PHY_TESTCLK);
0179 dsi_reg_up(ctx, PHY_TST_CTRL0, PHY_TESTCLK, 0);
0180 dsi_reg_up(ctx, PHY_TST_CTRL1, PHY_TESTEN, 0);
0181 dsi_reg_wr(ctx, PHY_TST_CTRL1, PHY_TESTDIN, 0, val);
0182 dsi_reg_up(ctx, PHY_TST_CTRL0, PHY_TESTCLK, PHY_TESTCLK);
0183 dsi_reg_up(ctx, PHY_TST_CTRL0, PHY_TESTCLK, 0);
0184
0185 return 0;
0186 }
0187
0188 static int regmap_tst_io_read(void *context, u32 reg, u32 *val)
0189 {
0190 struct sprd_dsi *dsi = context;
0191 struct dsi_context *ctx = &dsi->ctx;
0192 int ret;
0193
0194 if (reg > 0xff)
0195 return -EINVAL;
0196
0197 dsi_reg_up(ctx, PHY_TST_CTRL1, PHY_TESTEN, PHY_TESTEN);
0198 dsi_reg_wr(ctx, PHY_TST_CTRL1, PHY_TESTDIN, 0, reg);
0199 dsi_reg_up(ctx, PHY_TST_CTRL0, PHY_TESTCLK, PHY_TESTCLK);
0200 dsi_reg_up(ctx, PHY_TST_CTRL0, PHY_TESTCLK, 0);
0201 dsi_reg_up(ctx, PHY_TST_CTRL1, PHY_TESTEN, 0);
0202
0203 udelay(1);
0204
0205 ret = dsi_reg_rd(ctx, PHY_TST_CTRL1, PHY_TESTDOUT, 8);
0206 if (ret < 0)
0207 return ret;
0208
0209 *val = ret;
0210
0211 drm_dbg(dsi->drm, "reg = 0x%02x, val = 0x%02x\n", reg, *val);
0212 return 0;
0213 }
0214
0215 static struct regmap_bus regmap_tst_io = {
0216 .reg_write = regmap_tst_io_write,
0217 .reg_read = regmap_tst_io_read,
0218 };
0219
0220 static const struct regmap_config byte_config = {
0221 .reg_bits = 8,
0222 .val_bits = 8,
0223 };
0224
0225 static int dphy_wait_pll_locked(struct dsi_context *ctx)
0226 {
0227 struct sprd_dsi *dsi = container_of(ctx, struct sprd_dsi, ctx);
0228 int i;
0229
0230 for (i = 0; i < 50000; i++) {
0231 if (dsi_reg_rd(ctx, PHY_STATUS, PHY_LOCK, 1))
0232 return 0;
0233 udelay(3);
0234 }
0235
0236 drm_err(dsi->drm, "dphy pll can not be locked\n");
0237 return -ETIMEDOUT;
0238 }
0239
0240 static int dsi_wait_tx_payload_fifo_empty(struct dsi_context *ctx)
0241 {
0242 int i;
0243
0244 for (i = 0; i < 5000; i++) {
0245 if (dsi_reg_rd(ctx, CMD_MODE_STATUS, GEN_CMD_WDATA_FIFO_EMPTY, 3))
0246 return 0;
0247 udelay(1);
0248 }
0249
0250 return -ETIMEDOUT;
0251 }
0252
0253 static int dsi_wait_tx_cmd_fifo_empty(struct dsi_context *ctx)
0254 {
0255 int i;
0256
0257 for (i = 0; i < 5000; i++) {
0258 if (dsi_reg_rd(ctx, CMD_MODE_STATUS, GEN_CMD_CMD_FIFO_EMPTY, 5))
0259 return 0;
0260 udelay(1);
0261 }
0262
0263 return -ETIMEDOUT;
0264 }
0265
0266 static int dsi_wait_rd_resp_completed(struct dsi_context *ctx)
0267 {
0268 int i;
0269
0270 for (i = 0; i < 10000; i++) {
0271 if (dsi_reg_rd(ctx, CMD_MODE_STATUS, GEN_CMD_RDCMD_DONE, 7))
0272 return 0;
0273 udelay(10);
0274 }
0275
0276 return -ETIMEDOUT;
0277 }
0278
0279 static u16 calc_bytes_per_pixel_x100(int coding)
0280 {
0281 u16 bpp_x100;
0282
0283 switch (coding) {
0284 case COLOR_CODE_16BIT_CONFIG1:
0285 case COLOR_CODE_16BIT_CONFIG2:
0286 case COLOR_CODE_16BIT_CONFIG3:
0287 bpp_x100 = 200;
0288 break;
0289 case COLOR_CODE_18BIT_CONFIG1:
0290 case COLOR_CODE_18BIT_CONFIG2:
0291 bpp_x100 = 225;
0292 break;
0293 case COLOR_CODE_24BIT:
0294 bpp_x100 = 300;
0295 break;
0296 case COLOR_CODE_COMPRESSTION:
0297 bpp_x100 = 100;
0298 break;
0299 case COLOR_CODE_20BIT_YCC422_LOOSELY:
0300 bpp_x100 = 250;
0301 break;
0302 case COLOR_CODE_24BIT_YCC422:
0303 bpp_x100 = 300;
0304 break;
0305 case COLOR_CODE_16BIT_YCC422:
0306 bpp_x100 = 200;
0307 break;
0308 case COLOR_CODE_30BIT:
0309 bpp_x100 = 375;
0310 break;
0311 case COLOR_CODE_36BIT:
0312 bpp_x100 = 450;
0313 break;
0314 case COLOR_CODE_12BIT_YCC420:
0315 bpp_x100 = 150;
0316 break;
0317 default:
0318 DRM_ERROR("invalid color coding");
0319 bpp_x100 = 0;
0320 break;
0321 }
0322
0323 return bpp_x100;
0324 }
0325
0326 static u8 calc_video_size_step(int coding)
0327 {
0328 u8 video_size_step;
0329
0330 switch (coding) {
0331 case COLOR_CODE_16BIT_CONFIG1:
0332 case COLOR_CODE_16BIT_CONFIG2:
0333 case COLOR_CODE_16BIT_CONFIG3:
0334 case COLOR_CODE_18BIT_CONFIG1:
0335 case COLOR_CODE_18BIT_CONFIG2:
0336 case COLOR_CODE_24BIT:
0337 case COLOR_CODE_COMPRESSTION:
0338 return video_size_step = 1;
0339 case COLOR_CODE_20BIT_YCC422_LOOSELY:
0340 case COLOR_CODE_24BIT_YCC422:
0341 case COLOR_CODE_16BIT_YCC422:
0342 case COLOR_CODE_30BIT:
0343 case COLOR_CODE_36BIT:
0344 case COLOR_CODE_12BIT_YCC420:
0345 return video_size_step = 2;
0346 default:
0347 DRM_ERROR("invalid color coding");
0348 return 0;
0349 }
0350 }
0351
0352 static u16 round_video_size(int coding, u16 video_size)
0353 {
0354 switch (coding) {
0355 case COLOR_CODE_16BIT_YCC422:
0356 case COLOR_CODE_24BIT_YCC422:
0357 case COLOR_CODE_20BIT_YCC422_LOOSELY:
0358 case COLOR_CODE_12BIT_YCC420:
0359
0360 if ((video_size % 2) != 0)
0361 video_size += 1;
0362 break;
0363 default:
0364 break;
0365 }
0366
0367 return video_size;
0368 }
0369
0370 #define SPRD_MIPI_DSI_FMT_DSC 0xff
0371 static u32 fmt_to_coding(u32 fmt)
0372 {
0373 switch (fmt) {
0374 case MIPI_DSI_FMT_RGB565:
0375 return COLOR_CODE_16BIT_CONFIG1;
0376 case MIPI_DSI_FMT_RGB666:
0377 case MIPI_DSI_FMT_RGB666_PACKED:
0378 return COLOR_CODE_18BIT_CONFIG1;
0379 case MIPI_DSI_FMT_RGB888:
0380 return COLOR_CODE_24BIT;
0381 case SPRD_MIPI_DSI_FMT_DSC:
0382 return COLOR_CODE_COMPRESSTION;
0383 default:
0384 DRM_ERROR("Unsupported format (%d)\n", fmt);
0385 return COLOR_CODE_24BIT;
0386 }
0387 }
0388
0389 #define ns_to_cycle(ns, byte_clk) \
0390 DIV_ROUND_UP((ns) * (byte_clk), 1000000)
0391
0392 static void sprd_dsi_init(struct dsi_context *ctx)
0393 {
0394 struct sprd_dsi *dsi = container_of(ctx, struct sprd_dsi, ctx);
0395 u32 byte_clk = dsi->slave->hs_rate / 8;
0396 u16 data_hs2lp, data_lp2hs, clk_hs2lp, clk_lp2hs;
0397 u16 max_rd_time;
0398 int div;
0399
0400 writel(0, ctx->base + SOFT_RESET);
0401 writel(0xffffffff, ctx->base + MASK_PROTOCOL_INT);
0402 writel(0xffffffff, ctx->base + MASK_INTERNAL_INT);
0403 writel(1, ctx->base + DSI_MODE_CFG);
0404 dsi_reg_up(ctx, EOTP_EN, RX_EOTP_EN, 0);
0405 dsi_reg_up(ctx, EOTP_EN, TX_EOTP_EN, 0);
0406 dsi_reg_up(ctx, RX_PKT_CHECK_CONFIG, RX_PKT_ECC_EN, RX_PKT_ECC_EN);
0407 dsi_reg_up(ctx, RX_PKT_CHECK_CONFIG, RX_PKT_CRC_EN, RX_PKT_CRC_EN);
0408 writel(1, ctx->base + TA_EN);
0409 dsi_reg_up(ctx, VIRTUAL_CHANNEL_ID, VIDEO_PKT_VCID, 0);
0410 dsi_reg_up(ctx, VIRTUAL_CHANNEL_ID, GEN_RX_VCID, 0);
0411
0412 div = DIV_ROUND_UP(byte_clk, dsi->slave->lp_rate);
0413 writel(div, ctx->base + TX_ESC_CLK_CONFIG);
0414
0415 max_rd_time = ns_to_cycle(ctx->max_rd_time, byte_clk);
0416 writel(max_rd_time, ctx->base + MAX_READ_TIME);
0417
0418 data_hs2lp = ns_to_cycle(ctx->data_hs2lp, byte_clk);
0419 data_lp2hs = ns_to_cycle(ctx->data_lp2hs, byte_clk);
0420 clk_hs2lp = ns_to_cycle(ctx->clk_hs2lp, byte_clk);
0421 clk_lp2hs = ns_to_cycle(ctx->clk_lp2hs, byte_clk);
0422 dsi_reg_wr(ctx, PHY_DATALANE_TIME_CONFIG,
0423 PHY_DATALANE_HS_TO_LP_TIME, 16, data_hs2lp);
0424 dsi_reg_wr(ctx, PHY_DATALANE_TIME_CONFIG,
0425 PHY_DATALANE_LP_TO_HS_TIME, 0, data_lp2hs);
0426 dsi_reg_wr(ctx, PHY_CLKLANE_TIME_CONFIG,
0427 PHY_CLKLANE_HS_TO_LP_TIME, 16, clk_hs2lp);
0428 dsi_reg_wr(ctx, PHY_CLKLANE_TIME_CONFIG,
0429 PHY_CLKLANE_LP_TO_HS_TIME, 0, clk_lp2hs);
0430
0431 writel(1, ctx->base + SOFT_RESET);
0432 }
0433
0434
0435
0436
0437 static void sprd_dsi_fini(struct dsi_context *ctx)
0438 {
0439 writel(0xffffffff, ctx->base + MASK_PROTOCOL_INT);
0440 writel(0xffffffff, ctx->base + MASK_INTERNAL_INT);
0441 writel(0, ctx->base + SOFT_RESET);
0442 }
0443
0444
0445
0446
0447
0448
0449
0450 static int sprd_dsi_dpi_video(struct dsi_context *ctx)
0451 {
0452 struct sprd_dsi *dsi = container_of(ctx, struct sprd_dsi, ctx);
0453 struct videomode *vm = &ctx->vm;
0454 u32 byte_clk = dsi->slave->hs_rate / 8;
0455 u16 bpp_x100;
0456 u16 video_size;
0457 u32 ratio_x1000;
0458 u16 null_pkt_size = 0;
0459 u8 video_size_step;
0460 u32 hs_to;
0461 u32 total_bytes;
0462 u32 bytes_per_chunk;
0463 u32 chunks = 0;
0464 u32 bytes_left = 0;
0465 u32 chunk_overhead;
0466 const u8 pkt_header = 6;
0467 u8 coding;
0468 int div;
0469 u16 hline;
0470 u16 byte_cycle;
0471
0472 coding = fmt_to_coding(dsi->slave->format);
0473 video_size = round_video_size(coding, vm->hactive);
0474 bpp_x100 = calc_bytes_per_pixel_x100(coding);
0475 video_size_step = calc_video_size_step(coding);
0476 ratio_x1000 = byte_clk * 1000 / (vm->pixelclock / 1000);
0477 hline = vm->hactive + vm->hsync_len + vm->hfront_porch +
0478 vm->hback_porch;
0479
0480 writel(0, ctx->base + SOFT_RESET);
0481 dsi_reg_wr(ctx, VID_MODE_CFG, FRAME_BTA_ACK_EN, 15, ctx->frame_ack_en);
0482 dsi_reg_wr(ctx, DPI_VIDEO_FORMAT, DPI_VIDEO_MODE_FORMAT, 0, coding);
0483 dsi_reg_wr(ctx, VID_MODE_CFG, VID_MODE_TYPE, 0, ctx->burst_mode);
0484 byte_cycle = 95 * hline * ratio_x1000 / 100000;
0485 dsi_reg_wr(ctx, VIDEO_SIG_DELAY_CONFIG, VIDEO_SIG_DELAY, 0, byte_cycle);
0486 byte_cycle = hline * ratio_x1000 / 1000;
0487 writel(byte_cycle, ctx->base + VIDEO_LINE_TIME);
0488 byte_cycle = vm->hsync_len * ratio_x1000 / 1000;
0489 dsi_reg_wr(ctx, VIDEO_LINE_HBLK_TIME, VIDEO_LINE_HSA_TIME, 16, byte_cycle);
0490 byte_cycle = vm->hback_porch * ratio_x1000 / 1000;
0491 dsi_reg_wr(ctx, VIDEO_LINE_HBLK_TIME, VIDEO_LINE_HBP_TIME, 0, byte_cycle);
0492 writel(vm->vactive, ctx->base + VIDEO_VACTIVE_LINES);
0493 dsi_reg_wr(ctx, VIDEO_VBLK_LINES, VFP_LINES, 0, vm->vfront_porch);
0494 dsi_reg_wr(ctx, VIDEO_VBLK_LINES, VBP_LINES, 10, vm->vback_porch);
0495 dsi_reg_wr(ctx, VIDEO_VBLK_LINES, VSA_LINES, 20, vm->vsync_len);
0496 dsi_reg_up(ctx, VID_MODE_CFG, LP_HBP_EN | LP_HFP_EN | LP_VACT_EN |
0497 LP_VFP_EN | LP_VBP_EN | LP_VSA_EN, LP_HBP_EN | LP_HFP_EN |
0498 LP_VACT_EN | LP_VFP_EN | LP_VBP_EN | LP_VSA_EN);
0499
0500 hs_to = (hline * vm->vactive) + (2 * bpp_x100) / 100;
0501 for (div = 0x80; (div < hs_to) && (div > 2); div--) {
0502 if ((hs_to % div) == 0) {
0503 writel(div, ctx->base + TIMEOUT_CNT_CLK_CONFIG);
0504 writel(hs_to / div, ctx->base + LRX_H_TO_CONFIG);
0505 writel(hs_to / div, ctx->base + HTX_TO_CONFIG);
0506 break;
0507 }
0508 }
0509
0510 if (ctx->burst_mode == VIDEO_BURST_WITH_SYNC_PULSES) {
0511 dsi_reg_wr(ctx, VIDEO_PKT_CONFIG, VIDEO_PKT_SIZE, 0, video_size);
0512 writel(0, ctx->base + VIDEO_NULLPKT_SIZE);
0513 dsi_reg_up(ctx, VIDEO_PKT_CONFIG, VIDEO_LINE_CHUNK_NUM, 0);
0514 } else {
0515
0516 null_pkt_size = 0;
0517
0518
0519 bytes_per_chunk = vm->hactive * bpp_x100 / 100 + pkt_header;
0520
0521
0522 total_bytes = (vm->hactive + vm->hfront_porch) *
0523 ratio_x1000 / dsi->slave->lanes / 1000;
0524
0525
0526 if (total_bytes < bytes_per_chunk) {
0527 drm_err(dsi->drm, "current resolution can not be set\n");
0528 return -EINVAL;
0529 }
0530
0531 chunk_overhead = total_bytes - bytes_per_chunk;
0532
0533
0534 if (chunk_overhead > 1) {
0535
0536 for (video_size = video_size_step;
0537 video_size < vm->hactive;
0538 video_size += video_size_step) {
0539 if (vm->hactive * 1000 / video_size % 1000)
0540 continue;
0541
0542 chunks = vm->hactive / video_size;
0543 bytes_per_chunk = bpp_x100 * video_size / 100
0544 + pkt_header;
0545 if (total_bytes >= (bytes_per_chunk * chunks)) {
0546 bytes_left = total_bytes -
0547 bytes_per_chunk * chunks;
0548 break;
0549 }
0550 }
0551
0552
0553 if (bytes_left > (pkt_header * chunks)) {
0554 null_pkt_size = (bytes_left -
0555 pkt_header * chunks) / chunks;
0556
0557 if (null_pkt_size > 1023)
0558 null_pkt_size = 1023;
0559 }
0560
0561 } else {
0562
0563 chunks = 1;
0564
0565
0566 for (video_size = vm->hactive;
0567 (video_size % video_size_step) != 0;
0568 video_size++)
0569 ;
0570 }
0571
0572 dsi_reg_wr(ctx, VIDEO_PKT_CONFIG, VIDEO_PKT_SIZE, 0, video_size);
0573 writel(null_pkt_size, ctx->base + VIDEO_NULLPKT_SIZE);
0574 dsi_reg_wr(ctx, VIDEO_PKT_CONFIG, VIDEO_LINE_CHUNK_NUM, 16, chunks);
0575 }
0576
0577 writel(ctx->int0_mask, ctx->base + MASK_PROTOCOL_INT);
0578 writel(ctx->int1_mask, ctx->base + MASK_INTERNAL_INT);
0579 writel(1, ctx->base + SOFT_RESET);
0580
0581 return 0;
0582 }
0583
0584 static void sprd_dsi_edpi_video(struct dsi_context *ctx)
0585 {
0586 struct sprd_dsi *dsi = container_of(ctx, struct sprd_dsi, ctx);
0587 const u32 fifo_depth = 1096;
0588 const u32 word_length = 4;
0589 u32 hactive = ctx->vm.hactive;
0590 u32 bpp_x100;
0591 u32 max_fifo_len;
0592 u8 coding;
0593
0594 coding = fmt_to_coding(dsi->slave->format);
0595 bpp_x100 = calc_bytes_per_pixel_x100(coding);
0596 max_fifo_len = word_length * fifo_depth * 100 / bpp_x100;
0597
0598 writel(0, ctx->base + SOFT_RESET);
0599 dsi_reg_wr(ctx, DPI_VIDEO_FORMAT, DPI_VIDEO_MODE_FORMAT, 0, coding);
0600 dsi_reg_wr(ctx, CMD_MODE_CFG, TEAR_FX_EN, 0, ctx->te_ack_en);
0601
0602 if (max_fifo_len > hactive)
0603 writel(hactive, ctx->base + DCS_WM_PKT_SIZE);
0604 else
0605 writel(max_fifo_len, ctx->base + DCS_WM_PKT_SIZE);
0606
0607 writel(ctx->int0_mask, ctx->base + MASK_PROTOCOL_INT);
0608 writel(ctx->int1_mask, ctx->base + MASK_INTERNAL_INT);
0609 writel(1, ctx->base + SOFT_RESET);
0610 }
0611
0612
0613
0614
0615
0616
0617
0618
0619
0620
0621
0622 static int sprd_dsi_wr_pkt(struct dsi_context *ctx, u8 vc, u8 type,
0623 const u8 *param, u16 len)
0624 {
0625 struct sprd_dsi *dsi = container_of(ctx, struct sprd_dsi, ctx);
0626 u8 wc_lsbyte, wc_msbyte;
0627 u32 payload;
0628 int i, j, ret;
0629
0630 if (vc > 3)
0631 return -EINVAL;
0632
0633
0634 ret = dsi_wait_tx_payload_fifo_empty(ctx);
0635 if (ret) {
0636 drm_err(dsi->drm, "tx payload fifo is not empty\n");
0637 return ret;
0638 }
0639
0640 if (len > 2) {
0641 for (i = 0, j = 0; i < len; i += j) {
0642 payload = 0;
0643 for (j = 0; (j < 4) && ((j + i) < (len)); j++)
0644 payload |= param[i + j] << (j * 8);
0645
0646 writel(payload, ctx->base + GEN_PLD_DATA);
0647 }
0648 wc_lsbyte = len & 0xff;
0649 wc_msbyte = len >> 8;
0650 } else {
0651 wc_lsbyte = (len > 0) ? param[0] : 0;
0652 wc_msbyte = (len > 1) ? param[1] : 0;
0653 }
0654
0655
0656 ret = dsi_wait_tx_cmd_fifo_empty(ctx);
0657 if (ret) {
0658 drm_err(dsi->drm, "tx cmd fifo is not empty\n");
0659 return ret;
0660 }
0661
0662 writel(type | (vc << 6) | (wc_lsbyte << 8) | (wc_msbyte << 16),
0663 ctx->base + GEN_HDR);
0664
0665 return 0;
0666 }
0667
0668
0669
0670
0671
0672
0673
0674
0675
0676
0677 static int sprd_dsi_rd_pkt(struct dsi_context *ctx, u8 vc, u8 type,
0678 u8 msb_byte, u8 lsb_byte,
0679 u8 *buffer, u8 bytes_to_read)
0680 {
0681 struct sprd_dsi *dsi = container_of(ctx, struct sprd_dsi, ctx);
0682 int i, ret;
0683 int count = 0;
0684 u32 temp;
0685
0686 if (vc > 3)
0687 return -EINVAL;
0688
0689
0690 ret = dsi_reg_rd(ctx, CMD_MODE_STATUS, GEN_CMD_CMD_FIFO_EMPTY, 5);
0691 if (!ret)
0692 return -EIO;
0693
0694 writel(type | (vc << 6) | (lsb_byte << 8) | (msb_byte << 16),
0695 ctx->base + GEN_HDR);
0696
0697
0698 ret = dsi_wait_rd_resp_completed(ctx);
0699 if (ret) {
0700 drm_err(dsi->drm, "wait read response time out\n");
0701 return ret;
0702 }
0703
0704
0705 ret = dsi_reg_rd(ctx, CMD_MODE_STATUS, GEN_CMD_RDATA_FIFO_EMPTY, 1);
0706 if (ret) {
0707 drm_err(dsi->drm, "rx payload fifo empty\n");
0708 return -EIO;
0709 }
0710
0711 for (i = 0; i < 100; i++) {
0712 temp = readl(ctx->base + GEN_PLD_DATA);
0713
0714 if (count < bytes_to_read)
0715 buffer[count++] = temp & 0xff;
0716 if (count < bytes_to_read)
0717 buffer[count++] = (temp >> 8) & 0xff;
0718 if (count < bytes_to_read)
0719 buffer[count++] = (temp >> 16) & 0xff;
0720 if (count < bytes_to_read)
0721 buffer[count++] = (temp >> 24) & 0xff;
0722
0723 ret = dsi_reg_rd(ctx, CMD_MODE_STATUS, GEN_CMD_RDATA_FIFO_EMPTY, 1);
0724 if (ret)
0725 return count;
0726 }
0727
0728 return 0;
0729 }
0730
0731 static void sprd_dsi_set_work_mode(struct dsi_context *ctx, u8 mode)
0732 {
0733 if (mode == DSI_MODE_CMD)
0734 writel(1, ctx->base + DSI_MODE_CFG);
0735 else
0736 writel(0, ctx->base + DSI_MODE_CFG);
0737 }
0738
0739 static void sprd_dsi_state_reset(struct dsi_context *ctx)
0740 {
0741 writel(0, ctx->base + SOFT_RESET);
0742 udelay(100);
0743 writel(1, ctx->base + SOFT_RESET);
0744 }
0745
0746 static int sprd_dphy_init(struct dsi_context *ctx)
0747 {
0748 struct sprd_dsi *dsi = container_of(ctx, struct sprd_dsi, ctx);
0749 int ret;
0750
0751 dsi_reg_up(ctx, PHY_INTERFACE_CTRL, RF_PHY_RESET_N, 0);
0752 dsi_reg_up(ctx, PHY_INTERFACE_CTRL, RF_PHY_SHUTDOWN, 0);
0753 dsi_reg_up(ctx, PHY_INTERFACE_CTRL, RF_PHY_CLK_EN, 0);
0754
0755 dsi_reg_up(ctx, PHY_TST_CTRL0, PHY_TESTCLR, 0);
0756 dsi_reg_up(ctx, PHY_TST_CTRL0, PHY_TESTCLR, PHY_TESTCLR);
0757 dsi_reg_up(ctx, PHY_TST_CTRL0, PHY_TESTCLR, 0);
0758
0759 dphy_pll_config(ctx);
0760 dphy_timing_config(ctx);
0761
0762 dsi_reg_up(ctx, PHY_INTERFACE_CTRL, RF_PHY_SHUTDOWN, RF_PHY_SHUTDOWN);
0763 dsi_reg_up(ctx, PHY_INTERFACE_CTRL, RF_PHY_RESET_N, RF_PHY_RESET_N);
0764 writel(0x1C, ctx->base + PHY_MIN_STOP_TIME);
0765 dsi_reg_up(ctx, PHY_INTERFACE_CTRL, RF_PHY_CLK_EN, RF_PHY_CLK_EN);
0766 writel(dsi->slave->lanes - 1, ctx->base + PHY_LANE_NUM_CONFIG);
0767
0768 ret = dphy_wait_pll_locked(ctx);
0769 if (ret) {
0770 drm_err(dsi->drm, "dphy initial failed\n");
0771 return ret;
0772 }
0773
0774 return 0;
0775 }
0776
0777 static void sprd_dphy_fini(struct dsi_context *ctx)
0778 {
0779 dsi_reg_up(ctx, PHY_INTERFACE_CTRL, RF_PHY_RESET_N, 0);
0780 dsi_reg_up(ctx, PHY_INTERFACE_CTRL, RF_PHY_SHUTDOWN, 0);
0781 dsi_reg_up(ctx, PHY_INTERFACE_CTRL, RF_PHY_RESET_N, RF_PHY_RESET_N);
0782 }
0783
0784 static void sprd_dsi_encoder_mode_set(struct drm_encoder *encoder,
0785 struct drm_display_mode *mode,
0786 struct drm_display_mode *adj_mode)
0787 {
0788 struct sprd_dsi *dsi = encoder_to_dsi(encoder);
0789
0790 drm_display_mode_to_videomode(adj_mode, &dsi->ctx.vm);
0791 }
0792
0793 static void sprd_dsi_encoder_enable(struct drm_encoder *encoder)
0794 {
0795 struct sprd_dsi *dsi = encoder_to_dsi(encoder);
0796 struct sprd_dpu *dpu = to_sprd_crtc(encoder->crtc);
0797 struct dsi_context *ctx = &dsi->ctx;
0798
0799 if (ctx->enabled) {
0800 drm_warn(dsi->drm, "dsi is initialized\n");
0801 return;
0802 }
0803
0804 sprd_dsi_init(ctx);
0805 if (ctx->work_mode == DSI_MODE_VIDEO)
0806 sprd_dsi_dpi_video(ctx);
0807 else
0808 sprd_dsi_edpi_video(ctx);
0809
0810 sprd_dphy_init(ctx);
0811
0812 sprd_dsi_set_work_mode(ctx, ctx->work_mode);
0813 sprd_dsi_state_reset(ctx);
0814
0815 if (dsi->slave->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS) {
0816 dsi_reg_up(ctx, PHY_CLK_LANE_LP_CTRL, AUTO_CLKLANE_CTRL_EN,
0817 AUTO_CLKLANE_CTRL_EN);
0818 } else {
0819 dsi_reg_up(ctx, PHY_CLK_LANE_LP_CTRL, RF_PHY_CLK_EN, RF_PHY_CLK_EN);
0820 dsi_reg_up(ctx, PHY_CLK_LANE_LP_CTRL, PHY_CLKLANE_TX_REQ_HS,
0821 PHY_CLKLANE_TX_REQ_HS);
0822 dphy_wait_pll_locked(ctx);
0823 }
0824
0825 sprd_dpu_run(dpu);
0826
0827 ctx->enabled = true;
0828 }
0829
0830 static void sprd_dsi_encoder_disable(struct drm_encoder *encoder)
0831 {
0832 struct sprd_dsi *dsi = encoder_to_dsi(encoder);
0833 struct sprd_dpu *dpu = to_sprd_crtc(encoder->crtc);
0834 struct dsi_context *ctx = &dsi->ctx;
0835
0836 if (!ctx->enabled) {
0837 drm_warn(dsi->drm, "dsi isn't initialized\n");
0838 return;
0839 }
0840
0841 sprd_dpu_stop(dpu);
0842 sprd_dphy_fini(ctx);
0843 sprd_dsi_fini(ctx);
0844
0845 ctx->enabled = false;
0846 }
0847
0848 static const struct drm_encoder_helper_funcs sprd_encoder_helper_funcs = {
0849 .mode_set = sprd_dsi_encoder_mode_set,
0850 .enable = sprd_dsi_encoder_enable,
0851 .disable = sprd_dsi_encoder_disable
0852 };
0853
0854 static const struct drm_encoder_funcs sprd_encoder_funcs = {
0855 .destroy = drm_encoder_cleanup,
0856 };
0857
0858 static int sprd_dsi_encoder_init(struct sprd_dsi *dsi,
0859 struct device *dev)
0860 {
0861 struct drm_encoder *encoder = &dsi->encoder;
0862 u32 crtc_mask;
0863 int ret;
0864
0865 crtc_mask = drm_of_find_possible_crtcs(dsi->drm, dev->of_node);
0866 if (!crtc_mask) {
0867 drm_err(dsi->drm, "failed to find crtc mask\n");
0868 return -EINVAL;
0869 }
0870
0871 drm_dbg(dsi->drm, "find possible crtcs: 0x%08x\n", crtc_mask);
0872
0873 encoder->possible_crtcs = crtc_mask;
0874 ret = drm_encoder_init(dsi->drm, encoder, &sprd_encoder_funcs,
0875 DRM_MODE_ENCODER_DSI, NULL);
0876 if (ret) {
0877 drm_err(dsi->drm, "failed to init dsi encoder\n");
0878 return ret;
0879 }
0880
0881 drm_encoder_helper_add(encoder, &sprd_encoder_helper_funcs);
0882
0883 return 0;
0884 }
0885
0886 static int sprd_dsi_bridge_init(struct sprd_dsi *dsi,
0887 struct device *dev)
0888 {
0889 int ret;
0890
0891 dsi->panel_bridge = devm_drm_of_get_bridge(dev, dev->of_node, 1, 0);
0892 if (IS_ERR(dsi->panel_bridge))
0893 return PTR_ERR(dsi->panel_bridge);
0894
0895 ret = drm_bridge_attach(&dsi->encoder, dsi->panel_bridge, NULL, 0);
0896 if (ret)
0897 return ret;
0898
0899 return 0;
0900 }
0901
0902 static int sprd_dsi_context_init(struct sprd_dsi *dsi,
0903 struct device *dev)
0904 {
0905 struct platform_device *pdev = to_platform_device(dev);
0906 struct dsi_context *ctx = &dsi->ctx;
0907 struct resource *res;
0908
0909 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0910 if (!res) {
0911 dev_err(dev, "failed to get I/O resource\n");
0912 return -EINVAL;
0913 }
0914
0915 ctx->base = devm_ioremap(dev, res->start, resource_size(res));
0916 if (!ctx->base) {
0917 drm_err(dsi->drm, "failed to map dsi host registers\n");
0918 return -ENXIO;
0919 }
0920
0921 ctx->regmap = devm_regmap_init(dev, ®map_tst_io, dsi, &byte_config);
0922 if (IS_ERR(ctx->regmap)) {
0923 drm_err(dsi->drm, "dphy regmap init failed\n");
0924 return PTR_ERR(ctx->regmap);
0925 }
0926
0927 ctx->data_hs2lp = 120;
0928 ctx->data_lp2hs = 500;
0929 ctx->clk_hs2lp = 4;
0930 ctx->clk_lp2hs = 15;
0931 ctx->max_rd_time = 6000;
0932 ctx->int0_mask = 0xffffffff;
0933 ctx->int1_mask = 0xffffffff;
0934 ctx->enabled = true;
0935
0936 return 0;
0937 }
0938
0939 static int sprd_dsi_bind(struct device *dev, struct device *master, void *data)
0940 {
0941 struct drm_device *drm = data;
0942 struct sprd_dsi *dsi = dev_get_drvdata(dev);
0943 int ret;
0944
0945 dsi->drm = drm;
0946
0947 ret = sprd_dsi_encoder_init(dsi, dev);
0948 if (ret)
0949 return ret;
0950
0951 ret = sprd_dsi_bridge_init(dsi, dev);
0952 if (ret)
0953 return ret;
0954
0955 ret = sprd_dsi_context_init(dsi, dev);
0956 if (ret)
0957 return ret;
0958
0959 return 0;
0960 }
0961
0962 static void sprd_dsi_unbind(struct device *dev,
0963 struct device *master, void *data)
0964 {
0965 struct sprd_dsi *dsi = dev_get_drvdata(dev);
0966
0967 drm_of_panel_bridge_remove(dev->of_node, 1, 0);
0968
0969 drm_encoder_cleanup(&dsi->encoder);
0970 }
0971
0972 static const struct component_ops dsi_component_ops = {
0973 .bind = sprd_dsi_bind,
0974 .unbind = sprd_dsi_unbind,
0975 };
0976
0977 static int sprd_dsi_host_attach(struct mipi_dsi_host *host,
0978 struct mipi_dsi_device *slave)
0979 {
0980 struct sprd_dsi *dsi = host_to_dsi(host);
0981 struct dsi_context *ctx = &dsi->ctx;
0982
0983 dsi->slave = slave;
0984
0985 if (slave->mode_flags & MIPI_DSI_MODE_VIDEO)
0986 ctx->work_mode = DSI_MODE_VIDEO;
0987 else
0988 ctx->work_mode = DSI_MODE_CMD;
0989
0990 if (slave->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
0991 ctx->burst_mode = VIDEO_BURST_WITH_SYNC_PULSES;
0992 else if (slave->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
0993 ctx->burst_mode = VIDEO_NON_BURST_WITH_SYNC_PULSES;
0994 else
0995 ctx->burst_mode = VIDEO_NON_BURST_WITH_SYNC_EVENTS;
0996
0997 return component_add(host->dev, &dsi_component_ops);
0998 }
0999
1000 static int sprd_dsi_host_detach(struct mipi_dsi_host *host,
1001 struct mipi_dsi_device *slave)
1002 {
1003 component_del(host->dev, &dsi_component_ops);
1004
1005 return 0;
1006 }
1007
1008 static ssize_t sprd_dsi_host_transfer(struct mipi_dsi_host *host,
1009 const struct mipi_dsi_msg *msg)
1010 {
1011 struct sprd_dsi *dsi = host_to_dsi(host);
1012 const u8 *tx_buf = msg->tx_buf;
1013
1014 if (msg->rx_buf && msg->rx_len) {
1015 u8 lsb = (msg->tx_len > 0) ? tx_buf[0] : 0;
1016 u8 msb = (msg->tx_len > 1) ? tx_buf[1] : 0;
1017
1018 return sprd_dsi_rd_pkt(&dsi->ctx, msg->channel, msg->type,
1019 msb, lsb, msg->rx_buf, msg->rx_len);
1020 }
1021
1022 if (msg->tx_buf && msg->tx_len)
1023 return sprd_dsi_wr_pkt(&dsi->ctx, msg->channel, msg->type,
1024 tx_buf, msg->tx_len);
1025
1026 return 0;
1027 }
1028
1029 static const struct mipi_dsi_host_ops sprd_dsi_host_ops = {
1030 .attach = sprd_dsi_host_attach,
1031 .detach = sprd_dsi_host_detach,
1032 .transfer = sprd_dsi_host_transfer,
1033 };
1034
1035 static const struct of_device_id dsi_match_table[] = {
1036 { .compatible = "sprd,sharkl3-dsi-host" },
1037 { },
1038 };
1039
1040 static int sprd_dsi_probe(struct platform_device *pdev)
1041 {
1042 struct device *dev = &pdev->dev;
1043 struct sprd_dsi *dsi;
1044
1045 dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
1046 if (!dsi)
1047 return -ENOMEM;
1048
1049 dev_set_drvdata(dev, dsi);
1050
1051 dsi->host.ops = &sprd_dsi_host_ops;
1052 dsi->host.dev = dev;
1053
1054 return mipi_dsi_host_register(&dsi->host);
1055 }
1056
1057 static int sprd_dsi_remove(struct platform_device *pdev)
1058 {
1059 struct sprd_dsi *dsi = dev_get_drvdata(&pdev->dev);
1060
1061 mipi_dsi_host_unregister(&dsi->host);
1062
1063 return 0;
1064 }
1065
1066 struct platform_driver sprd_dsi_driver = {
1067 .probe = sprd_dsi_probe,
1068 .remove = sprd_dsi_remove,
1069 .driver = {
1070 .name = "sprd-dsi-drv",
1071 .of_match_table = dsi_match_table,
1072 },
1073 };
1074
1075 MODULE_AUTHOR("Leon He <leon.he@unisoc.com>");
1076 MODULE_AUTHOR("Kevin Tang <kevin.tang@unisoc.com>");
1077 MODULE_DESCRIPTION("Unisoc MIPI DSI HOST Controller Driver");
1078 MODULE_LICENSE("GPL v2");