mtk_dsi.c 32.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
C
CK Hu 已提交
2 3 4 5 6 7
/*
 * Copyright (c) 2015 MediaTek Inc.
 */

#include <linux/clk.h>
#include <linux/component.h>
D
Dan Carpenter 已提交
8
#include <linux/iopoll.h>
9
#include <linux/irq.h>
C
CK Hu 已提交
10 11 12 13
#include <linux/of.h>
#include <linux/of_platform.h>
#include <linux/phy/phy.h>
#include <linux/platform_device.h>
S
Sam Ravnborg 已提交
14

15
#include <video/mipi_display.h>
C
CK Hu 已提交
16 17
#include <video/videomode.h>

S
Sam Ravnborg 已提交
18
#include <drm/drm_atomic_helper.h>
19
#include <drm/drm_bridge.h>
S
Sam Ravnborg 已提交
20 21 22 23 24
#include <drm/drm_mipi_dsi.h>
#include <drm/drm_of.h>
#include <drm/drm_panel.h>
#include <drm/drm_print.h>
#include <drm/drm_probe_helper.h>
25
#include <drm/drm_simple_kms_helper.h>
S
Sam Ravnborg 已提交
26

C
CK Hu 已提交
27 28 29 30
#include "mtk_drm_ddp_comp.h"

#define DSI_START		0x00

31 32 33 34 35 36 37 38 39 40
#define DSI_INTEN		0x08

#define DSI_INTSTA		0x0c
#define LPRX_RD_RDY_INT_FLAG		BIT(0)
#define CMD_DONE_INT_FLAG		BIT(1)
#define TE_RDY_INT_FLAG			BIT(2)
#define VM_DONE_INT_FLAG		BIT(3)
#define EXT_TE_RDY_INT_FLAG		BIT(4)
#define DSI_BUSY			BIT(31)

C
CK Hu 已提交
41 42 43
#define DSI_CON_CTRL		0x10
#define DSI_RESET			BIT(0)
#define DSI_EN				BIT(1)
44
#define DPHY_RESET			BIT(2)
C
CK Hu 已提交
45 46 47 48 49 50 51 52 53 54 55

#define DSI_MODE_CTRL		0x14
#define MODE				(3)
#define CMD_MODE			0
#define SYNC_PULSE_MODE			1
#define SYNC_EVENT_MODE			2
#define BURST_MODE			3
#define FRM_MODE			BIT(16)
#define MIX_MODE			BIT(17)

#define DSI_TXRX_CTRL		0x18
56
#define VC_NUM				BIT(1)
C
CK Hu 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#define LANE_NUM			(0xf << 2)
#define DIS_EOT				BIT(6)
#define NULL_EN				BIT(7)
#define TE_FREERUN			BIT(8)
#define EXT_TE_EN			BIT(9)
#define EXT_TE_EDGE			BIT(10)
#define MAX_RTN_SIZE			(0xf << 12)
#define HSTX_CKLP_EN			BIT(16)

#define DSI_PSCTRL		0x1c
#define DSI_PS_WC			0x3fff
#define DSI_PS_SEL			(3 << 16)
#define PACKED_PS_16BIT_RGB565		(0 << 16)
#define LOOSELY_PS_18BIT_RGB666		(1 << 16)
#define PACKED_PS_18BIT_RGB666		(2 << 16)
#define PACKED_PS_24BIT_RGB888		(3 << 16)

#define DSI_VSA_NL		0x20
#define DSI_VBP_NL		0x24
#define DSI_VFP_NL		0x28
#define DSI_VACT_NL		0x2C
J
Jitao Shi 已提交
78
#define DSI_SIZE_CON		0x38
C
CK Hu 已提交
79 80 81 82
#define DSI_HSA_WC		0x50
#define DSI_HBP_WC		0x54
#define DSI_HFP_WC		0x58

83 84 85
#define DSI_CMDQ_SIZE		0x60
#define CMDQ_SIZE			0x3f

C
CK Hu 已提交
86 87
#define DSI_HSTX_CKL_WC		0x64

88 89 90 91 92
#define DSI_RX_DATA0		0x74
#define DSI_RX_DATA1		0x78
#define DSI_RX_DATA2		0x7c
#define DSI_RX_DATA3		0x80

93 94 95
#define DSI_RACK		0x84
#define RACK				BIT(0)

C
CK Hu 已提交
96 97 98 99 100 101 102 103 104 105 106 107
#define DSI_PHY_LCCON		0x104
#define LC_HS_TX_EN			BIT(0)
#define LC_ULPM_EN			BIT(1)
#define LC_WAKEUP_EN			BIT(2)

#define DSI_PHY_LD0CON		0x108
#define LD0_HS_TX_EN			BIT(0)
#define LD0_ULPM_EN			BIT(1)
#define LD0_WAKEUP_EN			BIT(2)

#define DSI_PHY_TIMECON0	0x110
#define LPX				(0xff << 0)
108
#define HS_PREP				(0xff << 8)
C
CK Hu 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
#define HS_ZERO				(0xff << 16)
#define HS_TRAIL			(0xff << 24)

#define DSI_PHY_TIMECON1	0x114
#define TA_GO				(0xff << 0)
#define TA_SURE				(0xff << 8)
#define TA_GET				(0xff << 16)
#define DA_HS_EXIT			(0xff << 24)

#define DSI_PHY_TIMECON2	0x118
#define CONT_DET			(0xff << 0)
#define CLK_ZERO			(0xff << 16)
#define CLK_TRAIL			(0xff << 24)

#define DSI_PHY_TIMECON3	0x11c
124
#define CLK_HS_PREP			(0xff << 0)
C
CK Hu 已提交
125 126 127
#define CLK_HS_POST			(0xff << 8)
#define CLK_HS_EXIT			(0xff << 16)

128 129 130 131
#define DSI_VM_CMD_CON		0x130
#define VM_CMD_EN			BIT(0)
#define TS_VFP_EN			BIT(5)

132 133 134 135
#define DSI_SHADOW_DEBUG	0x190U
#define FORCE_COMMIT			BIT(0)
#define BYPASS_SHADOW			BIT(1)

136 137 138 139 140 141 142 143
#define CONFIG				(0xff << 0)
#define SHORT_PACKET			0
#define LONG_PACKET			2
#define BTA				BIT(2)
#define DATA_ID				(0xff << 8)
#define DATA_0				(0xff << 16)
#define DATA_1				(0xff << 24)

C
CK Hu 已提交
144 145
#define NS_TO_CYCLE(n, c)    ((n) / (c) + (((n) % (c)) ? 1 : 0))

146 147 148 149 150 151
#define MTK_DSI_HOST_IS_READ(type) \
	((type == MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM) || \
	(type == MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM) || \
	(type == MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM) || \
	(type == MIPI_DSI_DCS_READ))

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
struct mtk_phy_timing {
	u32 lpx;
	u32 da_hs_prepare;
	u32 da_hs_zero;
	u32 da_hs_trail;

