linux/drivers/gpu/drm/i915/intel_dsi_vbt.c
<<
>>
Prefs
   1/*
   2 * Copyright © 2014 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: Shobhit Kumar <shobhit.kumar@intel.com>
  24 *
  25 */
  26
  27#include <drm/drmP.h>
  28#include <drm/drm_crtc.h>
  29#include <drm/drm_edid.h>
  30#include <drm/i915_drm.h>
  31#include <linux/gpio/consumer.h>
  32#include <linux/slab.h>
  33#include <video/mipi_display.h>
  34#include <video/mipi_display.h>
  35#include "i915_drv.h"
  36#include "intel_drv.h"
  37#include "intel_dsi.h"
  38
  39#define MIPI_TRANSFER_MODE_SHIFT        0
  40#define MIPI_VIRTUAL_CHANNEL_SHIFT      1
  41#define MIPI_PORT_SHIFT                 3
  42
  43#define PREPARE_CNT_MAX         0x3F
  44#define EXIT_ZERO_CNT_MAX       0x3F
  45#define CLK_ZERO_CNT_MAX        0xFF
  46#define TRAIL_CNT_MAX           0x1F
  47
  48#define NS_KHZ_RATIO 1000000
  49
  50/* base offsets for gpio pads */
  51#define VLV_GPIO_NC_0_HV_DDI0_HPD       0x4130
  52#define VLV_GPIO_NC_1_HV_DDI0_DDC_SDA   0x4120
  53#define VLV_GPIO_NC_2_HV_DDI0_DDC_SCL   0x4110
  54#define VLV_GPIO_NC_3_PANEL0_VDDEN      0x4140
  55#define VLV_GPIO_NC_4_PANEL0_BKLTEN     0x4150
  56#define VLV_GPIO_NC_5_PANEL0_BKLTCTL    0x4160
  57#define VLV_GPIO_NC_6_HV_DDI1_HPD       0x4180
  58#define VLV_GPIO_NC_7_HV_DDI1_DDC_SDA   0x4190
  59#define VLV_GPIO_NC_8_HV_DDI1_DDC_SCL   0x4170
  60#define VLV_GPIO_NC_9_PANEL1_VDDEN      0x4100
  61#define VLV_GPIO_NC_10_PANEL1_BKLTEN    0x40E0
  62#define VLV_GPIO_NC_11_PANEL1_BKLTCTL   0x40F0
  63
  64#define VLV_GPIO_PCONF0(base_offset)    (base_offset)
  65#define VLV_GPIO_PAD_VAL(base_offset)   ((base_offset) + 8)
  66
  67struct gpio_map {
  68        u16 base_offset;
  69        bool init;
  70};
  71
  72static struct gpio_map vlv_gpio_table[] = {
  73        { VLV_GPIO_NC_0_HV_DDI0_HPD },
  74        { VLV_GPIO_NC_1_HV_DDI0_DDC_SDA },
  75        { VLV_GPIO_NC_2_HV_DDI0_DDC_SCL },
  76        { VLV_GPIO_NC_3_PANEL0_VDDEN },
  77        { VLV_GPIO_NC_4_PANEL0_BKLTEN },
  78        { VLV_GPIO_NC_5_PANEL0_BKLTCTL },
  79        { VLV_GPIO_NC_6_HV_DDI1_HPD },
  80        { VLV_GPIO_NC_7_HV_DDI1_DDC_SDA },
  81        { VLV_GPIO_NC_8_HV_DDI1_DDC_SCL },
  82        { VLV_GPIO_NC_9_PANEL1_VDDEN },
  83        { VLV_GPIO_NC_10_PANEL1_BKLTEN },
  84        { VLV_GPIO_NC_11_PANEL1_BKLTCTL },
  85};
  86
  87#define CHV_GPIO_IDX_START_N            0
  88#define CHV_GPIO_IDX_START_E            73
  89#define CHV_GPIO_IDX_START_SW           100
  90#define CHV_GPIO_IDX_START_SE           198
  91
  92#define CHV_VBT_MAX_PINS_PER_FMLY       15
  93
  94#define CHV_GPIO_PAD_CFG0(f, i)         (0x4400 + (f) * 0x400 + (i) * 8)
  95#define  CHV_GPIO_GPIOEN                (1 << 15)
  96#define  CHV_GPIO_GPIOCFG_GPIO          (0 << 8)
  97#define  CHV_GPIO_GPIOCFG_GPO           (1 << 8)
  98#define  CHV_GPIO_GPIOCFG_GPI           (2 << 8)
  99#define  CHV_GPIO_GPIOCFG_HIZ           (3 << 8)
 100#define  CHV_GPIO_GPIOTXSTATE(state)    ((!!(state)) << 1)
 101
 102#define CHV_GPIO_PAD_CFG1(f, i)         (0x4400 + (f) * 0x400 + (i) * 8 + 4)
 103#define  CHV_GPIO_CFGLOCK               (1 << 31)
 104
 105static inline enum port intel_dsi_seq_port_to_port(u8 port)
 106{
 107        return port ? PORT_C : PORT_A;
 108}
 109
 110static const u8 *mipi_exec_send_packet(struct intel_dsi *intel_dsi,
 111                                       const u8 *data)
 112{
 113        struct mipi_dsi_device *dsi_device;
 114        u8 type, flags, seq_port;
 115        u16 len;
 116        enum port port;
 117
 118        DRM_DEBUG_KMS("\n");
 119
 120        flags = *data++;
 121        type = *data++;
 122
 123        len = *((u16 *) data);
 124        data += 2;
 125
 126        seq_port = (flags >> MIPI_PORT_SHIFT) & 3;
 127
 128        /* For DSI single link on Port A & C, the seq_port value which is
 129         * parsed from Sequence Block#53 of VBT has been set to 0
 130         * Now, read/write of packets for the DSI single link on Port A and
 131         * Port C will based on the DVO port from VBT block 2.
 132         */
 133        if (intel_dsi->ports == (1 << PORT_C))
 134                port = PORT_C;
 135        else
 136                port = intel_dsi_seq_port_to_port(seq_port);
 137
 138        dsi_device = intel_dsi->dsi_hosts[port]->device;
 139        if (!dsi_device) {
 140                DRM_DEBUG_KMS("no dsi device for port %c\n", port_name(port));
 141                goto out;
 142        }
 143
 144        if ((flags >> MIPI_TRANSFER_MODE_SHIFT) & 1)
 145                dsi_device->mode_flags &= ~MIPI_DSI_MODE_LPM;
 146        else
 147                dsi_device->mode_flags |= MIPI_DSI_MODE_LPM;
 148
 149        dsi_device->channel = (flags >> MIPI_VIRTUAL_CHANNEL_SHIFT) & 3;
 150
 151        switch (type) {
 152        case MIPI_DSI_GENERIC_SHORT_WRITE_0_PARAM:
 153                mipi_dsi_generic_write(dsi_device, NULL, 0);
 154                break;
 155        case MIPI_DSI_GENERIC_SHORT_WRITE_1_PARAM:
 156                mipi_dsi_generic_write(dsi_device, data, 1);
 157                break;
 158        case MIPI_DSI_GENERIC_SHORT_WRITE_2_PARAM:
 159                mipi_dsi_generic_write(dsi_device, data, 2);
 160                break;
 161        case MIPI_DSI_GENERIC_READ_REQUEST_0_PARAM:
 162        case MIPI_DSI_GENERIC_READ_REQUEST_1_PARAM:
 163        case MIPI_DSI_GENERIC_READ_REQUEST_2_PARAM:
 164                DRM_DEBUG_DRIVER("Generic Read not yet implemented or used\n");
 165                break;
 166        case MIPI_DSI_GENERIC_LONG_WRITE:
 167                mipi_dsi_generic_write(dsi_device, data, len);
 168                break;
 169        case MIPI_DSI_DCS_SHORT_WRITE:
 170                mipi_dsi_dcs_write_buffer(dsi_device, data, 1);
 171                break;
 172        case MIPI_DSI_DCS_SHORT_WRITE_PARAM:
 173                mipi_dsi_dcs_write_buffer(dsi_device, data, 2);
 174                break;
 175        case MIPI_DSI_DCS_READ:
 176                DRM_DEBUG_DRIVER("DCS Read not yet implemented or used\n");
 177                break;
 178        case MIPI_DSI_DCS_LONG_WRITE:
 179                mipi_dsi_dcs_write_buffer(dsi_device, data, len);
 180                break;
 181        }
 182
 183        wait_for_dsi_fifo_empty(intel_dsi, port);
 184
 185out:
 186        data += len;
 187
 188        return data;
 189}
 190
 191static const u8 *mipi_exec_delay(struct intel_dsi *intel_dsi, const u8 *data)
 192{
 193        u32 delay = *((const u32 *) data);
 194
 195        DRM_DEBUG_KMS("\n");
 196
 197        usleep_range(delay, delay + 10);
 198        data += 4;
 199
 200        return data;
 201}
 202
 203static void vlv_exec_gpio(struct drm_i915_private *dev_priv,
 204                          u8 gpio_source, u8 gpio_index, bool value)
 205{
 206        struct gpio_map *map;
 207        u16 pconf0, padval;
 208        u32 tmp;
 209        u8 port;
 210
 211        if (gpio_index >= ARRAY_SIZE(vlv_gpio_table)) {
 212                DRM_DEBUG_KMS("unknown gpio index %u\n", gpio_index);
 213                return;
 214        }
 215
 216        map = &vlv_gpio_table[gpio_index];
 217
 218        if (dev_priv->vbt.dsi.seq_version >= 3) {
 219                /* XXX: this assumes vlv_gpio_table only has NC GPIOs. */
 220                port = IOSF_PORT_GPIO_NC;
 221        } else {
 222                if (gpio_source == 0) {
 223                        port = IOSF_PORT_GPIO_NC;
 224                } else if (gpio_source == 1) {
 225                        DRM_DEBUG_KMS("SC gpio not supported\n");
 226                        return;
 227                } else {
 228                        DRM_DEBUG_KMS("unknown gpio source %u\n", gpio_source);
 229                        return;
 230                }
 231        }
 232
 233        pconf0 = VLV_GPIO_PCONF0(map->base_offset);
 234        padval = VLV_GPIO_PAD_VAL(map->base_offset);
 235
 236        mutex_lock(&dev_priv->sb_lock);
 237        if (!map->init) {
 238                /* FIXME: remove constant below */
 239                vlv_iosf_sb_write(dev_priv, port, pconf0, 0x2000CC00);
 240                map->init = true;
 241        }
 242
 243        tmp = 0x4 | value;
 244        vlv_iosf_sb_write(dev_priv, port, padval, tmp);
 245        mutex_unlock(&dev_priv->sb_lock);
 246}
 247
 248static void chv_exec_gpio(struct drm_i915_private *dev_priv,
 249                          u8 gpio_source, u8 gpio_index, bool value)
 250{
 251        u16 cfg0, cfg1;
 252        u16 family_num;
 253        u8 port;
 254
 255        if (dev_priv->vbt.dsi.seq_version >= 3) {
 256                if (gpio_index >= CHV_GPIO_IDX_START_SE) {
 257                        /* XXX: it's unclear whether 255->57 is part of SE. */
 258                        gpio_index -= CHV_GPIO_IDX_START_SE;
 259                        port = CHV_IOSF_PORT_GPIO_SE;
 260                } else if (gpio_index >= CHV_GPIO_IDX_START_SW) {
 261                        gpio_index -= CHV_GPIO_IDX_START_SW;
 262                        port = CHV_IOSF_PORT_GPIO_SW;
 263                } else if (gpio_index >= CHV_GPIO_IDX_START_E) {
 264                        gpio_index -= CHV_GPIO_IDX_START_E;
 265                        port = CHV_IOSF_PORT_GPIO_E;
 266                } else {
 267                        port = CHV_IOSF_PORT_GPIO_N;
 268                }
 269        } else {
 270                /* XXX: The spec is unclear about CHV GPIO on seq v2 */
 271                if (gpio_source != 0) {
 272                        DRM_DEBUG_KMS("unknown gpio source %u\n", gpio_source);
 273                        return;
 274                }
 275
 276                if (gpio_index >= CHV_GPIO_IDX_START_E) {
 277                        DRM_DEBUG_KMS("invalid gpio index %u for GPIO N\n",
 278                                      gpio_index);
 279                        return;
 280                }
 281
 282                port = CHV_IOSF_PORT_GPIO_N;
 283        }
 284
 285        family_num = gpio_index / CHV_VBT_MAX_PINS_PER_FMLY;
 286        gpio_index = gpio_index % CHV_VBT_MAX_PINS_PER_FMLY;
 287
 288        cfg0 = CHV_GPIO_PAD_CFG0(family_num, gpio_index);
 289        cfg1 = CHV_GPIO_PAD_CFG1(family_num, gpio_index);
 290
 291        mutex_lock(&dev_priv->sb_lock);
 292        vlv_iosf_sb_write(dev_priv, port, cfg1, 0);
 293        vlv_iosf_sb_write(dev_priv, port, cfg0,
 294                          CHV_GPIO_GPIOEN | CHV_GPIO_GPIOCFG_GPO |
 295                          CHV_GPIO_GPIOTXSTATE(value));
 296        mutex_unlock(&dev_priv->sb_lock);
 297}
 298
 299static void bxt_exec_gpio(struct drm_i915_private *dev_priv,
 300                          u8 gpio_source, u8 gpio_index, bool value)
 301{
 302        /* XXX: this table is a quick ugly hack. */
 303        static struct gpio_desc *bxt_gpio_table[U8_MAX + 1];
 304        struct gpio_desc *gpio_desc = bxt_gpio_table[gpio_index];
 305
 306        if (!gpio_desc) {
 307                gpio_desc = devm_gpiod_get_index(dev_priv->drm.dev,
 308                                                 NULL, gpio_index,
 309                                                 value ? GPIOD_OUT_LOW :
 310                                                 GPIOD_OUT_HIGH);
 311
 312                if (IS_ERR_OR_NULL(gpio_desc)) {
 313                        DRM_ERROR("GPIO index %u request failed (%ld)\n",
 314                                  gpio_index, PTR_ERR(gpio_desc));
 315                        return;
 316                }
 317
 318                bxt_gpio_table[gpio_index] = gpio_desc;
 319        }
 320
 321        gpiod_set_value(gpio_desc, value);
 322}
 323
 324static const u8 *mipi_exec_gpio(struct intel_dsi *intel_dsi, const u8 *data)
 325{
 326        struct drm_device *dev = intel_dsi->base.base.dev;
 327        struct drm_i915_private *dev_priv = to_i915(dev);
 328        u8 gpio_source, gpio_index = 0, gpio_number;
 329        bool value;
 330
 331        DRM_DEBUG_KMS("\n");
 332
 333        if (dev_priv->vbt.dsi.seq_version >= 3)
 334                gpio_index = *data++;
 335
 336        gpio_number = *data++;
 337
 338        /* gpio source in sequence v2 only */
 339        if (dev_priv->vbt.dsi.seq_version == 2)
 340                gpio_source = (*data >> 1) & 3;
 341        else
 342                gpio_source = 0;
 343
 344        /* pull up/down */
 345        value = *data++ & 1;
 346
 347        if (IS_VALLEYVIEW(dev_priv))
 348                vlv_exec_gpio(dev_priv, gpio_source, gpio_number, value);
 349        else if (IS_CHERRYVIEW(dev_priv))
 350                chv_exec_gpio(dev_priv, gpio_source, gpio_number, value);
 351        else
 352                bxt_exec_gpio(dev_priv, gpio_source, gpio_index, value);
 353
 354        return data;
 355}
 356
 357static const u8 *mipi_exec_i2c(struct intel_dsi *intel_dsi, const u8 *data)
 358{
 359        DRM_DEBUG_KMS("Skipping I2C element execution\n");
 360
 361        return data + *(data + 6) + 7;
 362}
 363
 364static const u8 *mipi_exec_spi(struct intel_dsi *intel_dsi, const u8 *data)
 365{
 366        DRM_DEBUG_KMS("Skipping SPI element execution\n");
 367
 368        return data + *(data + 5) + 6;
 369}
 370
 371static const u8 *mipi_exec_pmic(struct intel_dsi *intel_dsi, const u8 *data)
 372{
 373        DRM_DEBUG_KMS("Skipping PMIC element execution\n");
 374
 375        return data + 15;
 376}
 377
 378typedef const u8 * (*fn_mipi_elem_exec)(struct intel_dsi *intel_dsi,
 379                                        const u8 *data);
 380static const fn_mipi_elem_exec exec_elem[] = {
 381        [MIPI_SEQ_ELEM_SEND_PKT] = mipi_exec_send_packet,
 382        [MIPI_SEQ_ELEM_DELAY] = mipi_exec_delay,
 383        [MIPI_SEQ_ELEM_GPIO] = mipi_exec_gpio,
 384        [MIPI_SEQ_ELEM_I2C] = mipi_exec_i2c,
 385        [MIPI_SEQ_ELEM_SPI] = mipi_exec_spi,
 386        [MIPI_SEQ_ELEM_PMIC] = mipi_exec_pmic,
 387};
 388
 389/*
 390 * MIPI Sequence from VBT #53 parsing logic
 391 * We have already separated each seqence during bios parsing
 392 * Following is generic execution function for any sequence
 393 */
 394
 395static const char * const seq_name[] = {
 396        [MIPI_SEQ_DEASSERT_RESET] = "MIPI_SEQ_DEASSERT_RESET",
 397        [MIPI_SEQ_INIT_OTP] = "MIPI_SEQ_INIT_OTP",
 398        [MIPI_SEQ_DISPLAY_ON] = "MIPI_SEQ_DISPLAY_ON",
 399        [MIPI_SEQ_DISPLAY_OFF]  = "MIPI_SEQ_DISPLAY_OFF",
 400        [MIPI_SEQ_ASSERT_RESET] = "MIPI_SEQ_ASSERT_RESET",
 401        [MIPI_SEQ_BACKLIGHT_ON] = "MIPI_SEQ_BACKLIGHT_ON",
 402        [MIPI_SEQ_BACKLIGHT_OFF] = "MIPI_SEQ_BACKLIGHT_OFF",
 403        [MIPI_SEQ_TEAR_ON] = "MIPI_SEQ_TEAR_ON",
 404        [MIPI_SEQ_TEAR_OFF] = "MIPI_SEQ_TEAR_OFF",
 405        [MIPI_SEQ_POWER_ON] = "MIPI_SEQ_POWER_ON",
 406        [MIPI_SEQ_POWER_OFF] = "MIPI_SEQ_POWER_OFF",
 407};
 408
 409static const char *sequence_name(enum mipi_seq seq_id)
 410{
 411        if (seq_id < ARRAY_SIZE(seq_name) && seq_name[seq_id])
 412                return seq_name[seq_id];
 413        else
 414                return "(unknown)";
 415}
 416
 417void intel_dsi_vbt_exec_sequence(struct intel_dsi *intel_dsi,
 418                                 enum mipi_seq seq_id)
 419{
 420        struct drm_i915_private *dev_priv = to_i915(intel_dsi->base.base.dev);
 421        const u8 *data;
 422        fn_mipi_elem_exec mipi_elem_exec;
 423
 424        if (WARN_ON(seq_id >= ARRAY_SIZE(dev_priv->vbt.dsi.sequence)))
 425                return;
 426
 427        data = dev_priv->vbt.dsi.sequence[seq_id];
 428        if (!data)
 429                return;
 430
 431        WARN_ON(*data != seq_id);
 432
 433        DRM_DEBUG_KMS("Starting MIPI sequence %d - %s\n",
 434                      seq_id, sequence_name(seq_id));
 435
 436        /* Skip Sequence Byte. */
 437        data++;
 438
 439        /* Skip Size of Sequence. */
 440        if (dev_priv->vbt.dsi.seq_version >= 3)
 441                data += 4;
 442
 443        while (1) {
 444                u8 operation_byte = *data++;
 445                u8 operation_size = 0;
 446
 447                if (operation_byte == MIPI_SEQ_ELEM_END)
 448                        break;
 449
 450                if (operation_byte < ARRAY_SIZE(exec_elem))
 451                        mipi_elem_exec = exec_elem[operation_byte];
 452                else
 453                        mipi_elem_exec = NULL;
 454
 455                /* Size of Operation. */
 456                if (dev_priv->vbt.dsi.seq_version >= 3)
 457                        operation_size = *data++;
 458
 459                if (mipi_elem_exec) {
 460                        const u8 *next = data + operation_size;
 461
 462                        data = mipi_elem_exec(intel_dsi, data);
 463
 464                        /* Consistency check if we have size. */
 465                        if (operation_size && data != next) {
 466                                DRM_ERROR("Inconsistent operation size\n");
 467                                return;
 468                        }
 469                } else if (operation_size) {
 470                        /* We have size, skip. */
 471                        DRM_DEBUG_KMS("Unsupported MIPI operation byte %u\n",
 472                                      operation_byte);
 473                        data += operation_size;
 474                } else {
 475                        /* No size, can't skip without parsing. */
 476                        DRM_ERROR("Unsupported MIPI operation byte %u\n",
 477                                  operation_byte);
 478                        return;
 479                }
 480        }
 481}
 482
 483int intel_dsi_vbt_get_modes(struct intel_dsi *intel_dsi)
 484{
 485        struct intel_connector *connector = intel_dsi->attached_connector;
 486        struct drm_device *dev = intel_dsi->base.base.dev;
 487        struct drm_i915_private *dev_priv = to_i915(dev);
 488        struct drm_display_mode *mode;
 489
 490        mode = drm_mode_duplicate(dev, dev_priv->vbt.lfp_lvds_vbt_mode);
 491        if (!mode)
 492                return 0;
 493
 494        mode->type |= DRM_MODE_TYPE_PREFERRED;
 495
 496        drm_mode_probed_add(&connector->base, mode);
 497
 498        return 1;
 499}
 500
 501bool intel_dsi_vbt_init(struct intel_dsi *intel_dsi, u16 panel_id)
 502{
 503        struct drm_device *dev = intel_dsi->base.base.dev;
 504        struct drm_i915_private *dev_priv = to_i915(dev);
 505        struct mipi_config *mipi_config = dev_priv->vbt.dsi.config;
 506        struct mipi_pps_data *pps = dev_priv->vbt.dsi.pps;
 507        struct drm_display_mode *mode = dev_priv->vbt.lfp_lvds_vbt_mode;
 508        u32 bpp;
 509        u32 tlpx_ns, extra_byte_count, bitrate, tlpx_ui;
 510        u32 ui_num, ui_den;
 511        u32 prepare_cnt, exit_zero_cnt, clk_zero_cnt, trail_cnt;
 512        u32 ths_prepare_ns, tclk_trail_ns;
 513        u32 tclk_prepare_clkzero, ths_prepare_hszero;
 514        u32 lp_to_hs_switch, hs_to_lp_switch;
 515        u32 pclk, computed_ddr;
 516        u32 mul;
 517        u16 burst_mode_ratio;
 518        enum port port;
 519
 520        DRM_DEBUG_KMS("\n");
 521
 522        intel_dsi->eotp_pkt = mipi_config->eot_pkt_disabled ? 0 : 1;
 523        intel_dsi->clock_stop = mipi_config->enable_clk_stop ? 1 : 0;
 524        intel_dsi->lane_count = mipi_config->lane_cnt + 1;
 525        intel_dsi->pixel_format =
 526                        pixel_format_from_register_bits(
 527                                mipi_config->videomode_color_format << 7);
 528        bpp = mipi_dsi_pixel_format_to_bpp(intel_dsi->pixel_format);
 529
 530        intel_dsi->dual_link = mipi_config->dual_link;
 531        intel_dsi->pixel_overlap = mipi_config->pixel_overlap;
 532        intel_dsi->operation_mode = mipi_config->is_cmd_mode;
 533        intel_dsi->video_mode_format = mipi_config->video_transfer_mode;
 534        intel_dsi->escape_clk_div = mipi_config->byte_clk_sel;
 535        intel_dsi->lp_rx_timeout = mipi_config->lp_rx_timeout;
 536        intel_dsi->turn_arnd_val = mipi_config->turn_around_timeout;
 537        intel_dsi->rst_timer_val = mipi_config->device_reset_timer;
 538        intel_dsi->init_count = mipi_config->master_init_timer;
 539        intel_dsi->bw_timer = mipi_config->dbi_bw_timer;
 540        intel_dsi->video_frmt_cfg_bits =
 541                mipi_config->bta_enabled ? DISABLE_VIDEO_BTA : 0;
 542
 543        pclk = mode->clock;
 544
 545        /* In dual link mode each port needs half of pixel clock */
 546        if (intel_dsi->dual_link) {
 547                pclk = pclk / 2;
 548
 549                /* we can enable pixel_overlap if needed by panel. In this
 550                 * case we need to increase the pixelclock for extra pixels
 551                 */
 552                if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK) {
 553                        pclk += DIV_ROUND_UP(mode->vtotal *
 554                                                intel_dsi->pixel_overlap *
 555                                                60, 1000);
 556                }
 557        }
 558
 559        /* Burst Mode Ratio
 560         * Target ddr frequency from VBT / non burst ddr freq
 561         * multiply by 100 to preserve remainder
 562         */
 563        if (intel_dsi->video_mode_format == VIDEO_MODE_BURST) {
 564                if (mipi_config->target_burst_mode_freq) {
 565                        computed_ddr = (pclk * bpp) / intel_dsi->lane_count;
 566
 567                        if (mipi_config->target_burst_mode_freq <
 568                                                                computed_ddr) {
 569                                DRM_ERROR("Burst mode freq is less than computed\n");
 570                                return false;
 571                        }
 572
 573                        burst_mode_ratio = DIV_ROUND_UP(
 574                                mipi_config->target_burst_mode_freq * 100,
 575                                computed_ddr);
 576
 577                        pclk = DIV_ROUND_UP(pclk * burst_mode_ratio, 100);
 578                } else {
 579                        DRM_ERROR("Burst mode target is not set\n");
 580                        return false;
 581                }
 582        } else
 583                burst_mode_ratio = 100;
 584
 585        intel_dsi->burst_mode_ratio = burst_mode_ratio;
 586        intel_dsi->pclk = pclk;
 587
 588        bitrate = (pclk * bpp) / intel_dsi->lane_count;
 589
 590        switch (intel_dsi->escape_clk_div) {
 591        case 0:
 592                tlpx_ns = 50;
 593                break;
 594        case 1:
 595                tlpx_ns = 100;
 596                break;
 597
 598        case 2:
 599                tlpx_ns = 200;
 600                break;
 601        default:
 602                tlpx_ns = 50;
 603                break;
 604        }
 605
 606        switch (intel_dsi->lane_count) {
 607        case 1:
 608        case 2:
 609                extra_byte_count = 2;
 610                break;
 611        case 3:
 612                extra_byte_count = 4;
 613                break;
 614        case 4:
 615        default:
 616                extra_byte_count = 3;
 617                break;
 618        }
 619
 620        /* in Kbps */
 621        ui_num = NS_KHZ_RATIO;
 622        ui_den = bitrate;
 623
 624        tclk_prepare_clkzero = mipi_config->tclk_prepare_clkzero;
 625        ths_prepare_hszero = mipi_config->ths_prepare_hszero;
 626
 627        /*
 628         * B060
 629         * LP byte clock = TLPX/ (8UI)
 630         */
 631        intel_dsi->lp_byte_clk = DIV_ROUND_UP(tlpx_ns * ui_den, 8 * ui_num);
 632
 633        /* DDR clock period = 2 * UI
 634         * UI(sec) = 1/(bitrate * 10^3) (bitrate is in KHZ)
 635         * UI(nsec) = 10^6 / bitrate
 636         * DDR clock period (nsec) = 2 * UI = (2 * 10^6)/ bitrate
 637         * DDR clock count  = ns_value / DDR clock period
 638         *
 639         * For GEMINILAKE dphy_param_reg will be programmed in terms of
 640         * HS byte clock count for other platform in HS ddr clock count
 641         */
 642        mul = IS_GEMINILAKE(dev_priv) ? 8 : 2;
 643        ths_prepare_ns = max(mipi_config->ths_prepare,
 644                             mipi_config->tclk_prepare);
 645
 646        /* prepare count */
 647        prepare_cnt = DIV_ROUND_UP(ths_prepare_ns * ui_den, ui_num * mul);
 648
 649        /* exit zero count */
 650        exit_zero_cnt = DIV_ROUND_UP(
 651                                (ths_prepare_hszero - ths_prepare_ns) * ui_den,
 652                                ui_num * mul
 653                                );
 654
 655        /*
 656         * Exit zero is unified val ths_zero and ths_exit
 657         * minimum value for ths_exit = 110ns
 658         * min (exit_zero_cnt * 2) = 110/UI
 659         * exit_zero_cnt = 55/UI
 660         */
 661        if (exit_zero_cnt < (55 * ui_den / ui_num) && (55 * ui_den) % ui_num)
 662                exit_zero_cnt += 1;
 663
 664        /* clk zero count */
 665        clk_zero_cnt = DIV_ROUND_UP(
 666                                (tclk_prepare_clkzero - ths_prepare_ns)
 667                                * ui_den, ui_num * mul);
 668
 669        /* trail count */
 670        tclk_trail_ns = max(mipi_config->tclk_trail, mipi_config->ths_trail);
 671        trail_cnt = DIV_ROUND_UP(tclk_trail_ns * ui_den, ui_num * mul);
 672
 673        if (prepare_cnt > PREPARE_CNT_MAX ||
 674                exit_zero_cnt > EXIT_ZERO_CNT_MAX ||
 675                clk_zero_cnt > CLK_ZERO_CNT_MAX ||
 676                trail_cnt > TRAIL_CNT_MAX)
 677                DRM_DEBUG_DRIVER("Values crossing maximum limits, restricting to max values\n");
 678
 679        if (prepare_cnt > PREPARE_CNT_MAX)
 680                prepare_cnt = PREPARE_CNT_MAX;
 681
 682        if (exit_zero_cnt > EXIT_ZERO_CNT_MAX)
 683                exit_zero_cnt = EXIT_ZERO_CNT_MAX;
 684
 685        if (clk_zero_cnt > CLK_ZERO_CNT_MAX)
 686                clk_zero_cnt = CLK_ZERO_CNT_MAX;
 687
 688        if (trail_cnt > TRAIL_CNT_MAX)
 689                trail_cnt = TRAIL_CNT_MAX;
 690
 691        /* B080 */
 692        intel_dsi->dphy_reg = exit_zero_cnt << 24 | trail_cnt << 16 |
 693                                                clk_zero_cnt << 8 | prepare_cnt;
 694
 695        /*
 696         * LP to HS switch count = 4TLPX + PREP_COUNT * mul + EXIT_ZERO_COUNT *
 697         *                                      mul + 10UI + Extra Byte Count
 698         *
 699         * HS to LP switch count = THS-TRAIL + 2TLPX + Extra Byte Count
 700         * Extra Byte Count is calculated according to number of lanes.
 701         * High Low Switch Count is the Max of LP to HS and
 702         * HS to LP switch count
 703         *
 704         */
 705        tlpx_ui = DIV_ROUND_UP(tlpx_ns * ui_den, ui_num);
 706
 707        /* B044 */
 708        /* FIXME:
 709         * The comment above does not match with the code */
 710        lp_to_hs_switch = DIV_ROUND_UP(4 * tlpx_ui + prepare_cnt * mul +
 711                                                exit_zero_cnt * mul + 10, 8);
 712
 713        hs_to_lp_switch = DIV_ROUND_UP(mipi_config->ths_trail + 2 * tlpx_ui, 8);
 714
 715        intel_dsi->hs_to_lp_count = max(lp_to_hs_switch, hs_to_lp_switch);
 716        intel_dsi->hs_to_lp_count += extra_byte_count;
 717
 718        /* B088 */
 719        /* LP -> HS for clock lanes
 720         * LP clk sync + LP11 + LP01 + tclk_prepare + tclk_zero +
 721         *                                              extra byte count
 722         * 2TPLX + 1TLPX + 1 TPLX(in ns) + prepare_cnt * 2 + clk_zero_cnt *
 723         *                                      2(in UI) + extra byte count
 724         * In byteclks = (4TLPX + prepare_cnt * 2 + clk_zero_cnt *2 (in UI)) /
 725         *                                      8 + extra byte count
 726         */
 727        intel_dsi->clk_lp_to_hs_count =
 728                DIV_ROUND_UP(
 729                        4 * tlpx_ui + prepare_cnt * 2 +
 730                        clk_zero_cnt * 2,
 731                        8);
 732
 733        intel_dsi->clk_lp_to_hs_count += extra_byte_count;
 734
 735        /* HS->LP for Clock Lanes
 736         * Low Power clock synchronisations + 1Tx byteclk + tclk_trail +
 737         *                                              Extra byte count
 738         * 2TLPX + 8UI + (trail_count*2)(in UI) + Extra byte count
 739         * In byteclks = (2*TLpx(in UI) + trail_count*2 +8)(in UI)/8 +
 740         *                                              Extra byte count
 741         */
 742        intel_dsi->clk_hs_to_lp_count =
 743                DIV_ROUND_UP(2 * tlpx_ui + trail_cnt * 2 + 8,
 744                        8);
 745        intel_dsi->clk_hs_to_lp_count += extra_byte_count;
 746
 747        DRM_DEBUG_KMS("Pclk %d\n", intel_dsi->pclk);
 748        DRM_DEBUG_KMS("Pixel overlap %d\n", intel_dsi->pixel_overlap);
 749        DRM_DEBUG_KMS("Lane count %d\n", intel_dsi->lane_count);
 750        DRM_DEBUG_KMS("DPHY param reg 0x%x\n", intel_dsi->dphy_reg);
 751        DRM_DEBUG_KMS("Video mode format %s\n",
 752                      intel_dsi->video_mode_format == VIDEO_MODE_NON_BURST_WITH_SYNC_PULSE ?
 753                      "non-burst with sync pulse" :
 754                      intel_dsi->video_mode_format == VIDEO_MODE_NON_BURST_WITH_SYNC_EVENTS ?
 755                      "non-burst with sync events" :
 756                      intel_dsi->video_mode_format == VIDEO_MODE_BURST ?
 757                      "burst" : "<unknown>");
 758        DRM_DEBUG_KMS("Burst mode ratio %d\n", intel_dsi->burst_mode_ratio);
 759        DRM_DEBUG_KMS("Reset timer %d\n", intel_dsi->rst_timer_val);
 760        DRM_DEBUG_KMS("Eot %s\n", enableddisabled(intel_dsi->eotp_pkt));
 761        DRM_DEBUG_KMS("Clockstop %s\n", enableddisabled(!intel_dsi->clock_stop));
 762        DRM_DEBUG_KMS("Mode %s\n", intel_dsi->operation_mode ? "command" : "video");
 763        if (intel_dsi->dual_link == DSI_DUAL_LINK_FRONT_BACK)
 764                DRM_DEBUG_KMS("Dual link: DSI_DUAL_LINK_FRONT_BACK\n");
 765        else if (intel_dsi->dual_link == DSI_DUAL_LINK_PIXEL_ALT)
 766                DRM_DEBUG_KMS("Dual link: DSI_DUAL_LINK_PIXEL_ALT\n");
 767        else
 768                DRM_DEBUG_KMS("Dual link: NONE\n");
 769        DRM_DEBUG_KMS("Pixel Format %d\n", intel_dsi->pixel_format);
 770        DRM_DEBUG_KMS("TLPX %d\n", intel_dsi->escape_clk_div);
 771        DRM_DEBUG_KMS("LP RX Timeout 0x%x\n", intel_dsi->lp_rx_timeout);
 772        DRM_DEBUG_KMS("Turnaround Timeout 0x%x\n", intel_dsi->turn_arnd_val);
 773        DRM_DEBUG_KMS("Init Count 0x%x\n", intel_dsi->init_count);
 774        DRM_DEBUG_KMS("HS to LP Count 0x%x\n", intel_dsi->hs_to_lp_count);
 775        DRM_DEBUG_KMS("LP Byte Clock %d\n", intel_dsi->lp_byte_clk);
 776        DRM_DEBUG_KMS("DBI BW Timer 0x%x\n", intel_dsi->bw_timer);
 777        DRM_DEBUG_KMS("LP to HS Clock Count 0x%x\n", intel_dsi->clk_lp_to_hs_count);
 778        DRM_DEBUG_KMS("HS to LP Clock Count 0x%x\n", intel_dsi->clk_hs_to_lp_count);
 779        DRM_DEBUG_KMS("BTA %s\n",
 780                        enableddisabled(!(intel_dsi->video_frmt_cfg_bits & DISABLE_VIDEO_BTA)));
 781
 782        /* delays in VBT are in unit of 100us, so need to convert
 783         * here in ms
 784         * Delay (100us) * 100 /1000 = Delay / 10 (ms) */
 785        intel_dsi->backlight_off_delay = pps->bl_disable_delay / 10;
 786        intel_dsi->backlight_on_delay = pps->bl_enable_delay / 10;
 787        intel_dsi->panel_on_delay = pps->panel_on_delay / 10;
 788        intel_dsi->panel_off_delay = pps->panel_off_delay / 10;
 789        intel_dsi->panel_pwr_cycle_delay = pps->panel_power_cycle_delay / 10;
 790
 791        /* a regular driver would get the device in probe */
 792        for_each_dsi_port(port, intel_dsi->ports) {
 793                mipi_dsi_attach(intel_dsi->dsi_hosts[port]->device);
 794        }
 795
 796        return true;
 797}
 798