Linux Audio

Check our new training course

Loading...
v6.2
   1/*
   2 * Copyright © 2013 Intel Corporation
   3 *
   4 * Permission is hereby granted, free of charge, to any person obtaining a
   5 * copy of this software and associated documentation files (the "Software"),
   6 * to deal in the Software without restriction, including without limitation
   7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
   8 * and/or sell copies of the Software, and to permit persons to whom the
   9 * Software is furnished to do so, subject to the following conditions:
  10 *
  11 * The above copyright notice and this permission notice (including the next
  12 * paragraph) shall be included in all copies or substantial portions of the
  13 * Software.
  14 *
  15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21 * DEALINGS IN THE SOFTWARE.
  22 *
  23 * Author: Jani Nikula <jani.nikula@intel.com>
  24 */
  25
 
  26#include <linux/slab.h>
  27
  28#include <drm/drm_atomic_helper.h>
  29#include <drm/drm_crtc.h>
  30#include <drm/drm_edid.h>
  31#include <drm/drm_mipi_dsi.h>
  32
  33#include "i915_drv.h"
  34#include "i915_reg.h"
  35#include "intel_atomic.h"
  36#include "intel_backlight.h"
  37#include "intel_connector.h"
  38#include "intel_crtc.h"
  39#include "intel_de.h"
  40#include "intel_display_types.h"
  41#include "intel_dsi.h"
  42#include "intel_dsi_vbt.h"
  43#include "intel_fifo_underrun.h"
  44#include "intel_panel.h"
  45#include "skl_scaler.h"
  46#include "vlv_dsi.h"
  47#include "vlv_dsi_pll.h"
  48#include "vlv_dsi_regs.h"
  49#include "vlv_sideband.h"
  50
  51/* return pixels in terms of txbyteclkhs */
  52static u16 txbyteclkhs(u16 pixels, int bpp, int lane_count,
  53		       u16 burst_mode_ratio)
  54{
  55	return DIV_ROUND_UP(DIV_ROUND_UP(pixels * bpp * burst_mode_ratio,
  56					 8 * 100), lane_count);
  57}
  58
  59/* return pixels equvalent to txbyteclkhs */
  60static u16 pixels_from_txbyteclkhs(u16 clk_hs, int bpp, int lane_count,
  61			u16 burst_mode_ratio)
  62{
  63	return DIV_ROUND_UP((clk_hs * lane_count * 8 * 100),
  64						(bpp * burst_mode_ratio));
  65}
  66
  67enum mipi_dsi_pixel_format pixel_format_from_register_bits(u32 fmt)
  68{
  69	/* It just so happens the VBT matches register contents. */
  70	switch (fmt) {
  71	case VID_MODE_FORMAT_RGB888:
  72		return MIPI_DSI_FMT_RGB888;
  73	case VID_MODE_FORMAT_RGB666:
  74		return MIPI_DSI_FMT_RGB666;
  75	case VID_MODE_FORMAT_RGB666_PACKED:
  76		return MIPI_DSI_FMT_RGB666_PACKED;
  77	case VID_MODE_FORMAT_RGB565:
  78		return MIPI_DSI_FMT_RGB565;
  79	default:
  80		MISSING_CASE(fmt);
  81		return MIPI_DSI_FMT_RGB666;
  82	}
  83}
  84
  85void vlv_dsi_wait_for_fifo_empty(struct intel_dsi *intel_dsi, enum port port)
  86{
  87	struct drm_encoder *encoder = &intel_dsi->base.base;
  88	struct drm_device *dev = encoder->dev;
  89	struct drm_i915_private *dev_priv = to_i915(dev);
  90	u32 mask;
  91
  92	mask = LP_CTRL_FIFO_EMPTY | HS_CTRL_FIFO_EMPTY |
  93		LP_DATA_FIFO_EMPTY | HS_DATA_FIFO_EMPTY;
  94
  95	if (intel_de_wait_for_set(dev_priv, MIPI_GEN_FIFO_STAT(port),
  96				  mask, 100))
  97		drm_err(&dev_priv->drm, "DPI FIFOs are not empty\n");
  98}
  99
 100static void write_data(struct drm_i915_private *dev_priv,
 101		       i915_reg_t reg,
 102		       const u8 *data, u32 len)
 103{
 104	u32 i, j;
 105
 106	for (i = 0; i < len; i += 4) {
 107		u32 val = 0;
 108
 109		for (j = 0; j < min_t(u32, len - i, 4); j++)
 110			val |= *data++ << 8 * j;
 111
 112		intel_de_write(dev_priv, reg, val);
 113	}
 114}
 115
 116static void read_data(struct drm_i915_private *dev_priv,
 117		      i915_reg_t reg,
 118		      u8 *data, u32 len)
 119{
 120	u32 i, j;
 121
 122	for (i = 0; i < len; i += 4) {
 123		u32 val = intel_de_read(dev_priv, reg);
 124
 125		for (j = 0; j < min_t(u32, len - i, 4); j++)
 126			*data++ = val >> 8 * j;
 127	}
 128}
 129
 130static ssize_t intel_dsi_host_transfer(struct mipi_dsi_host *host,
 131				       const struct mipi_dsi_msg *msg)
 132{
 133	struct intel_dsi_host *intel_dsi_host = to_intel_dsi_host(host);
 134	struct drm_device *dev = intel_dsi_host->intel_dsi->base.base.dev;
 135	struct drm_i915_private *dev_priv = to_i915(dev);
 136	enum port port = intel_dsi_host->port;
 137	struct mipi_dsi_packet packet;
 138	ssize_t ret;
 139	const u8 *header, *data;
 140	i915_reg_t data_reg, ctrl_reg;
 141	u32 data_mask, ctrl_mask;
 142
 143	ret = mipi_dsi_create_packet(&packet, msg);
 144	if (ret < 0)
 145		return ret;
 146
 147	header = packet.header;
 148	data = packet.payload;
 149
 150	if (msg->flags & MIPI_DSI_MSG_USE_LPM) {
 151		data_reg = MIPI_LP_GEN_DATA(port);
 152		data_mask = LP_DATA_FIFO_FULL;
 153		ctrl_reg = MIPI_LP_GEN_CTRL(port);
 154		ctrl_mask = LP_CTRL_FIFO_FULL;
 155	} else {
 156		data_reg = MIPI_HS_GEN_DATA(port);
 157		data_mask = HS_DATA_FIFO_FULL;
 158		ctrl_reg = MIPI_HS_GEN_CTRL(port);
 159		ctrl_mask = HS_CTRL_FIFO_FULL;
 160	}
 161
 162	/* note: this is never true for reads */
 163	if (packet.payload_length) {
 164		if (intel_de_wait_for_clear(dev_priv, MIPI_GEN_FIFO_STAT(port),
 165					    data_mask, 50))
 166			drm_err(&dev_priv->drm,
 167				"Timeout waiting for HS/LP DATA FIFO !full\n");
 168
 169		write_data(dev_priv, data_reg, packet.payload,
 170			   packet.payload_length);
 171	}
 172
 173	if (msg->rx_len) {
 174		intel_de_write(dev_priv, MIPI_INTR_STAT(port),
 175			       GEN_READ_DATA_AVAIL);
 176	}
 177
 178	if (intel_de_wait_for_clear(dev_priv, MIPI_GEN_FIFO_STAT(port),
 179				    ctrl_mask, 50)) {
 180		drm_err(&dev_priv->drm,
 181			"Timeout waiting for HS/LP CTRL FIFO !full\n");
 182	}
 183
 184	intel_de_write(dev_priv, ctrl_reg,
 185		       header[2] << 16 | header[1] << 8 | header[0]);
 186
 187	/* ->rx_len is set only for reads */
 188	if (msg->rx_len) {
 189		data_mask = GEN_READ_DATA_AVAIL;
 190		if (intel_de_wait_for_set(dev_priv, MIPI_INTR_STAT(port),
 191					  data_mask, 50))
 192			drm_err(&dev_priv->drm,
 193				"Timeout waiting for read data.\n");
 194
 195		read_data(dev_priv, data_reg, msg->rx_buf, msg->rx_len);
 196	}
 197
 198	/* XXX: fix for reads and writes */
 199	return 4 + packet.payload_length;
 200}
 201
 202static int intel_dsi_host_attach(struct mipi_dsi_host *host,
 203				 struct mipi_dsi_device *dsi)
 204{
 205	return 0;
 206}
 207
 208static int intel_dsi_host_detach(struct mipi_dsi_host *host,
 209				 struct mipi_dsi_device *dsi)
 210{
 211	return 0;
 212}
 213
 214static const struct mipi_dsi_host_ops intel_dsi_host_ops = {
 215	.attach = intel_dsi_host_attach,
 216	.detach = intel_dsi_host_detach,
 217	.transfer = intel_dsi_host_transfer,
 218};
 219
 220/*
 221 * send a video mode command
 222 *
 223 * XXX: commands with data in MIPI_DPI_DATA?
 224 */
 225static int dpi_send_cmd(struct intel_dsi *intel_dsi, u32 cmd, bool hs,
 226			enum port port)
 227{
 228	struct drm_encoder *encoder = &intel_dsi->base.base;
 229	struct drm_device *dev = encoder->dev;
 230	struct drm_i915_private *dev_priv = to_i915(dev);
 231	u32 mask;
 232
 233	/* XXX: pipe, hs */
 234	if (hs)
 235		cmd &= ~DPI_LP_MODE;
 236	else
 237		cmd |= DPI_LP_MODE;
 238
 239	/* clear bit */
 240	intel_de_write(dev_priv, MIPI_INTR_STAT(port), SPL_PKT_SENT_INTERRUPT);
 241
 242	/* XXX: old code skips write if control unchanged */
 243	if (cmd == intel_de_read(dev_priv, MIPI_DPI_CONTROL(port)))
 244		drm_dbg_kms(&dev_priv->drm,
 245			    "Same special packet %02x twice in a row.\n", cmd);
 246
 247	intel_de_write(dev_priv, MIPI_DPI_CONTROL(port), cmd);
 248
 249	mask = SPL_PKT_SENT_INTERRUPT;
 250	if (intel_de_wait_for_set(dev_priv, MIPI_INTR_STAT(port), mask, 100))
 251		drm_err(&dev_priv->drm,
 252			"Video mode command 0x%08x send failed.\n", cmd);
 253
 254	return 0;
 255}
 256
 257static void band_gap_reset(struct drm_i915_private *dev_priv)
 258{
 259	vlv_flisdsi_get(dev_priv);
 260
 261	vlv_flisdsi_write(dev_priv, 0x08, 0x0001);
 262	vlv_flisdsi_write(dev_priv, 0x0F, 0x0005);
 263	vlv_flisdsi_write(dev_priv, 0x0F, 0x0025);
 264	udelay(150);
 265	vlv_flisdsi_write(dev_priv, 0x0F, 0x0000);
 266	vlv_flisdsi_write(dev_priv, 0x08, 0x0000);
 267
 268	vlv_flisdsi_put(dev_priv);
 269}
 270
 271static int intel_dsi_compute_config(struct intel_encoder *encoder,
 272				    struct intel_crtc_state *pipe_config,
 273				    struct drm_connector_state *conn_state)
 274{
 275	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 276	struct intel_dsi *intel_dsi = container_of(encoder, struct intel_dsi,
 277						   base);
 278	struct intel_connector *intel_connector = intel_dsi->attached_connector;
 279	struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
 280	int ret;
 281
 282	drm_dbg_kms(&dev_priv->drm, "\n");
 
 283	pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
 284
 285	ret = intel_panel_compute_config(intel_connector, adjusted_mode);
 286	if (ret)
 287		return ret;
 288
 289	ret = intel_panel_fitting(pipe_config, conn_state);
 290	if (ret)
 291		return ret;
 292
 293	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
 294		return -EINVAL;
 295
 296	/* DSI uses short packets for sync events, so clear mode flags for DSI */
 297	adjusted_mode->flags = 0;
 298
 299	if (intel_dsi->pixel_format == MIPI_DSI_FMT_RGB888)
 300		pipe_config->pipe_bpp = 24;
 301	else
 302		pipe_config->pipe_bpp = 18;
 303
 304	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
 305		/* Enable Frame time stamp based scanline reporting */
 306		pipe_config->mode_flags |=
 307			I915_MODE_FLAG_GET_SCANLINE_FROM_TIMESTAMP;
 308
 309		/* Dual link goes to DSI transcoder A. */
 310		if (intel_dsi->ports == BIT(PORT_C))
 311			pipe_config->cpu_transcoder = TRANSCODER_DSI_C;
 312		else
 313			pipe_config->cpu_transcoder = TRANSCODER_DSI_A;
 314
 315		ret = bxt_dsi_pll_compute(encoder, pipe_config);
 316		if (ret)
 317			return -EINVAL;
 318	} else {
 319		ret = vlv_dsi_pll_compute(encoder, pipe_config);
 320		if (ret)
 321			return -EINVAL;
 322	}
 323
 324	pipe_config->clock_set = true;
 325
 326	return 0;
 327}
 328
 329static bool glk_dsi_enable_io(struct intel_encoder *encoder)
 330{
 331	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 332	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
 333	enum port port;
 334	u32 tmp;
 335	bool cold_boot = false;
 336
 337	/* Set the MIPI mode
 338	 * If MIPI_Mode is off, then writing to LP_Wake bit is not reflecting.
 339	 * Power ON MIPI IO first and then write into IO reset and LP wake bits
 340	 */
 341	for_each_dsi_port(port, intel_dsi->ports) {
 342		tmp = intel_de_read(dev_priv, MIPI_CTRL(port));
 343		intel_de_write(dev_priv, MIPI_CTRL(port),
 344			       tmp | GLK_MIPIIO_ENABLE);
 345	}
 346
 347	/* Put the IO into reset */
 348	tmp = intel_de_read(dev_priv, MIPI_CTRL(PORT_A));
 349	tmp &= ~GLK_MIPIIO_RESET_RELEASED;
 350	intel_de_write(dev_priv, MIPI_CTRL(PORT_A), tmp);
 351
 352	/* Program LP Wake */
 353	for_each_dsi_port(port, intel_dsi->ports) {
 354		tmp = intel_de_read(dev_priv, MIPI_CTRL(port));
 355		if (!(intel_de_read(dev_priv, MIPI_DEVICE_READY(port)) & DEVICE_READY))
 356			tmp &= ~GLK_LP_WAKE;
 357		else
 358			tmp |= GLK_LP_WAKE;
 359		intel_de_write(dev_priv, MIPI_CTRL(port), tmp);
 360	}
 361
 362	/* Wait for Pwr ACK */
 363	for_each_dsi_port(port, intel_dsi->ports) {
 364		if (intel_de_wait_for_set(dev_priv, MIPI_CTRL(port),
 365					  GLK_MIPIIO_PORT_POWERED, 20))
 366			drm_err(&dev_priv->drm, "MIPIO port is powergated\n");
 367	}
 368
 369	/* Check for cold boot scenario */
 370	for_each_dsi_port(port, intel_dsi->ports) {
 371		cold_boot |=
 372			!(intel_de_read(dev_priv, MIPI_DEVICE_READY(port)) & DEVICE_READY);
 373	}
 374
 375	return cold_boot;
 376}
 377
 378static void glk_dsi_device_ready(struct intel_encoder *encoder)
 379{
 380	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 381	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
 382	enum port port;
 383	u32 val;
 384
 385	/* Wait for MIPI PHY status bit to set */
 386	for_each_dsi_port(port, intel_dsi->ports) {
 387		if (intel_de_wait_for_set(dev_priv, MIPI_CTRL(port),
 388					  GLK_PHY_STATUS_PORT_READY, 20))
 389			drm_err(&dev_priv->drm, "PHY is not ON\n");
 390	}
 391
 392	/* Get IO out of reset */
 393	val = intel_de_read(dev_priv, MIPI_CTRL(PORT_A));
 394	intel_de_write(dev_priv, MIPI_CTRL(PORT_A),
 395		       val | GLK_MIPIIO_RESET_RELEASED);
 396
 397	/* Get IO out of Low power state*/
 398	for_each_dsi_port(port, intel_dsi->ports) {
 399		if (!(intel_de_read(dev_priv, MIPI_DEVICE_READY(port)) & DEVICE_READY)) {
 400			val = intel_de_read(dev_priv, MIPI_DEVICE_READY(port));
 401			val &= ~ULPS_STATE_MASK;
 402			val |= DEVICE_READY;
 403			intel_de_write(dev_priv, MIPI_DEVICE_READY(port), val);
 404			usleep_range(10, 15);
 405		} else {
 406			/* Enter ULPS */
 407			val = intel_de_read(dev_priv, MIPI_DEVICE_READY(port));
 408			val &= ~ULPS_STATE_MASK;
 409			val |= (ULPS_STATE_ENTER | DEVICE_READY);
 410			intel_de_write(dev_priv, MIPI_DEVICE_READY(port), val);
 411
 412			/* Wait for ULPS active */
 413			if (intel_de_wait_for_clear(dev_priv, MIPI_CTRL(port),
 414						    GLK_ULPS_NOT_ACTIVE, 20))
 415				drm_err(&dev_priv->drm, "ULPS not active\n");
 416
 417			/* Exit ULPS */
 418			val = intel_de_read(dev_priv, MIPI_DEVICE_READY(port));
 419			val &= ~ULPS_STATE_MASK;
 420			val |= (ULPS_STATE_EXIT | DEVICE_READY);
 421			intel_de_write(dev_priv, MIPI_DEVICE_READY(port), val);
 422
 423			/* Enter Normal Mode */
 424			val = intel_de_read(dev_priv, MIPI_DEVICE_READY(port));
 425			val &= ~ULPS_STATE_MASK;
 426			val |= (ULPS_STATE_NORMAL_OPERATION | DEVICE_READY);
 427			intel_de_write(dev_priv, MIPI_DEVICE_READY(port), val);
 428
 429			val = intel_de_read(dev_priv, MIPI_CTRL(port));
 430			val &= ~GLK_LP_WAKE;
 431			intel_de_write(dev_priv, MIPI_CTRL(port), val);
 432		}
 433	}
 434
 435	/* Wait for Stop state */
 436	for_each_dsi_port(port, intel_dsi->ports) {
 437		if (intel_de_wait_for_set(dev_priv, MIPI_CTRL(port),
 438					  GLK_DATA_LANE_STOP_STATE, 20))
 439			drm_err(&dev_priv->drm,
 440				"Date lane not in STOP state\n");
 441	}
 442
 443	/* Wait for AFE LATCH */
 444	for_each_dsi_port(port, intel_dsi->ports) {
 445		if (intel_de_wait_for_set(dev_priv, BXT_MIPI_PORT_CTRL(port),
 446					  AFE_LATCHOUT, 20))
 447			drm_err(&dev_priv->drm,
 448				"D-PHY not entering LP-11 state\n");
 449	}
 450}
 451
 452static void bxt_dsi_device_ready(struct intel_encoder *encoder)
 453{
 454	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 455	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
 456	enum port port;
 457	u32 val;
 458
 459	drm_dbg_kms(&dev_priv->drm, "\n");
 460
 461	/* Enable MIPI PHY transparent latch */
 462	for_each_dsi_port(port, intel_dsi->ports) {
 463		val = intel_de_read(dev_priv, BXT_MIPI_PORT_CTRL(port));
 464		intel_de_write(dev_priv, BXT_MIPI_PORT_CTRL(port),
 465			       val | LP_OUTPUT_HOLD);
 466		usleep_range(2000, 2500);
 467	}
 468
 469	/* Clear ULPS and set device ready */
 470	for_each_dsi_port(port, intel_dsi->ports) {
 471		val = intel_de_read(dev_priv, MIPI_DEVICE_READY(port));
 472		val &= ~ULPS_STATE_MASK;
 473		intel_de_write(dev_priv, MIPI_DEVICE_READY(port), val);
 474		usleep_range(2000, 2500);
 475		val |= DEVICE_READY;
 476		intel_de_write(dev_priv, MIPI_DEVICE_READY(port), val);
 477	}
 478}
 479
 480static void vlv_dsi_device_ready(struct intel_encoder *encoder)
 481{
 482	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 483	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
 484	enum port port;
 485	u32 val;
 486
 487	drm_dbg_kms(&dev_priv->drm, "\n");
 488
 489	vlv_flisdsi_get(dev_priv);
 490	/* program rcomp for compliance, reduce from 50 ohms to 45 ohms
 491	 * needed everytime after power gate */
 492	vlv_flisdsi_write(dev_priv, 0x04, 0x0004);
 493	vlv_flisdsi_put(dev_priv);
 494
 495	/* bandgap reset is needed after everytime we do power gate */
 496	band_gap_reset(dev_priv);
 497
 498	for_each_dsi_port(port, intel_dsi->ports) {
 499
 500		intel_de_write(dev_priv, MIPI_DEVICE_READY(port),
 501			       ULPS_STATE_ENTER);
 502		usleep_range(2500, 3000);
 503
 504		/* Enable MIPI PHY transparent latch
 505		 * Common bit for both MIPI Port A & MIPI Port C
 506		 * No similar bit in MIPI Port C reg
 507		 */
 508		val = intel_de_read(dev_priv, MIPI_PORT_CTRL(PORT_A));
 509		intel_de_write(dev_priv, MIPI_PORT_CTRL(PORT_A),
 510			       val | LP_OUTPUT_HOLD);
 511		usleep_range(1000, 1500);
 512
 513		intel_de_write(dev_priv, MIPI_DEVICE_READY(port),
 514			       ULPS_STATE_EXIT);
 515		usleep_range(2500, 3000);
 516
 517		intel_de_write(dev_priv, MIPI_DEVICE_READY(port),
 518			       DEVICE_READY);
 519		usleep_range(2500, 3000);
 520	}
 521}
 522
 523static void intel_dsi_device_ready(struct intel_encoder *encoder)
 524{
 525	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 526
 527	if (IS_GEMINILAKE(dev_priv))
 528		glk_dsi_device_ready(encoder);
 529	else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
 530		bxt_dsi_device_ready(encoder);
 531	else
 532		vlv_dsi_device_ready(encoder);
 533}
 534
 535static void glk_dsi_enter_low_power_mode(struct intel_encoder *encoder)
 536{
 537	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 538	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
 539	enum port port;
 540	u32 val;
 541
 542	/* Enter ULPS */
 543	for_each_dsi_port(port, intel_dsi->ports) {
 544		val = intel_de_read(dev_priv, MIPI_DEVICE_READY(port));
 545		val &= ~ULPS_STATE_MASK;
 546		val |= (ULPS_STATE_ENTER | DEVICE_READY);
 547		intel_de_write(dev_priv, MIPI_DEVICE_READY(port), val);
 548	}
 549
 550	/* Wait for MIPI PHY status bit to unset */
 551	for_each_dsi_port(port, intel_dsi->ports) {
 552		if (intel_de_wait_for_clear(dev_priv, MIPI_CTRL(port),
 553					    GLK_PHY_STATUS_PORT_READY, 20))
 554			drm_err(&dev_priv->drm, "PHY is not turning OFF\n");
 555	}
 556
 557	/* Wait for Pwr ACK bit to unset */
 558	for_each_dsi_port(port, intel_dsi->ports) {
 559		if (intel_de_wait_for_clear(dev_priv, MIPI_CTRL(port),
 560					    GLK_MIPIIO_PORT_POWERED, 20))
 561			drm_err(&dev_priv->drm,
 562				"MIPI IO Port is not powergated\n");
 563	}
 564}
 565
 566static void glk_dsi_disable_mipi_io(struct intel_encoder *encoder)
 567{
 568	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 569	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
 570	enum port port;
 571	u32 tmp;
 572
 573	/* Put the IO into reset */
 574	tmp = intel_de_read(dev_priv, MIPI_CTRL(PORT_A));
 575	tmp &= ~GLK_MIPIIO_RESET_RELEASED;
 576	intel_de_write(dev_priv, MIPI_CTRL(PORT_A), tmp);
 577
 578	/* Wait for MIPI PHY status bit to unset */
 579	for_each_dsi_port(port, intel_dsi->ports) {
 580		if (intel_de_wait_for_clear(dev_priv, MIPI_CTRL(port),
 581					    GLK_PHY_STATUS_PORT_READY, 20))
 582			drm_err(&dev_priv->drm, "PHY is not turning OFF\n");
 583	}
 584
 585	/* Clear MIPI mode */
 586	for_each_dsi_port(port, intel_dsi->ports) {
 587		tmp = intel_de_read(dev_priv, MIPI_CTRL(port));
 588		tmp &= ~GLK_MIPIIO_ENABLE;
 589		intel_de_write(dev_priv, MIPI_CTRL(port), tmp);
 590	}
 591}
 592
 593static void glk_dsi_clear_device_ready(struct intel_encoder *encoder)
 594{
 595	glk_dsi_enter_low_power_mode(encoder);
 596	glk_dsi_disable_mipi_io(encoder);
 597}
 598
 
 
 
 
 
 
 599static void vlv_dsi_clear_device_ready(struct intel_encoder *encoder)
 600{
 601	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 602	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
 603	enum port port;
 604
 605	drm_dbg_kms(&dev_priv->drm, "\n");
 606	for_each_dsi_port(port, intel_dsi->ports) {
 607		/* Common bit for both MIPI Port A & MIPI Port C on VLV/CHV */
 608		i915_reg_t port_ctrl = IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv) ?
 609			BXT_MIPI_PORT_CTRL(port) : MIPI_PORT_CTRL(PORT_A);
 610		u32 val;
 611
 612		intel_de_write(dev_priv, MIPI_DEVICE_READY(port),
 613			       DEVICE_READY | ULPS_STATE_ENTER);
 614		usleep_range(2000, 2500);
 615
 616		intel_de_write(dev_priv, MIPI_DEVICE_READY(port),
 617			       DEVICE_READY | ULPS_STATE_EXIT);
 618		usleep_range(2000, 2500);
 619
 620		intel_de_write(dev_priv, MIPI_DEVICE_READY(port),
 621			       DEVICE_READY | ULPS_STATE_ENTER);
 622		usleep_range(2000, 2500);
 623
 624		/*
 625		 * On VLV/CHV, wait till Clock lanes are in LP-00 state for MIPI
 626		 * Port A only. MIPI Port C has no similar bit for checking.
 627		 */
 628		if ((IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv) || port == PORT_A) &&
 629		    intel_de_wait_for_clear(dev_priv, port_ctrl,
 630					    AFE_LATCHOUT, 30))
 631			drm_err(&dev_priv->drm, "DSI LP not going Low\n");
 632
 633		/* Disable MIPI PHY transparent latch */
 634		val = intel_de_read(dev_priv, port_ctrl);
 635		intel_de_write(dev_priv, port_ctrl, val & ~LP_OUTPUT_HOLD);
 636		usleep_range(1000, 1500);
 637
 638		intel_de_write(dev_priv, MIPI_DEVICE_READY(port), 0x00);
 639		usleep_range(2000, 2500);
 640	}
 641}
 642
 643static void intel_dsi_port_enable(struct intel_encoder *encoder,
 644				  const struct intel_crtc_state *crtc_state)
 645{
 646	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 647	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
 648	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
 649	enum port port;
 650
 651	if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK) {
 652		u32 temp;
 
 653		if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
 654			for_each_dsi_port(port, intel_dsi->ports) {
 655				temp = intel_de_read(dev_priv,
 656						     MIPI_CTRL(port));
 657				temp &= ~BXT_PIXEL_OVERLAP_CNT_MASK |
 658					intel_dsi->pixel_overlap <<
 659					BXT_PIXEL_OVERLAP_CNT_SHIFT;
 660				intel_de_write(dev_priv, MIPI_CTRL(port),
 661					       temp);
 662			}
 663		} else {
 664			temp = intel_de_read(dev_priv, VLV_CHICKEN_3);
 665			temp &= ~PIXEL_OVERLAP_CNT_MASK |
 666					intel_dsi->pixel_overlap <<
 667					PIXEL_OVERLAP_CNT_SHIFT;
 668			intel_de_write(dev_priv, VLV_CHICKEN_3, temp);
 669		}
 670	}
 671
 672	for_each_dsi_port(port, intel_dsi->ports) {
 673		i915_reg_t port_ctrl = IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv) ?
 674			BXT_MIPI_PORT_CTRL(port) : MIPI_PORT_CTRL(port);
 675		u32 temp;
 676
 677		temp = intel_de_read(dev_priv, port_ctrl);
 678
 679		temp &= ~LANE_CONFIGURATION_MASK;
 680		temp &= ~DUAL_LINK_MODE_MASK;
 681
 682		if (intel_dsi->ports == (BIT(PORT_A) | BIT(PORT_C))) {
 683			temp |= (intel_dsi->dual_link - 1)
 684						<< DUAL_LINK_MODE_SHIFT;
 685			if (IS_BROXTON(dev_priv))
 686				temp |= LANE_CONFIGURATION_DUAL_LINK_A;
 687			else
 688				temp |= crtc->pipe ?
 689					LANE_CONFIGURATION_DUAL_LINK_B :
 690					LANE_CONFIGURATION_DUAL_LINK_A;
 691		}
 692
 693		if (intel_dsi->pixel_format != MIPI_DSI_FMT_RGB888)
 694			temp |= DITHERING_ENABLE;
 695
 696		/* assert ip_tg_enable signal */
 697		intel_de_write(dev_priv, port_ctrl, temp | DPI_ENABLE);
 698		intel_de_posting_read(dev_priv, port_ctrl);
 699	}
 700}
 701
 702static void intel_dsi_port_disable(struct intel_encoder *encoder)
 703{
 704	struct drm_device *dev = encoder->base.dev;
 705	struct drm_i915_private *dev_priv = to_i915(dev);
 706	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
 707	enum port port;
 708
 709	for_each_dsi_port(port, intel_dsi->ports) {
 710		i915_reg_t port_ctrl = IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv) ?
 711			BXT_MIPI_PORT_CTRL(port) : MIPI_PORT_CTRL(port);
 712		u32 temp;
 713
 714		/* de-assert ip_tg_enable signal */
 715		temp = intel_de_read(dev_priv, port_ctrl);
 716		intel_de_write(dev_priv, port_ctrl, temp & ~DPI_ENABLE);
 717		intel_de_posting_read(dev_priv, port_ctrl);
 718	}
 719}
 720
 721static void intel_dsi_wait_panel_power_cycle(struct intel_dsi *intel_dsi)
 722{
 723	ktime_t panel_power_on_time;
 724	s64 panel_power_off_duration;
 725
 726	panel_power_on_time = ktime_get_boottime();
 727	panel_power_off_duration = ktime_ms_delta(panel_power_on_time,
 728						  intel_dsi->panel_power_off_time);
 729
 730	if (panel_power_off_duration < (s64)intel_dsi->panel_pwr_cycle_delay)
 731		msleep(intel_dsi->panel_pwr_cycle_delay - panel_power_off_duration);
 732}
 733
 734static void intel_dsi_prepare(struct intel_encoder *intel_encoder,
 735			      const struct intel_crtc_state *pipe_config);
 736static void intel_dsi_unprepare(struct intel_encoder *encoder);
 737
 738/*
 739 * Panel enable/disable sequences from the VBT spec.
 740 *
 741 * Note the spec has AssertReset / DeassertReset swapped from their
 742 * usual naming. We use the normal names to avoid confusion (so below
 743 * they are swapped compared to the spec).
 744 *
 745 * Steps starting with MIPI refer to VBT sequences, note that for v2
 746 * VBTs several steps which have a VBT in v2 are expected to be handled
 747 * directly by the driver, by directly driving gpios for example.
 748 *
 749 * v2 video mode seq         v3 video mode seq         command mode seq
 750 * - power on                - MIPIPanelPowerOn        - power on
 751 * - wait t1+t2                                        - wait t1+t2
 752 * - MIPIDeassertResetPin    - MIPIDeassertResetPin    - MIPIDeassertResetPin
 753 * - io lines to lp-11       - io lines to lp-11       - io lines to lp-11
 754 * - MIPISendInitialDcsCmds  - MIPISendInitialDcsCmds  - MIPISendInitialDcsCmds
 755 *                                                     - MIPITearOn
 756 *                                                     - MIPIDisplayOn
 757 * - turn on DPI             - turn on DPI             - set pipe to dsr mode
 758 * - MIPIDisplayOn           - MIPIDisplayOn
 759 * - wait t5                                           - wait t5
 760 * - backlight on            - MIPIBacklightOn         - backlight on
 761 * ...                       ...                       ... issue mem cmds ...
 762 * - backlight off           - MIPIBacklightOff        - backlight off
 763 * - wait t6                                           - wait t6
 764 * - MIPIDisplayOff
 765 * - turn off DPI            - turn off DPI            - disable pipe dsr mode
 766 *                                                     - MIPITearOff
 767 *                           - MIPIDisplayOff          - MIPIDisplayOff
 768 * - io lines to lp-00       - io lines to lp-00       - io lines to lp-00
 769 * - MIPIAssertResetPin      - MIPIAssertResetPin      - MIPIAssertResetPin
 770 * - wait t3                                           - wait t3
 771 * - power off               - MIPIPanelPowerOff       - power off
 772 * - wait t4                                           - wait t4
 773 */
 774
 775/*
 776 * DSI port enable has to be done before pipe and plane enable, so we do it in
 777 * the pre_enable hook instead of the enable hook.
 778 */
 779static void intel_dsi_pre_enable(struct intel_atomic_state *state,
 780				 struct intel_encoder *encoder,
 781				 const struct intel_crtc_state *pipe_config,
 782				 const struct drm_connector_state *conn_state)
 783{
 784	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
 785	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
 786	struct intel_connector *connector = to_intel_connector(conn_state->connector);
 787	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
 788	enum pipe pipe = crtc->pipe;
 789	enum port port;
 790	u32 val;
 791	bool glk_cold_boot = false;
 792
 793	drm_dbg_kms(&dev_priv->drm, "\n");
 794
 795	intel_dsi_wait_panel_power_cycle(intel_dsi);
 796
 797	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
 798
 799	/*
 800	 * The BIOS may leave the PLL in a wonky state where it doesn't
 801	 * lock. It needs to be fully powered down to fix it.
 802	 */
 803	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
 804		bxt_dsi_pll_disable(encoder);
 805		bxt_dsi_pll_enable(encoder, pipe_config);
 806	} else {
 807		vlv_dsi_pll_disable(encoder);
 808		vlv_dsi_pll_enable(encoder, pipe_config);
 809	}
 810
 811	if (IS_BROXTON(dev_priv)) {
 812		/* Add MIPI IO reset programming for modeset */
 813		val = intel_de_read(dev_priv, BXT_P_CR_GT_DISP_PWRON);
 814		intel_de_write(dev_priv, BXT_P_CR_GT_DISP_PWRON,
 815			       val | MIPIO_RST_CTRL);
 816
 817		/* Power up DSI regulator */
 818		intel_de_write(dev_priv, BXT_P_DSI_REGULATOR_CFG, STAP_SELECT);
 819		intel_de_write(dev_priv, BXT_P_DSI_REGULATOR_TX_CTRL, 0);
 820	}
 821
 822	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
 823		u32 val;
 824
 825		/* Disable DPOunit clock gating, can stall pipe */
 826		val = intel_de_read(dev_priv, DSPCLK_GATE_D(dev_priv));
 827		val |= DPOUNIT_CLOCK_GATE_DISABLE;
 828		intel_de_write(dev_priv, DSPCLK_GATE_D(dev_priv), val);
 829	}
 830
 831	if (!IS_GEMINILAKE(dev_priv))
 832		intel_dsi_prepare(encoder, pipe_config);
 833
 
 834	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_POWER_ON);
 835
 836	/*
 837	 * Give the panel time to power-on and then deassert its reset.
 838	 * Depending on the VBT MIPI sequences version the deassert-seq
 839	 * may contain the necessary delay, intel_dsi_msleep() will skip
 840	 * the delay in that case. If there is no deassert-seq, then an
 841	 * unconditional msleep is used to give the panel time to power-on.
 842	 */
 843	if (connector->panel.vbt.dsi.sequence[MIPI_SEQ_DEASSERT_RESET]) {
 844		intel_dsi_msleep(intel_dsi, intel_dsi->panel_on_delay);
 845		intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DEASSERT_RESET);
 846	} else {
 847		msleep(intel_dsi->panel_on_delay);
 848	}
 849
 850	if (IS_GEMINILAKE(dev_priv)) {
 851		glk_cold_boot = glk_dsi_enable_io(encoder);
 852
 853		/* Prepare port in cold boot(s3/s4) scenario */
 854		if (glk_cold_boot)
 855			intel_dsi_prepare(encoder, pipe_config);
 856	}
 857
 858	/* Put device in ready state (LP-11) */
 859	intel_dsi_device_ready(encoder);
 860
 861	/* Prepare port in normal boot scenario */
 862	if (IS_GEMINILAKE(dev_priv) && !glk_cold_boot)
 863		intel_dsi_prepare(encoder, pipe_config);
 864
 865	/* Send initialization commands in LP mode */
 866	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_INIT_OTP);
 867
 868	/*
 869	 * Enable port in pre-enable phase itself because as per hw team
 870	 * recommendation, port should be enabled before plane & pipe
 871	 */
 872	if (is_cmd_mode(intel_dsi)) {
 873		for_each_dsi_port(port, intel_dsi->ports)
 874			intel_de_write(dev_priv,
 875				       MIPI_MAX_RETURN_PKT_SIZE(port), 8 * 4);
 876		intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_TEAR_ON);
 877		intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DISPLAY_ON);
 878	} else {
 879		msleep(20); /* XXX */
 880		for_each_dsi_port(port, intel_dsi->ports)
 881			dpi_send_cmd(intel_dsi, TURN_ON, false, port);
 882		intel_dsi_msleep(intel_dsi, 100);
 883
 884		intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DISPLAY_ON);
 885
 886		intel_dsi_port_enable(encoder, pipe_config);
 887	}
 888
 889	intel_backlight_enable(pipe_config, conn_state);
 890	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_BACKLIGHT_ON);
 891}
 892
 893static void bxt_dsi_enable(struct intel_atomic_state *state,
 894			   struct intel_encoder *encoder,
 895			   const struct intel_crtc_state *crtc_state,
 896			   const struct drm_connector_state *conn_state)
 897{
 898	drm_WARN_ON(state->base.dev, crtc_state->has_pch_encoder);
 899
 900	intel_crtc_vblank_on(crtc_state);
 901}
 902
 903/*
 904 * DSI port disable has to be done after pipe and plane disable, so we do it in
 905 * the post_disable hook.
 906 */
 907static void intel_dsi_disable(struct intel_atomic_state *state,
 908			      struct intel_encoder *encoder,
 909			      const struct intel_crtc_state *old_crtc_state,
 910			      const struct drm_connector_state *old_conn_state)
 911{
 912	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
 913	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
 914	enum port port;
 915
 916	drm_dbg_kms(&i915->drm, "\n");
 917
 918	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_BACKLIGHT_OFF);
 919	intel_backlight_disable(old_conn_state);
 920
 921	/*
 922	 * According to the spec we should send SHUTDOWN before
 923	 * MIPI_SEQ_DISPLAY_OFF only for v3+ VBTs, but field testing
 924	 * has shown that the v3 sequence works for v2 VBTs too
 925	 */
 926	if (is_vid_mode(intel_dsi)) {
 927		/* Send Shutdown command to the panel in LP mode */
 928		for_each_dsi_port(port, intel_dsi->ports)
 929			dpi_send_cmd(intel_dsi, SHUTDOWN, false, port);
 930		msleep(10);
 931	}
 932}
 933
 934static void intel_dsi_clear_device_ready(struct intel_encoder *encoder)
 935{
 936	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 937
 938	if (IS_GEMINILAKE(dev_priv))
 939		glk_dsi_clear_device_ready(encoder);
 940	else
 941		vlv_dsi_clear_device_ready(encoder);
 942}
 943
 944static void intel_dsi_post_disable(struct intel_atomic_state *state,
 945				   struct intel_encoder *encoder,
 946				   const struct intel_crtc_state *old_crtc_state,
 947				   const struct drm_connector_state *old_conn_state)
 948{
 949	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 950	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
 951	enum port port;
 952	u32 val;
 953
 954	drm_dbg_kms(&dev_priv->drm, "\n");
 955
 956	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
 957		intel_crtc_vblank_off(old_crtc_state);
 958
 959		skl_scaler_disable(old_crtc_state);
 960	}
 961
 962	if (is_vid_mode(intel_dsi)) {
 963		for_each_dsi_port(port, intel_dsi->ports)
 964			vlv_dsi_wait_for_fifo_empty(intel_dsi, port);
 965
 966		intel_dsi_port_disable(encoder);
 967		usleep_range(2000, 5000);
 968	}
 969
 970	intel_dsi_unprepare(encoder);
 971
 972	/*
 973	 * if disable packets are sent before sending shutdown packet then in
 974	 * some next enable sequence send turn on packet error is observed
 975	 */
 976	if (is_cmd_mode(intel_dsi))
 977		intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_TEAR_OFF);
 978	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DISPLAY_OFF);
 979
 980	/* Transition to LP-00 */
 981	intel_dsi_clear_device_ready(encoder);
 982
 983	if (IS_BROXTON(dev_priv)) {
 984		/* Power down DSI regulator to save power */
 985		intel_de_write(dev_priv, BXT_P_DSI_REGULATOR_CFG, STAP_SELECT);
 986		intel_de_write(dev_priv, BXT_P_DSI_REGULATOR_TX_CTRL,
 987			       HS_IO_CTRL_SELECT);
 988
 989		/* Add MIPI IO reset programming for modeset */
 990		val = intel_de_read(dev_priv, BXT_P_CR_GT_DISP_PWRON);
 991		intel_de_write(dev_priv, BXT_P_CR_GT_DISP_PWRON,
 992			       val & ~MIPIO_RST_CTRL);
 993	}
 994
 995	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
 996		bxt_dsi_pll_disable(encoder);
 997	} else {
 998		u32 val;
 999
1000		vlv_dsi_pll_disable(encoder);
1001
1002		val = intel_de_read(dev_priv, DSPCLK_GATE_D(dev_priv));
1003		val &= ~DPOUNIT_CLOCK_GATE_DISABLE;
1004		intel_de_write(dev_priv, DSPCLK_GATE_D(dev_priv), val);
1005	}
1006
1007	/* Assert reset */
1008	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_ASSERT_RESET);
1009
1010	intel_dsi_msleep(intel_dsi, intel_dsi->panel_off_delay);
1011	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_POWER_OFF);
1012
1013	intel_dsi->panel_power_off_time = ktime_get_boottime();
1014}
1015
1016static void intel_dsi_shutdown(struct intel_encoder *encoder)
1017{
1018	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1019
1020	intel_dsi_wait_panel_power_cycle(intel_dsi);
1021}
1022
1023static bool intel_dsi_get_hw_state(struct intel_encoder *encoder,
1024				   enum pipe *pipe)
1025{
1026	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1027	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1028	intel_wakeref_t wakeref;
1029	enum port port;
1030	bool active = false;
1031
1032	drm_dbg_kms(&dev_priv->drm, "\n");
1033
1034	wakeref = intel_display_power_get_if_enabled(dev_priv,
1035						     encoder->power_domain);
1036	if (!wakeref)
1037		return false;
1038
1039	/*
1040	 * On Broxton the PLL needs to be enabled with a valid divider
1041	 * configuration, otherwise accessing DSI registers will hang the
1042	 * machine. See BSpec North Display Engine registers/MIPI[BXT].
1043	 */
1044	if ((IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) &&
1045	    !bxt_dsi_pll_is_enabled(dev_priv))
1046		goto out_put_power;
1047
1048	/* XXX: this only works for one DSI output */
1049	for_each_dsi_port(port, intel_dsi->ports) {
1050		i915_reg_t ctrl_reg = IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv) ?
1051			BXT_MIPI_PORT_CTRL(port) : MIPI_PORT_CTRL(port);
1052		bool enabled = intel_de_read(dev_priv, ctrl_reg) & DPI_ENABLE;
1053
1054		/*
1055		 * Due to some hardware limitations on VLV/CHV, the DPI enable
1056		 * bit in port C control register does not get set. As a
1057		 * workaround, check pipe B conf instead.
1058		 */
1059		if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
1060		    port == PORT_C)
1061			enabled = intel_de_read(dev_priv, PIPECONF(PIPE_B)) & PIPECONF_ENABLE;
1062
1063		/* Try command mode if video mode not enabled */
1064		if (!enabled) {
1065			u32 tmp = intel_de_read(dev_priv,
1066						MIPI_DSI_FUNC_PRG(port));
1067			enabled = tmp & CMD_MODE_DATA_WIDTH_MASK;
1068		}
1069
1070		if (!enabled)
1071			continue;
1072
1073		if (!(intel_de_read(dev_priv, MIPI_DEVICE_READY(port)) & DEVICE_READY))
1074			continue;
1075
1076		if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
1077			u32 tmp = intel_de_read(dev_priv, MIPI_CTRL(port));
1078			tmp &= BXT_PIPE_SELECT_MASK;
1079			tmp >>= BXT_PIPE_SELECT_SHIFT;
1080
1081			if (drm_WARN_ON(&dev_priv->drm, tmp > PIPE_C))
1082				continue;
1083
1084			*pipe = tmp;
1085		} else {
1086			*pipe = port == PORT_A ? PIPE_A : PIPE_B;
1087		}
1088
1089		active = true;
1090		break;
1091	}
1092
1093out_put_power:
1094	intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
1095
1096	return active;
1097}
1098
1099static void bxt_dsi_get_pipe_config(struct intel_encoder *encoder,
1100				    struct intel_crtc_state *pipe_config)
1101{
1102	struct drm_device *dev = encoder->base.dev;
1103	struct drm_i915_private *dev_priv = to_i915(dev);
1104	struct drm_display_mode *adjusted_mode =
1105					&pipe_config->hw.adjusted_mode;
1106	struct drm_display_mode *adjusted_mode_sw;
1107	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
1108	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1109	unsigned int lane_count = intel_dsi->lane_count;
1110	unsigned int bpp, fmt;
1111	enum port port;
1112	u16 hactive, hfp, hsync, hbp, vfp, vsync, vbp;
1113	u16 hfp_sw, hsync_sw, hbp_sw;
1114	u16 crtc_htotal_sw, crtc_hsync_start_sw, crtc_hsync_end_sw,
1115				crtc_hblank_start_sw, crtc_hblank_end_sw;
1116
1117	/* FIXME: hw readout should not depend on SW state */
1118	adjusted_mode_sw = &crtc->config->hw.adjusted_mode;
1119
1120	/*
1121	 * Atleast one port is active as encoder->get_config called only if
1122	 * encoder->get_hw_state() returns true.
1123	 */
1124	for_each_dsi_port(port, intel_dsi->ports) {
1125		if (intel_de_read(dev_priv, BXT_MIPI_PORT_CTRL(port)) & DPI_ENABLE)
1126			break;
1127	}
1128
1129	fmt = intel_de_read(dev_priv, MIPI_DSI_FUNC_PRG(port)) & VID_MODE_FORMAT_MASK;
1130	bpp = mipi_dsi_pixel_format_to_bpp(
1131			pixel_format_from_register_bits(fmt));
1132
1133	pipe_config->pipe_bpp = bdw_get_pipemisc_bpp(crtc);
1134
1135	/* Enable Frame time stamo based scanline reporting */
1136	pipe_config->mode_flags |=
1137		I915_MODE_FLAG_GET_SCANLINE_FROM_TIMESTAMP;
1138
1139	/* In terms of pixels */
1140	adjusted_mode->crtc_hdisplay =
1141				intel_de_read(dev_priv,
1142				              BXT_MIPI_TRANS_HACTIVE(port));
1143	adjusted_mode->crtc_vdisplay =
1144				intel_de_read(dev_priv,
1145				              BXT_MIPI_TRANS_VACTIVE(port));
1146	adjusted_mode->crtc_vtotal =
1147				intel_de_read(dev_priv,
1148				              BXT_MIPI_TRANS_VTOTAL(port));
1149
1150	hactive = adjusted_mode->crtc_hdisplay;
1151	hfp = intel_de_read(dev_priv, MIPI_HFP_COUNT(port));
1152
1153	/*
1154	 * Meaningful for video mode non-burst sync pulse mode only,
1155	 * can be zero for non-burst sync events and burst modes
1156	 */
1157	hsync = intel_de_read(dev_priv, MIPI_HSYNC_PADDING_COUNT(port));
1158	hbp = intel_de_read(dev_priv, MIPI_HBP_COUNT(port));
1159
1160	/* harizontal values are in terms of high speed byte clock */
1161	hfp = pixels_from_txbyteclkhs(hfp, bpp, lane_count,
1162						intel_dsi->burst_mode_ratio);
1163	hsync = pixels_from_txbyteclkhs(hsync, bpp, lane_count,
1164						intel_dsi->burst_mode_ratio);
1165	hbp = pixels_from_txbyteclkhs(hbp, bpp, lane_count,
1166						intel_dsi->burst_mode_ratio);
1167
1168	if (intel_dsi->dual_link) {
1169		hfp *= 2;
1170		hsync *= 2;
1171		hbp *= 2;
1172	}
1173
1174	/* vertical values are in terms of lines */
1175	vfp = intel_de_read(dev_priv, MIPI_VFP_COUNT(port));
1176	vsync = intel_de_read(dev_priv, MIPI_VSYNC_PADDING_COUNT(port));
1177	vbp = intel_de_read(dev_priv, MIPI_VBP_COUNT(port));
1178
1179	adjusted_mode->crtc_htotal = hactive + hfp + hsync + hbp;
1180	adjusted_mode->crtc_hsync_start = hfp + adjusted_mode->crtc_hdisplay;
1181	adjusted_mode->crtc_hsync_end = hsync + adjusted_mode->crtc_hsync_start;
1182	adjusted_mode->crtc_hblank_start = adjusted_mode->crtc_hdisplay;
1183	adjusted_mode->crtc_hblank_end = adjusted_mode->crtc_htotal;
1184
1185	adjusted_mode->crtc_vsync_start = vfp + adjusted_mode->crtc_vdisplay;
1186	adjusted_mode->crtc_vsync_end = vsync + adjusted_mode->crtc_vsync_start;
1187	adjusted_mode->crtc_vblank_start = adjusted_mode->crtc_vdisplay;
1188	adjusted_mode->crtc_vblank_end = adjusted_mode->crtc_vtotal;
1189
1190	/*
1191	 * In BXT DSI there is no regs programmed with few horizontal timings
1192	 * in Pixels but txbyteclkhs.. So retrieval process adds some
1193	 * ROUND_UP ERRORS in the process of PIXELS<==>txbyteclkhs.
1194	 * Actually here for the given adjusted_mode, we are calculating the
1195	 * value programmed to the port and then back to the horizontal timing
1196	 * param in pixels. This is the expected value, including roundup errors
1197	 * And if that is same as retrieved value from port, then
1198	 * (HW state) adjusted_mode's horizontal timings are corrected to
1199	 * match with SW state to nullify the errors.
1200	 */
1201	/* Calculating the value programmed to the Port register */
1202	hfp_sw = adjusted_mode_sw->crtc_hsync_start -
1203					adjusted_mode_sw->crtc_hdisplay;
1204	hsync_sw = adjusted_mode_sw->crtc_hsync_end -
1205					adjusted_mode_sw->crtc_hsync_start;
1206	hbp_sw = adjusted_mode_sw->crtc_htotal -
1207					adjusted_mode_sw->crtc_hsync_end;
1208
1209	if (intel_dsi->dual_link) {
1210		hfp_sw /= 2;
1211		hsync_sw /= 2;
1212		hbp_sw /= 2;
1213	}
1214
1215	hfp_sw = txbyteclkhs(hfp_sw, bpp, lane_count,
1216						intel_dsi->burst_mode_ratio);
1217	hsync_sw = txbyteclkhs(hsync_sw, bpp, lane_count,
1218			    intel_dsi->burst_mode_ratio);
1219	hbp_sw = txbyteclkhs(hbp_sw, bpp, lane_count,
1220						intel_dsi->burst_mode_ratio);
1221
1222	/* Reverse calculating the adjusted mode parameters from port reg vals*/
1223	hfp_sw = pixels_from_txbyteclkhs(hfp_sw, bpp, lane_count,
1224						intel_dsi->burst_mode_ratio);
1225	hsync_sw = pixels_from_txbyteclkhs(hsync_sw, bpp, lane_count,
1226						intel_dsi->burst_mode_ratio);
1227	hbp_sw = pixels_from_txbyteclkhs(hbp_sw, bpp, lane_count,
1228						intel_dsi->burst_mode_ratio);
1229
1230	if (intel_dsi->dual_link) {
1231		hfp_sw *= 2;
1232		hsync_sw *= 2;
1233		hbp_sw *= 2;
1234	}
1235
1236	crtc_htotal_sw = adjusted_mode_sw->crtc_hdisplay + hfp_sw +
1237							hsync_sw + hbp_sw;
1238	crtc_hsync_start_sw = hfp_sw + adjusted_mode_sw->crtc_hdisplay;
1239	crtc_hsync_end_sw = hsync_sw + crtc_hsync_start_sw;
1240	crtc_hblank_start_sw = adjusted_mode_sw->crtc_hdisplay;
1241	crtc_hblank_end_sw = crtc_htotal_sw;
1242
1243	if (adjusted_mode->crtc_htotal == crtc_htotal_sw)
1244		adjusted_mode->crtc_htotal = adjusted_mode_sw->crtc_htotal;
1245
1246	if (adjusted_mode->crtc_hsync_start == crtc_hsync_start_sw)
1247		adjusted_mode->crtc_hsync_start =
1248					adjusted_mode_sw->crtc_hsync_start;
1249
1250	if (adjusted_mode->crtc_hsync_end == crtc_hsync_end_sw)
1251		adjusted_mode->crtc_hsync_end =
1252					adjusted_mode_sw->crtc_hsync_end;
1253
1254	if (adjusted_mode->crtc_hblank_start == crtc_hblank_start_sw)
1255		adjusted_mode->crtc_hblank_start =
1256					adjusted_mode_sw->crtc_hblank_start;
1257
1258	if (adjusted_mode->crtc_hblank_end == crtc_hblank_end_sw)
1259		adjusted_mode->crtc_hblank_end =
1260					adjusted_mode_sw->crtc_hblank_end;
1261}
1262
1263static void intel_dsi_get_config(struct intel_encoder *encoder,
1264				 struct intel_crtc_state *pipe_config)
1265{
1266	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1267	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1268	u32 pclk;
1269
1270	drm_dbg_kms(&dev_priv->drm, "\n");
1271
1272	pipe_config->output_types |= BIT(INTEL_OUTPUT_DSI);
1273
1274	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
1275		bxt_dsi_get_pipe_config(encoder, pipe_config);
1276		pclk = bxt_dsi_get_pclk(encoder, pipe_config);
1277	} else {
1278		pclk = vlv_dsi_get_pclk(encoder, pipe_config);
1279	}
1280
1281	pipe_config->port_clock = pclk;
1282
1283	/* FIXME definitely not right for burst/cmd mode/pixel overlap */
1284	pipe_config->hw.adjusted_mode.crtc_clock = pclk;
1285	if (intel_dsi->dual_link)
1286		pipe_config->hw.adjusted_mode.crtc_clock *= 2;
1287}
1288
1289/* return txclkesc cycles in terms of divider and duration in us */
1290static u16 txclkesc(u32 divider, unsigned int us)
1291{
1292	switch (divider) {
1293	case ESCAPE_CLOCK_DIVIDER_1:
1294	default:
1295		return 20 * us;
1296	case ESCAPE_CLOCK_DIVIDER_2:
1297		return 10 * us;
1298	case ESCAPE_CLOCK_DIVIDER_4:
1299		return 5 * us;
1300	}
1301}
1302
1303static void set_dsi_timings(struct drm_encoder *encoder,
1304			    const struct drm_display_mode *adjusted_mode)
1305{
1306	struct drm_device *dev = encoder->dev;
1307	struct drm_i915_private *dev_priv = to_i915(dev);
1308	struct intel_dsi *intel_dsi = enc_to_intel_dsi(to_intel_encoder(encoder));
1309	enum port port;
1310	unsigned int bpp = mipi_dsi_pixel_format_to_bpp(intel_dsi->pixel_format);
1311	unsigned int lane_count = intel_dsi->lane_count;
1312
1313	u16 hactive, hfp, hsync, hbp, vfp, vsync, vbp;
1314
1315	hactive = adjusted_mode->crtc_hdisplay;
1316	hfp = adjusted_mode->crtc_hsync_start - adjusted_mode->crtc_hdisplay;
1317	hsync = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
1318	hbp = adjusted_mode->crtc_htotal - adjusted_mode->crtc_hsync_end;
1319
1320	if (intel_dsi->dual_link) {
1321		hactive /= 2;
1322		if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK)
1323			hactive += intel_dsi->pixel_overlap;
1324		hfp /= 2;
1325		hsync /= 2;
1326		hbp /= 2;
1327	}
1328
1329	vfp = adjusted_mode->crtc_vsync_start - adjusted_mode->crtc_vdisplay;
1330	vsync = adjusted_mode->crtc_vsync_end - adjusted_mode->crtc_vsync_start;
1331	vbp = adjusted_mode->crtc_vtotal - adjusted_mode->crtc_vsync_end;
1332
1333	/* horizontal values are in terms of high speed byte clock */
1334	hactive = txbyteclkhs(hactive, bpp, lane_count,
1335			      intel_dsi->burst_mode_ratio);
1336	hfp = txbyteclkhs(hfp, bpp, lane_count, intel_dsi->burst_mode_ratio);
1337	hsync = txbyteclkhs(hsync, bpp, lane_count,
1338			    intel_dsi->burst_mode_ratio);
1339	hbp = txbyteclkhs(hbp, bpp, lane_count, intel_dsi->burst_mode_ratio);
1340
1341	for_each_dsi_port(port, intel_dsi->ports) {
1342		if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
1343			/*
1344			 * Program hdisplay and vdisplay on MIPI transcoder.
1345			 * This is different from calculated hactive and
1346			 * vactive, as they are calculated per channel basis,
1347			 * whereas these values should be based on resolution.
1348			 */
1349			intel_de_write(dev_priv, BXT_MIPI_TRANS_HACTIVE(port),
1350				       adjusted_mode->crtc_hdisplay);
1351			intel_de_write(dev_priv, BXT_MIPI_TRANS_VACTIVE(port),
1352				       adjusted_mode->crtc_vdisplay);
1353			intel_de_write(dev_priv, BXT_MIPI_TRANS_VTOTAL(port),
1354				       adjusted_mode->crtc_vtotal);
1355		}
1356
1357		intel_de_write(dev_priv, MIPI_HACTIVE_AREA_COUNT(port),
1358			       hactive);
1359		intel_de_write(dev_priv, MIPI_HFP_COUNT(port), hfp);
1360
1361		/* meaningful for video mode non-burst sync pulse mode only,
1362		 * can be zero for non-burst sync events and burst modes */
1363		intel_de_write(dev_priv, MIPI_HSYNC_PADDING_COUNT(port),
1364			       hsync);
1365		intel_de_write(dev_priv, MIPI_HBP_COUNT(port), hbp);
1366
1367		/* vertical values are in terms of lines */
1368		intel_de_write(dev_priv, MIPI_VFP_COUNT(port), vfp);
1369		intel_de_write(dev_priv, MIPI_VSYNC_PADDING_COUNT(port),
1370			       vsync);
1371		intel_de_write(dev_priv, MIPI_VBP_COUNT(port), vbp);
1372	}
1373}
1374
1375static u32 pixel_format_to_reg(enum mipi_dsi_pixel_format fmt)
1376{
1377	switch (fmt) {
1378	case MIPI_DSI_FMT_RGB888:
1379		return VID_MODE_FORMAT_RGB888;
1380	case MIPI_DSI_FMT_RGB666:
1381		return VID_MODE_FORMAT_RGB666;
1382	case MIPI_DSI_FMT_RGB666_PACKED:
1383		return VID_MODE_FORMAT_RGB666_PACKED;
1384	case MIPI_DSI_FMT_RGB565:
1385		return VID_MODE_FORMAT_RGB565;
1386	default:
1387		MISSING_CASE(fmt);
1388		return VID_MODE_FORMAT_RGB666;
1389	}
1390}
1391
1392static void intel_dsi_prepare(struct intel_encoder *intel_encoder,
1393			      const struct intel_crtc_state *pipe_config)
1394{
1395	struct drm_encoder *encoder = &intel_encoder->base;
1396	struct drm_device *dev = encoder->dev;
1397	struct drm_i915_private *dev_priv = to_i915(dev);
1398	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
1399	struct intel_dsi *intel_dsi = enc_to_intel_dsi(to_intel_encoder(encoder));
1400	const struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
1401	enum port port;
1402	unsigned int bpp = mipi_dsi_pixel_format_to_bpp(intel_dsi->pixel_format);
1403	u32 val, tmp;
1404	u16 mode_hdisplay;
1405
1406	drm_dbg_kms(&dev_priv->drm, "pipe %c\n", pipe_name(crtc->pipe));
1407
1408	mode_hdisplay = adjusted_mode->crtc_hdisplay;
1409
1410	if (intel_dsi->dual_link) {
1411		mode_hdisplay /= 2;
1412		if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK)
1413			mode_hdisplay += intel_dsi->pixel_overlap;
1414	}
1415
1416	for_each_dsi_port(port, intel_dsi->ports) {
1417		if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
1418			/*
1419			 * escape clock divider, 20MHz, shared for A and C.
1420			 * device ready must be off when doing this! txclkesc?
1421			 */
1422			tmp = intel_de_read(dev_priv, MIPI_CTRL(PORT_A));
1423			tmp &= ~ESCAPE_CLOCK_DIVIDER_MASK;
1424			intel_de_write(dev_priv, MIPI_CTRL(PORT_A),
1425				       tmp | ESCAPE_CLOCK_DIVIDER_1);
1426
1427			/* read request priority is per pipe */
1428			tmp = intel_de_read(dev_priv, MIPI_CTRL(port));
1429			tmp &= ~READ_REQUEST_PRIORITY_MASK;
1430			intel_de_write(dev_priv, MIPI_CTRL(port),
1431				       tmp | READ_REQUEST_PRIORITY_HIGH);
1432		} else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
1433			enum pipe pipe = crtc->pipe;
1434
1435			tmp = intel_de_read(dev_priv, MIPI_CTRL(port));
1436			tmp &= ~BXT_PIPE_SELECT_MASK;
1437
1438			tmp |= BXT_PIPE_SELECT(pipe);
1439			intel_de_write(dev_priv, MIPI_CTRL(port), tmp);
1440		}
1441
1442		/* XXX: why here, why like this? handling in irq handler?! */
1443		intel_de_write(dev_priv, MIPI_INTR_STAT(port), 0xffffffff);
1444		intel_de_write(dev_priv, MIPI_INTR_EN(port), 0xffffffff);
1445
1446		intel_de_write(dev_priv, MIPI_DPHY_PARAM(port),
1447			       intel_dsi->dphy_reg);
1448
1449		intel_de_write(dev_priv, MIPI_DPI_RESOLUTION(port),
1450			       adjusted_mode->crtc_vdisplay << VERTICAL_ADDRESS_SHIFT | mode_hdisplay << HORIZONTAL_ADDRESS_SHIFT);
1451	}
1452
1453	set_dsi_timings(encoder, adjusted_mode);
1454
1455	val = intel_dsi->lane_count << DATA_LANES_PRG_REG_SHIFT;
1456	if (is_cmd_mode(intel_dsi)) {
1457		val |= intel_dsi->channel << CMD_MODE_CHANNEL_NUMBER_SHIFT;
1458		val |= CMD_MODE_DATA_WIDTH_8_BIT; /* XXX */
1459	} else {
1460		val |= intel_dsi->channel << VID_MODE_CHANNEL_NUMBER_SHIFT;
1461		val |= pixel_format_to_reg(intel_dsi->pixel_format);
1462	}
1463
1464	tmp = 0;
1465	if (intel_dsi->eotp_pkt == 0)
1466		tmp |= EOT_DISABLE;
1467	if (intel_dsi->clock_stop)
1468		tmp |= CLOCKSTOP;
1469
1470	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
1471		tmp |= BXT_DPHY_DEFEATURE_EN;
1472		if (!is_cmd_mode(intel_dsi))
1473			tmp |= BXT_DEFEATURE_DPI_FIFO_CTR;
1474	}
1475
1476	for_each_dsi_port(port, intel_dsi->ports) {
1477		intel_de_write(dev_priv, MIPI_DSI_FUNC_PRG(port), val);
1478
1479		/* timeouts for recovery. one frame IIUC. if counter expires,
1480		 * EOT and stop state. */
1481
1482		/*
1483		 * In burst mode, value greater than one DPI line Time in byte
1484		 * clock (txbyteclkhs) To timeout this timer 1+ of the above
1485		 * said value is recommended.
1486		 *
1487		 * In non-burst mode, Value greater than one DPI frame time in
1488		 * byte clock(txbyteclkhs) To timeout this timer 1+ of the above
1489		 * said value is recommended.
1490		 *
1491		 * In DBI only mode, value greater than one DBI frame time in
1492		 * byte clock(txbyteclkhs) To timeout this timer 1+ of the above
1493		 * said value is recommended.
1494		 */
1495
1496		if (is_vid_mode(intel_dsi) &&
1497			intel_dsi->video_mode == BURST_MODE) {
1498			intel_de_write(dev_priv, MIPI_HS_TX_TIMEOUT(port),
1499				       txbyteclkhs(adjusted_mode->crtc_htotal, bpp, intel_dsi->lane_count, intel_dsi->burst_mode_ratio) + 1);
1500		} else {
1501			intel_de_write(dev_priv, MIPI_HS_TX_TIMEOUT(port),
1502				       txbyteclkhs(adjusted_mode->crtc_vtotal * adjusted_mode->crtc_htotal, bpp, intel_dsi->lane_count, intel_dsi->burst_mode_ratio) + 1);
1503		}
1504		intel_de_write(dev_priv, MIPI_LP_RX_TIMEOUT(port),
1505			       intel_dsi->lp_rx_timeout);
1506		intel_de_write(dev_priv, MIPI_TURN_AROUND_TIMEOUT(port),
1507			       intel_dsi->turn_arnd_val);
1508		intel_de_write(dev_priv, MIPI_DEVICE_RESET_TIMER(port),
1509			       intel_dsi->rst_timer_val);
1510
1511		/* dphy stuff */
1512
1513		/* in terms of low power clock */
1514		intel_de_write(dev_priv, MIPI_INIT_COUNT(port),
1515			       txclkesc(intel_dsi->escape_clk_div, 100));
1516
1517		if ((IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) &&
1518		    !intel_dsi->dual_link) {
1519			/*
1520			 * BXT spec says write MIPI_INIT_COUNT for
1521			 * both the ports, even if only one is
1522			 * getting used. So write the other port
1523			 * if not in dual link mode.
1524			 */
1525			intel_de_write(dev_priv,
1526				       MIPI_INIT_COUNT(port == PORT_A ? PORT_C : PORT_A),
1527				       intel_dsi->init_count);
1528		}
1529
1530		/* recovery disables */
1531		intel_de_write(dev_priv, MIPI_EOT_DISABLE(port), tmp);
1532
1533		/* in terms of low power clock */
1534		intel_de_write(dev_priv, MIPI_INIT_COUNT(port),
1535			       intel_dsi->init_count);
1536
1537		/* in terms of txbyteclkhs. actual high to low switch +
1538		 * MIPI_STOP_STATE_STALL * MIPI_LP_BYTECLK.
1539		 *
1540		 * XXX: write MIPI_STOP_STATE_STALL?
1541		 */
1542		intel_de_write(dev_priv, MIPI_HIGH_LOW_SWITCH_COUNT(port),
1543			       intel_dsi->hs_to_lp_count);
1544
1545		/* XXX: low power clock equivalence in terms of byte clock.
1546		 * the number of byte clocks occupied in one low power clock.
1547		 * based on txbyteclkhs and txclkesc.
1548		 * txclkesc time / txbyteclk time * (105 + MIPI_STOP_STATE_STALL
1549		 * ) / 105.???
1550		 */
1551		intel_de_write(dev_priv, MIPI_LP_BYTECLK(port),
1552			       intel_dsi->lp_byte_clk);
1553
1554		if (IS_GEMINILAKE(dev_priv)) {
1555			intel_de_write(dev_priv, MIPI_TLPX_TIME_COUNT(port),
1556				       intel_dsi->lp_byte_clk);
1557			/* Shadow of DPHY reg */
1558			intel_de_write(dev_priv, MIPI_CLK_LANE_TIMING(port),
1559				       intel_dsi->dphy_reg);
1560		}
1561
1562		/* the bw essential for transmitting 16 long packets containing
1563		 * 252 bytes meant for dcs write memory command is programmed in
1564		 * this register in terms of byte clocks. based on dsi transfer
1565		 * rate and the number of lanes configured the time taken to
1566		 * transmit 16 long packets in a dsi stream varies. */
1567		intel_de_write(dev_priv, MIPI_DBI_BW_CTRL(port),
1568			       intel_dsi->bw_timer);
1569
1570		intel_de_write(dev_priv, MIPI_CLK_LANE_SWITCH_TIME_CNT(port),
1571			       intel_dsi->clk_lp_to_hs_count << LP_HS_SSW_CNT_SHIFT | intel_dsi->clk_hs_to_lp_count << HS_LP_PWR_SW_CNT_SHIFT);
1572
1573		if (is_vid_mode(intel_dsi)) {
1574			u32 fmt = intel_dsi->video_frmt_cfg_bits | IP_TG_CONFIG;
1575
1576			/*
1577			 * Some panels might have resolution which is not a
1578			 * multiple of 64 like 1366 x 768. Enable RANDOM
1579			 * resolution support for such panels by default.
1580			 */
1581			fmt |= RANDOM_DPI_DISPLAY_RESOLUTION;
1582
1583			switch (intel_dsi->video_mode) {
1584			default:
1585				MISSING_CASE(intel_dsi->video_mode);
1586				fallthrough;
1587			case NON_BURST_SYNC_EVENTS:
1588				fmt |= VIDEO_MODE_NON_BURST_WITH_SYNC_EVENTS;
1589				break;
1590			case NON_BURST_SYNC_PULSE:
1591				fmt |= VIDEO_MODE_NON_BURST_WITH_SYNC_PULSE;
1592				break;
1593			case BURST_MODE:
1594				fmt |= VIDEO_MODE_BURST;
1595				break;
1596			}
1597
1598			intel_de_write(dev_priv, MIPI_VIDEO_MODE_FORMAT(port), fmt);
1599		}
1600	}
1601}
1602
1603static void intel_dsi_unprepare(struct intel_encoder *encoder)
1604{
1605	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1606	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1607	enum port port;
1608	u32 val;
1609
1610	if (IS_GEMINILAKE(dev_priv))
1611		return;
1612
1613	for_each_dsi_port(port, intel_dsi->ports) {
1614		/* Panel commands can be sent when clock is in LP11 */
1615		intel_de_write(dev_priv, MIPI_DEVICE_READY(port), 0x0);
1616
1617		if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
1618			bxt_dsi_reset_clocks(encoder, port);
1619		else
1620			vlv_dsi_reset_clocks(encoder, port);
1621		intel_de_write(dev_priv, MIPI_EOT_DISABLE(port), CLOCKSTOP);
1622
1623		val = intel_de_read(dev_priv, MIPI_DSI_FUNC_PRG(port));
1624		val &= ~VID_MODE_FORMAT_MASK;
1625		intel_de_write(dev_priv, MIPI_DSI_FUNC_PRG(port), val);
1626
1627		intel_de_write(dev_priv, MIPI_DEVICE_READY(port), 0x1);
1628	}
1629}
1630
1631static void intel_dsi_encoder_destroy(struct drm_encoder *encoder)
 
 
 
 
 