	u32 ta_go;
	u32 ta_sure;
	u32 ta_get;
	u32 da_hs_exit;

	u32 clk_hs_zero;
	u32 clk_hs_trail;

	u32 clk_hs_prepare;
	u32 clk_hs_post;
	u32 clk_hs_exit;
};

C
CK Hu 已提交
171 172
struct phy;

173 174
struct mtk_dsi_driver_data {
	const u32 reg_cmdq_off;
175
	bool has_shadow_ctl;
J
Jitao Shi 已提交
176
	bool has_size_ctl;
177 178
};

C
CK Hu 已提交
179 180 181 182 183
struct mtk_dsi {
	struct mtk_ddp_comp ddp_comp;
	struct device *dev;
	struct mipi_dsi_host host;
	struct drm_encoder encoder;
184
	struct drm_bridge bridge;
C
CK Hu 已提交
185 186
	struct drm_connector conn;
	struct drm_panel *panel;
187
	struct drm_bridge *next_bridge;
C
CK Hu 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201
	struct phy *phy;

	void __iomem *regs;

	struct clk *engine_clk;
	struct clk *digital_clk;
	struct clk *hs_clk;

	u32 data_rate;

	unsigned long mode_flags;
	enum mipi_dsi_pixel_format format;
	unsigned int lanes;
	struct videomode vm;
202
	struct mtk_phy_timing phy_timing;
C
CK Hu 已提交
203 204
	int refcount;
	bool enabled;
205 206
	u32 irq_data;
	wait_queue_head_t irq_wait_queue;
207
	const struct mtk_dsi_driver_data *driver_data;
C
CK Hu 已提交
208 209
};

210
static inline struct mtk_dsi *bridge_to_dsi(struct drm_bridge *b)
C
CK Hu 已提交
211
{
212
	return container_of(b, struct mtk_dsi, bridge);
C
CK Hu 已提交
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
}

static inline struct mtk_dsi *connector_to_dsi(struct drm_connector *c)
{
	return container_of(c, struct mtk_dsi, conn);
}

static inline struct mtk_dsi *host_to_dsi(struct mipi_dsi_host *h)
{
	return container_of(h, struct mtk_dsi, host);
}

static void mtk_dsi_mask(struct mtk_dsi *dsi, u32 offset, u32 mask, u32 data)
{
	u32 temp = readl(dsi->regs + offset);

	writel((temp & ~mask) | (data & mask), dsi->regs + offset);
}

232
static void mtk_dsi_phy_timconfig(struct mtk_dsi *dsi)
C
CK Hu 已提交
233 234
{
	u32 timcon0, timcon1, timcon2, timcon3;
235
	u32 data_rate_mhz = DIV_ROUND_UP(dsi->data_rate, 1000000);
236 237
	struct mtk_phy_timing *timing = &dsi->phy_timing;

238 239 240 241 242
	timing->lpx = (60 * data_rate_mhz / (8 * 1000)) + 1;
	timing->da_hs_prepare = (80 * data_rate_mhz + 4 * 1000) / 8000;
	timing->da_hs_zero = (170 * data_rate_mhz + 10 * 1000) / 8000 + 1 -
			     timing->da_hs_prepare;
	timing->da_hs_trail = timing->da_hs_prepare + 1;
C
CK Hu 已提交
243

244 245 246 247
	timing->ta_go = 4 * timing->lpx - 2;
	timing->ta_sure = timing->lpx + 2;
	timing->ta_get = 4 * timing->lpx;
	timing->da_hs_exit = 2 * timing->lpx + 1;
C
CK Hu 已提交
248

249 250 251 252 253
	timing->clk_hs_prepare = 70 * data_rate_mhz / (8 * 1000);
	timing->clk_hs_post = timing->clk_hs_prepare + 8;
	timing->clk_hs_trail = timing->clk_hs_prepare;
	timing->clk_hs_zero = timing->clk_hs_trail * 4;
	timing->clk_hs_exit = 2 * timing->clk_hs_trail;
254 255 256 257 258 259 260 261 262

	timcon0 = timing->lpx | timing->da_hs_prepare << 8 |
		  timing->da_hs_zero << 16 | timing->da_hs_trail << 24;
	timcon1 = timing->ta_go | timing->ta_sure << 8 |
		  timing->ta_get << 16 | timing->da_hs_exit << 24;
	timcon2 = 1 << 8 | timing->clk_hs_zero << 16 |
		  timing->clk_hs_trail << 24;
	timcon3 = timing->clk_hs_prepare | timing->clk_hs_post << 8 |
		  timing->clk_hs_exit << 16;
C
CK Hu 已提交
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279

	writel(timcon0, dsi->regs + DSI_PHY_TIMECON0);
	writel(timcon1, dsi->regs + DSI_PHY_TIMECON1);
	writel(timcon2, dsi->regs + DSI_PHY_TIMECON2);
	writel(timcon3, dsi->regs + DSI_PHY_TIMECON3);
}

static void mtk_dsi_enable(struct mtk_dsi *dsi)
{
	mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_EN, DSI_EN);
}

static void mtk_dsi_disable(struct mtk_dsi *dsi)
{
	mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_EN, 0);
}

280
static void mtk_dsi_reset_engine(struct mtk_dsi *dsi)
C
CK Hu 已提交
281 282 283 284 285
{
	mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_RESET, DSI_RESET);
	mtk_dsi_mask(dsi, DSI_CON_CTRL, DSI_RESET, 0);
}

286 287 288 289 290 291
static void mtk_dsi_reset_dphy(struct mtk_dsi *dsi)
{
	mtk_dsi_mask(dsi, DSI_CON_CTRL, DPHY_RESET, DPHY_RESET);
	mtk_dsi_mask(dsi, DSI_CON_CTRL, DPHY_RESET, 0);
}

292
static void mtk_dsi_clk_ulp_mode_enter(struct mtk_dsi *dsi)
C
CK Hu 已提交
293 294 295 296 297
{
	mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_HS_TX_EN, 0);
	mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_ULPM_EN, 0);
}

298
static void mtk_dsi_clk_ulp_mode_leave(struct mtk_dsi *dsi)
C
CK Hu 已提交
299 300 301 302 303 304
{
	mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_ULPM_EN, 0);
	mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_WAKEUP_EN, LC_WAKEUP_EN);
	mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_WAKEUP_EN, 0);
}

305
static void mtk_dsi_lane0_ulp_mode_enter(struct mtk_dsi *dsi)
C
CK Hu 已提交
306 307 308 309 310
{
	mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_HS_TX_EN, 0);
	mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_ULPM_EN, 0);
}

311
static void mtk_dsi_lane0_ulp_mode_leave(struct mtk_dsi *dsi)
C
CK Hu 已提交
312 313 314 315 316 317
{
	mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_ULPM_EN, 0);
	mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_WAKEUP_EN, LD0_WAKEUP_EN);
	mtk_dsi_mask(dsi, DSI_PHY_LD0CON, LD0_WAKEUP_EN, 0);
}

