linux/drivers/gpu/drm/i915/vlv_dsi.c
<<
>>
Prefs
   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 <drm/drm_atomic_helper.h>
  27#include <drm/drm_crtc.h>
  28#include <drm/drm_edid.h>
  29#include <drm/i915_drm.h>
  30#include <drm/drm_mipi_dsi.h>
  31#include <linux/slab.h>
  32#include <linux/gpio/consumer.h>
  33#include "i915_drv.h"
  34#include "intel_drv.h"
  35#include "intel_dsi.h"
  36
  37/* return pixels in terms of txbyteclkhs */
  38static u16 txbyteclkhs(u16 pixels, int bpp, int lane_count,
  39                       u16 burst_mode_ratio)
  40{
  41        return DIV_ROUND_UP(DIV_ROUND_UP(pixels * bpp * burst_mode_ratio,
  42                                         8 * 100), lane_count);
  43}
  44
  45/* return pixels equvalent to txbyteclkhs */
  46static u16 pixels_from_txbyteclkhs(u16 clk_hs, int bpp, int lane_count,
  47                        u16 burst_mode_ratio)
  48{
  49        return DIV_ROUND_UP((clk_hs * lane_count * 8 * 100),
  50                                                (bpp * burst_mode_ratio));
  51}
  52
  53enum mipi_dsi_pixel_format pixel_format_from_register_bits(u32 fmt)
  54{
  55        /* It just so happens the VBT matches register contents. */
  56        switch (fmt) {
  57        case VID_MODE_FORMAT_RGB888:
  58                return MIPI_DSI_FMT_RGB888;
  59        case VID_MODE_FORMAT_RGB666:
  60                return MIPI_DSI_FMT_RGB666;
  61        case VID_MODE_FORMAT_RGB666_PACKED:
  62                return MIPI_DSI_FMT_RGB666_PACKED;
  63        case VID_MODE_FORMAT_RGB565:
  64                return MIPI_DSI_FMT_RGB565;
  65        default:
  66                MISSING_CASE(fmt);
  67                return MIPI_DSI_FMT_RGB666;
  68        }
  69}
  70
  71void vlv_dsi_wait_for_fifo_empty(struct intel_dsi *intel_dsi, enum port port)
  72{
  73        struct drm_encoder *encoder = &intel_dsi->base.base;
  74        struct drm_device *dev = encoder->dev;
  75        struct drm_i915_private *dev_priv = to_i915(dev);
  76        u32 mask;
  77
  78        mask = LP_CTRL_FIFO_EMPTY | HS_CTRL_FIFO_EMPTY |
  79                LP_DATA_FIFO_EMPTY | HS_DATA_FIFO_EMPTY;
  80
  81        if (intel_wait_for_register(dev_priv,
  82                                    MIPI_GEN_FIFO_STAT(port), mask, mask,
  83                                    100))
  84                DRM_ERROR("DPI FIFOs are not empty\n");
  85}
  86
  87static void write_data(struct drm_i915_private *dev_priv,
  88                       i915_reg_t reg,
  89                       const u8 *data, u32 len)
  90{
  91        u32 i, j;
  92
  93        for (i = 0; i < len; i += 4) {
  94                u32 val = 0;
  95
  96                for (j = 0; j < min_t(u32, len - i, 4); j++)
  97                        val |= *data++ << 8 * j;
  98
  99                I915_WRITE(reg, val);
 100        }
 101}
 102
 103static void read_data(struct drm_i915_private *dev_priv,
 104                      i915_reg_t reg,
 105                      u8 *data, u32 len)
 106{
 107        u32 i, j;
 108
 109        for (i = 0; i < len; i += 4) {
 110                u32 val = I915_READ(reg);
 111
 112                for (j = 0; j < min_t(u32, len - i, 4); j++)
 113                        *data++ = val >> 8 * j;
 114        }
 115}
 116
 117static ssize_t intel_dsi_host_transfer(struct mipi_dsi_host *host,
 118                                       const struct mipi_dsi_msg *msg)
 119{
 120        struct intel_dsi_host *intel_dsi_host = to_intel_dsi_host(host);
 121        struct drm_device *dev = intel_dsi_host->intel_dsi->base.base.dev;
 122        struct drm_i915_private *dev_priv = to_i915(dev);
 123        enum port port = intel_dsi_host->port;
 124        struct mipi_dsi_packet packet;
 125        ssize_t ret;
 126        const u8 *header, *data;
 127        i915_reg_t data_reg, ctrl_reg;
 128        u32 data_mask, ctrl_mask;
 129
 130        ret = mipi_dsi_create_packet(&packet, msg);
 131        if (ret < 0)
 132                return ret;
 133
 134        header = packet.header;
 135        data = packet.payload;
 136
 137        if (msg->flags & MIPI_DSI_MSG_USE_LPM) {
 138                data_reg = MIPI_LP_GEN_DATA(port);
 139                data_mask = LP_DATA_FIFO_FULL;
 140                ctrl_reg = MIPI_LP_GEN_CTRL(port);
 141                ctrl_mask = LP_CTRL_FIFO_FULL;
 142        } else {
 143                data_reg = MIPI_HS_GEN_DATA(port);
 144                data_mask = HS_DATA_FIFO_FULL;
 145                ctrl_reg = MIPI_HS_GEN_CTRL(port);
 146                ctrl_mask = HS_CTRL_FIFO_FULL;
 147        }
 148
 149        /* note: this is never true for reads */
 150        if (packet.payload_length) {
 151                if (intel_wait_for_register(dev_priv,
 152                                            MIPI_GEN_FIFO_STAT(port),
 153                                            data_mask, 0,
 154                                            50))
 155                        DRM_ERROR("Timeout waiting for HS/LP DATA FIFO !full\n");
 156
 157                write_data(dev_priv, data_reg, packet.payload,
 158                           packet.payload_length);
 159        }
 160
 161        if (msg->rx_len) {
 162                I915_WRITE(MIPI_INTR_STAT(port), GEN_READ_DATA_AVAIL);
 163        }
 164
 165        if (intel_wait_for_register(dev_priv,
 166                                    MIPI_GEN_FIFO_STAT(port),
 167                                    ctrl_mask, 0,
 168                                    50)) {
 169                DRM_ERROR("Timeout waiting for HS/LP CTRL FIFO !full\n");
 170        }
 171
 172        I915_WRITE(ctrl_reg, header[2] << 16 | header[1] << 8 | header[0]);
 173
 174        /* ->rx_len is set only for reads */
 175        if (msg->rx_len) {
 176                data_mask = GEN_READ_DATA_AVAIL;
 177                if (intel_wait_for_register(dev_priv,
 178                                            MIPI_INTR_STAT(port),
 179                                            data_mask, data_mask,
 180                                            50))
 181                        DRM_ERROR("Timeout waiting for read data.\n");
 182
 183                read_data(dev_priv, data_reg, msg->rx_buf, msg->rx_len);
 184        }
 185
 186        /* XXX: fix for reads and writes */
 187        return 4 + packet.payload_length;
 188}
 189
 190static int intel_dsi_host_attach(struct mipi_dsi_host *host,
 191                                 struct mipi_dsi_device *dsi)
 192{
 193        return 0;
 194}
 195
 196static int intel_dsi_host_detach(struct mipi_dsi_host *host,
 197                                 struct mipi_dsi_device *dsi)
 198{
 199        return 0;
 200}
 201
 202static const struct mipi_dsi_host_ops intel_dsi_host_ops = {
 203        .attach = intel_dsi_host_attach,
 204        .detach = intel_dsi_host_detach,
 205        .transfer = intel_dsi_host_transfer,
 206};
 207
 208/*
 209 * send a video mode command
 210 *
 211 * XXX: commands with data in MIPI_DPI_DATA?
 212 */
 213static int dpi_send_cmd(struct intel_dsi *intel_dsi, u32 cmd, bool hs,
 214                        enum port port)
 215{
 216        struct drm_encoder *encoder = &intel_dsi->base.base;
 217        struct drm_device *dev = encoder->dev;
 218        struct drm_i915_private *dev_priv = to_i915(dev);
 219        u32 mask;
 220
 221        /* XXX: pipe, hs */
 222        if (hs)
 223                cmd &= ~DPI_LP_MODE;
 224        else
 225                cmd |= DPI_LP_MODE;
 226
 227        /* clear bit */
 228        I915_WRITE(MIPI_INTR_STAT(port), SPL_PKT_SENT_INTERRUPT);
 229
 230        /* XXX: old code skips write if control unchanged */
 231        if (cmd == I915_READ(MIPI_DPI_CONTROL(port)))
 232                DRM_DEBUG_KMS("Same special packet %02x twice in a row.\n", cmd);
 233
 234        I915_WRITE(MIPI_DPI_CONTROL(port), cmd);
 235
 236        mask = SPL_PKT_SENT_INTERRUPT;
 237        if (intel_wait_for_register(dev_priv,
 238                                    MIPI_INTR_STAT(port), mask, mask,
 239                                    100))
 240                DRM_ERROR("Video mode command 0x%08x send failed.\n", cmd);
 241
 242        return 0;
 243}
 244
 245static void band_gap_reset(struct drm_i915_private *dev_priv)
 246{
 247        mutex_lock(&dev_priv->sb_lock);
 248
 249        vlv_flisdsi_write(dev_priv, 0x08, 0x0001);
 250        vlv_flisdsi_write(dev_priv, 0x0F, 0x0005);
 251        vlv_flisdsi_write(dev_priv, 0x0F, 0x0025);
 252        udelay(150);
 253        vlv_flisdsi_write(dev_priv, 0x0F, 0x0000);
 254        vlv_flisdsi_write(dev_priv, 0x08, 0x0000);
 255
 256        mutex_unlock(&dev_priv->sb_lock);
 257}
 258
 259static int bdw_get_pipemisc_bpp(struct intel_crtc *crtc)
 260{
 261        struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
 262        u32 tmp;
 263
 264        tmp = I915_READ(PIPEMISC(crtc->pipe));
 265
 266        switch (tmp & PIPEMISC_DITHER_BPC_MASK) {
 267        case PIPEMISC_DITHER_6_BPC:
 268                return 18;
 269        case PIPEMISC_DITHER_8_BPC:
 270                return 24;
 271        case PIPEMISC_DITHER_10_BPC:
 272                return 30;
 273        case PIPEMISC_DITHER_12_BPC:
 274                return 36;
 275        default:
 276                MISSING_CASE(tmp);
 277                return 0;
 278        }
 279}
 280
 281static int intel_dsi_compute_config(struct intel_encoder *encoder,
 282                                    struct intel_crtc_state *pipe_config,
 283                                    struct drm_connector_state *conn_state)
 284{
 285        struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 286        struct intel_dsi *intel_dsi = container_of(encoder, struct intel_dsi,
 287                                                   base);
 288        struct intel_connector *intel_connector = intel_dsi->attached_connector;
 289        struct intel_crtc *crtc = to_intel_crtc(pipe_config->base.crtc);
 290        const struct drm_display_mode *fixed_mode = intel_connector->panel.fixed_mode;
 291        struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
 292        int ret;
 293
 294        DRM_DEBUG_KMS("\n");
 295        pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
 296
 297        if (fixed_mode) {
 298                intel_fixed_panel_mode(fixed_mode, adjusted_mode);
 299
 300                if (HAS_GMCH(dev_priv))
 301                        intel_gmch_panel_fitting(crtc, pipe_config,
 302                                                 conn_state->scaling_mode);
 303                else
 304                        intel_pch_panel_fitting(crtc, pipe_config,
 305                                                conn_state->scaling_mode);
 306        }
 307
 308        if (adjusted_mode->flags & DRM_MODE_FLAG_DBLSCAN)
 309                return -EINVAL;
 310
 311        /* DSI uses short packets for sync events, so clear mode flags for DSI */
 312        adjusted_mode->flags = 0;
 313
 314        if (intel_dsi->pixel_format == MIPI_DSI_FMT_RGB888)
 315                pipe_config->pipe_bpp = 24;
 316        else
 317                pipe_config->pipe_bpp = 18;
 318
 319        if (IS_GEN9_LP(dev_priv)) {
 320                /* Enable Frame time stamp based scanline reporting */
 321                adjusted_mode->private_flags |=
 322                        I915_MODE_FLAG_GET_SCANLINE_FROM_TIMESTAMP;
 323
 324                /* Dual link goes to DSI transcoder A. */
 325                if (intel_dsi->ports == BIT(PORT_C))
 326                        pipe_config->cpu_transcoder = TRANSCODER_DSI_C;
 327                else
 328                        pipe_config->cpu_transcoder = TRANSCODER_DSI_A;
 329
 330                ret = bxt_dsi_pll_compute(encoder, pipe_config);
 331                if (ret)
 332                        return -EINVAL;
 333        } else {
 334                ret = vlv_dsi_pll_compute(encoder, pipe_config);
 335                if (ret)
 336                        return -EINVAL;
 337        }
 338
 339        pipe_config->clock_set = true;
 340
 341        return 0;
 342}
 343
 344static bool glk_dsi_enable_io(struct intel_encoder *encoder)
 345{
 346        struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 347        struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
 348        enum port port;
 349        u32 tmp;
 350        bool cold_boot = false;
 351
 352        /* Set the MIPI mode
 353         * If MIPI_Mode is off, then writing to LP_Wake bit is not reflecting.
 354         * Power ON MIPI IO first and then write into IO reset and LP wake bits
 355         */
 356        for_each_dsi_port(port, intel_dsi->ports) {
 357                tmp = I915_READ(MIPI_CTRL(port));
 358                I915_WRITE(MIPI_CTRL(port), tmp | GLK_MIPIIO_ENABLE);
 359        }
 360
 361        /* Put the IO into reset */
 362        tmp = I915_READ(MIPI_CTRL(PORT_A));
 363        tmp &= ~GLK_MIPIIO_RESET_RELEASED;
 364        I915_WRITE(MIPI_CTRL(PORT_A), tmp);
 365
 366        /* Program LP Wake */
 367        for_each_dsi_port(port, intel_dsi->ports) {
 368                tmp = I915_READ(MIPI_CTRL(port));
 369                if (!(I915_READ(MIPI_DEVICE_READY(port)) & DEVICE_READY))
 370                        tmp &= ~GLK_LP_WAKE;
 371                else
 372                        tmp |= GLK_LP_WAKE;
 373                I915_WRITE(MIPI_CTRL(port), tmp);
 374        }
 375
 376        /* Wait for Pwr ACK */
 377        for_each_dsi_port(port, intel_dsi->ports) {
 378                if (intel_wait_for_register(dev_priv,
 379                                MIPI_CTRL(port), GLK_MIPIIO_PORT_POWERED,
 380                                GLK_MIPIIO_PORT_POWERED, 20))
 381                        DRM_ERROR("MIPIO port is powergated\n");
 382        }
 383
 384        /* Check for cold boot scenario */
 385        for_each_dsi_port(port, intel_dsi->ports) {
 386                cold_boot |= !(I915_READ(MIPI_DEVICE_READY(port)) &
 387                                                        DEVICE_READY);
 388        }
 389
 390        return cold_boot;
 391}
 392
 393static void glk_dsi_device_ready(struct intel_encoder *encoder)
 394{
 395        struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 396        struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
 397        enum port port;
 398        u32 val;
 399
 400        /* Wait for MIPI PHY status bit to set */
 401        for_each_dsi_port(port, intel_dsi->ports) {
 402                if (intel_wait_for_register(dev_priv,
 403                                MIPI_CTRL(port), GLK_PHY_STATUS_PORT_READY,
 404                                GLK_PHY_STATUS_PORT_READY, 20))
 405                        DRM_ERROR("PHY is not ON\n");
 406        }
 407
 408        /* Get IO out of reset */
 409        val = I915_READ(MIPI_CTRL(PORT_A));
 410        I915_WRITE(MIPI_CTRL(PORT_A), val | GLK_MIPIIO_RESET_RELEASED);
 411
 412        /* Get IO out of Low power state*/
 413        for_each_dsi_port(port, intel_dsi->ports) {
 414                if (!(I915_READ(MIPI_DEVICE_READY(port)) & DEVICE_READY)) {
 415                        val = I915_READ(MIPI_DEVICE_READY(port));
 416                        val &= ~ULPS_STATE_MASK;
 417                        val |= DEVICE_READY;
 418                        I915_WRITE(MIPI_DEVICE_READY(port), val);
 419                        usleep_range(10, 15);
 420                } else {
 421                        /* Enter ULPS */
 422                        val = I915_READ(MIPI_DEVICE_READY(port));
 423                        val &= ~ULPS_STATE_MASK;
 424                        val |= (ULPS_STATE_ENTER | DEVICE_READY);
 425                        I915_WRITE(MIPI_DEVICE_READY(port), val);
 426
 427                        /* Wait for ULPS active */
 428                        if (intel_wait_for_register(dev_priv,
 429                                MIPI_CTRL(port), GLK_ULPS_NOT_ACTIVE, 0, 20))
 430                                DRM_ERROR("ULPS not active\n");
 431
 432                        /* Exit ULPS */
 433                        val = I915_READ(MIPI_DEVICE_READY(port));
 434                        val &= ~ULPS_STATE_MASK;
 435                        val |= (ULPS_STATE_EXIT | DEVICE_READY);
 436                        I915_WRITE(MIPI_DEVICE_READY(port), val);
 437
 438                        /* Enter Normal Mode */
 439                        val = I915_READ(MIPI_DEVICE_READY(port));
 440                        val &= ~ULPS_STATE_MASK;
 441                        val |= (ULPS_STATE_NORMAL_OPERATION | DEVICE_READY);
 442                        I915_WRITE(MIPI_DEVICE_READY(port), val);
 443
 444                        val = I915_READ(MIPI_CTRL(port));
 445                        val &= ~GLK_LP_WAKE;
 446                        I915_WRITE(MIPI_CTRL(port), val);
 447                }
 448        }
 449
 450        /* Wait for Stop state */
 451        for_each_dsi_port(port, intel_dsi->ports) {
 452                if (intel_wait_for_register(dev_priv,
 453                                MIPI_CTRL(port), GLK_DATA_LANE_STOP_STATE,
 454                                GLK_DATA_LANE_STOP_STATE, 20))
 455                        DRM_ERROR("Date lane not in STOP state\n");
 456        }
 457
 458        /* Wait for AFE LATCH */
 459        for_each_dsi_port(port, intel_dsi->ports) {
 460                if (intel_wait_for_register(dev_priv,
 461                                BXT_MIPI_PORT_CTRL(port), AFE_LATCHOUT,
 462                                AFE_LATCHOUT, 20))
 463                        DRM_ERROR("D-PHY not entering LP-11 state\n");
 464        }
 465}
 466
 467static void bxt_dsi_device_ready(struct intel_encoder *encoder)
 468{
 469        struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 470        struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
 471        enum port port;
 472        u32 val;
 473
 474        DRM_DEBUG_KMS("\n");
 475
 476        /* Enable MIPI PHY transparent latch */
 477        for_each_dsi_port(port, intel_dsi->ports) {
 478                val = I915_READ(BXT_MIPI_PORT_CTRL(port));
 479                I915_WRITE(BXT_MIPI_PORT_CTRL(port), val | LP_OUTPUT_HOLD);
 480                usleep_range(2000, 2500);
 481        }
 482
 483        /* Clear ULPS and set device ready */
 484        for_each_dsi_port(port, intel_dsi->ports) {
 485                val = I915_READ(MIPI_DEVICE_READY(port));
 486                val &= ~ULPS_STATE_MASK;
 487                I915_WRITE(MIPI_DEVICE_READY(port), val);
 488                usleep_range(2000, 2500);
 489                val |= DEVICE_READY;
 490                I915_WRITE(MIPI_DEVICE_READY(port), val);
 491        }
 492}
 493
 494static void vlv_dsi_device_ready(struct intel_encoder *encoder)
 495{
 496        struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 497        struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
 498        enum port port;
 499        u32 val;
 500
 501        DRM_DEBUG_KMS("\n");
 502
 503        mutex_lock(&dev_priv->sb_lock);
 504        /* program rcomp for compliance, reduce from 50 ohms to 45 ohms
 505         * needed everytime after power gate */
 506        vlv_flisdsi_write(dev_priv, 0x04, 0x0004);
 507        mutex_unlock(&dev_priv->sb_lock);
 508
 509        /* bandgap reset is needed after everytime we do power gate */
 510        band_gap_reset(dev_priv);
 511
 512        for_each_dsi_port(port, intel_dsi->ports) {
 513
 514                I915_WRITE(MIPI_DEVICE_READY(port), ULPS_STATE_ENTER);
 515                usleep_range(2500, 3000);
 516
 517                /* Enable MIPI PHY transparent latch
 518                 * Common bit for both MIPI Port A & MIPI Port C
 519                 * No similar bit in MIPI Port C reg
 520                 */
 521                val = I915_READ(MIPI_PORT_CTRL(PORT_A));
 522                I915_WRITE(MIPI_PORT_CTRL(PORT_A), val | LP_OUTPUT_HOLD);
 523                usleep_range(1000, 1500);
 524
 525                I915_WRITE(MIPI_DEVICE_READY(port), ULPS_STATE_EXIT);
 526                usleep_range(2500, 3000);
 527
 528                I915_WRITE(MIPI_DEVICE_READY(port), DEVICE_READY);
 529                usleep_range(2500, 3000);
 530        }
 531}
 532
 533static void intel_dsi_device_ready(struct intel_encoder *encoder)
 534{
 535        struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 536
 537        if (IS_GEMINILAKE(dev_priv))
 538                glk_dsi_device_ready(encoder);
 539        else if (IS_GEN9_LP(dev_priv))
 540                bxt_dsi_device_ready(encoder);
 541        else
 542                vlv_dsi_device_ready(encoder);
 543}
 544
 545static void glk_dsi_enter_low_power_mode(struct intel_encoder *encoder)
 546{
 547        struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 548        struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
 549        enum port port;
 550        u32 val;
 551
 552        /* Enter ULPS */
 553        for_each_dsi_port(port, intel_dsi->ports) {
 554                val = I915_READ(MIPI_DEVICE_READY(port));
 555                val &= ~ULPS_STATE_MASK;
 556                val |= (ULPS_STATE_ENTER | DEVICE_READY);
 557                I915_WRITE(MIPI_DEVICE_READY(port), val);
 558        }
 559
 560        /* Wait for MIPI PHY status bit to unset */
 561        for_each_dsi_port(port, intel_dsi->ports) {
 562                if (intel_wait_for_register(dev_priv,
 563                                            MIPI_CTRL(port),
 564                                            GLK_PHY_STATUS_PORT_READY, 0, 20))
 565                        DRM_ERROR("PHY is not turning OFF\n");
 566        }
 567
 568        /* Wait for Pwr ACK bit to unset */
 569        for_each_dsi_port(port, intel_dsi->ports) {
 570                if (intel_wait_for_register(dev_priv,
 571                                            MIPI_CTRL(port),
 572                                            GLK_MIPIIO_PORT_POWERED, 0, 20))
 573                        DRM_ERROR("MIPI IO Port is not powergated\n");
 574        }
 575}
 576
 577static void glk_dsi_disable_mipi_io(struct intel_encoder *encoder)
 578{
 579        struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 580        struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
 581        enum port port;
 582        u32 tmp;
 583
 584        /* Put the IO into reset */
 585        tmp = I915_READ(MIPI_CTRL(PORT_A));
 586        tmp &= ~GLK_MIPIIO_RESET_RELEASED;
 587        I915_WRITE(MIPI_CTRL(PORT_A), tmp);
 588
 589        /* Wait for MIPI PHY status bit to unset */
 590        for_each_dsi_port(port, intel_dsi->ports) {
 591                if (intel_wait_for_register(dev_priv,
 592                                            MIPI_CTRL(port),
 593                                            GLK_PHY_STATUS_PORT_READY, 0, 20))
 594                        DRM_ERROR("PHY is not turning OFF\n");
 595        }
 596
 597        /* Clear MIPI mode */
 598        for_each_dsi_port(port, intel_dsi->ports) {
 599                tmp = I915_READ(MIPI_CTRL(port));
 600                tmp &= ~GLK_MIPIIO_ENABLE;
 601                I915_WRITE(MIPI_CTRL(port), tmp);
 602        }
 603}
 604
 605static void glk_dsi_clear_device_ready(struct intel_encoder *encoder)
 606{
 607        glk_dsi_enter_low_power_mode(encoder);
 608        glk_dsi_disable_mipi_io(encoder);
 609}
 610
 611static void vlv_dsi_clear_device_ready(struct intel_encoder *encoder)
 612{
 613        struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 614        struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
 615        enum port port;
 616
 617        DRM_DEBUG_KMS("\n");
 618        for_each_dsi_port(port, intel_dsi->ports) {
 619                /* Common bit for both MIPI Port A & MIPI Port C on VLV/CHV */
 620                i915_reg_t port_ctrl = IS_GEN9_LP(dev_priv) ?
 621                        BXT_MIPI_PORT_CTRL(port) : MIPI_PORT_CTRL(PORT_A);
 622                u32 val;
 623
 624                I915_WRITE(MIPI_DEVICE_READY(port), DEVICE_READY |
 625                                                        ULPS_STATE_ENTER);
 626                usleep_range(2000, 2500);
 627
 628                I915_WRITE(MIPI_DEVICE_READY(port), DEVICE_READY |
 629                                                        ULPS_STATE_EXIT);
 630                usleep_range(2000, 2500);
 631
 632                I915_WRITE(MIPI_DEVICE_READY(port), DEVICE_READY |
 633                                                        ULPS_STATE_ENTER);
 634                usleep_range(2000, 2500);
 635
 636                /*
 637                 * On VLV/CHV, wait till Clock lanes are in LP-00 state for MIPI
 638                 * Port A only. MIPI Port C has no similar bit for checking.
 639                 */
 640                if ((IS_GEN9_LP(dev_priv) || port == PORT_A) &&
 641                    intel_wait_for_register(dev_priv,
 642                                            port_ctrl, AFE_LATCHOUT, 0,
 643                                            30))
 644                        DRM_ERROR("DSI LP not going Low\n");
 645
 646                /* Disable MIPI PHY transparent latch */
 647                val = I915_READ(port_ctrl);
 648                I915_WRITE(port_ctrl, val & ~LP_OUTPUT_HOLD);
 649                usleep_range(1000, 1500);
 650
 651                I915_WRITE(MIPI_DEVICE_READY(port), 0x00);
 652                usleep_range(2000, 2500);
 653        }
 654}
 655
 656static void intel_dsi_port_enable(struct intel_encoder *encoder,
 657                                  const struct intel_crtc_state *crtc_state)
 658{
 659        struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 660        struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
 661        struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
 662        enum port port;
 663
 664        if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK) {
 665                u32 temp;
 666                if (IS_GEN9_LP(dev_priv)) {
 667                        for_each_dsi_port(port, intel_dsi->ports) {
 668                                temp = I915_READ(MIPI_CTRL(port));
 669                                temp &= ~BXT_PIXEL_OVERLAP_CNT_MASK |
 670                                        intel_dsi->pixel_overlap <<
 671                                        BXT_PIXEL_OVERLAP_CNT_SHIFT;
 672                                I915_WRITE(MIPI_CTRL(port), temp);
 673                        }
 674                } else {
 675                        temp = I915_READ(VLV_CHICKEN_3);
 676                        temp &= ~PIXEL_OVERLAP_CNT_MASK |
 677                                        intel_dsi->pixel_overlap <<
 678                                        PIXEL_OVERLAP_CNT_SHIFT;
 679                        I915_WRITE(VLV_CHICKEN_3, temp);
 680                }
 681        }
 682
 683        for_each_dsi_port(port, intel_dsi->ports) {
 684                i915_reg_t port_ctrl = IS_GEN9_LP(dev_priv) ?
 685                        BXT_MIPI_PORT_CTRL(port) : MIPI_PORT_CTRL(port);
 686                u32 temp;
 687
 688                temp = I915_READ(port_ctrl);
 689
 690                temp &= ~LANE_CONFIGURATION_MASK;
 691                temp &= ~DUAL_LINK_MODE_MASK;
 692
 693                if (intel_dsi->ports == (BIT(PORT_A) | BIT(PORT_C))) {
 694                        temp |= (intel_dsi->dual_link - 1)
 695                                                << DUAL_LINK_MODE_SHIFT;
 696                        if (IS_BROXTON(dev_priv))
 697                                temp |= LANE_CONFIGURATION_DUAL_LINK_A;
 698                        else
 699                                temp |= crtc->pipe ?
 700                                        LANE_CONFIGURATION_DUAL_LINK_B :
 701                                        LANE_CONFIGURATION_DUAL_LINK_A;
 702                }
 703
 704                if (intel_dsi->pixel_format != MIPI_DSI_FMT_RGB888)
 705                        temp |= DITHERING_ENABLE;
 706
 707                /* assert ip_tg_enable signal */
 708                I915_WRITE(port_ctrl, temp | DPI_ENABLE);
 709                POSTING_READ(port_ctrl);
 710        }
 711}
 712
 713static void intel_dsi_port_disable(struct intel_encoder *encoder)
 714{
 715        struct drm_device *dev = encoder->base.dev;
 716        struct drm_i915_private *dev_priv = to_i915(dev);
 717        struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
 718        enum port port;
 719
 720        for_each_dsi_port(port, intel_dsi->ports) {
 721                i915_reg_t port_ctrl = IS_GEN9_LP(dev_priv) ?
 722                        BXT_MIPI_PORT_CTRL(port) : MIPI_PORT_CTRL(port);
 723                u32 temp;
 724
 725                /* de-assert ip_tg_enable signal */
 726                temp = I915_READ(port_ctrl);
 727                I915_WRITE(port_ctrl, temp & ~DPI_ENABLE);
 728                POSTING_READ(port_ctrl);
 729        }
 730}
 731
 732static void intel_dsi_prepare(struct intel_encoder *intel_encoder,
 733                              const struct intel_crtc_state *pipe_config);
 734static void intel_dsi_unprepare(struct intel_encoder *encoder);
 735
 736/*
 737 * Panel enable/disable sequences from the VBT spec.
 738 *
 739 * Note the spec has AssertReset / DeassertReset swapped from their
 740 * usual naming. We use the normal names to avoid confusion (so below
 741 * they are swapped compared to the spec).
 742 *
 743 * Steps starting with MIPI refer to VBT sequences, note that for v2
 744 * VBTs several steps which have a VBT in v2 are expected to be handled
 745 * directly by the driver, by directly driving gpios for example.
 746 *
 747 * v2 video mode seq         v3 video mode seq         command mode seq
 748 * - power on                - MIPIPanelPowerOn        - power on
 749 * - wait t1+t2                                        - wait t1+t2
 750 * - MIPIDeassertResetPin    - MIPIDeassertResetPin    - MIPIDeassertResetPin
 751 * - io lines to lp-11       - io lines to lp-11       - io lines to lp-11
 752 * - MIPISendInitialDcsCmds  - MIPISendInitialDcsCmds  - MIPISendInitialDcsCmds
 753 *                                                     - MIPITearOn
 754 *                                                     - MIPIDisplayOn
 755 * - turn on DPI             - turn on DPI             - set pipe to dsr mode
 756 * - MIPIDisplayOn           - MIPIDisplayOn
 757 * - wait t5                                           - wait t5
 758 * - backlight on            - MIPIBacklightOn         - backlight on
 759 * ...                       ...                       ... issue mem cmds ...
 760 * - backlight off           - MIPIBacklightOff        - backlight off
 761 * - wait t6                                           - wait t6
 762 * - MIPIDisplayOff
 763 * - turn off DPI            - turn off DPI            - disable pipe dsr mode
 764 *                                                     - MIPITearOff
 765 *                           - MIPIDisplayOff          - MIPIDisplayOff
 766 * - io lines to lp-00       - io lines to lp-00       - io lines to lp-00
 767 * - MIPIAssertResetPin      - MIPIAssertResetPin      - MIPIAssertResetPin
 768 * - wait t3                                           - wait t3
 769 * - power off               - MIPIPanelPowerOff       - power off
 770 * - wait t4                                           - wait t4
 771 */
 772
 773/*
 774 * DSI port enable has to be done before pipe and plane enable, so we do it in
 775 * the pre_enable hook instead of the enable hook.
 776 */
 777static void intel_dsi_pre_enable(struct intel_encoder *encoder,
 778                                 const struct intel_crtc_state *pipe_config,
 779                                 const struct drm_connector_state *conn_state)
 780{
 781        struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
 782        struct drm_crtc *crtc = pipe_config->base.crtc;
 783        struct drm_i915_private *dev_priv = to_i915(crtc->dev);
 784        struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
 785        int pipe = intel_crtc->pipe;
 786        enum port port;
 787        u32 val;
 788        bool glk_cold_boot = false;
 789
 790        DRM_DEBUG_KMS("\n");
 791
 792        intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
 793
 794        /*
 795         * The BIOS may leave the PLL in a wonky state where it doesn't
 796         * lock. It needs to be fully powered down to fix it.
 797         */
 798        if (IS_GEN9_LP(dev_priv)) {
 799                bxt_dsi_pll_disable(encoder);
 800                bxt_dsi_pll_enable(encoder, pipe_config);
 801        } else {
 802                vlv_dsi_pll_disable(encoder);
 803                vlv_dsi_pll_enable(encoder, pipe_config);
 804        }
 805
 806        if (IS_BROXTON(dev_priv)) {
 807                /* Add MIPI IO reset programming for modeset */
 808                val = I915_READ(BXT_P_CR_GT_DISP_PWRON);
 809                I915_WRITE(BXT_P_CR_GT_DISP_PWRON,
 810                                        val | MIPIO_RST_CTRL);
 811
 812                /* Power up DSI regulator */
 813                I915_WRITE(BXT_P_DSI_REGULATOR_CFG, STAP_SELECT);
 814                I915_WRITE(BXT_P_DSI_REGULATOR_TX_CTRL, 0);
 815        }
 816
 817        if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
 818                u32 val;
 819
 820                /* Disable DPOunit clock gating, can stall pipe */
 821                val = I915_READ(DSPCLK_GATE_D);
 822                val |= DPOUNIT_CLOCK_GATE_DISABLE;
 823                I915_WRITE(DSPCLK_GATE_D, val);
 824        }
 825
 826        if (!IS_GEMINILAKE(dev_priv))
 827                intel_dsi_prepare(encoder, pipe_config);
 828
 829        /* Power on, try both CRC pmic gpio and VBT */
 830        if (intel_dsi->gpio_panel)
 831                gpiod_set_value_cansleep(intel_dsi->gpio_panel, 1);
 832        intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_POWER_ON);
 833        intel_dsi_msleep(intel_dsi, intel_dsi->panel_on_delay);
 834
 835        /* Deassert reset */
 836        intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DEASSERT_RESET);
 837
 838        if (IS_GEMINILAKE(dev_priv)) {
 839                glk_cold_boot = glk_dsi_enable_io(encoder);
 840
 841                /* Prepare port in cold boot(s3/s4) scenario */
 842                if (glk_cold_boot)
 843                        intel_dsi_prepare(encoder, pipe_config);
 844        }
 845
 846        /* Put device in ready state (LP-11) */
 847        intel_dsi_device_ready(encoder);
 848
 849        /* Prepare port in normal boot scenario */
 850        if (IS_GEMINILAKE(dev_priv) && !glk_cold_boot)
 851                intel_dsi_prepare(encoder, pipe_config);
 852
 853        /* Send initialization commands in LP mode */
 854        intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_INIT_OTP);
 855
 856        /* Enable port in pre-enable phase itself because as per hw team
 857         * recommendation, port should be enabled befor plane & pipe */
 858        if (is_cmd_mode(intel_dsi)) {
 859                for_each_dsi_port(port, intel_dsi->ports)
 860                        I915_WRITE(MIPI_MAX_RETURN_PKT_SIZE(port), 8 * 4);
 861                intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_TEAR_ON);
 862                intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DISPLAY_ON);
 863        } else {
 864                msleep(20); /* XXX */
 865                for_each_dsi_port(port, intel_dsi->ports)
 866                        dpi_send_cmd(intel_dsi, TURN_ON, false, port);
 867                intel_dsi_msleep(intel_dsi, 100);
 868
 869                intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DISPLAY_ON);
 870
 871                intel_dsi_port_enable(encoder, pipe_config);
 872        }
 873
 874        intel_panel_enable_backlight(pipe_config, conn_state);
 875        intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_BACKLIGHT_ON);
 876}
 877
 878/*
 879 * DSI port disable has to be done after pipe and plane disable, so we do it in
 880 * the post_disable hook.
 881 */
 882static void intel_dsi_disable(struct intel_encoder *encoder,
 883                              const struct intel_crtc_state *old_crtc_state,
 884                              const struct drm_connector_state *old_conn_state)
 885{
 886        struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
 887        enum port port;
 888
 889        DRM_DEBUG_KMS("\n");
 890
 891        intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_BACKLIGHT_OFF);
 892        intel_panel_disable_backlight(old_conn_state);
 893
 894        /*
 895         * According to the spec we should send SHUTDOWN before
 896         * MIPI_SEQ_DISPLAY_OFF only for v3+ VBTs, but field testing
 897         * has shown that the v3 sequence works for v2 VBTs too
 898         */
 899        if (is_vid_mode(intel_dsi)) {
 900                /* Send Shutdown command to the panel in LP mode */
 901                for_each_dsi_port(port, intel_dsi->ports)
 902                        dpi_send_cmd(intel_dsi, SHUTDOWN, false, port);
 903                msleep(10);
 904        }
 905}
 906
 907static void intel_dsi_clear_device_ready(struct intel_encoder *encoder)
 908{
 909        struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 910
 911        if (IS_GEMINILAKE(dev_priv))
 912                glk_dsi_clear_device_ready(encoder);
 913        else
 914                vlv_dsi_clear_device_ready(encoder);
 915}
 916
 917static void intel_dsi_post_disable(struct intel_encoder *encoder,
 918                                   const struct intel_crtc_state *pipe_config,
 919                                   const struct drm_connector_state *conn_state)
 920{
 921        struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 922        struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
 923        enum port port;
 924        u32 val;
 925
 926        DRM_DEBUG_KMS("\n");
 927
 928        if (is_vid_mode(intel_dsi)) {
 929                for_each_dsi_port(port, intel_dsi->ports)
 930                        vlv_dsi_wait_for_fifo_empty(intel_dsi, port);
 931
 932                intel_dsi_port_disable(encoder);
 933                usleep_range(2000, 5000);
 934        }
 935
 936        intel_dsi_unprepare(encoder);
 937
 938        /*
 939         * if disable packets are sent before sending shutdown packet then in
 940         * some next enable sequence send turn on packet error is observed
 941         */
 942        if (is_cmd_mode(intel_dsi))
 943                intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_TEAR_OFF);
 944        intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_DISPLAY_OFF);
 945
 946        /* Transition to LP-00 */
 947        intel_dsi_clear_device_ready(encoder);
 948
 949        if (IS_BROXTON(dev_priv)) {
 950                /* Power down DSI regulator to save power */
 951                I915_WRITE(BXT_P_DSI_REGULATOR_CFG, STAP_SELECT);
 952                I915_WRITE(BXT_P_DSI_REGULATOR_TX_CTRL, HS_IO_CTRL_SELECT);
 953
 954                /* Add MIPI IO reset programming for modeset */
 955                val = I915_READ(BXT_P_CR_GT_DISP_PWRON);
 956                I915_WRITE(BXT_P_CR_GT_DISP_PWRON,
 957                                val & ~MIPIO_RST_CTRL);
 958        }
 959
 960        if (IS_GEN9_LP(dev_priv)) {
 961                bxt_dsi_pll_disable(encoder);
 962        } else {
 963                u32 val;
 964
 965                vlv_dsi_pll_disable(encoder);
 966
 967                val = I915_READ(DSPCLK_GATE_D);
 968                val &= ~DPOUNIT_CLOCK_GATE_DISABLE;
 969                I915_WRITE(DSPCLK_GATE_D, val);
 970        }
 971
 972        /* Assert reset */
 973        intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_ASSERT_RESET);
 974
 975        /* Power off, try both CRC pmic gpio and VBT */
 976        intel_dsi_msleep(intel_dsi, intel_dsi->panel_off_delay);
 977        intel_dsi_vbt_exec_sequence(intel_dsi, MIPI_SEQ_POWER_OFF);
 978        if (intel_dsi->gpio_panel)
 979                gpiod_set_value_cansleep(intel_dsi->gpio_panel, 0);
 980
 981        /*
 982         * FIXME As we do with eDP, just make a note of the time here
 983         * and perform the wait before the next panel power on.
 984         */
 985        intel_dsi_msleep(intel_dsi, intel_dsi->panel_pwr_cycle_delay);
 986}
 987
 988static bool intel_dsi_get_hw_state(struct intel_encoder *encoder,
 989                                   enum pipe *pipe)
 990{
 991        struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
 992        struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
 993        intel_wakeref_t wakeref;
 994        enum port port;
 995        bool active = false;
 996
 997        DRM_DEBUG_KMS("\n");
 998
 999        wakeref = intel_display_power_get_if_enabled(dev_priv,
1000                                                     encoder->power_domain);
1001        if (!wakeref)
1002                return false;
1003
1004        /*
1005         * On Broxton the PLL needs to be enabled with a valid divider
1006         * configuration, otherwise accessing DSI registers will hang the
1007         * machine. See BSpec North Display Engine registers/MIPI[BXT].
1008         */
1009        if (IS_GEN9_LP(dev_priv) && !bxt_dsi_pll_is_enabled(dev_priv))
1010                goto out_put_power;
1011
1012        /* XXX: this only works for one DSI output */
1013        for_each_dsi_port(port, intel_dsi->ports) {
1014                i915_reg_t ctrl_reg = IS_GEN9_LP(dev_priv) ?
1015                        BXT_MIPI_PORT_CTRL(port) : MIPI_PORT_CTRL(port);
1016                bool enabled = I915_READ(ctrl_reg) & DPI_ENABLE;
1017
1018                /*
1019                 * Due to some hardware limitations on VLV/CHV, the DPI enable
1020                 * bit in port C control register does not get set. As a
1021                 * workaround, check pipe B conf instead.
1022                 */
1023                if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
1024                    port == PORT_C)
1025                        enabled = I915_READ(PIPECONF(PIPE_B)) & PIPECONF_ENABLE;
1026
1027                /* Try command mode if video mode not enabled */
1028                if (!enabled) {
1029                        u32 tmp = I915_READ(MIPI_DSI_FUNC_PRG(port));
1030                        enabled = tmp & CMD_MODE_DATA_WIDTH_MASK;
1031                }
1032
1033                if (!enabled)
1034                        continue;
1035
1036                if (!(I915_READ(MIPI_DEVICE_READY(port)) & DEVICE_READY))
1037                        continue;
1038
1039                if (IS_GEN9_LP(dev_priv)) {
1040                        u32 tmp = I915_READ(MIPI_CTRL(port));
1041                        tmp &= BXT_PIPE_SELECT_MASK;
1042                        tmp >>= BXT_PIPE_SELECT_SHIFT;
1043
1044                        if (WARN_ON(tmp > PIPE_C))
1045                                continue;
1046
1047                        *pipe = tmp;
1048                } else {
1049                        *pipe = port == PORT_A ? PIPE_A : PIPE_B;
1050                }
1051
1052                active = true;
1053                break;
1054        }
1055
1056out_put_power:
1057        intel_display_power_put(dev_priv, encoder->power_domain, wakeref);
1058
1059        return active;
1060}
1061
1062static void bxt_dsi_get_pipe_config(struct intel_encoder *encoder,
1063                                    struct intel_crtc_state *pipe_config)
1064{
1065        struct drm_device *dev = encoder->base.dev;
1066        struct drm_i915_private *dev_priv = to_i915(dev);
1067        struct drm_display_mode *adjusted_mode =
1068                                        &pipe_config->base.adjusted_mode;
1069        struct drm_display_mode *adjusted_mode_sw;
1070        struct intel_crtc *crtc = to_intel_crtc(pipe_config->base.crtc);
1071        struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
1072        unsigned int lane_count = intel_dsi->lane_count;
1073        unsigned int bpp, fmt;
1074        enum port port;
1075        u16 hactive, hfp, hsync, hbp, vfp, vsync, vbp;
1076        u16 hfp_sw, hsync_sw, hbp_sw;
1077        u16 crtc_htotal_sw, crtc_hsync_start_sw, crtc_hsync_end_sw,
1078                                crtc_hblank_start_sw, crtc_hblank_end_sw;
1079
1080        /* FIXME: hw readout should not depend on SW state */
1081        adjusted_mode_sw = &crtc->config->base.adjusted_mode;
1082
1083        /*
1084         * Atleast one port is active as encoder->get_config called only if
1085         * encoder->get_hw_state() returns true.
1086         */
1087        for_each_dsi_port(port, intel_dsi->ports) {
1088                if (I915_READ(BXT_MIPI_PORT_CTRL(port)) & DPI_ENABLE)
1089                        break;
1090        }
1091
1092        fmt = I915_READ(MIPI_DSI_FUNC_PRG(port)) & VID_MODE_FORMAT_MASK;
1093        bpp = mipi_dsi_pixel_format_to_bpp(
1094                        pixel_format_from_register_bits(fmt));
1095
1096        pipe_config->pipe_bpp = bdw_get_pipemisc_bpp(crtc);
1097
1098        /* Enable Frame time stamo based scanline reporting */
1099        adjusted_mode->private_flags |=
1100                        I915_MODE_FLAG_GET_SCANLINE_FROM_TIMESTAMP;
1101
1102        /* In terms of pixels */
1103        adjusted_mode->crtc_hdisplay =
1104                                I915_READ(BXT_MIPI_TRANS_HACTIVE(port));
1105        adjusted_mode->crtc_vdisplay =
1106                                I915_READ(BXT_MIPI_TRANS_VACTIVE(port));
1107        adjusted_mode->crtc_vtotal =
1108                                I915_READ(BXT_MIPI_TRANS_VTOTAL(port));
1109
1110        hactive = adjusted_mode->crtc_hdisplay;
1111        hfp = I915_READ(MIPI_HFP_COUNT(port));
1112
1113        /*
1114         * Meaningful for video mode non-burst sync pulse mode only,
1115         * can be zero for non-burst sync events and burst modes
1116         */
1117        hsync = I915_READ(MIPI_HSYNC_PADDING_COUNT(port));
1118        hbp = I915_READ(MIPI_HBP_COUNT(port));
1119
1120        /* harizontal values are in terms of high speed byte clock */
1121        hfp = pixels_from_txbyteclkhs(hfp, bpp, lane_count,
1122                                                intel_dsi->burst_mode_ratio);
1123        hsync = pixels_from_txbyteclkhs(hsync, bpp, lane_count,
1124                                                intel_dsi->burst_mode_ratio);
1125        hbp = pixels_from_txbyteclkhs(hbp, bpp, lane_count,
1126                                                intel_dsi->burst_mode_ratio);
1127
1128        if (intel_dsi->dual_link) {
1129                hfp *= 2;
1130                hsync *= 2;
1131                hbp *= 2;
1132        }
1133
1134        /* vertical values are in terms of lines */
1135        vfp = I915_READ(MIPI_VFP_COUNT(port));
1136        vsync = I915_READ(MIPI_VSYNC_PADDING_COUNT(port));
1137        vbp = I915_READ(MIPI_VBP_COUNT(port));
1138
1139        adjusted_mode->crtc_htotal = hactive + hfp + hsync + hbp;
1140        adjusted_mode->crtc_hsync_start = hfp + adjusted_mode->crtc_hdisplay;
1141        adjusted_mode->crtc_hsync_end = hsync + adjusted_mode->crtc_hsync_start;
1142        adjusted_mode->crtc_hblank_start = adjusted_mode->crtc_hdisplay;
1143        adjusted_mode->crtc_hblank_end = adjusted_mode->crtc_htotal;
1144
1145        adjusted_mode->crtc_vsync_start = vfp + adjusted_mode->crtc_vdisplay;
1146        adjusted_mode->crtc_vsync_end = vsync + adjusted_mode->crtc_vsync_start;
1147        adjusted_mode->crtc_vblank_start = adjusted_mode->crtc_vdisplay;
1148        adjusted_mode->crtc_vblank_end = adjusted_mode->crtc_vtotal;
1149
1150        /*
1151         * In BXT DSI there is no regs programmed with few horizontal timings
1152         * in Pixels but txbyteclkhs.. So retrieval process adds some
1153         * ROUND_UP ERRORS in the process of PIXELS<==>txbyteclkhs.
1154         * Actually here for the given adjusted_mode, we are calculating the
1155         * value programmed to the port and then back to the horizontal timing
1156         * param in pixels. This is the expected value, including roundup errors
1157         * And if that is same as retrieved value from port, then
1158         * (HW state) adjusted_mode's horizontal timings are corrected to
1159         * match with SW state to nullify the errors.
1160         */
1161        /* Calculating the value programmed to the Port register */
1162        hfp_sw = adjusted_mode_sw->crtc_hsync_start -
1163                                        adjusted_mode_sw->crtc_hdisplay;
1164        hsync_sw = adjusted_mode_sw->crtc_hsync_end -
1165                                        adjusted_mode_sw->crtc_hsync_start;
1166        hbp_sw = adjusted_mode_sw->crtc_htotal -
1167                                        adjusted_mode_sw->crtc_hsync_end;
1168
1169        if (intel_dsi->dual_link) {
1170                hfp_sw /= 2;
1171                hsync_sw /= 2;
1172                hbp_sw /= 2;
1173        }
1174
1175        hfp_sw = txbyteclkhs(hfp_sw, bpp, lane_count,
1176                                                intel_dsi->burst_mode_ratio);
1177        hsync_sw = txbyteclkhs(hsync_sw, bpp, lane_count,
1178                            intel_dsi->burst_mode_ratio);
1179        hbp_sw = txbyteclkhs(hbp_sw, bpp, lane_count,
1180                                                intel_dsi->burst_mode_ratio);
1181
1182        /* Reverse calculating the adjusted mode parameters from port reg vals*/
1183        hfp_sw = pixels_from_txbyteclkhs(hfp_sw, bpp, lane_count,
1184                                                intel_dsi->burst_mode_ratio);
1185        hsync_sw = pixels_from_txbyteclkhs(hsync_sw, bpp, lane_count,
1186                                                intel_dsi->burst_mode_ratio);
1187        hbp_sw = pixels_from_txbyteclkhs(hbp_sw, bpp, lane_count,
1188                                                intel_dsi->burst_mode_ratio);
1189
1190        if (intel_dsi->dual_link) {
1191                hfp_sw *= 2;
1192                hsync_sw *= 2;
1193                hbp_sw *= 2;
1194        }
1195
1196        crtc_htotal_sw = adjusted_mode_sw->crtc_hdisplay + hfp_sw +
1197                                                        hsync_sw + hbp_sw;
1198        crtc_hsync_start_sw = hfp_sw + adjusted_mode_sw->crtc_hdisplay;
1199        crtc_hsync_end_sw = hsync_sw + crtc_hsync_start_sw;
1200        crtc_hblank_start_sw = adjusted_mode_sw->crtc_hdisplay;
1201        crtc_hblank_end_sw = crtc_htotal_sw;
1202
1203        if (adjusted_mode->crtc_htotal == crtc_htotal_sw)
1204                adjusted_mode->crtc_htotal = adjusted_mode_sw->crtc_htotal;
1205
1206        if (adjusted_mode->crtc_hsync_start == crtc_hsync_start_sw)
1207                adjusted_mode->crtc_hsync_start =
1208                                        adjusted_mode_sw->crtc_hsync_start;
1209
1210        if (adjusted_mode->crtc_hsync_end == crtc_hsync_end_sw)
1211                adjusted_mode->crtc_hsync_end =
1212                                        adjusted_mode_sw->crtc_hsync_end;
1213
1214        if (adjusted_mode->crtc_hblank_start == crtc_hblank_start_sw)
1215                adjusted_mode->crtc_hblank_start =
1216                                        adjusted_mode_sw->crtc_hblank_start;
1217
1218        if (adjusted_mode->crtc_hblank_end == crtc_hblank_end_sw)
1219                adjusted_mode->crtc_hblank_end =
1220                                        adjusted_mode_sw->crtc_hblank_end;
1221}
1222
1223static void intel_dsi_get_config(struct intel_encoder *encoder,
1224                                 struct intel_crtc_state *pipe_config)
1225{
1226        struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1227        u32 pclk;
1228        DRM_DEBUG_KMS("\n");
1229
1230        pipe_config->output_types |= BIT(INTEL_OUTPUT_DSI);
1231
1232        if (IS_GEN9_LP(dev_priv)) {
1233                bxt_dsi_get_pipe_config(encoder, pipe_config);
1234                pclk = bxt_dsi_get_pclk(encoder, pipe_config);
1235        } else {
1236                pclk = vlv_dsi_get_pclk(encoder, pipe_config);
1237        }
1238
1239        if (pclk) {
1240                pipe_config->base.adjusted_mode.crtc_clock = pclk;
1241                pipe_config->port_clock = pclk;
1242        }
1243}
1244
1245/* return txclkesc cycles in terms of divider and duration in us */
1246static u16 txclkesc(u32 divider, unsigned int us)
1247{
1248        switch (divider) {
1249        case ESCAPE_CLOCK_DIVIDER_1:
1250        default:
1251                return 20 * us;
1252        case ESCAPE_CLOCK_DIVIDER_2:
1253                return 10 * us;
1254        case ESCAPE_CLOCK_DIVIDER_4:
1255                return 5 * us;
1256        }
1257}
1258
1259static void set_dsi_timings(struct drm_encoder *encoder,
1260                            const struct drm_display_mode *adjusted_mode)
1261{
1262        struct drm_device *dev = encoder->dev;
1263        struct drm_i915_private *dev_priv = to_i915(dev);
1264        struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1265        enum port port;
1266        unsigned int bpp = mipi_dsi_pixel_format_to_bpp(intel_dsi->pixel_format);
1267        unsigned int lane_count = intel_dsi->lane_count;
1268
1269        u16 hactive, hfp, hsync, hbp, vfp, vsync, vbp;
1270
1271        hactive = adjusted_mode->crtc_hdisplay;
1272        hfp = adjusted_mode->crtc_hsync_start - adjusted_mode->crtc_hdisplay;
1273        hsync = adjusted_mode->crtc_hsync_end - adjusted_mode->crtc_hsync_start;
1274        hbp = adjusted_mode->crtc_htotal - adjusted_mode->crtc_hsync_end;
1275
1276        if (intel_dsi->dual_link) {
1277                hactive /= 2;
1278                if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK)
1279                        hactive += intel_dsi->pixel_overlap;
1280                hfp /= 2;
1281                hsync /= 2;
1282                hbp /= 2;
1283        }
1284
1285        vfp = adjusted_mode->crtc_vsync_start - adjusted_mode->crtc_vdisplay;
1286        vsync = adjusted_mode->crtc_vsync_end - adjusted_mode->crtc_vsync_start;
1287        vbp = adjusted_mode->crtc_vtotal - adjusted_mode->crtc_vsync_end;
1288
1289        /* horizontal values are in terms of high speed byte clock */
1290        hactive = txbyteclkhs(hactive, bpp, lane_count,
1291                              intel_dsi->burst_mode_ratio);
1292        hfp = txbyteclkhs(hfp, bpp, lane_count, intel_dsi->burst_mode_ratio);
1293        hsync = txbyteclkhs(hsync, bpp, lane_count,
1294                            intel_dsi->burst_mode_ratio);
1295        hbp = txbyteclkhs(hbp, bpp, lane_count, intel_dsi->burst_mode_ratio);
1296
1297        for_each_dsi_port(port, intel_dsi->ports) {
1298                if (IS_GEN9_LP(dev_priv)) {
1299                        /*
1300                         * Program hdisplay and vdisplay on MIPI transcoder.
1301                         * This is different from calculated hactive and
1302                         * vactive, as they are calculated per channel basis,
1303                         * whereas these values should be based on resolution.
1304                         */
1305                        I915_WRITE(BXT_MIPI_TRANS_HACTIVE(port),
1306                                   adjusted_mode->crtc_hdisplay);
1307                        I915_WRITE(BXT_MIPI_TRANS_VACTIVE(port),
1308                                   adjusted_mode->crtc_vdisplay);
1309                        I915_WRITE(BXT_MIPI_TRANS_VTOTAL(port),
1310                                   adjusted_mode->crtc_vtotal);
1311                }
1312
1313                I915_WRITE(MIPI_HACTIVE_AREA_COUNT(port), hactive);
1314                I915_WRITE(MIPI_HFP_COUNT(port), hfp);
1315
1316                /* meaningful for video mode non-burst sync pulse mode only,
1317                 * can be zero for non-burst sync events and burst modes */
1318                I915_WRITE(MIPI_HSYNC_PADDING_COUNT(port), hsync);
1319                I915_WRITE(MIPI_HBP_COUNT(port), hbp);
1320
1321                /* vertical values are in terms of lines */
1322                I915_WRITE(MIPI_VFP_COUNT(port), vfp);
1323                I915_WRITE(MIPI_VSYNC_PADDING_COUNT(port), vsync);
1324                I915_WRITE(MIPI_VBP_COUNT(port), vbp);
1325        }
1326}
1327
1328static u32 pixel_format_to_reg(enum mipi_dsi_pixel_format fmt)
1329{
1330        switch (fmt) {
1331        case MIPI_DSI_FMT_RGB888:
1332                return VID_MODE_FORMAT_RGB888;
1333        case MIPI_DSI_FMT_RGB666:
1334                return VID_MODE_FORMAT_RGB666;
1335        case MIPI_DSI_FMT_RGB666_PACKED:
1336                return VID_MODE_FORMAT_RGB666_PACKED;
1337        case MIPI_DSI_FMT_RGB565:
1338                return VID_MODE_FORMAT_RGB565;
1339        default:
1340                MISSING_CASE(fmt);
1341                return VID_MODE_FORMAT_RGB666;
1342        }
1343}
1344
1345static void intel_dsi_prepare(struct intel_encoder *intel_encoder,
1346                              const struct intel_crtc_state *pipe_config)
1347{
1348        struct drm_encoder *encoder = &intel_encoder->base;
1349        struct drm_device *dev = encoder->dev;
1350        struct drm_i915_private *dev_priv = to_i915(dev);
1351        struct intel_crtc *intel_crtc = to_intel_crtc(pipe_config->base.crtc);
1352        struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1353        const struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
1354        enum port port;
1355        unsigned int bpp = mipi_dsi_pixel_format_to_bpp(intel_dsi->pixel_format);
1356        u32 val, tmp;
1357        u16 mode_hdisplay;
1358
1359        DRM_DEBUG_KMS("pipe %c\n", pipe_name(intel_crtc->pipe));
1360
1361        mode_hdisplay = adjusted_mode->crtc_hdisplay;
1362
1363        if (intel_dsi->dual_link) {
1364                mode_hdisplay /= 2;
1365                if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK)
1366                        mode_hdisplay += intel_dsi->pixel_overlap;
1367        }
1368
1369        for_each_dsi_port(port, intel_dsi->ports) {
1370                if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
1371                        /*
1372                         * escape clock divider, 20MHz, shared for A and C.
1373                         * device ready must be off when doing this! txclkesc?
1374                         */
1375                        tmp = I915_READ(MIPI_CTRL(PORT_A));
1376                        tmp &= ~ESCAPE_CLOCK_DIVIDER_MASK;
1377                        I915_WRITE(MIPI_CTRL(PORT_A), tmp |
1378                                        ESCAPE_CLOCK_DIVIDER_1);
1379
1380                        /* read request priority is per pipe */
1381                        tmp = I915_READ(MIPI_CTRL(port));
1382                        tmp &= ~READ_REQUEST_PRIORITY_MASK;
1383                        I915_WRITE(MIPI_CTRL(port), tmp |
1384                                        READ_REQUEST_PRIORITY_HIGH);
1385                } else if (IS_GEN9_LP(dev_priv)) {
1386                        enum pipe pipe = intel_crtc->pipe;
1387
1388                        tmp = I915_READ(MIPI_CTRL(port));
1389                        tmp &= ~BXT_PIPE_SELECT_MASK;
1390
1391                        tmp |= BXT_PIPE_SELECT(pipe);
1392                        I915_WRITE(MIPI_CTRL(port), tmp);
1393                }
1394
1395                /* XXX: why here, why like this? handling in irq handler?! */
1396                I915_WRITE(MIPI_INTR_STAT(port), 0xffffffff);
1397                I915_WRITE(MIPI_INTR_EN(port), 0xffffffff);
1398
1399                I915_WRITE(MIPI_DPHY_PARAM(port), intel_dsi->dphy_reg);
1400
1401                I915_WRITE(MIPI_DPI_RESOLUTION(port),
1402                        adjusted_mode->crtc_vdisplay << VERTICAL_ADDRESS_SHIFT |
1403                        mode_hdisplay << HORIZONTAL_ADDRESS_SHIFT);
1404        }
1405
1406        set_dsi_timings(encoder, adjusted_mode);
1407
1408        val = intel_dsi->lane_count << DATA_LANES_PRG_REG_SHIFT;
1409        if (is_cmd_mode(intel_dsi)) {
1410                val |= intel_dsi->channel << CMD_MODE_CHANNEL_NUMBER_SHIFT;
1411                val |= CMD_MODE_DATA_WIDTH_8_BIT; /* XXX */
1412        } else {
1413                val |= intel_dsi->channel << VID_MODE_CHANNEL_NUMBER_SHIFT;
1414                val |= pixel_format_to_reg(intel_dsi->pixel_format);
1415        }
1416
1417        tmp = 0;
1418        if (intel_dsi->eotp_pkt == 0)
1419                tmp |= EOT_DISABLE;
1420        if (intel_dsi->clock_stop)
1421                tmp |= CLOCKSTOP;
1422
1423        if (IS_GEN9_LP(dev_priv)) {
1424                tmp |= BXT_DPHY_DEFEATURE_EN;
1425                if (!is_cmd_mode(intel_dsi))
1426                        tmp |= BXT_DEFEATURE_DPI_FIFO_CTR;
1427        }
1428
1429        for_each_dsi_port(port, intel_dsi->ports) {
1430                I915_WRITE(MIPI_DSI_FUNC_PRG(port), val);
1431
1432                /* timeouts for recovery. one frame IIUC. if counter expires,
1433                 * EOT and stop state. */
1434
1435                /*
1436                 * In burst mode, value greater than one DPI line Time in byte
1437                 * clock (txbyteclkhs) To timeout this timer 1+ of the above
1438                 * said value is recommended.
1439                 *
1440                 * In non-burst mode, Value greater than one DPI frame time in
1441                 * byte clock(txbyteclkhs) To timeout this timer 1+ of the above
1442                 * said value is recommended.
1443                 *
1444                 * In DBI only mode, value greater than one DBI frame time in
1445                 * byte clock(txbyteclkhs) To timeout this timer 1+ of the above
1446                 * said value is recommended.
1447                 */
1448
1449                if (is_vid_mode(intel_dsi) &&
1450                        intel_dsi->video_mode_format == VIDEO_MODE_BURST) {
1451                        I915_WRITE(MIPI_HS_TX_TIMEOUT(port),
1452                                txbyteclkhs(adjusted_mode->crtc_htotal, bpp,
1453                                            intel_dsi->lane_count,
1454                                            intel_dsi->burst_mode_ratio) + 1);
1455                } else {
1456                        I915_WRITE(MIPI_HS_TX_TIMEOUT(port),
1457                                txbyteclkhs(adjusted_mode->crtc_vtotal *
1458                                            adjusted_mode->crtc_htotal,
1459                                            bpp, intel_dsi->lane_count,
1460                                            intel_dsi->burst_mode_ratio) + 1);
1461                }
1462                I915_WRITE(MIPI_LP_RX_TIMEOUT(port), intel_dsi->lp_rx_timeout);
1463                I915_WRITE(MIPI_TURN_AROUND_TIMEOUT(port),
1464                                                intel_dsi->turn_arnd_val);
1465                I915_WRITE(MIPI_DEVICE_RESET_TIMER(port),
1466                                                intel_dsi->rst_timer_val);
1467
1468                /* dphy stuff */
1469
1470                /* in terms of low power clock */
1471                I915_WRITE(MIPI_INIT_COUNT(port),
1472                                txclkesc(intel_dsi->escape_clk_div, 100));
1473
1474                if (IS_GEN9_LP(dev_priv) && (!intel_dsi->dual_link)) {
1475                        /*
1476                         * BXT spec says write MIPI_INIT_COUNT for
1477                         * both the ports, even if only one is
1478                         * getting used. So write the other port
1479                         * if not in dual link mode.
1480                         */
1481                        I915_WRITE(MIPI_INIT_COUNT(port ==
1482                                                PORT_A ? PORT_C : PORT_A),
1483                                        intel_dsi->init_count);
1484                }
1485
1486                /* recovery disables */
1487                I915_WRITE(MIPI_EOT_DISABLE(port), tmp);
1488
1489                /* in terms of low power clock */
1490                I915_WRITE(MIPI_INIT_COUNT(port), intel_dsi->init_count);
1491
1492                /* in terms of txbyteclkhs. actual high to low switch +
1493                 * MIPI_STOP_STATE_STALL * MIPI_LP_BYTECLK.
1494                 *
1495                 * XXX: write MIPI_STOP_STATE_STALL?
1496                 */
1497                I915_WRITE(MIPI_HIGH_LOW_SWITCH_COUNT(port),
1498                                                intel_dsi->hs_to_lp_count);
1499
1500                /* XXX: low power clock equivalence in terms of byte clock.
1501                 * the number of byte clocks occupied in one low power clock.
1502                 * based on txbyteclkhs and txclkesc.
1503                 * txclkesc time / txbyteclk time * (105 + MIPI_STOP_STATE_STALL
1504                 * ) / 105.???
1505                 */
1506                I915_WRITE(MIPI_LP_BYTECLK(port), intel_dsi->lp_byte_clk);
1507
1508                if (IS_GEMINILAKE(dev_priv)) {
1509                        I915_WRITE(MIPI_TLPX_TIME_COUNT(port),
1510                                        intel_dsi->lp_byte_clk);
1511                        /* Shadow of DPHY reg */
1512                        I915_WRITE(MIPI_CLK_LANE_TIMING(port),
1513                                        intel_dsi->dphy_reg);
1514                }
1515
1516                /* the bw essential for transmitting 16 long packets containing
1517                 * 252 bytes meant for dcs write memory command is programmed in
1518                 * this register in terms of byte clocks. based on dsi transfer
1519                 * rate and the number of lanes configured the time taken to
1520                 * transmit 16 long packets in a dsi stream varies. */
1521                I915_WRITE(MIPI_DBI_BW_CTRL(port), intel_dsi->bw_timer);
1522
1523                I915_WRITE(MIPI_CLK_LANE_SWITCH_TIME_CNT(port),
1524                intel_dsi->clk_lp_to_hs_count << LP_HS_SSW_CNT_SHIFT |
1525                intel_dsi->clk_hs_to_lp_count << HS_LP_PWR_SW_CNT_SHIFT);
1526
1527                if (is_vid_mode(intel_dsi))
1528                        /* Some panels might have resolution which is not a
1529                         * multiple of 64 like 1366 x 768. Enable RANDOM
1530                         * resolution support for such panels by default */
1531                        I915_WRITE(MIPI_VIDEO_MODE_FORMAT(port),
1532                                intel_dsi->video_frmt_cfg_bits |
1533                                intel_dsi->video_mode_format |
1534                                IP_TG_CONFIG |
1535                                RANDOM_DPI_DISPLAY_RESOLUTION);
1536        }
1537}
1538
1539static void intel_dsi_unprepare(struct intel_encoder *encoder)
1540{
1541        struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
1542        struct intel_dsi *intel_dsi = enc_to_intel_dsi(&encoder->base);
1543        enum port port;
1544        u32 val;
1545
1546        if (IS_GEMINILAKE(dev_priv))
1547                return;
1548
1549        for_each_dsi_port(port, intel_dsi->ports) {
1550                /* Panel commands can be sent when clock is in LP11 */
1551                I915_WRITE(MIPI_DEVICE_READY(port), 0x0);
1552
1553                if (IS_GEN9_LP(dev_priv))
1554                        bxt_dsi_reset_clocks(encoder, port);
1555                else
1556                        vlv_dsi_reset_clocks(encoder, port);
1557                I915_WRITE(MIPI_EOT_DISABLE(port), CLOCKSTOP);
1558
1559                val = I915_READ(MIPI_DSI_FUNC_PRG(port));
1560                val &= ~VID_MODE_FORMAT_MASK;
1561                I915_WRITE(MIPI_DSI_FUNC_PRG(port), val);
1562
1563                I915_WRITE(MIPI_DEVICE_READY(port), 0x1);
1564        }
1565}
1566
1567static void intel_dsi_encoder_destroy(struct drm_encoder *encoder)
1568{
1569        struct intel_dsi *intel_dsi = enc_to_intel_dsi(encoder);
1570
1571        /* dispose of the gpios */
1572        if (intel_dsi->gpio_panel)
1573                gpiod_put(intel_dsi->gpio_panel);
1574
1575        intel_encoder_destroy(encoder);
1576}
1577
1578static const struct drm_encoder_funcs intel_dsi_funcs = {
1579        .destroy = intel_dsi_encoder_destroy,
1580};
1581
1582static const struct drm_connector_helper_funcs intel_dsi_connector_helper_funcs = {
1583        .get_modes = intel_dsi_get_modes,
1584        .mode_valid = intel_dsi_mode_valid,
1585        .atomic_check = intel_digital_connector_atomic_check,
1586};
1587
1588static const struct drm_connector_funcs intel_dsi_connector_funcs = {
1589        .late_register = intel_connector_register,
1590        .early_unregister = intel_connector_unregister,
1591        .destroy = intel_connector_destroy,
1592        .fill_modes = drm_helper_probe_single_connector_modes,
1593        .atomic_get_property = intel_digital_connector_atomic_get_property,
1594        .atomic_set_property = intel_digital_connector_atomic_set_property,
1595        .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1596        .atomic_duplicate_state = intel_digital_connector_duplicate_state,
1597};
1598
1599static enum drm_panel_orientation
1600vlv_dsi_get_hw_panel_orientation(struct intel_connector *connector)
1601{
1602        struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1603        struct intel_encoder *encoder = connector->encoder;
1604        enum intel_display_power_domain power_domain;
1605        enum drm_panel_orientation orientation;
1606        struct intel_plane *plane;
1607        struct intel_crtc *crtc;
1608        intel_wakeref_t wakeref;
1609        enum pipe pipe;
1610        u32 val;
1611
1612        if (!encoder->get_hw_state(encoder, &pipe))
1613                return DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
1614
1615        crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
1616        plane = to_intel_plane(crtc->base.primary);
1617
1618        power_domain = POWER_DOMAIN_PIPE(pipe);
1619        wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
1620        if (!wakeref)
1621                return DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
1622
1623        val = I915_READ(DSPCNTR(plane->i9xx_plane));
1624
1625        if (!(val & DISPLAY_PLANE_ENABLE))
1626                orientation = DRM_MODE_PANEL_ORIENTATION_UNKNOWN;
1627        else if (val & DISPPLANE_ROTATE_180)
1628                orientation = DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP;
1629        else
1630                orientation = DRM_MODE_PANEL_ORIENTATION_NORMAL;
1631
1632        intel_display_power_put(dev_priv, power_domain, wakeref);
1633
1634        return orientation;
1635}
1636
1637static enum drm_panel_orientation
1638vlv_dsi_get_panel_orientation(struct intel_connector *connector)
1639{
1640        struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1641        enum drm_panel_orientation orientation;
1642
1643        if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
1644                orientation = vlv_dsi_get_hw_panel_orientation(connector);
1645                if (orientation != DRM_MODE_PANEL_ORIENTATION_UNKNOWN)
1646                        return orientation;
1647        }
1648
1649        return intel_dsi_get_panel_orientation(connector);
1650}
1651
1652static void intel_dsi_add_properties(struct intel_connector *connector)
1653{
1654        struct drm_i915_private *dev_priv = to_i915(connector->base.dev);
1655
1656        if (connector->panel.fixed_mode) {
1657                u32 allowed_scalers;
1658
1659                allowed_scalers = BIT(DRM_MODE_SCALE_ASPECT) | BIT(DRM_MODE_SCALE_FULLSCREEN);
1660                if (!HAS_GMCH(dev_priv))
1661                        allowed_scalers |= BIT(DRM_MODE_SCALE_CENTER);
1662
1663                drm_connector_attach_scaling_mode_property(&connector->base,
1664                                                                allowed_scalers);
1665
1666                connector->base.state->scaling_mode = DRM_MODE_SCALE_ASPECT;
1667
1668                connector->base.display_info.panel_orientation =
1669                        vlv_dsi_get_panel_orientation(connector);
1670                drm_connector_init_panel_orientation_property(
1671                                &connector->base,
1672                                connector->panel.fixed_mode->hdisplay,
1673                                connector->panel.fixed_mode->vdisplay);
1674        }
1675}
1676
1677void vlv_dsi_init(struct drm_i915_private *dev_priv)
1678{
1679        struct drm_device *dev = &dev_priv->drm;
1680        struct intel_dsi *intel_dsi;
1681        struct intel_encoder *intel_encoder;
1682        struct drm_encoder *encoder;
1683        struct intel_connector *intel_connector;
1684        struct drm_connector *connector;
1685        struct drm_display_mode *scan, *fixed_mode = NULL;
1686        enum port port;
1687
1688        DRM_DEBUG_KMS("\n");
1689
1690        /* There is no detection method for MIPI so rely on VBT */
1691        if (!intel_bios_is_dsi_present(dev_priv, &port))
1692                return;
1693
1694        if (IS_GEN9_LP(dev_priv))
1695                dev_priv->mipi_mmio_base = BXT_MIPI_BASE;
1696        else
1697                dev_priv->mipi_mmio_base = VLV_MIPI_BASE;
1698
1699        intel_dsi = kzalloc(sizeof(*intel_dsi), GFP_KERNEL);
1700        if (!intel_dsi)
1701                return;
1702
1703        intel_connector = intel_connector_alloc();
1704        if (!intel_connector) {
1705                kfree(intel_dsi);
1706                return;
1707        }
1708
1709        intel_encoder = &intel_dsi->base;
1710        encoder = &intel_encoder->base;
1711        intel_dsi->attached_connector = intel_connector;
1712
1713        connector = &intel_connector->base;
1714
1715        drm_encoder_init(dev, encoder, &intel_dsi_funcs, DRM_MODE_ENCODER_DSI,
1716                         "DSI %c", port_name(port));
1717
1718        intel_encoder->compute_config = intel_dsi_compute_config;
1719        intel_encoder->pre_enable = intel_dsi_pre_enable;
1720        intel_encoder->disable = intel_dsi_disable;
1721        intel_encoder->post_disable = intel_dsi_post_disable;
1722        intel_encoder->get_hw_state = intel_dsi_get_hw_state;
1723        intel_encoder->get_config = intel_dsi_get_config;
1724        intel_encoder->update_pipe = intel_panel_update_backlight;
1725
1726        intel_connector->get_hw_state = intel_connector_get_hw_state;
1727
1728        intel_encoder->port = port;
1729
1730        /*
1731         * On BYT/CHV, pipe A maps to MIPI DSI port A, pipe B maps to MIPI DSI
1732         * port C. BXT isn't limited like this.
1733         */
1734        if (IS_GEN9_LP(dev_priv))
1735                intel_encoder->crtc_mask = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C);
1736        else if (port == PORT_A)
1737                intel_encoder->crtc_mask = BIT(PIPE_A);
1738        else
1739                intel_encoder->crtc_mask = BIT(PIPE_B);
1740
1741        if (dev_priv->vbt.dsi.config->dual_link)
1742                intel_dsi->ports = BIT(PORT_A) | BIT(PORT_C);
1743        else
1744                intel_dsi->ports = BIT(port);
1745
1746        intel_dsi->dcs_backlight_ports = dev_priv->vbt.dsi.bl_ports;
1747        intel_dsi->dcs_cabc_ports = dev_priv->vbt.dsi.cabc_ports;
1748
1749        /* Create a DSI host (and a device) for each port. */
1750        for_each_dsi_port(port, intel_dsi->ports) {
1751                struct intel_dsi_host *host;
1752
1753                host = intel_dsi_host_init(intel_dsi, &intel_dsi_host_ops,
1754                                           port);
1755                if (!host)
1756                        goto err;
1757
1758                intel_dsi->dsi_hosts[port] = host;
1759        }
1760
1761        if (!intel_dsi_vbt_init(intel_dsi, MIPI_DSI_GENERIC_PANEL_ID)) {
1762                DRM_DEBUG_KMS("no device found\n");
1763                goto err;
1764        }
1765
1766        /*
1767         * In case of BYT with CRC PMIC, we need to use GPIO for
1768         * Panel control.
1769         */
1770        if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
1771            (dev_priv->vbt.dsi.config->pwm_blc == PPS_BLC_PMIC)) {
1772                intel_dsi->gpio_panel =
1773                        gpiod_get(dev->dev, "panel", GPIOD_OUT_HIGH);
1774
1775                if (IS_ERR(intel_dsi->gpio_panel)) {
1776                        DRM_ERROR("Failed to own gpio for panel control\n");
1777                        intel_dsi->gpio_panel = NULL;
1778                }
1779        }
1780
1781        intel_encoder->type = INTEL_OUTPUT_DSI;
1782        intel_encoder->power_domain = POWER_DOMAIN_PORT_DSI;
1783        intel_encoder->cloneable = 0;
1784        drm_connector_init(dev, connector, &intel_dsi_connector_funcs,
1785                           DRM_MODE_CONNECTOR_DSI);
1786
1787        drm_connector_helper_add(connector, &intel_dsi_connector_helper_funcs);
1788
1789        connector->display_info.subpixel_order = SubPixelHorizontalRGB; /*XXX*/
1790        connector->interlace_allowed = false;
1791        connector->doublescan_allowed = false;
1792
1793        intel_connector_attach_encoder(intel_connector, intel_encoder);
1794
1795        mutex_lock(&dev->mode_config.mutex);
1796        intel_dsi_vbt_get_modes(intel_dsi);
1797        list_for_each_entry(scan, &connector->probed_modes, head) {
1798                if ((scan->type & DRM_MODE_TYPE_PREFERRED)) {
1799                        fixed_mode = drm_mode_duplicate(dev, scan);
1800                        break;
1801                }
1802        }
1803        mutex_unlock(&dev->mode_config.mutex);
1804
1805        if (!fixed_mode) {
1806                DRM_DEBUG_KMS("no fixed mode\n");
1807                goto err;
1808        }
1809
1810        connector->display_info.width_mm = fixed_mode->width_mm;
1811        connector->display_info.height_mm = fixed_mode->height_mm;
1812
1813        intel_panel_init(&intel_connector->panel, fixed_mode, NULL);
1814        intel_panel_setup_backlight(connector, INVALID_PIPE);
1815
1816        intel_dsi_add_properties(intel_connector);
1817
1818        return;
1819
1820err:
1821        drm_encoder_cleanup(&intel_encoder->base);
1822        kfree(intel_dsi);
1823        kfree(intel_connector);
1824}
1825