1632{
1633	struct intel_dsi *intel_dsi = enc_to_intel_dsi(to_intel_encoder(encoder));
1634
1635	intel_dsi_vbt_gpio_cleanup(intel_dsi);
1636	intel_encoder_destroy(encoder);
1637}
1638
1639static const struct drm_encoder_funcs intel_dsi_funcs = {
1640	.destroy = intel_dsi_encoder_destroy,
1641};
 
 
 
 
1642
1643static const struct drm_connector_helper_funcs intel_dsi_connector_helper_funcs = {
1644	.get_modes = intel_dsi_get_modes,
1645	.mode_valid = intel_dsi_mode_valid,
1646	.atomic_check = intel_digital_connector_atomic_check,
1647};
1648
1649static const struct drm_connector_funcs intel_dsi_connector_funcs = {
1650	.detect = intel_panel_detect,
1651	.late_register = intel_connector_register,
1652	.early_unregister = intel_connector_unregister,
1653	.destroy = intel_connector_destroy,
1654	.fill_modes = drm_helper_probe_single_connector_modes,
1655	.atomic_get_property = intel_digital_connector_atomic_get_property,
1656	.atomic_set_property = intel_digital_connector_atomic_set_property,
1657	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1658	.atomic_duplicate_state = intel_digital_connector_duplicate_state,
1659};
1660
1661static void vlv_dsi_add_properties(struct intel_connector *connector)
1662{
1663	const struct drm_display_mode *fixed_mode =
1664		intel_panel_preferred_fixed_mode(connector);
1665
1666	intel_attach_scaling_mode_property(&connector->base);
1667
1668	drm_connector_set_panel_orientation_with_quirk(&connector->base,
1669						       intel_dsi_get_panel_orientation(connector),
1670						       fixed_mode->hdisplay,
1671						       fixed_mode->vdisplay);
1672}
1673
1674#define NS_KHZ_RATIO		1000000
1675
1676#define PREPARE_CNT_MAX		0x3F
1677#define EXIT_ZERO_CNT_MAX	0x3F
1678#define CLK_ZERO_CNT_MAX	0xFF
1679#define TRAIL_CNT_MAX		0x1F
1680
1681static void vlv_dphy_param_init(struct intel_dsi *intel_dsi)
1682{
1683	struct drm_device *dev = intel_dsi->base.base.dev;
1684	struct drm_i915_private *dev_priv = to_i915(dev);
1685	struct intel_connector *connector = intel_dsi->attached_connector;
1686	struct mipi_config *mipi_config = connector->panel.vbt.dsi.config;
1687	u32 tlpx_ns, extra_byte_count, tlpx_ui;
1688	u32 ui_num, ui_den;
1689	u32 prepare_cnt, exit_zero_cnt, clk_zero_cnt, trail_cnt;
1690	u32 ths_prepare_ns, tclk_trail_ns;
1691	u32 tclk_prepare_clkzero, ths_prepare_hszero;
1692	u32 lp_to_hs_switch, hs_to_lp_switch;
1693	u32 mul;
1694
1695	tlpx_ns = intel_dsi_tlpx_ns(intel_dsi);
1696
1697	switch (intel_dsi->lane_count) {
1698	case 1:
1699	case 2:
1700		extra_byte_count = 2;
1701		break;
1702	case 3:
1703		extra_byte_count = 4;
1704		break;
1705	case 4:
1706	default:
1707		extra_byte_count = 3;
1708		break;
1709	}
1710
1711	/* in Kbps */
1712	ui_num = NS_KHZ_RATIO;
1713	ui_den = intel_dsi_bitrate(intel_dsi);
1714
1715	tclk_prepare_clkzero = mipi_config->tclk_prepare_clkzero;
1716	ths_prepare_hszero = mipi_config->ths_prepare_hszero;
1717
1718	/*
1719	 * B060
1720	 * LP byte clock = TLPX/ (8UI)
1721	 */
1722	intel_dsi->lp_byte_clk = DIV_ROUND_UP(tlpx_ns * ui_den, 8 * ui_num);
1723
1724	/* DDR clock period = 2 * UI
1725	 * UI(sec) = 1/(bitrate * 10^3) (bitrate is in KHZ)
1726	 * UI(nsec) = 10^6 / bitrate
1727	 * DDR clock period (nsec) = 2 * UI = (2 * 10^6)/ bitrate
1728	 * DDR clock count  = ns_value / DDR clock period
1729	 *
1730	 * For GEMINILAKE dphy_param_reg will be programmed in terms of
1731	 * HS byte clock count for other platform in HS ddr clock count
1732	 */
1733	mul = IS_GEMINILAKE(dev_priv) ? 8 : 2;
1734	ths_prepare_ns = max(mipi_config->ths_prepare,
1735			     mipi_config->tclk_prepare);
1736
1737	/* prepare count */
1738	prepare_cnt = DIV_ROUND_UP(ths_prepare_ns * ui_den, ui_num * mul);
1739
1740	if (prepare_cnt > PREPARE_CNT_MAX) {
1741		drm_dbg_kms(&dev_priv->drm, "prepare count too high %u\n",
1742			    prepare_cnt);
1743		prepare_cnt = PREPARE_CNT_MAX;
1744	}
1745
1746	/* exit zero count */
1747	exit_zero_cnt = DIV_ROUND_UP(
1748				(ths_prepare_hszero - ths_prepare_ns) * ui_den,
1749				ui_num * mul
1750				);
1751
1752	/*
1753	 * Exit zero is unified val ths_zero and ths_exit
1754	 * minimum value for ths_exit = 110ns
1755	 * min (exit_zero_cnt * 2) = 110/UI
1756	 * exit_zero_cnt = 55/UI
1757	 */
1758	if (exit_zero_cnt < (55 * ui_den / ui_num) && (55 * ui_den) % ui_num)
1759		exit_zero_cnt += 1;
1760
1761	if (exit_zero_cnt > EXIT_ZERO_CNT_MAX) {
1762		drm_dbg_kms(&dev_priv->drm, "exit zero count too high %u\n",
1763			    exit_zero_cnt);
1764		exit_zero_cnt = EXIT_ZERO_CNT_MAX;
1765	}
1766
1767	/* clk zero count */
1768	clk_zero_cnt = DIV_ROUND_UP(
1769				(tclk_prepare_clkzero -	ths_prepare_ns)
1770				* ui_den, ui_num * mul);
1771
1772	if (clk_zero_cnt > CLK_ZERO_CNT_MAX) {
1773		drm_dbg_kms(&dev_priv->drm, "clock zero count too high %u\n",
1774			    clk_zero_cnt);
1775		clk_zero_cnt = CLK_ZERO_CNT_MAX;
1776	}
1777
1778	/* trail count */
1779	tclk_trail_ns = max(mipi_config->tclk_trail, mipi_config->ths_trail);
1780	trail_cnt = DIV_ROUND_UP(tclk_trail_ns * ui_den, ui_num * mul);
1781
1782	if (trail_cnt > TRAIL_CNT_MAX) {
1783		drm_dbg_kms(&dev_priv->drm, "trail count too high %u\n",
1784			    trail_cnt);
1785		trail_cnt = TRAIL_CNT_MAX;
1786	}
1787
1788	/* B080 */
1789	intel_dsi->dphy_reg = exit_zero_cnt << 24 | trail_cnt << 16 |
1790						clk_zero_cnt << 8 | prepare_cnt;
1791
1792	/*
1793	 * LP to HS switch count = 4TLPX + PREP_COUNT * mul + EXIT_ZERO_COUNT *
1794	 *					mul + 10UI + Extra Byte Count
1795	 *
1796	 * HS to LP switch count = THS-TRAIL + 2TLPX + Extra Byte Count
1797	 * Extra Byte Count is calculated according to number of lanes.
1798	 * High Low Switch Count is the Max of LP to HS and
1799	 * HS to LP switch count
1800	 *
1801	 */
1802	tlpx_ui = DIV_ROUND_UP(tlpx_ns * ui_den, ui_num);
1803
1804	/* B044 */
1805	/* FIXME:
1806	 * The comment above does not match with the code */
1807	lp_to_hs_switch = DIV_ROUND_UP(4 * tlpx_ui + prepare_cnt * mul +
1808						exit_zero_cnt * mul + 10, 8);
1809
1810	hs_to_lp_switch = DIV_ROUND_UP(mipi_config->ths_trail + 2 * tlpx_ui, 8);
1811
1812	intel_dsi->hs_to_lp_count = max(lp_to_hs_switch, hs_to_lp_switch);
1813	intel_dsi->hs_to_lp_count += extra_byte_count;
1814
1815	/* B088 */
1816	/* LP -> HS for clock lanes
1817	 * LP clk sync + LP11 + LP01 + tclk_prepare + tclk_zero +
1818	 *						extra byte count
1819	 * 2TPLX + 1TLPX + 1 TPLX(in ns) + prepare_cnt * 2 + clk_zero_cnt *
1820	 *					2(in UI) + extra byte count
1821	 * In byteclks = (4TLPX + prepare_cnt * 2 + clk_zero_cnt *2 (in UI)) /
1822	 *					8 + extra byte count
1823	 */
1824	intel_dsi->clk_lp_to_hs_count =
1825		DIV_ROUND_UP(
1826			4 * tlpx_ui + prepare_cnt * 2 +
1827			clk_zero_cnt * 2,
1828			8);
1829
1830	intel_dsi->clk_lp_to_hs_count += extra_byte_count;
1831
1832	/* HS->LP for Clock Lanes
1833	 * Low Power clock synchronisations + 1Tx byteclk + tclk_trail +
1834	 *						Extra byte count
1835	 * 2TLPX + 8UI + (trail_count*2)(in UI) + Extra byte count
1836	 * In byteclks = (2*TLpx(in UI) + trail_count*2 +8)(in UI)/8 +
1837	 *						Extra byte count
1838	 */
1839	intel_dsi->clk_hs_to_lp_count =
1840		DIV_ROUND_UP(2 * tlpx_ui + trail_cnt * 2 + 8,
1841			8);
1842	intel_dsi->clk_hs_to_lp_count += extra_byte_count;
1843
1844	intel_dsi_log_params(intel_dsi);
1845}
1846
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1847void vlv_dsi_init(struct drm_i915_private *dev_priv)
1848{
1849	struct intel_dsi *intel_dsi;
1850	struct intel_encoder *intel_encoder;
1851	struct drm_encoder *encoder;
1852	struct intel_connector *intel_connector;
1853	struct drm_connector *connector;
1854	struct drm_display_mode *current_mode;
 
1855	enum port port;
1856	enum pipe pipe;
1857
1858	drm_dbg_kms(&dev_priv->drm, "\n");
1859
1860	/* There is no detection method for MIPI so rely on VBT */
1861	if (!intel_bios_is_dsi_present(dev_priv, &port))
1862		return;
1863
1864	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
1865		dev_priv->display.dsi.mmio_base = BXT_MIPI_BASE;
1866	else
1867		dev_priv->display.dsi.mmio_base = VLV_MIPI_BASE;
1868
1869	intel_dsi = kzalloc(sizeof(*intel_dsi), GFP_KERNEL);
1870	if (!intel_dsi)
1871		return;
1872
1873	intel_connector = intel_connector_alloc();
1874	if (!intel_connector) {
1875		kfree(intel_dsi);
1876		return;
1877	}
1878
1879	intel_encoder = &intel_dsi->base;
1880	encoder = &intel_encoder->base;
1881	intel_dsi->attached_connector = intel_connector;
1882
1883	connector = &intel_connector->base;
1884
1885	drm_encoder_init(&dev_priv->drm, encoder, &intel_dsi_funcs, DRM_MODE_ENCODER_DSI,
1886			 "DSI %c", port_name(port));
1887
1888	intel_encoder->compute_config = intel_dsi_compute_config;
1889	intel_encoder->pre_enable = intel_dsi_pre_enable;
1890	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
1891		intel_encoder->enable = bxt_dsi_enable;
1892	intel_encoder->disable = intel_dsi_disable;
1893	intel_encoder->post_disable = intel_dsi_post_disable;
1894	intel_encoder->get_hw_state = intel_dsi_get_hw_state;
1895	intel_encoder->get_config = intel_dsi_get_config;
1896	intel_encoder->update_pipe = intel_backlight_update;
1897	intel_encoder->shutdown = intel_dsi_shutdown;
1898
1899	intel_connector->get_hw_state = intel_connector_get_hw_state;
1900
1901	intel_encoder->port = port;
1902	intel_encoder->type = INTEL_OUTPUT_DSI;
1903	intel_encoder->power_domain = POWER_DOMAIN_PORT_DSI;
1904	intel_encoder->cloneable = 0;
1905
1906	/*
1907	 * On BYT/CHV, pipe A maps to MIPI DSI port A, pipe B maps to MIPI DSI
1908	 * port C. BXT isn't limited like this.
1909	 */
1910	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
1911		intel_encoder->pipe_mask = ~0;
1912	else if (port == PORT_A)
1913		intel_encoder->pipe_mask = BIT(PIPE_A);
1914	else
1915		intel_encoder->pipe_mask = BIT(PIPE_B);
1916
1917	intel_dsi->panel_power_off_time = ktime_get_boottime();
1918
1919	intel_bios_init_panel(dev_priv, &intel_connector->panel, NULL, NULL);
1920
1921	if (intel_connector->panel.vbt.dsi.config->dual_link)
1922		intel_dsi->ports = BIT(PORT_A) | BIT(PORT_C);
1923	else
1924		intel_dsi->ports = BIT(port);
1925
1926	if (drm_WARN_ON(&dev_priv->drm, intel_connector->panel.vbt.dsi.bl_ports & ~intel_dsi->ports))
1927		intel_connector->panel.vbt.dsi.bl_ports &= intel_dsi->ports;
1928
1929	if (drm_WARN_ON(&dev_priv->drm, intel_connector->panel.vbt.dsi.cabc_ports & ~intel_dsi->ports))
1930		intel_connector->panel.vbt.dsi.cabc_ports &= intel_dsi->ports;
1931
1932	/* Create a DSI host (and a device) for each port. */
1933	for_each_dsi_port(port, intel_dsi->ports) {
1934		struct intel_dsi_host *host;
1935
1936		host = intel_dsi_host_init(intel_dsi, &intel_dsi_host_ops,
1937					   port);
1938		if (!host)
1939			goto err;
1940
1941		intel_dsi->dsi_hosts[port] = host;
1942	}
1943
1944	if (!intel_dsi_vbt_init(intel_dsi, MIPI_DSI_GENERIC_PANEL_ID)) {
1945		drm_dbg_kms(&dev_priv->drm, "no device found\n");
1946		goto err;
1947	}
1948
1949	/* Use clock read-back from current hw-state for fastboot */
1950	current_mode = intel_encoder_current_mode(intel_encoder);
1951	if (current_mode) {
1952		drm_dbg_kms(&dev_priv->drm, "Calculated pclk %d GOP %d\n",
1953			    intel_dsi->pclk, current_mode->clock);
1954		if (intel_fuzzy_clock_check(intel_dsi->pclk,
1955					    current_mode->clock)) {
1956			drm_dbg_kms(&dev_priv->drm, "Using GOP pclk\n");
1957			intel_dsi->pclk = current_mode->clock;
1958		}
1959
1960		kfree(current_mode);
1961	}
1962
1963	vlv_dphy_param_init(intel_dsi);
1964
1965	intel_dsi_vbt_gpio_init(intel_dsi,
1966				intel_dsi_get_hw_state(intel_encoder, &pipe));
1967
1968	drm_connector_init(&dev_priv->drm, connector, &intel_dsi_connector_funcs,
1969			   DRM_MODE_CONNECTOR_DSI);
1970
1971	drm_connector_helper_add(connector, &intel_dsi_connector_helper_funcs);
1972
1973	connector->display_info.subpixel_order = SubPixelHorizontalRGB; /*XXX*/
1974
1975	intel_connector_attach_encoder(intel_connector, intel_encoder);
1976
1977	mutex_lock(&dev_priv->drm.mode_config.mutex);
1978	intel_panel_add_vbt_lfp_fixed_mode(intel_connector);
1979	mutex_unlock(&dev_priv->drm.mode_config.mutex);
1980
1981	if (!intel_panel_preferred_fixed_mode(intel_connector)) {
1982		drm_dbg_kms(&dev_priv->drm, "no fixed mode\n");
1983		goto err_cleanup_connector;
1984	}
1985
1986	intel_panel_init(intel_connector);
 
 
 
 
 
 
 
 
1987
1988	intel_backlight_setup(intel_connector, INVALID_PIPE);
1989
1990	vlv_dsi_add_properties(intel_connector);
1991
1992	return;
1993
1994err_cleanup_connector:
1995	drm_connector_cleanup(&intel_connector->base);
1996err:
1997	drm_encoder_cleanup(&intel_encoder->base);
1998	kfree(intel_dsi);
1999	kfree(intel_connector);
2000}
v6.9.4
   1/*
   2 * Copyright © 2013 Intel Corporation
   3 *
   4 * Permission is hereby granted, free of charge, to any person obtaining a
   5 * copy of this software and associated documentation files (the "Software"),
   6 * to deal in the Software without restriction, including without limitation
   7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
   8 * and/or sell copies of the Software, and to permit persons to whom the
   9 * Software is furnished to do so, subject to the following conditions:
  10 *
  11 * The above copyright notice and this permission notice (including the next
  12 * paragraph) shall be included in all copies or substantial portions of the
  13 * Software.
  14 *
  15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21 * DEALINGS IN THE SOFTWARE.
  22 *
  23 * Author: Jani Nikula <jani.nikula@intel.com>
  24 */
  25
  26#include <linux/dmi.h>
  27#include <linux/slab.h>
  28
  29#include <drm/drm_atomic_helper.h>
  30#include <drm/drm_crtc.h>
  31#include <drm/drm_edid.h>
  32#include <drm/drm_mipi_dsi.h>
  33
  34#include "i915_drv.h"
  35#include "i915_reg.h"
  36#include "intel_atomic.h"
  37#include "intel_backlight.h"
  38#include "intel_connector.h"
  39#include "intel_crtc.h"
  40#include "intel_de.h"
  41#include "intel_display_types.h"
  42#include "intel_dsi.h"
  43#include "intel_dsi_vbt.h"
  44#include "intel_fifo_underrun.h"
  45#include "intel_panel.h"
  46#include "skl_scaler.h"
  47#include "vlv_dsi.h"
  48#include "vlv_dsi_pll.h"
  49#include "vlv_dsi_regs.h"
  50#include "vlv_sideband.h"
  51
  52/* return pixels in terms of txbyteclkhs */
  53static u16 txbyteclkhs(u16 pixels, int bpp, int lane_count,
  54		       u16 burst_mode_ratio)
  55{
  56	return DIV_ROUND_UP(DIV_ROUND_UP(pixels * bpp * burst_mode_ratio,
  57					 8 * 100), lane_count);
  58}
  59
  60/* return pixels equvalent to txbyteclkhs */
  61static u16 pixels_from_txbyteclkhs(u16 clk_hs, int bpp, int lane_count,
  62			u16 burst_mode_ratio)
  63{
  64	return DIV_ROUND_UP((clk_hs * lane_count * 8 * 100),
  65						(bpp * burst_mode_ratio));
  66}
  67
  68enum mipi_dsi_pixel_format pixel_format_from_register_bits(u32 fmt)
  69{
  70	/* It just so happens the VBT matches register contents. */
  71	switch (fmt) {
  72	case VID_MODE_FORMAT_RGB888:
  73		return MIPI_DSI_FMT_RGB888;
  74	case VID_MODE_FORMAT_RGB666:
  75		return MIPI_DSI_FMT_RGB666;
  76	case VID_MODE_FORMAT_RGB666_PACKED:
  77		return MIPI_DSI_FMT_RGB666_PACKED;
  78	case VID_MODE_FORMAT_RGB565:
  79		return MIPI_DSI_FMT_RGB565;
  80	default:
  81		MISSING_CASE(fmt);
  82		return MIPI_DSI_FMT_RGB666;
  83	}
  84}
  85
  86void vlv_dsi_wait_for_fifo_empty(struct intel_dsi *intel_dsi, enum port port)
  87{
  88	struct drm_encoder *encoder = &intel_dsi->base.base;
  89	struct drm_device *dev = encoder->dev;
  90	struct drm_i915_private *dev_priv = to_i915(dev);
  91	u32 mask;
  92
  93	mask = LP_CTRL_FIFO_EMPTY | HS_CTRL_FIFO_EMPTY |
  94		LP_DATA_FIFO_EMPTY | HS_DATA_FIFO_EMPTY;
  95
  96	if (intel_de_wait_for_set(dev_priv, MIPI_GEN_FIFO_STAT(port),
  97				  mask, 100))
  98		drm_err(&dev_priv->drm, "DPI FIFOs are not empty\n");
  99}
 100
 101static void write_data(struct drm_i915_private *dev_priv,
 102		       i915_reg_t reg,
 103		       const u8 *data, u32 len)
 104{
 105	u32 i, j;
 106
 107	for (i = 0; i < len; i += 4) {
 108		u32 val = 0;
 109
 110		for (j = 0; j < min_t(u32, len - i, 4); j++)
 111			val |= *data++ << 8 * j;
 112
 113		intel_de_write(dev_priv, reg, val);
 114	}
 115}
 116
 117static void read_data(struct drm_i915_private *dev_priv,
 118		      i915_reg_t reg,
 119		      u8 *data, u32 len)
 120{
 121	u32 i, j;
 122
 123	for (i = 0; i < len; i += 4) {
 124		u32 val = intel_de_read(dev_priv, reg);
 125
 126		for (j = 0; j < min_t(u32, len - i, 4); j++)
 127			*data++ = val >> 8 * j;
 128	}
 129}
 130
 131static ssize_t intel_dsi_host_transfer(struct mipi_dsi_host *host,
 132				       const struct mipi_dsi_msg *msg)
 133{
 134	struct intel_dsi_host *intel_dsi_host = to_intel_dsi_host(host);
 135	struct drm_device *dev = intel_dsi_host->intel_dsi->base.base.dev;
 136	struct drm_i915_private *dev_priv = to_i915(dev);
 137	enum port port = intel_dsi_host->port;
 138	struct mipi_dsi_packet packet;
 139	ssize_t ret;
 140	const u8 *header;
 141	i915_reg_t data_reg, ctrl_reg;
 142	u32 data_mask, ctrl_mask;
 143
 144	ret = mipi_dsi_create_packet(&packet, msg);
 145	if (ret < 0)
 146		return ret;
 147
 148	header = packet.header;
 
 149
 150	if (msg->flags & MIPI_DSI_MSG_USE_LPM) {
 151		data_reg = MIPI_LP_GEN_DATA(port);
 152		data_mask = LP_DATA_FIFO_FULL;
 153		ctrl_reg = MIPI_LP_GEN_CTRL(port);
 154		ctrl_mask = LP_CTRL_FIFO_FULL;
 155	} else {
 156		data_reg = MIPI_HS_GEN_DATA(port);
 157		data_mask = HS_DATA_FIFO_FULL;
 158		ctrl_reg = MIPI_HS_GEN_CTRL(port);
 159		ctrl_mask = HS_CTRL_FIFO_FULL;
 160	}
 161
 162	/* note: this is never true for reads */
 163	if (packet.payload_length) {
 164		if (intel_de_wait_for_clear(dev_priv, MIPI_GEN_FIFO_STAT(port),
 165					    data_mask, 50))
 166			drm_err(&dev_priv->drm,
 167				"Timeout waiting for HS/LP DATA FIFO !full\n");
 168
 169		write_data(dev_priv, data_reg, packet.payload,
 170			   packet.payload_length);
 171	}
 172
 173	if (msg->rx_len) {
 174		intel_de_write(dev_priv, MIPI_INTR_STAT(port),
 175			       GEN_READ_DATA_AVAIL);
 176	}
 177
 178	if (intel_de_wait_for_clear(dev_priv, MIPI_GEN_FIFO_STAT(port),
 179				    ctrl_mask, 50)) {
 180		drm_err(&dev_priv->drm,
 181			"Timeout waiting for HS/LP CTRL FIFO !full\n");
 182	}
 183
 184	intel_de_write(dev_priv, ctrl_reg,
 185		       header[2] << 16 | header[1] << 8 | header[0]);
 186
 187	/* ->rx_len is set only for reads */
 188	if (msg->rx_len) {
 189		data_mask = GEN_READ_DATA_AVAIL;
 190		if (intel_de_wait_for_set(dev_priv, MIPI_INTR_STAT(port),
 191					  data_mask, 50))
 192			drm_err(&dev_priv->drm,
 193				"Timeout waiting for read data.\n");
 194
 195		read_data(dev_priv, data_reg, msg->rx_buf, msg->rx_len);
 196	}
 197
 198	/* XXX: fix for reads and writes */
 199	return 4 + packet.payload_length;
 200}
 201
 202static int intel_dsi_host_attach(struct mipi_dsi_host *host,
 203				 struct mipi_dsi_device *dsi)
 204{
 205	return 0;
 206}
 207
 208static int intel_dsi_host_detach(struct mipi_dsi_host *host,
 209				 struct mipi_dsi_device *dsi)
 210{
 211	return 0;
 212}
 213
 214static const struct mipi_dsi_host_ops intel_dsi_host_ops = {
 215	.attach = intel_dsi_host_attach,
 216	.detach = intel_dsi_host_detach,
 217	.transfer = intel_dsi_host_transfer,
 218};
 219
 220/*
 221 * send a video mode command
 222 *
 223 * XXX: commands with data in MIPI_DPI_DATA?
 224 */
 225static int dpi_send_cmd(struct intel_dsi *intel_dsi, u32 cmd, bool hs,
 226			enum port port)
 227{
 228	struct drm_encoder *encoder = &intel_dsi->base.base;
 229	struct drm_device *dev = encoder->dev;
 230	struct drm_i915_private *dev_priv = to_i915(dev);
 231	u32 mask;
 232
 233	/* XXX: pipe, hs */
 234	if (hs)
 235		cmd &= ~DPI_LP_MODE;
 236	else
 237		cmd |= DPI_LP_MODE;
 238
 239	/* clear bit */
 240	intel_de_write(dev_priv, MIPI_INTR_STAT(port), SPL_PKT_SENT_INTERRUPT);
 241
 242	/* XXX: old code skips write if control unchanged */
 243	if (cmd == intel_de_read(dev_priv, MIPI_DPI_CONTROL(port)))
 244		drm_dbg_kms(&dev_priv->drm,
 245			    "Same special packet %02x twice in a row.\n", cmd);
 246
 247	intel_de_write(dev_priv, MIPI_DPI_CONTROL(port), cmd);
 248
 249	mask = SPL_PKT_SENT_INTERRUPT;
 250	if (intel_de_wait_for_set(dev_priv, MIPI_INTR_STAT(port), mask, 100))
 251		drm_err(&dev_priv->drm,
 252			"Video mode command 0x%08x send failed.\n", cmd);
 253
 254	return 0;
 255}
 256
 257static void band_gap_reset(struct drm_i915_private *dev_priv)
 258{
 259	vlv_flisdsi_get(dev_priv);
 260
 261	vlv_flisdsi_write(dev_priv, 0x08, 0x0001);
 262	vlv_flisdsi_write(dev_priv, 0x0F, 0x0005);
 263	vlv_flisdsi_write(dev_priv, 0x0F, 0x0025);
 264	udelay(150);
 265	vlv_flisdsi_write(dev_priv, 0x0F, 0x0000);
 266	vlv_flisdsi_write(dev_priv, 0x08, 0x0000);
 267
 268	vlv_flisdsi_put(dev_priv);
 269}
 270
 271static int intel_dsi_compute_config(struct intel_encoder *encoder,
 272				    struct intel_crtc_state *pipe_config,
 273				    struct drm_connector_state *conn_state)
 274{
 275	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 276	struct intel_dsi *intel_dsi = container_of(encoder, struct intel_dsi,
 277						   base);
 278	struct intel_connector *intel_connector = intel_dsi->attached_connector;
 279	struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
 280	int ret;
 281
 282	drm_dbg_kms(&dev_priv->drm, "\n");
 283	pipe_config->sink_format = INTEL_OUTPUT_FORMAT_RGB;
 284	pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
 285
 286	ret = intel_panel_compute_config(intel_connector, adjusted_mode);
 287	if (ret)
 288		return ret;
 289
 290	ret = intel_panel_fitting(pipe_config, conn_state);
 291	if (ret)
 292		return ret;
 293
 294	if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
 295		return -EINVAL;
 296
 297	/* DSI uses short packets for sync events, so clear mode flags for DSI */
 298	adjusted_mode->flags = 0;
 299
 300	if (intel_dsi->pixel_format == MIPI_DSI_FMT_RGB888)
 301		pipe_config->pipe_bpp = 24;
 302	else
 303		pipe_config->pipe_bpp = 18;
 304
 305	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
 306		/* Enable Frame time stamp based scanline reporting */
 307		pipe_config->mode_flags |=
 308			I915_MODE_FLAG_GET_SCANLINE_FROM_TIMESTAMP;
 309
 310		/* Dual link goes to DSI transcoder A. */
 311		if (intel_dsi->ports == BIT(PORT_C))
 312			pipe_config->cpu_transcoder = TRANSCODER_DSI_C;
 313		else
 314			pipe_config->cpu_transcoder = TRANSCODER_DSI_A;
 315
 316		ret = bxt_dsi_pll_compute(encoder, pipe_config);
 317		if (ret)
 318			return -EINVAL;
 319	} else {
 320		ret = vlv_dsi_pll_compute(encoder, pipe_config);
 321		if (ret)
 322			return -EINVAL;
 323	}
 324
 325	pipe_config->clock_set = true;
 326
 327	return 0;
 328}
 329
 330static bool glk_dsi_enable_io(struct intel_encoder *encoder)
 331{
 332	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 333	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
 334	enum port port;
 
 335	bool cold_boot = false;
 336
 337	/* Set the MIPI mode
 338	 * If MIPI_Mode is off, then writing to LP_Wake bit is not reflecting.
 339	 * Power ON MIPI IO first and then write into IO reset and LP wake bits
 340	 */
 341	for_each_dsi_port(port, intel_dsi->ports)
 342		intel_de_rmw(dev_priv, MIPI_CTRL(port), 0, GLK_MIPIIO_ENABLE);
 
 
 
 343
 344	/* Put the IO into reset */
 345	intel_de_rmw(dev_priv, MIPI_CTRL(PORT_A), GLK_MIPIIO_RESET_RELEASED, 0);
 
 
 346
 347	/* Program LP Wake */
 348	for_each_dsi_port(port, intel_dsi->ports) {
 349		u32 tmp = intel_de_read(dev_priv, MIPI_DEVICE_READY(port));
 350		intel_de_rmw(dev_priv, MIPI_CTRL(port),
 351			     GLK_LP_WAKE, (tmp & DEVICE_READY) ? GLK_LP_WAKE : 0);
 
 
 
 352	}
 353
 354	/* Wait for Pwr ACK */
 355	for_each_dsi_port(port, intel_dsi->ports) {
 356		if (intel_de_wait_for_set(dev_priv, MIPI_CTRL(port),
 357					  GLK_MIPIIO_PORT_POWERED, 20))
 358			drm_err(&dev_priv->drm, "MIPIO port is powergated\n");
 359	}
 360
 361	/* Check for cold boot scenario */
 362	for_each_dsi_port(port, intel_dsi->ports) {
 363		cold_boot |=
 364			!(intel_de_read(dev_priv, MIPI_DEVICE_READY(port)) & DEVICE_READY);
 365	}
 366
 367	return cold_boot;
 368}
 369
 370static void glk_dsi_device_ready(struct intel_encoder *encoder)
 371{
 372	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 373	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
 374	enum port port;
 
 375
 376	/* Wait for MIPI PHY status bit to set */
 377	for_each_dsi_port(port, intel_dsi->ports) {
 378		if (intel_de_wait_for_set(dev_priv, MIPI_CTRL(port),
 379					  GLK_PHY_STATUS_PORT_READY, 20))
 380			drm_err(&dev_priv->drm, "PHY is not ON\n");
 381	}
 382
 383	/* Get IO out of reset */
 384	intel_de_rmw(dev_priv, MIPI_CTRL(PORT_A), 0, GLK_MIPIIO_RESET_RELEASED);
 
 
 385
 386	/* Get IO out of Low power state*/
 387	for_each_dsi_port(port, intel_dsi->ports) {
 388		if (!(intel_de_read(dev_priv, MIPI_DEVICE_READY(port)) & DEVICE_READY)) {
 389			intel_de_rmw(dev_priv, MIPI_DEVICE_READY(port),
 390				     ULPS_STATE_MASK, DEVICE_READY);
 
 
 391			usleep_range(10, 15);
 392		} else {
 393			/* Enter ULPS */
 394			intel_de_rmw(dev_priv, MIPI_DEVICE_READY(port),
 395				     ULPS_STATE_MASK, ULPS_STATE_ENTER | DEVICE_READY);
 
 
 396
 397			/* Wait for ULPS active */
 398			if (intel_de_wait_for_clear(dev_priv, MIPI_CTRL(port),
 399						    GLK_ULPS_NOT_ACTIVE, 20))
 400				drm_err(&dev_priv->drm, "ULPS not active\n");
 401
 402			/* Exit ULPS */
 403			intel_de_rmw(dev_priv, MIPI_DEVICE_READY(port),
 404				     ULPS_STATE_MASK, ULPS_STATE_EXIT | DEVICE_READY);
 
 
 405
 406			/* Enter Normal Mode */
 407			intel_de_rmw(dev_priv, MIPI_DEVICE_READY(port),
 408				     ULPS_STATE_MASK,
 409				     ULPS_STATE_NORMAL_OPERATION | DEVICE_READY);
 410
 411			intel_de_rmw(dev_priv, MIPI_CTRL(port), GLK_LP_WAKE, 0);
 
 
 
 412		}
 413	}
 414
 415	/* Wait for Stop state */
 416	for_each_dsi_port(port, intel_dsi->ports) {
 417		if (intel_de_wait_for_set(dev_priv, MIPI_CTRL(port),
 418					  GLK_DATA_LANE_STOP_STATE, 20))
 419			drm_err(&dev_priv->drm,
 420				"Date lane not in STOP state\n");
 421	}
 422
 423	/* Wait for AFE LATCH */
 424	for_each_dsi_port(port, intel_dsi->ports) {
 425		if (intel_de_wait_for_set(dev_priv, BXT_MIPI_PORT_CTRL(port),
 426					  AFE_LATCHOUT, 20))
 427			drm_err(&dev_priv->drm,
 428				"D-PHY not entering LP-11 state\n");
 429	}
 430}
 431
 432static void bxt_dsi_device_ready(struct intel_encoder *encoder)
 433{
 434	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 435	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
 436	enum port port;
 437	u32 val;
 438
 439	drm_dbg_kms(&dev_priv->drm, "\n");
 440
 441	/* Enable MIPI PHY transparent latch */
 442	for_each_dsi_port(port, intel_dsi->ports) {
 443		intel_de_rmw(dev_priv, BXT_MIPI_PORT_CTRL(port), 0, LP_OUTPUT_HOLD);
 
 
 444		usleep_range(2000, 2500);
 445	}
 446
 447	/* Clear ULPS and set device ready */
 448	for_each_dsi_port(port, intel_dsi->ports) {
 449		val = intel_de_read(dev_priv, MIPI_DEVICE_READY(port));
 450		val &= ~ULPS_STATE_MASK;
 451		intel_de_write(dev_priv, MIPI_DEVICE_READY(port), val);
 452		usleep_range(2000, 2500);
 453		val |= DEVICE_READY;
 454		intel_de_write(dev_priv, MIPI_DEVICE_READY(port), val);
 455	}
 456}
 457
 458static void vlv_dsi_device_ready(struct intel_encoder *encoder)
 459{
 460	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 461	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
 462	enum port port;
 
 463
 464	drm_dbg_kms(&dev_priv->drm, "\n");
 465
 466	vlv_flisdsi_get(dev_priv);
 467	/* program rcomp for compliance, reduce from 50 ohms to 45 ohms
 468	 * needed everytime after power gate */
 469	vlv_flisdsi_write(dev_priv, 0x04, 0x0004);
 470	vlv_flisdsi_put(dev_priv);
 471
 472	/* bandgap reset is needed after everytime we do power gate */
 473	band_gap_reset(dev_priv);
 474
 475	for_each_dsi_port(port, intel_dsi->ports) {
 476
 477		intel_de_write(dev_priv, MIPI_DEVICE_READY(port),
 478			       ULPS_STATE_ENTER);
 479		usleep_range(2500, 3000);
 480
 481		/* Enable MIPI PHY transparent latch
 482		 * Common bit for both MIPI Port A & MIPI Port C
 483		 * No similar bit in MIPI Port C reg
 484		 */
 485		intel_de_rmw(dev_priv, MIPI_PORT_CTRL(PORT_A), 0, LP_OUTPUT_HOLD);
 
 
 486		usleep_range(1000, 1500);
 487
 488		intel_de_write(dev_priv, MIPI_DEVICE_READY(port),
 489			       ULPS_STATE_EXIT);
 490		usleep_range(2500, 3000);
 491
 492		intel_de_write(dev_priv, MIPI_DEVICE_READY(port),
 493			       DEVICE_READY);
 494		usleep_range(2500, 3000);
 495	}
 496}
 497
 498static void intel_dsi_device_ready(struct intel_encoder *encoder)
 499{
 500	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 501
 502	if (IS_GEMINILAKE(dev_priv))
 503		glk_dsi_device_ready(encoder);
 504	else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
 505		bxt_dsi_device_ready(encoder);
 506	else
 507		vlv_dsi_device_ready(encoder);
 508}
 509
 510static void glk_dsi_enter_low_power_mode(struct intel_encoder *encoder)
 511{
 512	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 513	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
 514	enum port port;
 
 515
 516	/* Enter ULPS */
 517	for_each_dsi_port(port, intel_dsi->ports)
 518		intel_de_rmw(dev_priv, MIPI_DEVICE_READY(port),
 519			     ULPS_STATE_MASK, ULPS_STATE_ENTER | DEVICE_READY);
 
 
 
 520
 521	/* Wait for MIPI PHY status bit to unset */
 522	for_each_dsi_port(port, intel_dsi->ports) {
 523		if (intel_de_wait_for_clear(dev_priv, MIPI_CTRL(port),
 524					    GLK_PHY_STATUS_PORT_READY, 20))
 525			drm_err(&dev_priv->drm, "PHY is not turning OFF\n");
 526	}
 527
 528	/* Wait for Pwr ACK bit to unset */
 529	for_each_dsi_port(port, intel_dsi->ports) {
 530		if (intel_de_wait_for_clear(dev_priv, MIPI_CTRL(port),
 531					    GLK_MIPIIO_PORT_POWERED, 20))
 532			drm_err(&dev_priv->drm,
 533				"MIPI IO Port is not powergated\n");
 534	}
 535}
 536
 537static void glk_dsi_disable_mipi_io(struct intel_encoder *encoder)
 538{
 539	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 540	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
 541	enum port port;
 
 542
 543	/* Put the IO into reset */
 544	intel_de_rmw(dev_priv, MIPI_CTRL(PORT_A), GLK_MIPIIO_RESET_RELEASED, 0);
 
 
 545
 546	/* Wait for MIPI PHY status bit to unset */
 547	for_each_dsi_port(port, intel_dsi->ports) {
 548		if (intel_de_wait_for_clear(dev_priv, MIPI_CTRL(port),
 549					    GLK_PHY_STATUS_PORT_READY, 20))
 550			drm_err(&dev_priv->drm, "PHY is not turning OFF\n");
 551	}
 552
 553	/* Clear MIPI mode */
 554	for_each_dsi_port(port, intel_dsi->ports)
 555		intel_de_rmw(dev_priv, MIPI_CTRL(port), GLK_MIPIIO_ENABLE, 0);
 
 
 
 556}
 557
 558static void glk_dsi_clear_device_ready(struct intel_encoder *encoder)
 559{
 560	glk_dsi_enter_low_power_mode(encoder);
 561	glk_dsi_disable_mipi_io(encoder);
 562}
 563
 564static i915_reg_t port_ctrl_reg(struct drm_i915_private *i915, enum port port)
 565{
 566	return IS_GEMINILAKE(i915) || IS_BROXTON(i915) ?
 567		BXT_MIPI_PORT_CTRL(port) : MIPI_PORT_CTRL(port);
 568}
 569
 570static void vlv_dsi_clear_device_ready(struct intel_encoder *encoder)
 571{
 572	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 573	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
 574	enum port port;
 575
 576	drm_dbg_kms(&dev_priv->drm, "\n");
 577	for_each_dsi_port(port, intel_dsi->ports) {
 578		/* Common bit for both MIPI Port A & MIPI Port C on VLV/CHV */
 579		i915_reg_t port_ctrl = IS_BROXTON(dev_priv) ?
 580			BXT_MIPI_PORT_CTRL(port) : MIPI_PORT_CTRL(PORT_A);
 
 581
 582		intel_de_write(dev_priv, MIPI_DEVICE_READY(port),
 583			       DEVICE_READY | ULPS_STATE_ENTER);
 584		usleep_range(2000, 2500);
 585
 586		intel_de_write(dev_priv, MIPI_DEVICE_READY(port),
 587			       DEVICE_READY | ULPS_STATE_EXIT);
 588		usleep_range(2000, 2500);
 589
 590		intel_de_write(dev_priv, MIPI_DEVICE_READY(port),
 591			       DEVICE_READY | ULPS_STATE_ENTER);
 592		usleep_range(2000, 2500);
 593
 594		/*
 595		 * On VLV/CHV, wait till Clock lanes are in LP-00 state for MIPI
 596		 * Port A only. MIPI Port C has no similar bit for checking.
 597		 */
 598		if ((IS_BROXTON(dev_priv) || port == PORT_A) &&
 599		    intel_de_wait_for_clear(dev_priv, port_ctrl,
 600					    AFE_LATCHOUT, 30))
 601			drm_err(&dev_priv->drm, "DSI LP not going Low\n");
 602
 603		/* Disable MIPI PHY transparent latch */
 604		intel_de_rmw(dev_priv, port_ctrl, LP_OUTPUT_HOLD, 0);
 
 605		usleep_range(1000, 1500);
 606
 607		intel_de_write(dev_priv, MIPI_DEVICE_READY(port), 0x00);
 608		usleep_range(2000, 2500);
 609	}
 610}
 611
 612static void intel_dsi_port_enable(struct intel_encoder *encoder,
 613				  const struct intel_crtc_state *crtc_state)
 614{
 615	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 616	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
 617	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
 618	enum port port;
 619
 620	if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK) {
 621		u32 temp = intel_dsi->pixel_overlap;
 622
 623		if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
 624			for_each_dsi_port(port, intel_dsi->ports)
 625				intel_de_rmw(dev_priv, MIPI_CTRL(port),
 626					     BXT_PIXEL_OVERLAP_CNT_MASK,
 627					     temp << BXT_PIXEL_OVERLAP_CNT_SHIFT);
 
 
 
 
 
 628		} else {
 629			intel_de_rmw(dev_priv, VLV_CHICKEN_3,
 630				     PIXEL_OVERLAP_CNT_MASK,
 631				     temp << PIXEL_OVERLAP_CNT_SHIFT);
 
 
 632		}
 633	}
 634
 635	for_each_dsi_port(port, intel_dsi->ports) {
 636		i915_reg_t port_ctrl = port_ctrl_reg(dev_priv, port);
 
 637		u32 temp;
 638
 639		temp = intel_de_read(dev_priv, port_ctrl);
 640
 641		temp &= ~LANE_CONFIGURATION_MASK;
 642		temp &= ~DUAL_LINK_MODE_MASK;
 643
 644		if (intel_dsi->ports == (BIT(PORT_A) | BIT(PORT_C))) {
 645			temp |= (intel_dsi->dual_link - 1)
 646						<< DUAL_LINK_MODE_SHIFT;
 647			if (IS_BROXTON(dev_priv))
 648				temp |= LANE_CONFIGURATION_DUAL_LINK_A;
 649			else
 650				temp |= crtc->pipe ?
 651					LANE_CONFIGURATION_DUAL_LINK_B :
 652					LANE_CONFIGURATION_DUAL_LINK_A;
 653		}
 654
 655		if (intel_dsi->pixel_format != MIPI_DSI_FMT_RGB888)
 656			temp |= DITHERING_ENABLE;
 657
 658		/* assert ip_tg_enable signal */
 659		intel_de_write(dev_priv, port_ctrl, temp | DPI_ENABLE);
 660		intel_de_posting_read(dev_priv, port_ctrl);
 661	}
 662}
 663
 664static void intel_dsi_port_disable(struct intel_encoder *encoder)
 665{
 666	struct drm_device *dev = encoder->base.dev;
 667	struct drm_i915_private *dev_priv = to_i915(dev);
 668	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
 669	enum port port;
 670
 671	for_each_dsi_port(port, intel_dsi->ports) {
 672		i915_reg_t port_ctrl = port_ctrl_reg(dev_priv, port);
 
 
 673
 674		/* de-assert ip_tg_enable signal */
 675		intel_de_rmw(dev_priv, port_ctrl, DPI_ENABLE, 0);
 
 676		intel_de_posting_read(dev_priv, port_ctrl);
 677	}
 678}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 679static void intel_dsi_prepare(struct intel_encoder *intel_encoder,
 680			      const struct intel_crtc_state *pipe_config);
 681static void intel_dsi_unprepare(struct intel_encoder *encoder);
 682
 683/*
 684 * Panel enable/disable sequences from the VBT spec.
 685 *
 686 * Note the spec has AssertReset / DeassertReset swapped from their
 687 * usual naming. We use the normal names to avoid confusion (so below
 688 * they are swapped compared to the spec).
 689 *
 690 * Steps starting with MIPI refer to VBT sequences, note that for v2
 691 * VBTs several steps which have a VBT in v2 are expected to be handled
 692 * directly by the driver, by directly driving gpios for example.
 693 *
 694 * v2 video mode seq         v3 video mode seq         command mode seq
 695 * - power on                - MIPIPanelPowerOn        - power on
 696 * - wait t1+t2                                        - wait t1+t2
 697 * - MIPIDeassertResetPin    - MIPIDeassertResetPin    - MIPIDeassertResetPin
 698 * - io lines to lp-11       - io lines to lp-11       - io lines to lp-11
 699 * - MIPISendInitialDcsCmds  - MIPISendInitialDcsCmds  - MIPISendInitialDcsCmds
 700 *                                                     - MIPITearOn
 701 *                                                     - MIPIDisplayOn
 702 * - turn on DPI             - turn on DPI             - set pipe to dsr mode
 703 * - MIPIDisplayOn           - MIPIDisplayOn
 704 * - wait t5                                           - wait t5
 705 * - backlight on            - MIPIBacklightOn         - backlight on
 706 * ...                       ...                       ... issue mem cmds ...
 707 * - backlight off           - MIPIBacklightOff        - backlight off
 708 * - wait t6                                           - wait t6
 709 * - MIPIDisplayOff
 710 * - turn off DPI            - turn off DPI            - disable pipe dsr mode
 711 *                                                     - MIPITearOff
 712 *                           - MIPIDisplayOff          - MIPIDisplayOff
 713 * - io lines to lp-00       - io lines to lp-00       - io lines to lp-00
 714 * - MIPIAssertResetPin      - MIPIAssertResetPin      - MIPIAssertResetPin
 715 * - wait t3                                           - wait t3
 716 * - power off               - MIPIPanelPowerOff       - power off
 717 * - wait t4                                           - wait t4
 718 */
 719
 720/*
 721 * DSI port enable has to be done before pipe and plane enable, so we do it in
 722 * the pre_enable hook instead of the enable hook.
 723 */
 724static void intel_dsi_pre_enable(struct intel_atomic_state *state,
 725				 struct intel_encoder *encoder,
 726				 const struct intel_crtc_state *pipe_config,
 727				 const struct drm_connector_state *conn_state)
 728{
 729	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
 730	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
 
 731	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
 732	enum pipe pipe = crtc->pipe;
 733	enum port port;
 
 734	bool glk_cold_boot = false;
 735
 736	drm_dbg_kms(&dev_priv->drm, "\n");
 737
 738	intel_dsi_wait_panel_power_cycle(intel_dsi);
 739
 740	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
 741
 742	/*
 743	 * The BIOS may leave the PLL in a wonky state where it doesn't
 744	 * lock. It needs to be fully powered down to fix it.
 745	 */
 746	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
 747		bxt_dsi_pll_disable(encoder);
 748		bxt_dsi_pll_enable(encoder, pipe_config);
 749	} else {
 750		vlv_dsi_pll_disable(encoder);
 751		vlv_dsi_pll_enable(encoder, pipe_config);
 752	}
 753
 754	if (IS_BROXTON(dev_priv)) {
 755		/* Add MIPI IO reset programming for modeset */
 756		intel_de_rmw(dev_priv, BXT_P_CR_GT_DISP_PWRON, 0, MIPIO_RST_CTRL);
 
 
 757
 758		/* Power up DSI regulator */
 759		intel_de_write(dev_priv, BXT_P_DSI_REGULATOR_CFG, STAP_SELECT);
 760		intel_de_write(dev_priv, BXT_P_DSI_REGULATOR_TX_CTRL, 0);
 761	}
 762
 763	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
 
 
 764		/* Disable DPOunit clock gating, can stall pipe */
 765		intel_de_rmw(dev_priv, DSPCLK_GATE_D(dev_priv),
 766			     0, DPOUNIT_CLOCK_GATE_DISABLE);
 
 767	}
 768
 769	if (!IS_GEMINILAKE(dev_priv))
 770		intel_dsi_prepare(encoder, pipe_config);
 771
 772	/* Give the panel time to power-on and then deassert its reset */
 773	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_POWER_ON);
 774	msleep(intel_dsi->panel_on_delay);
 775	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DEASSERT_RESET);
 
 
 
 
 
 
 
 
 
 
 
 
 776
 777	if (IS_GEMINILAKE(dev_priv)) {
 778		glk_cold_boot = glk_dsi_enable_io(encoder);
 779
 780		/* Prepare port in cold boot(s3/s4) scenario */
 781		if (glk_cold_boot)
 782			intel_dsi_prepare(encoder, pipe_config);
 783	}
 784
 785	/* Put device in ready state (LP-11) */
 786	intel_dsi_device_ready(encoder);
 787
 788	/* Prepare port in normal boot scenario */
 789	if (IS_GEMINILAKE(dev_priv) && !glk_cold_boot)
 790		intel_dsi_prepare(encoder, pipe_config);
 791
 792	/* Send initialization commands in LP mode */
 793	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_INIT_OTP);
 794
 795	/*
 796	 * Enable port in pre-enable phase itself because as per hw team
 797	 * recommendation, port should be enabled before plane & pipe
 798	 */
 799	if (is_cmd_mode(intel_dsi)) {
 800		for_each_dsi_port(port, intel_dsi->ports)
 801			intel_de_write(dev_priv,
 802				       MIPI_MAX_RETURN_PKT_SIZE(port), 8 * 4);
 803		intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_TEAR_ON);
 804		intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DISPLAY_ON);
 805	} else {
 806		msleep(20); /* XXX */
 807		for_each_dsi_port(port, intel_dsi->ports)
 808			dpi_send_cmd(intel_dsi, TURN_ON, false, port);
 809		msleep(100);
 810
 811		intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DISPLAY_ON);
 812
 813		intel_dsi_port_enable(encoder, pipe_config);
 814	}
 815
 816	intel_backlight_enable(pipe_config, conn_state);
 817	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_BACKLIGHT_ON);
 818}
 819
 820static void bxt_dsi_enable(struct intel_atomic_state *state,
 821			   struct intel_encoder *encoder,
 822			   const struct intel_crtc_state *crtc_state,
 823			   const struct drm_connector_state *conn_state)
 824{
 
 
 825	intel_crtc_vblank_on(crtc_state);
 826}
 827
 828/*
 829 * DSI port disable has to be done after pipe and plane disable, so we do it in
 830 * the post_disable hook.
 831 */
 832static void intel_dsi_disable(struct intel_atomic_state *state,
 833			      struct intel_encoder *encoder,
 834			      const struct intel_crtc_state *old_crtc_state,
 835			      const struct drm_connector_state *old_conn_state)
 836{
 837	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
 838	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
 839	enum port port;
 840
 841	drm_dbg_kms(&i915->drm, "\n");
 842
 843	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_BACKLIGHT_OFF);
 844	intel_backlight_disable(old_conn_state);
 845
 846	/*
 847	 * According to the spec we should send SHUTDOWN before
 848	 * MIPI_SEQ_DISPLAY_OFF only for v3+ VBTs, but field testing
 849	 * has shown that the v3 sequence works for v2 VBTs too
 850	 */
 851	if (is_vid_mode(intel_dsi)) {
 852		/* Send Shutdown command to the panel in LP mode */
 853		for_each_dsi_port(port, intel_dsi->ports)
 854			dpi_send_cmd(intel_dsi, SHUTDOWN, false, port);
 855		msleep(10);
 856	}
 857}
 858
 859static void intel_dsi_clear_device_ready(struct intel_encoder *encoder)
 860{
 861	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 862
 863	if (IS_GEMINILAKE(dev_priv))
 864		glk_dsi_clear_device_ready(encoder);
 865	else
 866		vlv_dsi_clear_device_ready(encoder);
 867}
 868
 869static void intel_dsi_post_disable(struct intel_atomic_state *state,
 870				   struct intel_encoder *encoder,
 871				   const struct intel_crtc_state *old_crtc_state,
 872				   const struct drm_connector_state *old_conn_state)
 873{
 874	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 875	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
 876	enum port port;
 
 877
 878	drm_dbg_kms(&dev_priv->drm, "\n");
 879
 880	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
 881		intel_crtc_vblank_off(old_crtc_state);
 882
 883		skl_scaler_disable(old_crtc_state);
 884	}
 885
 886	if (is_vid_mode(intel_dsi)) {
 887		for_each_dsi_port(port, intel_dsi->ports)
 888			vlv_dsi_wait_for_fifo_empty(intel_dsi, port);
 889
 890		intel_dsi_port_disable(encoder);
 891		usleep_range(2000, 5000);
 892	}
 893
 894	intel_dsi_unprepare(encoder);
 895
 896	/*
 897	 * if disable packets are sent before sending shutdown packet then in
 898	 * some next enable sequence send turn on packet error is observed
 899	 */
 900	if (is_cmd_mode(intel_dsi))
 901		intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_TEAR_OFF);
 902	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DISPLAY_OFF);
 903
 904	/* Transition to LP-00 */
 905	intel_dsi_clear_device_ready(encoder);
 906
 907	if (IS_BROXTON(dev_priv)) {
 908		/* Power down DSI regulator to save power */
 909		intel_de_write(dev_priv, BXT_P_DSI_REGULATOR_CFG, STAP_SELECT);
 910		intel_de_write(dev_priv, BXT_P_DSI_REGULATOR_TX_CTRL,
 911			       HS_IO_CTRL_SELECT);
 912
 913		/* Add MIPI IO reset programming for modeset */
 914		intel_de_rmw(dev_priv, BXT_P_CR_GT_DISP_PWRON, MIPIO_RST_CTRL, 0);
 
 
 915	}
 916
 917	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
 918		bxt_dsi_pll_disable(encoder);
 919	} else {
 
 
 920		vlv_dsi_pll_disable(encoder);
 921
 922		intel_de_rmw(dev_priv, DSPCLK_GATE_D(dev_priv),
 923			     DPOUNIT_CLOCK_GATE_DISABLE, 0);
 
 924	}
 925
 926	/* Assert reset */
 927	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_ASSERT_RESET);
 928
 929	msleep(intel_dsi->panel_off_delay);
 930	intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_POWER_OFF);
 931
 932	intel_dsi->panel_power_off_time = ktime_get_boottime();
 933}
 934
 
 
 
 
 
 
 
 935static bool intel_dsi_get_hw_state(struct intel_encoder *encoder,
 936				   enum pipe *pipe)
 937{
 938	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 939	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
 940	intel_wakeref_t wakeref;
 941	enum port port;
 942	bool active = false;
 943
 944	drm_dbg_kms(&dev_priv->drm, "\n");
 945
 946	wakeref = intel_display_power_get_if_enabled(dev_priv,
 947						     encoder->power_domain);
 948	if (!wakeref)
 949		return false;
 950
 951	/*
 952	 * On Broxton the PLL needs to be enabled with a valid divider
 953	 * configuration, otherwise accessing DSI registers will hang the
 954	 * machine. See BSpec North Display Engine registers/MIPI[BXT].
 955	 */
 956	if ((IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) &&
 957	    !bxt_dsi_pll_is_enabled(dev_priv))
 958		goto out_put_power;
 959
 960	/* XXX: this only works for one DSI output */
 961	for_each_dsi_port(port, intel_dsi->ports) {
 962		i915_reg_t port_ctrl = port_ctrl_reg(dev_priv, port);
 963		bool enabled = intel_de_read(dev_priv, port_ctrl) & DPI_ENABLE;
 
 964
 965		/*
 966		 * Due to some hardware limitations on VLV/CHV, the DPI enable
 967		 * bit in port C control register does not get set. As a
 968		 * workaround, check pipe B conf instead.
 969		 */
 970		if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
 971		    port == PORT_C)
 972			enabled = intel_de_read(dev_priv, TRANSCONF(PIPE_B)) & TRANSCONF_ENABLE;
 973
 974		/* Try command mode if video mode not enabled */
 975		if (!enabled) {
 976			u32 tmp = intel_de_read(dev_priv,
 977						MIPI_DSI_FUNC_PRG(port));
 978			enabled = tmp & CMD_MODE_DATA_WIDTH_MASK;
 979		}
 980
 981		if (!enabled)
 982			continue;
 983
 984		if (!(intel_de_read(dev_priv, MIPI_DEVICE_READY(port)) & DEVICE_READY))
 985			continue;
 986
 987		if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
 988			u32 tmp = intel_de_read(dev_priv, MIPI_CTRL(port));
 989			tmp &= BXT_PIPE_SELECT_MASK;
 990			tmp >>= BXT_PIPE_SELECT_SHIFT;
 991
 992			if (drm_WARN_ON(&dev_priv->drm, tmp > PIPE_C))
 993				continue;
 994
 995			*pipe = tmp;
 996		} else {
 997			*pipe = port == PORT_A ? PIPE_A : PIPE_B;
 998		}
 999