318
static bool mtk_dsi_clk_hs_state(struct mtk_dsi *dsi)
C
CK Hu 已提交
319
{
320
	return readl(dsi->regs + DSI_PHY_LCCON) & LC_HS_TX_EN;
C
CK Hu 已提交
321 322
}

323
static void mtk_dsi_clk_hs_mode(struct mtk_dsi *dsi, bool enter)
C
CK Hu 已提交
324
{
325
	if (enter && !mtk_dsi_clk_hs_state(dsi))
C
CK Hu 已提交
326
		mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_HS_TX_EN, LC_HS_TX_EN);
327
	else if (!enter && mtk_dsi_clk_hs_state(dsi))
C
CK Hu 已提交
328 329 330
		mtk_dsi_mask(dsi, DSI_PHY_LCCON, LC_HS_TX_EN, 0);
}

331
static void mtk_dsi_set_mode(struct mtk_dsi *dsi)
C
CK Hu 已提交
332 333 334 335
{
	u32 vid_mode = CMD_MODE;

	if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO) {
336
		if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST)
C
CK Hu 已提交
337
			vid_mode = BURST_MODE;
338 339 340 341
		else if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
			vid_mode = SYNC_PULSE_MODE;
		else
			vid_mode = SYNC_EVENT_MODE;
C
CK Hu 已提交
342 343 344 345 346
	}

	writel(vid_mode, dsi->regs + DSI_MODE_CTRL);
}

347 348 349 350 351 352
static void mtk_dsi_set_vm_cmd(struct mtk_dsi *dsi)
{
	mtk_dsi_mask(dsi, DSI_VM_CMD_CON, VM_CMD_EN, VM_CMD_EN);
	mtk_dsi_mask(dsi, DSI_VM_CMD_CON, TS_VFP_EN, TS_VFP_EN);
}

353
static void mtk_dsi_ps_control_vact(struct mtk_dsi *dsi)
C
CK Hu 已提交
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
{
	struct videomode *vm = &dsi->vm;
	u32 dsi_buf_bpp, ps_wc;
	u32 ps_bpp_mode;

	if (dsi->format == MIPI_DSI_FMT_RGB565)
		dsi_buf_bpp = 2;
	else
		dsi_buf_bpp = 3;

	ps_wc = vm->hactive * dsi_buf_bpp;
	ps_bpp_mode = ps_wc;

	switch (dsi->format) {
	case MIPI_DSI_FMT_RGB888:
		ps_bpp_mode |= PACKED_PS_24BIT_RGB888;
		break;
	case MIPI_DSI_FMT_RGB666:
		ps_bpp_mode |= PACKED_PS_18BIT_RGB666;
		break;
	case MIPI_DSI_FMT_RGB666_PACKED:
		ps_bpp_mode |= LOOSELY_PS_18BIT_RGB666;
		break;
	case MIPI_DSI_FMT_RGB565:
		ps_bpp_mode |= PACKED_PS_16BIT_RGB565;
		break;
	}

	writel(vm->vactive, dsi->regs + DSI_VACT_NL);
	writel(ps_bpp_mode, dsi->regs + DSI_PSCTRL);
	writel(ps_wc, dsi->regs + DSI_HSTX_CKL_WC);
}

387
static void mtk_dsi_rxtx_control(struct mtk_dsi *dsi)
C
CK Hu 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
{
	u32 tmp_reg;

	switch (dsi->lanes) {
	case 1:
		tmp_reg = 1 << 2;
		break;
	case 2:
		tmp_reg = 3 << 2;
		break;
	case 3:
		tmp_reg = 7 << 2;
		break;
	case 4:
		tmp_reg = 0xf << 2;
		break;
	default:
		tmp_reg = 0xf << 2;
		break;
	}

409 410 411
	tmp_reg |= (dsi->mode_flags & MIPI_DSI_CLOCK_NON_CONTINUOUS) << 6;
	tmp_reg |= (dsi->mode_flags & MIPI_DSI_MODE_EOT_PACKET) >> 3;

C
CK Hu 已提交
412 413 414
	writel(tmp_reg, dsi->regs + DSI_TXRX_CTRL);
}

415
static void mtk_dsi_ps_control(struct mtk_dsi *dsi)
C
CK Hu 已提交
416
{
417
	u32 dsi_tmp_buf_bpp;
C
CK Hu 已提交
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
	u32 tmp_reg;

	switch (dsi->format) {
	case MIPI_DSI_FMT_RGB888:
		tmp_reg = PACKED_PS_24BIT_RGB888;
		dsi_tmp_buf_bpp = 3;
		break;
	case MIPI_DSI_FMT_RGB666:
		tmp_reg = LOOSELY_PS_18BIT_RGB666;
		dsi_tmp_buf_bpp = 3;
		break;
	case MIPI_DSI_FMT_RGB666_PACKED:
		tmp_reg = PACKED_PS_18BIT_RGB666;
		dsi_tmp_buf_bpp = 3;
		break;
	case MIPI_DSI_FMT_RGB565:
		tmp_reg = PACKED_PS_16BIT_RGB565;
		dsi_tmp_buf_bpp = 2;
		break;
	default:
		tmp_reg = PACKED_PS_24BIT_RGB888;
		dsi_tmp_buf_bpp = 3;
		break;
	}

	tmp_reg += dsi->vm.hactive * dsi_tmp_buf_bpp & DSI_PS_WC;
	writel(tmp_reg, dsi->regs + DSI_PSCTRL);
}

447
static void mtk_dsi_config_vdo_timing(struct mtk_dsi *dsi)
C
CK Hu 已提交
448
{
449 450 451
	u32 horizontal_sync_active_byte;
	u32 horizontal_backporch_byte;
	u32 horizontal_frontporch_byte;
452 453
	u32 dsi_tmp_buf_bpp, data_phy_cycles;
	struct mtk_phy_timing *timing = &dsi->phy_timing;
C
CK Hu 已提交
454 455 456 457 458 459 460 461 462 463 464 465 466

	struct videomode *vm = &dsi->vm;

	if (dsi->format == MIPI_DSI_FMT_RGB565)
		dsi_tmp_buf_bpp = 2;
	else
		dsi_tmp_buf_bpp = 3;

	writel(vm->vsync_len, dsi->regs + DSI_VSA_NL);
	writel(vm->vback_porch, dsi->regs + DSI_VBP_NL);
	writel(vm->vfront_porch, dsi->regs + DSI_VFP_NL);
	writel(vm->vactive, dsi->regs + DSI_VACT_NL);

J
Jitao Shi 已提交
467 468 469 470
	if (dsi->driver_data->has_size_ctl)
		writel(vm->vactive << 16 | vm->hactive,
		       dsi->regs + DSI_SIZE_CON);

C
CK Hu 已提交
471 472 473 474 475 476 477 478 479
	horizontal_sync_active_byte = (vm->hsync_len * dsi_tmp_buf_bpp - 10);

	if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_SYNC_PULSE)
		horizontal_backporch_byte =
			(vm->hback_porch * dsi_tmp_buf_bpp - 10);
	else
		horizontal_backporch_byte = ((vm->hback_porch + vm->hsync_len) *
			dsi_tmp_buf_bpp - 10);

480
	data_phy_cycles = timing->lpx + timing->da_hs_prepare +
481
			  timing->da_hs_zero + timing->da_hs_exit + 3;
482 483

	if (dsi->mode_flags & MIPI_DSI_MODE_VIDEO_BURST) {
484
		if ((vm->hfront_porch + vm->hback_porch) * dsi_tmp_buf_bpp >
485
		    data_phy_cycles * dsi->lanes + 18) {
486 487 488 489 490 491 492 493 494 495 496
			horizontal_frontporch_byte =
				vm->hfront_porch * dsi_tmp_buf_bpp -
				(data_phy_cycles * dsi->lanes + 18) *
				vm->hfront_porch /
				(vm->hfront_porch + vm->hback_porch);

			horizontal_backporch_byte =
				horizontal_backporch_byte -
				(data_phy_cycles * dsi->lanes + 18) *
				vm->hback_porch /
				(vm->hfront_porch + vm->hback_porch);
497 498 499 500 501 502
		} else {
			DRM_WARN("HFP less than d-phy, FPS will under 60Hz\n");
			horizontal_frontporch_byte = vm->hfront_porch *
						     dsi_tmp_buf_bpp;
		}
	} else {
503
		if ((vm->hfront_porch + vm->hback_porch) * dsi_tmp_buf_bpp >
504
		    data_phy_cycles * dsi->lanes + 12) {
505 506 507 508 509 510 511 512 513
			horizontal_frontporch_byte =
				vm->hfront_porch * dsi_tmp_buf_bpp -
				(data_phy_cycles * dsi->lanes + 12) *
				vm->hfront_porch /
				(vm->hfront_porch + vm->hback_porch);
			horizontal_backporch_byte = horizontal_backporch_byte -
				(data_phy_cycles * dsi->lanes + 12) *
				vm->hback_porch /
				(vm->hfront_porch + vm->hback_porch);
514 515 516 517 518 519
		} else {
			DRM_WARN("HFP less than d-phy, FPS will under 60Hz\n");
			horizontal_frontporch_byte = vm->hfront_porch *
						     dsi_tmp_buf_bpp;
		}
	}
C
CK Hu 已提交
520 521 522 523 524

	writel(horizontal_sync_active_byte, dsi->regs + DSI_HSA_WC);
	writel(horizontal_backporch_byte, dsi->regs + DSI_HBP_WC);
	writel(horizontal_frontporch_byte, dsi->regs + DSI_HFP_WC);

525
	mtk_dsi_ps_control(dsi);
C
CK Hu 已提交
526 527 528 529 530 531 532 533
}

static void mtk_dsi_start(struct mtk_dsi *dsi)
{
	writel(0, dsi->regs + DSI_START);
	writel(1, dsi->regs + DSI_START);
}

534 535 536 537 538 539 540 541 542 543
static void mtk_dsi_stop(struct mtk_dsi *dsi)
{
	writel(0, dsi->regs + DSI_START);
}

static void mtk_dsi_set_cmd_mode(struct mtk_dsi *dsi)
{
	writel(CMD_MODE, dsi->regs + DSI_MODE_CTRL);
}

544 545 546 547 548 549 550 551 552 553 554 555
static void mtk_dsi_set_interrupt_enable(struct mtk_dsi *dsi)
{
	u32 inten = LPRX_RD_RDY_INT_FLAG | CMD_DONE_INT_FLAG | VM_DONE_INT_FLAG;

	writel(inten, dsi->regs + DSI_INTEN);
}

static void mtk_dsi_irq_data_set(struct mtk_dsi *dsi, u32 irq_bit)
{
	dsi->irq_data |= irq_bit;
}

556
static void mtk_dsi_irq_data_clear(struct mtk_dsi *dsi, u32 irq_bit)
557 558 559 560
{
	dsi->irq_data &= ~irq_bit;
}

561
static s32 mtk_dsi_wait_for_irq_done(struct mtk_dsi *dsi, u32 irq_flag,
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
				     unsigned int timeout)
{
	s32 ret = 0;
	unsigned long jiffies = msecs_to_jiffies(timeout);

	ret = wait_event_interruptible_timeout(dsi->irq_wait_queue,
					       dsi->irq_data & irq_flag,
					       jiffies);
	if (ret == 0) {
		DRM_WARN("Wait DSI IRQ(0x%08x) Timeout\n", irq_flag);

		mtk_dsi_enable(dsi);
		mtk_dsi_reset_engine(dsi);
	}

	return ret;
}

static irqreturn_t mtk_dsi_irq(int irq, void *dev_id)
{
	struct mtk_dsi *dsi = dev_id;
	u32 status, tmp;
	u32 flag = LPRX_RD_RDY_INT_FLAG | CMD_DONE_INT_FLAG | VM_DONE_INT_FLAG;

	status = readl(dsi->regs + DSI_INTSTA) & flag;

	if (status) {
		do {
			mtk_dsi_mask(dsi, DSI_RACK, RACK, RACK);
			tmp = readl(dsi->regs + DSI_INTSTA);
		} while (tmp & DSI_BUSY);

		mtk_dsi_mask(dsi, DSI_INTSTA, status, 0);
		mtk_dsi_irq_data_set(dsi, status);
		wake_up_interruptible(&dsi->irq_wait_queue);
	}

	return IRQ_HANDLED;
}

602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
static s32 mtk_dsi_switch_to_cmd_mode(struct mtk_dsi *dsi, u8 irq_flag, u32 t)
{
	mtk_dsi_irq_data_clear(dsi, irq_flag);
	mtk_dsi_set_cmd_mode(dsi);

	if (!mtk_dsi_wait_for_irq_done(dsi, irq_flag, t)) {
		DRM_ERROR("failed to switch cmd mode\n");
		return -ETIME;
	} else {
		return 0;
	}
}