1000		active = true;
1001		break;
1002	}
1003
1004out_put_power:
1005	intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
1006
1007	return active;
1008}
1009
1010static void bxt_dsi_get_pipe_config(struct intel_encoder *encoder,
1011				    struct intel_crtc_state *pipe_config)
1012{
1013	struct drm_device *dev = encoder->base.dev;
1014	struct drm_i915_private *dev_priv = to_i915(dev);
1015	struct drm_display_mode *adjusted_mode =
1016					&pipe_config->hw.adjusted_mode;
1017	struct drm_display_mode *adjusted_mode_sw;
1018	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
1019	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1020	unsigned int lane_count = intel_dsi->lane_count;
1021	unsigned int bpp, fmt;
1022	enum port port;
1023	u16 hactive, hfp, hsync, hbp, vfp, vsync;
1024	u16 hfp_sw, hsync_sw, hbp_sw;
1025	u16 crtc_htotal_sw, crtc_hsync_start_sw, crtc_hsync_end_sw,
1026				crtc_hblank_start_sw, crtc_hblank_end_sw;
1027
1028	/* FIXME: hw readout should not depend on SW state */
1029	adjusted_mode_sw = &crtc->config->hw.adjusted_mode;
1030
1031	/*
1032	 * Atleast one port is active as encoder->get_config called only if
1033	 * encoder->get_hw_state() returns true.
1034	 */
1035	for_each_dsi_port(port, intel_dsi->ports) {
1036		if (intel_de_read(dev_priv, BXT_MIPI_PORT_CTRL(port)) & DPI_ENABLE)
1037			break;
1038	}
1039
1040	fmt = intel_de_read(dev_priv, MIPI_DSI_FUNC_PRG(port)) & VID_MODE_FORMAT_MASK;
1041	bpp = mipi_dsi_pixel_format_to_bpp(
1042			pixel_format_from_register_bits(fmt));
1043
1044	pipe_config->pipe_bpp = bdw_get_pipe_misc_bpp(crtc);
1045
1046	/* Enable Frame time stamo based scanline reporting */
1047	pipe_config->mode_flags |=
1048		I915_MODE_FLAG_GET_SCANLINE_FROM_TIMESTAMP;
1049
1050	/* In terms of pixels */
1051	adjusted_mode->crtc_hdisplay =
1052				intel_de_read(dev_priv,
1053				              BXT_MIPI_TRANS_HACTIVE(port));
1054	adjusted_mode->crtc_vdisplay =
1055				intel_de_read(dev_priv,
1056				              BXT_MIPI_TRANS_VACTIVE(port));
1057	adjusted_mode->crtc_vtotal =
1058				intel_de_read(dev_priv,
1059				              BXT_MIPI_TRANS_VTOTAL(port));
1060
1061	hactive = adjusted_mode->crtc_hdisplay;
1062	hfp = intel_de_read(dev_priv, MIPI_HFP_COUNT(port));
1063
1064	/*
1065	 * Meaningful for video mode non-burst sync pulse mode only,
1066	 * can be zero for non-burst sync events and burst modes
1067	 */
1068	hsync = intel_de_read(dev_priv, MIPI_HSYNC_PADDING_COUNT(port));
1069	hbp = intel_de_read(dev_priv, MIPI_HBP_COUNT(port));
1070
1071	/* harizontal values are in terms of high speed byte clock */
1072	hfp = pixels_from_txbyteclkhs(hfp, bpp, lane_count,
1073						intel_dsi->burst_mode_ratio);
1074	hsync = pixels_from_txbyteclkhs(hsync, bpp, lane_count,
1075						intel_dsi->burst_mode_ratio);
1076	hbp = pixels_from_txbyteclkhs(hbp, bpp, lane_count,
1077						intel_dsi->burst_mode_ratio);
1078
1079	if (intel_dsi->dual_link) {
1080		hfp *= 2;
1081		hsync *= 2;
1082		hbp *= 2;
1083	}
1084
1085	/* vertical values are in terms of lines */
1086	vfp = intel_de_read(dev_priv, MIPI_VFP_COUNT(port));
1087	vsync = intel_de_read(dev_priv, MIPI_VSYNC_PADDING_COUNT(port));
 
1088
1089	adjusted_mode->crtc_htotal = hactive + hfp + hsync + hbp;
1090	adjusted_mode->crtc_hsync_start = hfp + adjusted_mode->crtc_hdisplay;
1091	adjusted_mode->crtc_hsync_end = hsync + adjusted_mode->crtc_hsync_start;
1092	adjusted_mode->crtc_hblank_start = adjusted_mode->crtc_hdisplay;
1093	adjusted_mode->crtc_hblank_end = adjusted_mode->crtc_htotal;
1094
1095	adjusted_mode->crtc_vsync_start = vfp + adjusted_mode->crtc_vdisplay;
1096	adjusted_mode->crtc_vsync_end = vsync + adjusted_mode->crtc_vsync_start;
1097	adjusted_mode->crtc_vblank_start = adjusted_mode->crtc_vdisplay;
1098	adjusted_mode->crtc_vblank_end = adjusted_mode->crtc_vtotal;
1099
1100	/*
1101	 * In BXT DSI there is no regs programmed with few horizontal timings
1102	 * in Pixels but txbyteclkhs.. So retrieval process adds some
1103	 * ROUND_UP ERRORS in the process of PIXELS<==>txbyteclkhs.
1104	 * Actually here for the given adjusted_mode, we are calculating the
1105	 * value programmed to the port and then back to the horizontal timing
1106	 * param in pixels. This is the expected value, including roundup errors
1107	 * And if that is same as retrieved value from port, then
1108	 * (HW state) adjusted_mode's horizontal timings are corrected to
1109	 * match with SW state to nullify the errors.
1110	 */
1111	/* Calculating the value programmed to the Port register */
1112	hfp_sw = adjusted_mode_sw->crtc_hsync_start -
1113					adjusted_mode_sw->crtc_hdisplay;
1114	hsync_sw = adjusted_mode_sw->crtc_hsync_end -
1115					adjusted_mode_sw->crtc_hsync_start;
1116	hbp_sw = adjusted_mode_sw->crtc_htotal -
1117					adjusted_mode_sw->crtc_hsync_end;
1118
1119	if (intel_dsi->dual_link) {
1120		hfp_sw /= 2;
1121		hsync_sw /= 2;
1122		hbp_sw /= 2;
1123	}
1124
1125	hfp_sw = txbyteclkhs(hfp_sw, bpp, lane_count,
1126						intel_dsi->burst_mode_ratio);
1127	hsync_sw = txbyteclkhs(hsync_sw, bpp, lane_count,
1128			    intel_dsi->burst_mode_ratio);
1129	hbp_sw = txbyteclkhs(hbp_sw, bpp, lane_count,
1130						intel_dsi->burst_mode_ratio);
1131
1132	/* Reverse calculating the adjusted mode parameters from port reg vals*/
1133	hfp_sw = pixels_from_txbyteclkhs(hfp_sw, bpp, lane_count,
1134						intel_dsi->burst_mode_ratio);
1135	hsync_sw = pixels_from_txbyteclkhs(hsync_sw, bpp, lane_count,
1136						intel_dsi->burst_mode_ratio);
1137	hbp_sw = pixels_from_txbyteclkhs(hbp_sw, bpp, lane_count,
1138						intel_dsi->burst_mode_ratio);
1139
1140	if (intel_dsi->dual_link) {
1141		hfp_sw *= 2;
1142		hsync_sw *= 2;
1143		hbp_sw *= 2;
1144	}
1145
1146	crtc_htotal_sw = adjusted_mode_sw->crtc_hdisplay + hfp_sw +
1147							hsync_sw + hbp_sw;
1148	crtc_hsync_start_sw = hfp_sw + adjusted_mode_sw->crtc_hdisplay;
1149	crtc_hsync_end_sw = hsync_sw + crtc_hsync_start_sw;
1150	crtc_hblank_start_sw = adjusted_mode_sw->crtc_hdisplay;
1151	crtc_hblank_end_sw = crtc_htotal_sw;
1152
1153	if (adjusted_mode->crtc_htotal == crtc_htotal_sw)
1154		adjusted_mode->crtc_htotal = adjusted_mode_sw->crtc_htotal;
1155
1156	if (adjusted_mode->crtc_hsync_start == crtc_hsync_start_sw)
1157		adjusted_mode->crtc_hsync_start =
1158					adjusted_mode_sw->crtc_hsync_start;
1159
1160	if (adjusted_mode->crtc_hsync_end == crtc_hsync_end_sw)
1161		adjusted_mode->crtc_hsync_end =
1162					adjusted_mode_sw->crtc_hsync_end;
1163
1164	if (adjusted_mode->crtc_hblank_start == crtc_hblank_start_sw)
1165		adjusted_mode->crtc_hblank_start =
1166					adjusted_mode_sw->crtc_hblank_start;
1167
1168	if (adjusted_mode->crtc_hblank_end == crtc_hblank_end_sw)
1169		adjusted_mode->crtc_hblank_end =
1170					adjusted_mode_sw->crtc_hblank_end;
1171}
1172
1173static void intel_dsi_get_config(struct intel_encoder *encoder,
1174				 struct intel_crtc_state *pipe_config)
1175{
1176	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1177	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1178	u32 pclk;
1179
1180	drm_dbg_kms(&dev_priv->drm, "\n");
1181
1182	pipe_config->output_types |= BIT(INTEL_OUTPUT_DSI);
1183
1184	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
1185		bxt_dsi_get_pipe_config(encoder, pipe_config);
1186		pclk = bxt_dsi_get_pclk(encoder, pipe_config);
1187	} else {
1188		pclk = vlv_dsi_get_pclk(encoder, pipe_config);
1189	}
1190
1191	pipe_config->port_clock = pclk;
1192
1193	/* FIXME definitely not right for burst/cmd mode/pixel overlap */
1194	pipe_config->hw.adjusted_mode.crtc_clock = pclk;
1195	if (intel_dsi->dual_link)
1196		pipe_config->hw.adjusted_mode.crtc_clock *= 2;
1197}
1198
1199/* return txclkesc cycles in terms of divider and duration in us */
1200static u16 txclkesc(u32 divider, unsigned int us)
1201{
1202	switch (divider) {
1203	case ESCAPE_CLOCK_DIVIDER_1:
1204	default:
1205		return 20 * us;
1206	case ESCAPE_CLOCK_DIVIDER_2:
1207		return 10 * us;
1208	case ESCAPE_CLOCK_DIVIDER_4:
1209		return 5 * us;
1210	}
1211}
1212
1213static void set_dsi_timings(struct drm_encoder *encoder,
1214			    const struct drm_display_mode *adjusted_mode)
1215{
1216	struct drm_device *dev = encoder->dev;
1217	struct drm_i915_private *dev_priv = to_i915(dev);
1218	struct intel_dsi *intel_dsi = enc_to_intel_dsi(to_intel_encoder(encoder));
1219	enum port port;
1220	unsigned int bpp = mipi_dsi_pixel_format_to_bpp(intel_dsi->pixel_format);
1221	unsigned int lane_count = intel_dsi->lane_count;
1222
1223	u16 hactive, hfp, hsync, hbp, vfp, vsync, vbp;
1224
1225	hactive = adjusted_mode->crtc_hdisplay;
1226	hfp = adjusted_mode->crtc_hsync_start - adjusted_mode->crtc_hdisplay;
1227	hsync = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
1228	hbp = adjusted_mode->crtc_htotal - adjusted_mode->crtc_hsync_end;
1229
1230	if (intel_dsi->dual_link) {
1231		hactive /= 2;
1232		if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK)
1233			hactive += intel_dsi->pixel_overlap;
1234		hfp /= 2;
1235		hsync /= 2;
1236		hbp /= 2;
1237	}
1238
1239	vfp = adjusted_mode->crtc_vsync_start - adjusted_mode->crtc_vdisplay;
1240	vsync = adjusted_mode->crtc_vsync_end - adjusted_mode->crtc_vsync_start;
1241	vbp = adjusted_mode->crtc_vtotal - adjusted_mode->crtc_vsync_end;
1242
1243	/* horizontal values are in terms of high speed byte clock */
1244	hactive = txbyteclkhs(hactive, bpp, lane_count,
1245			      intel_dsi->burst_mode_ratio);
1246	hfp = txbyteclkhs(hfp, bpp, lane_count, intel_dsi->burst_mode_ratio);
1247	hsync = txbyteclkhs(hsync, bpp, lane_count,
1248			    intel_dsi->burst_mode_ratio);
1249	hbp = txbyteclkhs(hbp, bpp, lane_count, intel_dsi->burst_mode_ratio);
1250
1251	for_each_dsi_port(port, intel_dsi->ports) {
1252		if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
1253			/*
1254			 * Program hdisplay and vdisplay on MIPI transcoder.
1255			 * This is different from calculated hactive and
1256			 * vactive, as they are calculated per channel basis,
1257			 * whereas these values should be based on resolution.
1258			 */
1259			intel_de_write(dev_priv, BXT_MIPI_TRANS_HACTIVE(port),
1260				       adjusted_mode->crtc_hdisplay);
1261			intel_de_write(dev_priv, BXT_MIPI_TRANS_VACTIVE(port),
1262				       adjusted_mode->crtc_vdisplay);
1263			intel_de_write(dev_priv, BXT_MIPI_TRANS_VTOTAL(port),
1264				       adjusted_mode->crtc_vtotal);
1265		}
1266
1267		intel_de_write(dev_priv, MIPI_HACTIVE_AREA_COUNT(port),
1268			       hactive);
1269		intel_de_write(dev_priv, MIPI_HFP_COUNT(port), hfp);
1270
1271		/* meaningful for video mode non-burst sync pulse mode only,
1272		 * can be zero for non-burst sync events and burst modes */
1273		intel_de_write(dev_priv, MIPI_HSYNC_PADDING_COUNT(port),
1274			       hsync);
1275		intel_de_write(dev_priv, MIPI_HBP_COUNT(port), hbp);
1276
1277		/* vertical values are in terms of lines */
1278		intel_de_write(dev_priv, MIPI_VFP_COUNT(port), vfp);
1279		intel_de_write(dev_priv, MIPI_VSYNC_PADDING_COUNT(port),
1280			       vsync);
1281		intel_de_write(dev_priv, MIPI_VBP_COUNT(port), vbp);
1282	}
1283}
1284
1285static u32 pixel_format_to_reg(enum mipi_dsi_pixel_format fmt)
1286{
1287	switch (fmt) {
1288	case MIPI_DSI_FMT_RGB888:
1289		return VID_MODE_FORMAT_RGB888;
1290	case MIPI_DSI_FMT_RGB666:
1291		return VID_MODE_FORMAT_RGB666;
1292	case MIPI_DSI_FMT_RGB666_PACKED:
1293		return VID_MODE_FORMAT_RGB666_PACKED;
1294	case MIPI_DSI_FMT_RGB565:
1295		return VID_MODE_FORMAT_RGB565;
1296	default:
1297		MISSING_CASE(fmt);
1298		return VID_MODE_FORMAT_RGB666;
1299	}
1300}
1301
1302static void intel_dsi_prepare(struct intel_encoder *intel_encoder,
1303			      const struct intel_crtc_state *pipe_config)
1304{
1305	struct drm_encoder *encoder = &intel_encoder->base;
1306	struct drm_device *dev = encoder->dev;
1307	struct drm_i915_private *dev_priv = to_i915(dev);
1308	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
1309	struct intel_dsi *intel_dsi = enc_to_intel_dsi(to_intel_encoder(encoder));
1310	const struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
1311	enum port port;
1312	unsigned int bpp = mipi_dsi_pixel_format_to_bpp(intel_dsi->pixel_format);
1313	u32 val, tmp;
1314	u16 mode_hdisplay;
1315
1316	drm_dbg_kms(&dev_priv->drm, "pipe %c\n", pipe_name(crtc->pipe));
1317
1318	mode_hdisplay = adjusted_mode->crtc_hdisplay;
1319
1320	if (intel_dsi->dual_link) {
1321		mode_hdisplay /= 2;
1322		if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK)
1323			mode_hdisplay += intel_dsi->pixel_overlap;
1324	}
1325
1326	for_each_dsi_port(port, intel_dsi->ports) {
1327		if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
1328			/*
1329			 * escape clock divider, 20MHz, shared for A and C.
1330			 * device ready must be off when doing this! txclkesc?
1331			 */
1332			tmp = intel_de_read(dev_priv, MIPI_CTRL(PORT_A));
1333			tmp &= ~ESCAPE_CLOCK_DIVIDER_MASK;
1334			intel_de_write(dev_priv, MIPI_CTRL(PORT_A),
1335				       tmp | ESCAPE_CLOCK_DIVIDER_1);
1336
1337			/* read request priority is per pipe */
1338			tmp = intel_de_read(dev_priv, MIPI_CTRL(port));
1339			tmp &= ~READ_REQUEST_PRIORITY_MASK;
1340			intel_de_write(dev_priv, MIPI_CTRL(port),
1341				       tmp | READ_REQUEST_PRIORITY_HIGH);
1342		} else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
1343			enum pipe pipe = crtc->pipe;
1344
1345			intel_de_rmw(dev_priv, MIPI_CTRL(port),
1346				     BXT_PIPE_SELECT_MASK, BXT_PIPE_SELECT(pipe));
 
 
 