static int mtk_dsi_poweron(struct mtk_dsi *dsi)
{
617
	struct device *dev = dsi->host.dev;
618
	int ret;
619
	u32 bit_per_pixel;
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637

	if (++dsi->refcount != 1)
		return 0;

	switch (dsi->format) {
	case MIPI_DSI_FMT_RGB565:
		bit_per_pixel = 16;
		break;
	case MIPI_DSI_FMT_RGB666_PACKED:
		bit_per_pixel = 18;
		break;
	case MIPI_DSI_FMT_RGB666:
	case MIPI_DSI_FMT_RGB888:
	default:
		bit_per_pixel = 24;
		break;
	}

638 639
	dsi->data_rate = DIV_ROUND_UP_ULL(dsi->vm.pixelclock * bit_per_pixel,
					  dsi->lanes);
640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661

	ret = clk_set_rate(dsi->hs_clk, dsi->data_rate);
	if (ret < 0) {
		dev_err(dev, "Failed to set data rate: %d\n", ret);
		goto err_refcount;
	}

	phy_power_on(dsi->phy);

	ret = clk_prepare_enable(dsi->engine_clk);
	if (ret < 0) {
		dev_err(dev, "Failed to enable engine clock: %d\n", ret);
		goto err_phy_power_off;
	}

	ret = clk_prepare_enable(dsi->digital_clk);
	if (ret < 0) {
		dev_err(dev, "Failed to enable digital clock: %d\n", ret);
		goto err_disable_engine_clk;
	}

	mtk_dsi_enable(dsi);
662 663 664 665 666

	if (dsi->driver_data->has_shadow_ctl)
		writel(FORCE_COMMIT | BYPASS_SHADOW,
		       dsi->regs + DSI_SHADOW_DEBUG);

667 668 669 670
	mtk_dsi_reset_engine(dsi);
	mtk_dsi_phy_timconfig(dsi);

	mtk_dsi_rxtx_control(dsi);
671 672
	usleep_range(30, 100);
	mtk_dsi_reset_dphy(dsi);
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
	mtk_dsi_ps_control_vact(dsi);
	mtk_dsi_set_vm_cmd(dsi);
	mtk_dsi_config_vdo_timing(dsi);
	mtk_dsi_set_interrupt_enable(dsi);

	mtk_dsi_clk_ulp_mode_leave(dsi);
	mtk_dsi_lane0_ulp_mode_leave(dsi);
	mtk_dsi_clk_hs_mode(dsi, 0);

	if (dsi->panel) {
		if (drm_panel_prepare(dsi->panel)) {
			DRM_ERROR("failed to prepare the panel\n");
			goto err_disable_digital_clk;
		}
	}

	return 0;
err_disable_digital_clk:
	clk_disable_unprepare(dsi->digital_clk);
err_disable_engine_clk:
	clk_disable_unprepare(dsi->engine_clk);
err_phy_power_off:
	phy_power_off(dsi->phy);
err_refcount:
	dsi->refcount--;
	return ret;
}

C
CK Hu 已提交
701 702 703 704 705 706 707 708
static void mtk_dsi_poweroff(struct mtk_dsi *dsi)
{
	if (WARN_ON(dsi->refcount == 0))
		return;

	if (--dsi->refcount != 0)
		return;

709 710 711 712 713 714 715 716 717
	/*
	 * mtk_dsi_stop() and mtk_dsi_start() is asymmetric, since
	 * mtk_dsi_stop() should be called after mtk_drm_crtc_atomic_disable(),
	 * which needs irq for vblank, and mtk_dsi_stop() will disable irq.
	 * mtk_dsi_start() needs to be called in mtk_output_dsi_enable(),
	 * after dsi is fully set.
	 */
	mtk_dsi_stop(dsi);

718 719 720 721 722 723 724 725 726 727
	if (!mtk_dsi_switch_to_cmd_mode(dsi, VM_DONE_INT_FLAG, 500)) {
		if (dsi->panel) {
			if (drm_panel_unprepare(dsi->panel)) {
				DRM_ERROR("failed to unprepare the panel\n");
				return;
			}
		}
	}

	mtk_dsi_reset_engine(dsi);
728 729
	mtk_dsi_lane0_ulp_mode_enter(dsi);
	mtk_dsi_clk_ulp_mode_enter(dsi);
C
CK Hu 已提交
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751

	mtk_dsi_disable(dsi);

	clk_disable_unprepare(dsi->engine_clk);
	clk_disable_unprepare(dsi->digital_clk);

	phy_power_off(dsi->phy);
}

static void mtk_output_dsi_enable(struct mtk_dsi *dsi)
{
	int ret;

	if (dsi->enabled)
		return;

	ret = mtk_dsi_poweron(dsi);
	if (ret < 0) {
		DRM_ERROR("failed to power on dsi\n");
		return;
	}

752 753
	mtk_dsi_set_mode(dsi);
	mtk_dsi_clk_hs_mode(dsi, 1);
C
CK Hu 已提交
754 755 756

	mtk_dsi_start(dsi);

757 758 759 760 761 762 763
	if (dsi->panel) {
		if (drm_panel_enable(dsi->panel)) {
			DRM_ERROR("failed to enable the panel\n");
			goto err_dsi_power_off;
		}
	}

C
CK Hu 已提交
764
	dsi->enabled = true;
765 766 767 768 769

	return;
err_dsi_power_off:
	mtk_dsi_stop(dsi);
	mtk_dsi_poweroff(dsi);
C
CK Hu 已提交
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
}

static void mtk_output_dsi_disable(struct mtk_dsi *dsi)
{
	if (!dsi->enabled)
		return;

	if (dsi->panel) {
		if (drm_panel_disable(dsi->panel)) {
			DRM_ERROR("failed to disable the panel\n");
			return;
		}
	}

	mtk_dsi_poweroff(dsi);

	dsi->enabled = false;
}

789
static void mtk_dsi_encoder_destroy(struct drm_encoder *encoder)
C
CK Hu 已提交
790
{
791
	drm_encoder_cleanup(encoder);
C
CK Hu 已提交
792 793
}

794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
static const struct drm_encoder_funcs mtk_dsi_encoder_funcs = {
	.destroy = mtk_dsi_encoder_destroy,
};

static int mtk_dsi_create_conn_enc(struct drm_device *drm, struct mtk_dsi *dsi);
static void mtk_dsi_destroy_conn_enc(struct mtk_dsi *dsi);

static int mtk_dsi_bridge_attach(struct drm_bridge *bridge,
				 enum drm_bridge_attach_flags flags)
{
	struct mtk_dsi *dsi = bridge_to_dsi(bridge);

	return mtk_dsi_create_conn_enc(bridge->dev, dsi);
}

static void mtk_dsi_bridge_detach(struct drm_bridge *bridge)
C
CK Hu 已提交
810
{
811 812 813 814 815 816 817 818 819 820
	struct mtk_dsi *dsi = bridge_to_dsi(bridge);

	mtk_dsi_destroy_conn_enc(dsi);
}

static void mtk_dsi_bridge_mode_set(struct drm_bridge *bridge,
				    const struct drm_display_mode *mode,
				    const struct drm_display_mode *adjusted)
{
	struct mtk_dsi *dsi = bridge_to_dsi(bridge);
C
CK Hu 已提交
821

822
	drm_display_mode_to_videomode(adjusted, &dsi->vm);
C
CK Hu 已提交
823 824
}

825
static void mtk_dsi_bridge_disable(struct drm_bridge *bridge)
C
CK Hu 已提交
826
{
827
	struct mtk_dsi *dsi = bridge_to_dsi(bridge);
C
CK Hu 已提交
828 829 830 831

	mtk_output_dsi_disable(dsi);
}

832
static void mtk_dsi_bridge_enable(struct drm_bridge *bridge)
C
CK Hu 已提交
833
{
834
	struct mtk_dsi *dsi = bridge_to_dsi(bridge);
C
CK Hu 已提交
835 836 837 838 839 840 841 842

	mtk_output_dsi_enable(dsi);
}

static int mtk_dsi_connector_get_modes(struct drm_connector *connector)
{
	struct mtk_dsi *dsi = connector_to_dsi(connector);

843
	return drm_panel_get_modes(dsi->panel, connector);
C
CK Hu 已提交
844 845
}

846 847 848 849 850 851
static const struct drm_bridge_funcs mtk_dsi_bridge_funcs = {
	.attach = mtk_dsi_bridge_attach,
	.detach = mtk_dsi_bridge_detach,
	.disable = mtk_dsi_bridge_disable,
	.enable = mtk_dsi_bridge_enable,
	.mode_set = mtk_dsi_bridge_mode_set,
C
CK Hu 已提交
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
};

static const struct drm_connector_funcs mtk_dsi_connector_funcs = {
	.fill_modes = drm_helper_probe_single_connector_modes,
	.destroy = drm_connector_cleanup,
	.reset = drm_atomic_helper_connector_reset,
	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
};

static const struct drm_connector_helper_funcs
	mtk_dsi_connector_helper_funcs = {
	.get_modes = mtk_dsi_connector_get_modes,
};

static int mtk_dsi_create_connector(struct drm_device *drm, struct mtk_dsi *dsi)
{
	int ret;

	ret = drm_connector_init(drm, &dsi->conn, &mtk_dsi_connector_funcs,
				 DRM_MODE_CONNECTOR_DSI);
	if (ret) {
		DRM_ERROR("Failed to connector init to drm\n");
		return ret;
	}

	drm_connector_helper_add(&dsi->conn, &mtk_dsi_connector_helper_funcs);

	dsi->conn.dpms = DRM_MODE_DPMS_OFF;
881
	drm_connector_attach_encoder(&dsi->conn, &dsi->encoder);
C
CK Hu 已提交
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901

	if (dsi->panel) {
		ret = drm_panel_attach(dsi->panel, &dsi->conn);
		if (ret) {
			DRM_ERROR("Failed to attach panel to drm\n");
			goto err_connector_cleanup;
		}
	}

	return 0;

err_connector_cleanup:
	drm_connector_cleanup(&dsi->conn);
	return ret;
}

static int mtk_dsi_create_conn_enc(struct drm_device *drm, struct mtk_dsi *dsi)
{
	int ret;

902 903 904 905
	/* If there's a next bridge, attach to it and let it create the connector */
	if (dsi->next_bridge) {
		ret = drm_bridge_attach(&dsi->encoder, dsi->next_bridge, NULL,
					0);
906 907 908 909 910
		if (ret) {
			DRM_ERROR("Failed to attach bridge to drm\n");
			goto err_encoder_cleanup;
		}
	} else {
C
CK Hu 已提交
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
		/* Otherwise create our own connector and attach to a panel */
		ret = mtk_dsi_create_connector(drm, dsi);
		if (ret)
			goto err_encoder_cleanup;
	}

	return 0;

err_encoder_cleanup:
	drm_encoder_cleanup(&dsi->encoder);
	return ret;
}

static void mtk_dsi_destroy_conn_enc(struct mtk_dsi *dsi)
{
	drm_encoder_cleanup(&dsi->encoder);
	/* Skip connector cleanup if creation was delegated to the bridge */
928
	if (dsi->conn.dev)
C
CK Hu 已提交
929
		drm_connector_cleanup(&dsi->conn);
930 931
	if (dsi->panel)
		drm_panel_detach(dsi->panel);
C
CK Hu 已提交
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
}

static void mtk_dsi_ddp_start(struct mtk_ddp_comp *comp)
{
	struct mtk_dsi *dsi = container_of(comp, struct mtk_dsi, ddp_comp);

	mtk_dsi_poweron(dsi);
}

static void mtk_dsi_ddp_stop(struct mtk_ddp_comp *comp)
{
	struct mtk_dsi *dsi = container_of(comp, struct mtk_dsi, ddp_comp);

	mtk_dsi_poweroff(dsi);
}

static const struct mtk_ddp_comp_funcs mtk_dsi_funcs = {
	.start = mtk_dsi_ddp_start,
	.stop = mtk_dsi_ddp_stop,
};

static int mtk_dsi_host_attach(struct mipi_dsi_host *host,
			       struct mipi_dsi_device *device)
{
	struct mtk_dsi *dsi = host_to_dsi(host);

	dsi->lanes = device->lanes;
	dsi->format = device->format;
	dsi->mode_flags = device->mode_flags;

	if (dsi->conn.dev)
		drm_helper_hpd_irq_event(dsi->conn.dev);

	return 0;
}

static int mtk_dsi_host_detach(struct mipi_dsi_host *host,
			       struct mipi_dsi_device *device)
{
	struct mtk_dsi *dsi = host_to_dsi(host);

	if (dsi->conn.dev)
		drm_helper_hpd_irq_event(dsi->conn.dev);

	return 0;
}

979 980
static void mtk_dsi_wait_for_idle(struct mtk_dsi *dsi)
{
D
Dan Carpenter 已提交
981 982
	int ret;
	u32 val;
983

D
Dan Carpenter 已提交
984 985 986
	ret = readl_poll_timeout(dsi->regs + DSI_INTSTA, val, !(val & DSI_BUSY),
				 4, 2000000);
	if (ret) {
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
		DRM_WARN("polling dsi wait not busy timeout!\n");

		mtk_dsi_enable(dsi);
		mtk_dsi_reset_engine(dsi);
	}
}

static u32 mtk_dsi_recv_cnt(u8 type, u8 *read_data)
{
	switch (type) {
	case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_1BYTE:
	case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_1BYTE:
		return 1;
	case MIPI_DSI_RX_GENERIC_SHORT_READ_RESPONSE_2BYTE:
	case MIPI_DSI_RX_DCS_SHORT_READ_RESPONSE_2BYTE:
		return 2;
	case MIPI_DSI_RX_GENERIC_LONG_READ_RESPONSE:
	case MIPI_DSI_RX_DCS_LONG_READ_RESPONSE:
		return read_data[1] + read_data[2] * 16;
	case MIPI_DSI_RX_ACKNOWLEDGE_AND_ERROR_REPORT:
		DRM_INFO("type is 0x02, try again\n");
		break;
	default:
1010
		DRM_INFO("type(0x%x) not recognized\n", type);
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
		break;
	}

	return 0;
}