1347		}
1348
1349		/* XXX: why here, why like this? handling in irq handler?! */
1350		intel_de_write(dev_priv, MIPI_INTR_STAT(port), 0xffffffff);
1351		intel_de_write(dev_priv, MIPI_INTR_EN(port), 0xffffffff);
1352
1353		intel_de_write(dev_priv, MIPI_DPHY_PARAM(port),
1354			       intel_dsi->dphy_reg);
1355
1356		intel_de_write(dev_priv, MIPI_DPI_RESOLUTION(port),
1357			       adjusted_mode->crtc_vdisplay << VERTICAL_ADDRESS_SHIFT | mode_hdisplay << HORIZONTAL_ADDRESS_SHIFT);
1358	}
1359
1360	set_dsi_timings(encoder, adjusted_mode);
1361
1362	val = intel_dsi->lane_count << DATA_LANES_PRG_REG_SHIFT;
1363	if (is_cmd_mode(intel_dsi)) {
1364		val |= intel_dsi->channel << CMD_MODE_CHANNEL_NUMBER_SHIFT;
1365		val |= CMD_MODE_DATA_WIDTH_8_BIT; /* XXX */
1366	} else {
1367		val |= intel_dsi->channel << VID_MODE_CHANNEL_NUMBER_SHIFT;
1368		val |= pixel_format_to_reg(intel_dsi->pixel_format);
1369	}
1370
1371	tmp = 0;
1372	if (intel_dsi->eotp_pkt == 0)
1373		tmp |= EOT_DISABLE;
1374	if (intel_dsi->clock_stop)
1375		tmp |= CLOCKSTOP;
1376
1377	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
1378		tmp |= BXT_DPHY_DEFEATURE_EN;
1379		if (!is_cmd_mode(intel_dsi))
1380			tmp |= BXT_DEFEATURE_DPI_FIFO_CTR;
1381	}
1382
1383	for_each_dsi_port(port, intel_dsi->ports) {
1384		intel_de_write(dev_priv, MIPI_DSI_FUNC_PRG(port), val);
1385
1386		/* timeouts for recovery. one frame IIUC. if counter expires,
1387		 * EOT and stop state. */
1388
1389		/*
1390		 * In burst mode, value greater than one DPI line Time in byte
1391		 * clock (txbyteclkhs) To timeout this timer 1+ of the above
1392		 * said value is recommended.
1393		 *
1394		 * In non-burst mode, Value greater than one DPI frame time in
1395		 * byte clock(txbyteclkhs) To timeout this timer 1+ of the above
1396		 * said value is recommended.
1397		 *
1398		 * In DBI only mode, value greater than one DBI frame time in
1399		 * byte clock(txbyteclkhs) To timeout this timer 1+ of the above
1400		 * said value is recommended.
1401		 */
1402
1403		if (is_vid_mode(intel_dsi) &&
1404			intel_dsi->video_mode == BURST_MODE) {
1405			intel_de_write(dev_priv, MIPI_HS_TX_TIMEOUT(port),
1406				       txbyteclkhs(adjusted_mode->crtc_htotal, bpp, intel_dsi->lane_count, intel_dsi->burst_mode_ratio) + 1);
1407		} else {
1408			intel_de_write(dev_priv, MIPI_HS_TX_TIMEOUT(port),
1409				       txbyteclkhs(adjusted_mode->crtc_vtotal * adjusted_mode->crtc_htotal, bpp, intel_dsi->lane_count, intel_dsi->burst_mode_ratio) + 1);
1410		}
1411		intel_de_write(dev_priv, MIPI_LP_RX_TIMEOUT(port),
1412			       intel_dsi->lp_rx_timeout);
1413		intel_de_write(dev_priv, MIPI_TURN_AROUND_TIMEOUT(port),
1414			       intel_dsi->turn_arnd_val);
1415		intel_de_write(dev_priv, MIPI_DEVICE_RESET_TIMER(port),
1416			       intel_dsi->rst_timer_val);
1417
1418		/* dphy stuff */
1419
1420		/* in terms of low power clock */
1421		intel_de_write(dev_priv, MIPI_INIT_COUNT(port),
1422			       txclkesc(intel_dsi->escape_clk_div, 100));
1423
1424		if ((IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) &&
1425		    !intel_dsi->dual_link) {
1426			/*
1427			 * BXT spec says write MIPI_INIT_COUNT for
1428			 * both the ports, even if only one is
1429			 * getting used. So write the other port
1430			 * if not in dual link mode.
1431			 */
1432			intel_de_write(dev_priv,
1433				       MIPI_INIT_COUNT(port == PORT_A ? PORT_C : PORT_A),
1434				       intel_dsi->init_count);
1435		}
1436
1437		/* recovery disables */
1438		intel_de_write(dev_priv, MIPI_EOT_DISABLE(port), tmp);
1439
1440		/* in terms of low power clock */
1441		intel_de_write(dev_priv, MIPI_INIT_COUNT(port),
1442			       intel_dsi->init_count);
1443
1444		/* in terms of txbyteclkhs. actual high to low switch +
1445		 * MIPI_STOP_STATE_STALL * MIPI_LP_BYTECLK.
1446		 *
1447		 * XXX: write MIPI_STOP_STATE_STALL?
1448		 */
1449		intel_de_write(dev_priv, MIPI_HIGH_LOW_SWITCH_COUNT(port),
1450			       intel_dsi->hs_to_lp_count);
1451
1452		/* XXX: low power clock equivalence in terms of byte clock.
1453		 * the number of byte clocks occupied in one low power clock.
1454		 * based on txbyteclkhs and txclkesc.
1455		 * txclkesc time / txbyteclk time * (105 + MIPI_STOP_STATE_STALL
1456		 * ) / 105.???
1457		 */
1458		intel_de_write(dev_priv, MIPI_LP_BYTECLK(port),
1459			       intel_dsi->lp_byte_clk);
1460
1461		if (IS_GEMINILAKE(dev_priv)) {
1462			intel_de_write(dev_priv, MIPI_TLPX_TIME_COUNT(port),
1463				       intel_dsi->lp_byte_clk);
1464			/* Shadow of DPHY reg */
1465			intel_de_write(dev_priv, MIPI_CLK_LANE_TIMING(port),
1466				       intel_dsi->dphy_reg);
1467		}
1468
1469		/* the bw essential for transmitting 16 long packets containing
1470		 * 252 bytes meant for dcs write memory command is programmed in
1471		 * this register in terms of byte clocks. based on dsi transfer
1472		 * rate and the number of lanes configured the time taken to
1473		 * transmit 16 long packets in a dsi stream varies. */
1474		intel_de_write(dev_priv, MIPI_DBI_BW_CTRL(port),
1475			       intel_dsi->bw_timer);
1476
1477		intel_de_write(dev_priv, MIPI_CLK_LANE_SWITCH_TIME_CNT(port),
1478			       intel_dsi->clk_lp_to_hs_count << LP_HS_SSW_CNT_SHIFT | intel_dsi->clk_hs_to_lp_count << HS_LP_PWR_SW_CNT_SHIFT);
1479
1480		if (is_vid_mode(intel_dsi)) {
1481			u32 fmt = intel_dsi->video_frmt_cfg_bits | IP_TG_CONFIG;
1482
1483			/*
1484			 * Some panels might have resolution which is not a
1485			 * multiple of 64 like 1366 x 768. Enable RANDOM
1486			 * resolution support for such panels by default.
1487			 */
1488			fmt |= RANDOM_DPI_DISPLAY_RESOLUTION;
1489
1490			switch (intel_dsi->video_mode) {
1491			default:
1492				MISSING_CASE(intel_dsi->video_mode);
1493				fallthrough;
1494			case NON_BURST_SYNC_EVENTS:
1495				fmt |= VIDEO_MODE_NON_BURST_WITH_SYNC_EVENTS;
1496				break;
1497			case NON_BURST_SYNC_PULSE:
1498				fmt |= VIDEO_MODE_NON_BURST_WITH_SYNC_PULSE;
1499				break;
1500			case BURST_MODE:
1501				fmt |= VIDEO_MODE_BURST;
1502				break;
1503			}
1504
1505			intel_de_write(dev_priv, MIPI_VIDEO_MODE_FORMAT(port), fmt);
1506		}
1507	}
1508}
1509
1510static void intel_dsi_unprepare(struct intel_encoder *encoder)
1511{
1512	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1513	struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1514	enum port port;
 
1515
1516	if (IS_GEMINILAKE(dev_priv))
1517		return;
1518
1519	for_each_dsi_port(port, intel_dsi->ports) {
1520		/* Panel commands can be sent when clock is in LP11 */
1521		intel_de_write(dev_priv, MIPI_DEVICE_READY(port), 0x0);
1522
1523		if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
1524			bxt_dsi_reset_clocks(encoder, port);
1525		else
1526			vlv_dsi_reset_clocks(encoder, port);
1527		intel_de_write(dev_priv, MIPI_EOT_DISABLE(port), CLOCKSTOP);
1528
1529		intel_de_rmw(dev_priv, MIPI_DSI_FUNC_PRG(port), VID_MODE_FORMAT_MASK, 0);
 
 
1530
1531		intel_de_write(dev_priv, MIPI_DEVICE_READY(port), 0x1);
1532	}
1533}
1534
1535static const struct drm_encoder_funcs intel_dsi_funcs = {
1536	.destroy = intel_encoder_destroy,
1537};
1538
1539static enum drm_mode_status vlv_dsi_mode_valid(struct drm_connector *connector,
1540					       struct drm_display_mode *mode)
1541{
1542	struct drm_i915_private *i915 = to_i915(connector->dev);
1543
1544	if (IS_VALLEYVIEW(i915) || IS_CHERRYVIEW(i915)) {
1545		enum drm_mode_status status;
 
1546
1547		status = intel_cpu_transcoder_mode_valid(i915, mode);
1548		if (status != MODE_OK)
1549			return status;
1550	}
1551
1552	return intel_dsi_mode_valid(connector, mode);
1553}
1554
1555static const struct drm_connector_helper_funcs intel_dsi_connector_helper_funcs = {
1556	.get_modes = intel_dsi_get_modes,
1557	.mode_valid = vlv_dsi_mode_valid,
1558	.atomic_check = intel_digital_connector_atomic_check,
1559};
1560
1561static const struct drm_connector_funcs intel_dsi_connector_funcs = {
1562	.detect = intel_panel_detect,
1563	.late_register = intel_connector_register,
1564	.early_unregister = intel_connector_unregister,
1565	.destroy = intel_connector_destroy,
1566	.fill_modes = drm_helper_probe_single_connector_modes,
1567	.atomic_get_property = intel_digital_connector_atomic_get_property,
1568	.atomic_set_property = intel_digital_connector_atomic_set_property,
1569	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1570	.atomic_duplicate_state = intel_digital_connector_duplicate_state,
1571};
1572
1573static void vlv_dsi_add_properties(struct intel_connector *connector)
1574{
1575	const struct drm_display_mode *fixed_mode =
1576		intel_panel_preferred_fixed_mode(connector);
1577
1578	intel_attach_scaling_mode_property(&connector->base);
1579
1580	drm_connector_set_panel_orientation_with_quirk(&connector->base,
1581						       intel_dsi_get_panel_orientation(connector),
1582						       fixed_mode->hdisplay,
1583						       fixed_mode->vdisplay);
1584}
1585
1586#define NS_KHZ_RATIO		1000000
1587
1588#define PREPARE_CNT_MAX		0x3F
1589#define EXIT_ZERO_CNT_MAX	0x3F
1590#define CLK_ZERO_CNT_MAX	0xFF
1591#define TRAIL_CNT_MAX		0x1F
1592
1593static void vlv_dphy_param_init(struct intel_dsi *intel_dsi)
1594{
1595	struct drm_device *dev = intel_dsi->base.base.dev;
1596	struct drm_i915_private *dev_priv = to_i915(dev);
1597	struct intel_connector *connector = intel_dsi->attached_connector;
1598	struct mipi_config *mipi_config = connector->panel.vbt.dsi.config;
1599	u32 tlpx_ns, extra_byte_count, tlpx_ui;
1600	u32 ui_num, ui_den;
1601	u32 prepare_cnt, exit_zero_cnt, clk_zero_cnt, trail_cnt;
1602	u32 ths_prepare_ns, tclk_trail_ns;
1603	u32 tclk_prepare_clkzero, ths_prepare_hszero;
1604	u32 lp_to_hs_switch, hs_to_lp_switch;
1605	u32 mul;
1606
1607	tlpx_ns = intel_dsi_tlpx_ns(intel_dsi);
1608
1609	switch (intel_dsi->lane_count) {
1610	case 1:
1611	case 2:
1612		extra_byte_count = 2;
1613		break;
1614	case 3:
1615		extra_byte_count = 4;
1616		break;
1617	case 4:
1618	default:
1619		extra_byte_count = 3;
1620		break;
1621	}
1622
1623	/* in Kbps */
1624	ui_num = NS_KHZ_RATIO;
1625	ui_den = intel_dsi_bitrate(intel_dsi);
1626
1627	tclk_prepare_clkzero = mipi_config->tclk_prepare_clkzero;
1628	ths_prepare_hszero = mipi_config->ths_prepare_hszero;
1629
1630	/*
1631	 * B060
1632	 * LP byte clock = TLPX/ (8UI)
1633	 */
1634	intel_dsi->lp_byte_clk = DIV_ROUND_UP(tlpx_ns * ui_den, 8 * ui_num);
1635
1636	/* DDR clock period = 2 * UI
1637	 * UI(sec) = 1/(bitrate * 10^3) (bitrate is in KHZ)
1638	 * UI(nsec) = 10^6 / bitrate
1639	 * DDR clock period (nsec) = 2 * UI = (2 * 10^6)/ bitrate
1640	 * DDR clock count  = ns_value / DDR clock period
1641	 *
1642	 * For GEMINILAKE dphy_param_reg will be programmed in terms of
1643	 * HS byte clock count for other platform in HS ddr clock count
1644	 */
1645	mul = IS_GEMINILAKE(dev_priv) ? 8 : 2;
1646	ths_prepare_ns = max(mipi_config->ths_prepare,
1647			     mipi_config->tclk_prepare);
1648
1649	/* prepare count */
1650	prepare_cnt = DIV_ROUND_UP(ths_prepare_ns * ui_den, ui_num * mul);
1651
1652	if (prepare_cnt > PREPARE_CNT_MAX) {
1653		drm_dbg_kms(&dev_priv->drm, "prepare count too high %u\n",
1654			    prepare_cnt);
1655		prepare_cnt = PREPARE_CNT_MAX;
1656	}
1657
1658	/* exit zero count */
1659	exit_zero_cnt = DIV_ROUND_UP(
1660				(ths_prepare_hszero - ths_prepare_ns) * ui_den,
1661				ui_num * mul
1662				);
1663
1664	/*
1665	 * Exit zero is unified val ths_zero and ths_exit
1666	 * minimum value for ths_exit = 110ns
1667	 * min (exit_zero_cnt * 2) = 110/UI
1668	 * exit_zero_cnt = 55/UI
1669	 */
1670	if (exit_zero_cnt < (55 * ui_den / ui_num) && (55 * ui_den) % ui_num)
1671		exit_zero_cnt += 1;
1672
1673	if (exit_zero_cnt > EXIT_ZERO_CNT_MAX) {
1674		drm_dbg_kms(&dev_priv->drm, "exit zero count too high %u\n",
1675			    exit_zero_cnt);
1676		exit_zero_cnt = EXIT_ZERO_CNT_MAX;
1677	}
1678
1679	/* clk zero count */
1680	clk_zero_cnt = DIV_ROUND_UP(
1681				(tclk_prepare_clkzero -	ths_prepare_ns)
1682				* ui_den, ui_num * mul);
1683
1684	if (clk_zero_cnt > CLK_ZERO_CNT_MAX) {
1685		drm_dbg_kms(&dev_priv->drm, "clock zero count too high %u\n",
1686			    clk_zero_cnt);
1687		clk_zero_cnt = CLK_ZERO_CNT_MAX;
1688	}
1689
1690	/* trail count */
1691	tclk_trail_ns = max(mipi_config->tclk_trail, mipi_config->ths_trail);
1692	trail_cnt = DIV_ROUND_UP(tclk_trail_ns * ui_den, ui_num * mul);
1693
1694	if (trail_cnt > TRAIL_CNT_MAX) {
1695		drm_dbg_kms(&dev_priv->drm, "trail count too high %u\n",
1696			    trail_cnt);
1697		trail_cnt = TRAIL_CNT_MAX;
1698	}
1699
1700	/* B080 */
1701	intel_dsi->dphy_reg = exit_zero_cnt << 24 | trail_cnt << 16 |
1702						clk_zero_cnt << 8 | prepare_cnt;
1703
1704	/*
1705	 * LP to HS switch count = 4TLPX + PREP_COUNT * mul + EXIT_ZERO_COUNT *
1706	 *					mul + 10UI + Extra Byte Count
1707	 *
1708	 * HS to LP switch count = THS-TRAIL + 2TLPX + Extra Byte Count
1709	 * Extra Byte Count is calculated according to number of lanes.
1710	 * High Low Switch Count is the Max of LP to HS and
1711	 * HS to LP switch count
1712	 *
1713	 */
1714	tlpx_ui = DIV_ROUND_UP(tlpx_ns * ui_den, ui_num);
1715
1716	/* B044 */
1717	/* FIXME:
1718	 * The comment above does not match with the code */
1719	lp_to_hs_switch = DIV_ROUND_UP(4 * tlpx_ui + prepare_cnt * mul +
1720						exit_zero_cnt * mul + 10, 8);
1721
1722	hs_to_lp_switch = DIV_ROUND_UP(mipi_config->ths_trail + 2 * tlpx_ui, 8);
1723
1724	intel_dsi->hs_to_lp_count = max(lp_to_hs_switch, hs_to_lp_switch);
1725	intel_dsi->hs_to_lp_count += extra_byte_count;
1726
1727	/* B088 */
1728	/* LP -> HS for clock lanes
1729	 * LP clk sync + LP11 + LP01 + tclk_prepare + tclk_zero +
1730	 *						extra byte count
1731	 * 2TPLX + 1TLPX + 1 TPLX(in ns) + prepare_cnt * 2 + clk_zero_cnt *
1732	 *					2(in UI) + extra byte count
1733	 * In byteclks = (4TLPX + prepare_cnt * 2 + clk_zero_cnt *2 (in UI)) /
1734	 *					8 + extra byte count
1735	 */
1736	intel_dsi->clk_lp_to_hs_count =
1737		DIV_ROUND_UP(
1738			4 * tlpx_ui + prepare_cnt * 2 +
1739			clk_zero_cnt * 2,
1740			8);
1741
1742	intel_dsi->clk_lp_to_hs_count += extra_byte_count;
1743
1744	/* HS->LP for Clock Lanes
1745	 * Low Power clock synchronisations + 1Tx byteclk + tclk_trail +
1746	 *						Extra byte count
1747	 * 2TLPX + 8UI + (trail_count*2)(in UI) + Extra byte count
1748	 * In byteclks = (2*TLpx(in UI) + trail_count*2 +8)(in UI)/8 +
1749	 *						Extra byte count
1750	 */
1751	intel_dsi->clk_hs_to_lp_count =
1752		DIV_ROUND_UP(2 * tlpx_ui + trail_cnt * 2 + 8,
1753			8);
1754	intel_dsi->clk_hs_to_lp_count += extra_byte_count;
1755
1756	intel_dsi_log_params(intel_dsi);
1757}
1758
1759typedef void (*vlv_dsi_dmi_quirk_func)(struct intel_dsi *intel_dsi);
1760
1761/*
1762 * Vtotal is wrong on the Asus TF103C leading to the last line of the display
1763 * being shown as the first line. The factory installed Android has a hardcoded
1764 * modeline, causing it to not suffer from this BIOS bug.
1765 *
1766 * Original mode: "1280x800": 60 67700 1280 1312 1328 1376 800 808 812 820 0x8 0xa
1767 * Fixed    mode: "1280x800": 60 67700 1280 1312 1328 1376 800 808 812 816 0x8 0xa
1768 *
1769 * https://gitlab.freedesktop.org/drm/intel/-/issues/9381
1770 */
1771static void vlv_dsi_asus_tf103c_mode_fixup(struct intel_dsi *intel_dsi)
1772{
1773	/* Cast away the const as we want to fixup the mode */
1774	struct drm_display_mode *fixed_mode = (struct drm_display_mode *)
1775		intel_panel_preferred_fixed_mode(intel_dsi->attached_connector);
1776
1777	if (fixed_mode->vtotal == 820)
1778		fixed_mode->vtotal -= 4;
1779}
1780
1781/*
1782 * On the Lenovo Yoga Tablet 2 830 / 1050 there are 2 problems:
1783 * 1. The I2C MIPI sequence elements reference bus 3. ACPI has I2C1 - I2C7
1784 *    which under Linux become bus 0 - 6. And the MIPI sequence reference
1785 *    to bus 3 is indented for I2C3 which is bus 2 under Linux.
1786 *
1787 *    Note mipi_exec_i2c() cannot just subtract 1 from the bus
1788 *    given in the I2C MIPI sequence element. Since on other
1789 *    devices the I2C bus-numbers used in the MIPI sequences do
1790 *    actually start at 0.
1791 *
1792 * 2. width_/height_mm contain a bogus 192mm x 120mm size. This is
1793 *    especially a problem on the 8" 830 version which uses a 10:16
1794 *    portrait screen where as the bogus size is 16:10.
1795 *
1796 * https://gitlab.freedesktop.org/drm/intel/-/issues/9379
1797 */
1798static void vlv_dsi_lenovo_yoga_tab2_size_fixup(struct intel_dsi *intel_dsi)
1799{
1800	const struct drm_display_mode *fixed_mode =
1801		intel_panel_preferred_fixed_mode(intel_dsi->attached_connector);
1802	struct drm_display_info *info = &intel_dsi->attached_connector->base.display_info;
1803
1804	intel_dsi->i2c_bus_num = 2;
1805
1806	/*
1807	 * The 10" 1050 uses a 1920x1200 landscape screen, where as the 8" 830
1808	 * uses a 1200x1920 portrait screen.
1809	 */
1810	if (fixed_mode->hdisplay == 1920) {
1811		info->width_mm = 216;
1812		info->height_mm = 135;
1813	} else {
1814		info->width_mm = 107;
1815		info->height_mm = 171;
1816	}
1817}
1818
1819/*
1820 * On the Lenovo Yoga Tab 3 Pro YT3-X90F there are 2 problems:
1821 * 1. i2c_acpi_find_adapter() picks the wrong adapter causing mipi_exec_i2c()
1822 *    to not work. Fix this by setting i2c_bus_num.
1823 * 2. There is no backlight off MIPI sequence, causing the backlight to stay on.
1824 *    Add a backlight off sequence mirroring the existing backlight on sequence.
1825 *
1826 * https://gitlab.freedesktop.org/drm/intel/-/issues/9380
1827 */
1828static void vlv_dsi_lenovo_yoga_tab3_backlight_fixup(struct intel_dsi *intel_dsi)
1829{
1830	static const u8 backlight_off_sequence[16] = {
1831		/* Header Seq-id 7, length after header 11 bytes */
1832		0x07, 0x0b, 0x00, 0x00, 0x00,
1833		/* MIPI_SEQ_ELEM_I2C bus 0 addr 0x2c reg 0x00 data-len 1 data 0x00 */
1834		0x04, 0x08, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x01, 0x00,
1835		/* MIPI_SEQ_ELEM_END */
1836		0x00
1837	};
1838	struct intel_connector *connector = intel_dsi->attached_connector;
1839
1840	intel_dsi->i2c_bus_num = 0;
1841	connector->panel.vbt.dsi.sequence[MIPI_SEQ_BACKLIGHT_OFF] = backlight_off_sequence;
1842}
1843
1844static const struct dmi_system_id vlv_dsi_dmi_quirk_table[] = {
1845	{
1846		/* Asus Transformer Pad TF103C */
1847		.matches = {
1848			DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
1849			DMI_MATCH(DMI_PRODUCT_NAME, "TF103C"),
1850		},
1851		.driver_data = (void *)vlv_dsi_asus_tf103c_mode_fixup,
1852	},
1853	{
1854		/*
1855		 * Lenovo Yoga Tablet 2 830F/L or 1050F/L (The 8" and 10"
1856		 * Lenovo Yoga Tablet 2 use the same mainboard)
1857		 */
1858		.matches = {
1859			DMI_MATCH(DMI_SYS_VENDOR, "Intel Corp."),
1860			DMI_MATCH(DMI_PRODUCT_NAME, "VALLEYVIEW C0 PLATFORM"),
1861			DMI_MATCH(DMI_BOARD_NAME, "BYT-T FFD8"),
1862			/* Partial match on beginning of BIOS version */
1863			DMI_MATCH(DMI_BIOS_VERSION, "BLADE_21"),
1864		},
1865		.driver_data = (void *)vlv_dsi_lenovo_yoga_tab2_size_fixup,
1866	},
1867	{
1868		/* Lenovo Yoga Tab 3 Pro YT3-X90F */
1869		.matches = {
1870			DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
1871			DMI_MATCH(DMI_PRODUCT_NAME, "CHERRYVIEW D1 PLATFORM"),
1872			DMI_MATCH(DMI_PRODUCT_VERSION, "Blade3-10A-001"),
1873		},
1874		.driver_data = (void *)vlv_dsi_lenovo_yoga_tab3_backlight_fixup,
1875	},
1876	{ }
1877};
1878
1879void vlv_dsi_init(struct drm_i915_private *dev_priv)
1880{
1881	struct intel_dsi *intel_dsi;
1882	struct intel_encoder *intel_encoder;
1883	struct drm_encoder *encoder;
1884	struct intel_connector *intel_connector;
1885	struct drm_connector *connector;
1886	struct drm_display_mode *current_mode;
1887	const struct dmi_system_id *dmi_id;
1888	enum port port;
1889	enum pipe pipe;
1890
1891	drm_dbg_kms(&dev_priv->drm, "\n");
1892
1893	/* There is no detection method for MIPI so rely on VBT */
1894	if (!intel_bios_is_dsi_present(dev_priv, &port))
1895		return;
1896
1897	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
1898		dev_priv->display.dsi.mmio_base = BXT_MIPI_BASE;
1899	else
1900		dev_priv->display.dsi.mmio_base = VLV_MIPI_BASE;
1901
1902	intel_dsi = kzalloc(sizeof(*intel_dsi), GFP_KERNEL);
1903	if (!intel_dsi)
1904		return;
1905
1906	intel_connector = intel_connector_alloc();
1907	if (!intel_connector) {
1908		kfree(intel_dsi);
1909		return;
1910	}
1911
1912	intel_encoder = &intel_dsi->base;
1913	encoder = &intel_encoder->base;
1914	intel_dsi->attached_connector = intel_connector;
1915
1916	connector = &intel_connector->base;
1917
1918	drm_encoder_init(&dev_priv->drm, encoder, &intel_dsi_funcs, DRM_MODE_ENCODER_DSI,
1919			 "DSI %c", port_name(port));
1920
1921	intel_encoder->compute_config = intel_dsi_compute_config;
1922	intel_encoder->pre_enable = intel_dsi_pre_enable;
1923	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
1924		intel_encoder->enable = bxt_dsi_enable;
1925	intel_encoder->disable = intel_dsi_disable;
1926	intel_encoder->post_disable = intel_dsi_post_disable;
1927	intel_encoder->get_hw_state = intel_dsi_get_hw_state;
1928	intel_encoder->get_config = intel_dsi_get_config;
1929	intel_encoder->update_pipe = intel_backlight_update;
1930	intel_encoder->shutdown = intel_dsi_shutdown;
1931
1932	intel_connector->get_hw_state = intel_connector_get_hw_state;
1933
1934	intel_encoder->port = port;
1935	intel_encoder->type = INTEL_OUTPUT_DSI;
1936	intel_encoder->power_domain = POWER_DOMAIN_PORT_DSI;
1937	intel_encoder->cloneable = 0;
1938
1939	/*
1940	 * On BYT/CHV, pipe A maps to MIPI DSI port A, pipe B maps to MIPI DSI
1941	 * port C. BXT isn't limited like this.
1942	 */
1943	if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
1944		intel_encoder->pipe_mask = ~0;
1945	else if (port == PORT_A)
1946		intel_encoder->pipe_mask = BIT(PIPE_A);
1947	else
1948		intel_encoder->pipe_mask = BIT(PIPE_B);
1949
1950	intel_dsi->panel_power_off_time = ktime_get_boottime();
1951
1952	intel_bios_init_panel_late(dev_priv, &intel_connector->panel, NULL, NULL);
1953
1954	if (intel_connector->panel.vbt.dsi.config->dual_link)
1955		intel_dsi->ports = BIT(PORT_A) | BIT(PORT_C);
1956	else
1957		intel_dsi->ports = BIT(port);
1958
1959	if (drm_WARN_ON(&dev_priv->drm, intel_connector->panel.vbt.dsi.bl_ports & ~intel_dsi->ports))
1960		intel_connector->panel.vbt.dsi.bl_ports &= intel_dsi->ports;
1961
1962	if (drm_WARN_ON(&dev_priv->drm, intel_connector->panel.vbt.dsi.cabc_ports & ~intel_dsi->ports))
1963		intel_connector->panel.vbt.dsi.cabc_ports &= intel_dsi->ports;
1964
1965	/* Create a DSI host (and a device) for each port. */
1966	for_each_dsi_port(port, intel_dsi->ports) {
1967		struct intel_dsi_host *host;
1968
1969		host = intel_dsi_host_init(intel_dsi, &intel_dsi_host_ops,
1970					   port);
1971		if (!host)
1972			goto err;
1973
1974		intel_dsi->dsi_hosts[port] = host;
1975	}
1976
1977	if (!intel_dsi_vbt_init(intel_dsi, MIPI_DSI_GENERIC_PANEL_ID)) {
1978		drm_dbg_kms(&dev_priv->drm, "no device found\n");
1979		goto err;
1980	}
1981
1982	/* Use clock read-back from current hw-state for fastboot */
1983	current_mode = intel_encoder_current_mode(intel_encoder);
1984	if (current_mode) {
1985		drm_dbg_kms(&dev_priv->drm, "Calculated pclk %d GOP %d\n",
1986			    intel_dsi->pclk, current_mode->clock);
1987		if (intel_fuzzy_clock_check(intel_dsi->pclk,
1988					    current_mode->clock)) {
1989			drm_dbg_kms(&dev_priv->drm, "Using GOP pclk\n");
1990			intel_dsi->pclk = current_mode->clock;
1991		}
1992
1993		kfree(current_mode);
1994	}
1995
1996	vlv_dphy_param_init(intel_dsi);
1997
1998	intel_dsi_vbt_gpio_init(intel_dsi,
1999				intel_dsi_get_hw_state(intel_encoder, &pipe));
2000
2001	drm_connector_init(&dev_priv->drm, connector, &intel_dsi_connector_funcs,
2002			   DRM_MODE_CONNECTOR_DSI);
2003
2004	drm_connector_helper_add(connector, &intel_dsi_connector_helper_funcs);
2005
2006	connector->display_info.subpixel_order = SubPixelHorizontalRGB; /*XXX*/
2007
2008	intel_connector_attach_encoder(intel_connector, intel_encoder);
2009
2010	mutex_lock(&dev_priv->drm.mode_config.mutex);
2011	intel_panel_add_vbt_lfp_fixed_mode(intel_connector);
2012	mutex_unlock(&dev_priv->drm.mode_config.mutex);
2013
2014	if (!intel_panel_preferred_fixed_mode(intel_connector)) {
2015		drm_dbg_kms(&dev_priv->drm, "no fixed mode\n");
2016		goto err_cleanup_connector;
2017	}
2018
2019	dmi_id = dmi_first_match(vlv_dsi_dmi_quirk_table);
2020	if (dmi_id) {
2021		vlv_dsi_dmi_quirk_func quirk_func =
2022			(vlv_dsi_dmi_quirk_func)dmi_id->driver_data;
2023
2024		quirk_func(intel_dsi);
2025	}
2026
2027	intel_panel_init(intel_connector, NULL);
2028
2029	intel_backlight_setup(intel_connector, INVALID_PIPE);
2030
2031	vlv_dsi_add_properties(intel_connector);
2032
2033	return;
2034
2035err_cleanup_connector:
2036	drm_connector_cleanup(&intel_connector->base);
2037err:
2038	drm_encoder_cleanup(&intel_encoder->base);
2039	kfree(intel_dsi);
2040	kfree(intel_connector);
2041}