static void mtk_dsi_cmdq(struct mtk_dsi *dsi, const struct mipi_dsi_msg *msg)
{
	const char *tx_buf = msg->tx_buf;
	u8 config, cmdq_size, cmdq_off, type = msg->type;
	u32 reg_val, cmdq_mask, i;
1022
	u32 reg_cmdq_off = dsi->driver_data->reg_cmdq_off;
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041

	if (MTK_DSI_HOST_IS_READ(type))
		config = BTA;
	else
		config = (msg->tx_len > 2) ? LONG_PACKET : SHORT_PACKET;

	if (msg->tx_len > 2) {
		cmdq_size = 1 + (msg->tx_len + 3) / 4;
		cmdq_off = 4;
		cmdq_mask = CONFIG | DATA_ID | DATA_0 | DATA_1;
		reg_val = (msg->tx_len << 16) | (type << 8) | config;
	} else {
		cmdq_size = 1;
		cmdq_off = 2;
		cmdq_mask = CONFIG | DATA_ID;
		reg_val = (type << 8) | config;
	}

	for (i = 0; i < msg->tx_len; i++)
1042 1043 1044
		mtk_dsi_mask(dsi, (reg_cmdq_off + cmdq_off + i) & (~0x3U),
			     (0xffUL << (((i + cmdq_off) & 3U) * 8U)),
			     tx_buf[i] << (((i + cmdq_off) & 3U) * 8U));
1045

1046
	mtk_dsi_mask(dsi, reg_cmdq_off, cmdq_mask, reg_val);
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
	mtk_dsi_mask(dsi, DSI_CMDQ_SIZE, CMDQ_SIZE, cmdq_size);
}

static ssize_t mtk_dsi_host_send_cmd(struct mtk_dsi *dsi,
				     const struct mipi_dsi_msg *msg, u8 flag)
{
	mtk_dsi_wait_for_idle(dsi);
	mtk_dsi_irq_data_clear(dsi, flag);
	mtk_dsi_cmdq(dsi, msg);
	mtk_dsi_start(dsi);

	if (!mtk_dsi_wait_for_irq_done(dsi, flag, 2000))
		return -ETIME;
	else
		return 0;
}

static ssize_t mtk_dsi_host_transfer(struct mipi_dsi_host *host,
				     const struct mipi_dsi_msg *msg)
{
	struct mtk_dsi *dsi = host_to_dsi(host);
	u32 recv_cnt, i;
	u8 read_data[16];
	void *src_addr;
	u8 irq_flag = CMD_DONE_INT_FLAG;

	if (readl(dsi->regs + DSI_MODE_CTRL) & MODE) {
		DRM_ERROR("dsi engine is not command mode\n");
		return -EINVAL;
	}

	if (MTK_DSI_HOST_IS_READ(msg->type))
		irq_flag |= LPRX_RD_RDY_INT_FLAG;

	if (mtk_dsi_host_send_cmd(dsi, msg, irq_flag) < 0)
		return -ETIME;

	if (!MTK_DSI_HOST_IS_READ(msg->type))
		return 0;

	if (!msg->rx_buf) {
		DRM_ERROR("dsi receive buffer size may be NULL\n");
		return -EINVAL;
	}

	for (i = 0; i < 16; i++)
		*(read_data + i) = readb(dsi->regs + DSI_RX_DATA0 + i);

	recv_cnt = mtk_dsi_recv_cnt(read_data[0], read_data);

	if (recv_cnt > 2)
		src_addr = &read_data[4];
	else
		src_addr = &read_data[1];

	if (recv_cnt > 10)
		recv_cnt = 10;

	if (recv_cnt > msg->rx_len)
		recv_cnt = msg->rx_len;

	if (recv_cnt)
		memcpy(msg->rx_buf, src_addr, recv_cnt);

	DRM_INFO("dsi get %d byte data from the panel address(0x%x)\n",
		 recv_cnt, *((u8 *)(msg->tx_buf)));

	return recv_cnt;
}

C
CK Hu 已提交
1117 1118 1119
static const struct mipi_dsi_host_ops mtk_dsi_ops = {
	.attach = mtk_dsi_host_attach,
	.detach = mtk_dsi_host_detach,
1120
	.transfer = mtk_dsi_host_transfer,
C
CK Hu 已提交
1121 1122
};

1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150
static int mtk_dsi_encoder_init(struct drm_device *drm, struct mtk_dsi *dsi)
{
	int ret;

	ret = drm_encoder_init(drm, &dsi->encoder, &mtk_dsi_encoder_funcs,
			       DRM_MODE_ENCODER_DSI, NULL);
	if (ret) {
		DRM_ERROR("Failed to encoder init to drm\n");
		return ret;
	}

	/*
	 * Currently display data paths are statically assigned to a crtc each.
	 * crtc 0 is OVL0 -> COLOR0 -> AAL -> OD -> RDMA0 -> UFOE -> DSI0
	 */
	dsi->encoder.possible_crtcs = 1;

	ret = drm_bridge_attach(&dsi->encoder, &dsi->bridge, NULL, 0);
	if (ret)
		goto err_cleanup_encoder;

	return 0;

err_cleanup_encoder:
	drm_encoder_cleanup(&dsi->encoder);
	return ret;
}

C
CK Hu 已提交
1151 1152 1153 1154 1155 1156 1157 1158
static int mtk_dsi_bind(struct device *dev, struct device *master, void *data)
{
	int ret;
	struct drm_device *drm = data;
	struct mtk_dsi *dsi = dev_get_drvdata(dev);

	ret = mtk_ddp_comp_register(drm, &dsi->ddp_comp);
	if (ret < 0) {
1159 1160
		dev_err(dev, "Failed to register component %pOF: %d\n",
			dev->of_node, ret);
C
CK Hu 已提交
1161 1162 1163
		return ret;
	}

1164 1165
	ret = mtk_dsi_encoder_init(drm, dsi);
	if (ret)
C
CK Hu 已提交
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
		goto err_unregister;

	return 0;

err_unregister:
	mtk_ddp_comp_unregister(drm, &dsi->ddp_comp);
	return ret;
}

static void mtk_dsi_unbind(struct device *dev, struct device *master,
			   void *data)
{
	struct drm_device *drm = data;
	struct mtk_dsi *dsi = dev_get_drvdata(dev);

1181
	drm_encoder_cleanup(&dsi->encoder);
C
CK Hu 已提交
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
	mtk_ddp_comp_unregister(drm, &dsi->ddp_comp);
}

static const struct component_ops mtk_dsi_component_ops = {
	.bind = mtk_dsi_bind,
	.unbind = mtk_dsi_unbind,
};

static int mtk_dsi_probe(struct platform_device *pdev)
{
	struct mtk_dsi *dsi;
	struct device *dev = &pdev->dev;
	struct resource *regs;
1195
	int irq_num;
C
CK Hu 已提交
1196 1197 1198 1199 1200 1201 1202 1203 1204
	int comp_id;
	int ret;

	dsi = devm_kzalloc(dev, sizeof(*dsi), GFP_KERNEL);
	if (!dsi)
		return -ENOMEM;

	dsi->host.ops = &mtk_dsi_ops;
	dsi->host.dev = dev;
1205 1206 1207 1208 1209
	ret = mipi_dsi_host_register(&dsi->host);
	if (ret < 0) {
		dev_err(dev, "failed to register DSI host: %d\n", ret);
		return ret;
	}
C
CK Hu 已提交
1210

1211
	ret = drm_of_find_panel_or_bridge(dev->of_node, 0, 0,
1212
					  &dsi->panel, &dsi->next_bridge);
1213
	if (ret)
1214
		goto err_unregister_host;
C
CK Hu 已提交
1215

1216
	dsi->driver_data = of_device_get_match_data(dev);
C
CK Hu 已提交
1217 1218 1219 1220

	dsi->engine_clk = devm_clk_get(dev, "engine");
	if (IS_ERR(dsi->engine_clk)) {
		ret = PTR_ERR(dsi->engine_clk);
1221 1222 1223

		if (ret != -EPROBE_DEFER)
			dev_err(dev, "Failed to get engine clock: %d\n", ret);
1224
		goto err_unregister_host;
C
CK Hu 已提交
1225 1226 1227 1228 1229
	}

	dsi->digital_clk = devm_clk_get(dev, "digital");
	if (IS_ERR(dsi->digital_clk)) {
		ret = PTR_ERR(dsi->digital_clk);
1230 1231 1232

		if (ret != -EPROBE_DEFER)
			dev_err(dev, "Failed to get digital clock: %d\n", ret);
1233
		goto err_unregister_host;
C
CK Hu 已提交
1234 1235 1236 1237 1238 1239
	}

	dsi->hs_clk = devm_clk_get(dev, "hs");
	if (IS_ERR(dsi->hs_clk)) {
		ret = PTR_ERR(dsi->hs_clk);
		dev_err(dev, "Failed to get hs clock: %d\n", ret);
1240
		goto err_unregister_host;
C
CK Hu 已提交
1241 1242 1243 1244 1245 1246 1247
	}

	regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	dsi->regs = devm_ioremap_resource(dev, regs);
	if (IS_ERR(dsi->regs)) {
		ret = PTR_ERR(dsi->regs);
		dev_err(dev, "Failed to ioremap memory: %d\n", ret);
1248
		goto err_unregister_host;
C
CK Hu 已提交
1249 1250 1251 1252 1253 1254
	}

	dsi->phy = devm_phy_get(dev, "dphy");
	if (IS_ERR(dsi->phy)) {
		ret = PTR_ERR(dsi->phy);
		dev_err(dev, "Failed to get MIPI-DPHY: %d\n", ret);
1255
		goto err_unregister_host;
C
CK Hu 已提交
1256 1257 1258 1259 1260
	}

	comp_id = mtk_ddp_comp_get_id(dev->of_node, MTK_DSI);
	if (comp_id < 0) {
		dev_err(dev, "Failed to identify by alias: %d\n", comp_id);
1261 1262
		ret = comp_id;
		goto err_unregister_host;
C
CK Hu 已提交
1263 1264 1265 1266 1267 1268
	}

	ret = mtk_ddp_comp_init(dev, dev->of_node, &dsi->ddp_comp, comp_id,
				&mtk_dsi_funcs);
	if (ret) {
		dev_err(dev, "Failed to initialize component: %d\n", ret);
1269
		goto err_unregister_host;
C
CK Hu 已提交
1270 1271
	}

1272 1273
	irq_num = platform_get_irq(pdev, 0);
	if (irq_num < 0) {
1274 1275 1276
		dev_err(&pdev->dev, "failed to get dsi irq_num: %d\n", irq_num);
		ret = irq_num;
		goto err_unregister_host;
1277 1278 1279 1280 1281 1282 1283
	}

	irq_set_status_flags(irq_num, IRQ_TYPE_LEVEL_LOW);
	ret = devm_request_irq(&pdev->dev, irq_num, mtk_dsi_irq,
			       IRQF_TRIGGER_LOW, dev_name(&pdev->dev), dsi);
	if (ret) {
		dev_err(&pdev->dev, "failed to request mediatek dsi irq\n");
1284
		goto err_unregister_host;
1285 1286 1287 1288
	}

	init_waitqueue_head(&dsi->irq_wait_queue);

C
CK Hu 已提交
1289 1290
	platform_set_drvdata(pdev, dsi);

1291 1292 1293 1294 1295 1296
	dsi->bridge.funcs = &mtk_dsi_bridge_funcs;
	dsi->bridge.of_node = dev->of_node;
	dsi->bridge.type = DRM_MODE_CONNECTOR_DSI;

	drm_bridge_add(&dsi->bridge);

1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
	ret = component_add(&pdev->dev, &mtk_dsi_component_ops);
	if (ret) {
		dev_err(&pdev->dev, "failed to add component: %d\n", ret);
		goto err_unregister_host;
	}

	return 0;

err_unregister_host:
	mipi_dsi_host_unregister(&dsi->host);
	return ret;
C
CK Hu 已提交
1308 1309 1310 1311 1312 1313 1314
}

static int mtk_dsi_remove(struct platform_device *pdev)
{
	struct mtk_dsi *dsi = platform_get_drvdata(pdev);

	mtk_output_dsi_disable(dsi);
1315
	drm_bridge_remove(&dsi->bridge);
C
CK Hu 已提交
1316
	component_del(&pdev->dev, &mtk_dsi_component_ops);
1317
	mipi_dsi_host_unregister(&dsi->host);
C
CK Hu 已提交
1318 1319 1320 1321

	return 0;
}

1322 1323 1324 1325 1326 1327 1328 1329
static const struct mtk_dsi_driver_data mt8173_dsi_driver_data = {
	.reg_cmdq_off = 0x200,
};

static const struct mtk_dsi_driver_data mt2701_dsi_driver_data = {
	.reg_cmdq_off = 0x180,
};

1330 1331 1332 1333 1334 1335
static const struct mtk_dsi_driver_data mt8183_dsi_driver_data = {
	.reg_cmdq_off = 0x200,
	.has_shadow_ctl = true,
	.has_size_ctl = true,
};

C
CK Hu 已提交
1336
static const struct of_device_id mtk_dsi_of_match[] = {
1337 1338 1339 1340
	{ .compatible = "mediatek,mt2701-dsi",
	  .data = &mt2701_dsi_driver_data },
	{ .compatible = "mediatek,mt8173-dsi",
	  .data = &mt8173_dsi_driver_data },
1341 1342
	{ .compatible = "mediatek,mt8183-dsi",
	  .data = &mt8183_dsi_driver_data },
C
CK Hu 已提交
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353
	{ },
};

struct platform_driver mtk_dsi_driver = {
	.probe = mtk_dsi_probe,
	.remove = mtk_dsi_remove,
	.driver = {
		.name = "mtk-dsi",
		.of_match_table = mtk_dsi_of_match,
	},
};