linux/drivers/gpu/drm/xilinx/xilinx_drm_dp.c
<<
>>
Prefs
   1/*
   2 * Xilinx DRM DisplayPort encoder driver for Xilinx
   3 *
   4 *  Copyright (C) 2014 Xilinx, Inc.
   5 *
   6 *  Author: Hyun Woo Kwon <hyunk@xilinx.com>
   7 *
   8 * This software is licensed under the terms of the GNU General Public
   9 * License version 2, as published by the Free Software Foundation, and
  10 * may be copied, distributed, and modified under those terms.
  11 *
  12 * This program is distributed in the hope that it will be useful,
  13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15 * GNU General Public License for more details.
  16 */
  17
  18#include <drm/drmP.h>
  19#include <drm/drm_crtc.h>
  20#include <drm/drm_crtc_helper.h>
  21#include <drm/drm_dp_helper.h>
  22#include <drm/drm_encoder_slave.h>
  23
  24#include <linux/clk.h>
  25#include <linux/debugfs.h>
  26#include <linux/delay.h>
  27#include <linux/device.h>
  28#include <linux/module.h>
  29#include <linux/mutex.h>
  30#include <linux/phy/phy.h>
  31#include <linux/phy/phy-zynqmp.h>
  32#include <linux/platform_device.h>
  33#include <linux/pm_runtime.h>
  34#include <linux/uaccess.h>
  35
  36#include "xilinx_drm_dp_sub.h"
  37#include "xilinx_drm_drv.h"
  38
  39static uint xilinx_drm_dp_aux_timeout_ms = 50;
  40module_param_named(aux_timeout_ms, xilinx_drm_dp_aux_timeout_ms, uint, 0444);
  41MODULE_PARM_DESC(aux_timeout_ms,
  42                 "DP aux timeout value in msec (default: 50)");
  43
  44static uint xilinx_drm_dp_power_on_delay_ms = 4;
  45module_param_named(power_on_delay_ms, xilinx_drm_dp_power_on_delay_ms, uint,
  46                   0644);
  47MODULE_PARM_DESC(power_on_delay,
  48                 "Delay after power on request in msec (default: 4)");
  49
  50/* Link configuration registers */
  51#define XILINX_DP_TX_LINK_BW_SET                        0x0
  52#define XILINX_DP_TX_LANE_CNT_SET                       0x4
  53#define XILINX_DP_TX_ENHANCED_FRAME_EN                  0x8
  54#define XILINX_DP_TX_TRAINING_PATTERN_SET               0xc
  55#define XILINX_DP_TX_SCRAMBLING_DISABLE                 0x14
  56#define XILINX_DP_TX_DOWNSPREAD_CTL                     0x18
  57#define XILINX_DP_TX_SW_RESET                           0x1c
  58#define XILINX_DP_TX_SW_RESET_STREAM1                   BIT(0)
  59#define XILINX_DP_TX_SW_RESET_STREAM2                   BIT(1)
  60#define XILINX_DP_TX_SW_RESET_STREAM3                   BIT(2)
  61#define XILINX_DP_TX_SW_RESET_STREAM4                   BIT(3)
  62#define XILINX_DP_TX_SW_RESET_AUX                       BIT(7)
  63#define XILINX_DP_TX_SW_RESET_ALL                       (XILINX_DP_TX_SW_RESET_STREAM1 | \
  64                                                         XILINX_DP_TX_SW_RESET_STREAM2 | \
  65                                                         XILINX_DP_TX_SW_RESET_STREAM3 | \
  66                                                         XILINX_DP_TX_SW_RESET_STREAM4 | \
  67                                                         XILINX_DP_TX_SW_RESET_AUX)
  68
  69/* Core enable registers */
  70#define XILINX_DP_TX_ENABLE                             0x80
  71#define XILINX_DP_TX_ENABLE_MAIN_STREAM                 0x84
  72#define XILINX_DP_TX_FORCE_SCRAMBLER_RESET              0xc0
  73#define XILINX_DP_TX_VERSION                            0xf8
  74#define XILINX_DP_TX_VERSION_MAJOR_MASK                 (0xff << 24)
  75#define XILINX_DP_TX_VERSION_MAJOR_SHIFT                24
  76#define XILINX_DP_TX_VERSION_MINOR_MASK                 (0xff << 16)
  77#define XILINX_DP_TX_VERSION_MINOR_SHIFT                16
  78#define XILINX_DP_TX_VERSION_REVISION_MASK              (0xf << 12)
  79#define XILINX_DP_TX_VERSION_REVISION_SHIFT             12
  80#define XILINX_DP_TX_VERSION_PATCH_MASK                 (0xf << 8)
  81#define XILINX_DP_TX_VERSION_PATCH_SHIFT                8
  82#define XILINX_DP_TX_VERSION_INTERNAL_MASK              (0xff << 0)
  83#define XILINX_DP_TX_VERSION_INTERNAL_SHIFT             0
  84
  85/* Core ID registers */
  86#define XILINX_DP_TX_CORE_ID                            0xfc
  87#define XILINX_DP_TX_CORE_ID_MAJOR_MASK                 (0xff << 24)
  88#define XILINX_DP_TX_CORE_ID_MAJOR_SHIFT                24
  89#define XILINX_DP_TX_CORE_ID_MINOR_MASK                 (0xff << 16)
  90#define XILINX_DP_TX_CORE_ID_MINOR_SHIFT                16
  91#define XILINX_DP_TX_CORE_ID_REVISION_MASK              (0xff << 8)
  92#define XILINX_DP_TX_CORE_ID_REVISION_SHIFT             8
  93#define XILINX_DP_TX_CORE_ID_DIRECTION                  BIT(0)
  94
  95/* AUX channel interface registers */
  96#define XILINX_DP_TX_AUX_COMMAND                        0x100
  97#define XILINX_DP_TX_AUX_COMMAND_CMD_SHIFT              8
  98#define XILINX_DP_TX_AUX_COMMAND_ADDRESS_ONLY           BIT(12)
  99#define XILINX_DP_TX_AUX_COMMAND_BYTES_SHIFT            0
 100#define XILINX_DP_TX_AUX_WRITE_FIFO                     0x104
 101#define XILINX_DP_TX_AUX_ADDRESS                        0x108
 102#define XILINX_DP_TX_CLK_DIVIDER                        0x10c
 103#define XILINX_DP_TX_CLK_DIVIDER_MHZ                    1000000
 104#define XILINX_DP_TX_CLK_DIVIDER_AUX_FILTER_SHIFT       8
 105#define XILINX_DP_TX_INTR_SIGNAL_STATE                  0x130
 106#define XILINX_DP_TX_INTR_SIGNAL_STATE_HPD              BIT(0)
 107#define XILINX_DP_TX_INTR_SIGNAL_STATE_REQUEST          BIT(1)
 108#define XILINX_DP_TX_INTR_SIGNAL_STATE_REPLY            BIT(2)
 109#define XILINX_DP_TX_INTR_SIGNAL_STATE_REPLY_TIMEOUT    BIT(3)
 110#define XILINX_DP_TX_AUX_REPLY_DATA                     0x134
 111#define XILINX_DP_TX_AUX_REPLY_CODE                     0x138
 112#define XILINX_DP_TX_AUX_REPLY_CODE_AUX_ACK             (0)
 113#define XILINX_DP_TX_AUX_REPLY_CODE_AUX_NACK            BIT(0)
 114#define XILINX_DP_TX_AUX_REPLY_CODE_AUX_DEFER           BIT(1)
 115#define XILINX_DP_TX_AUX_REPLY_CODE_I2C_ACK             (0)
 116#define XILINX_DP_TX_AUX_REPLY_CODE_I2C_NACK            BIT(2)
 117#define XILINX_DP_TX_AUX_REPLY_CODE_I2C_DEFER           BIT(3)
 118#define XILINX_DP_TX_AUX_REPLY_CNT                      0x13c
 119#define XILINX_DP_TX_AUX_REPLY_CNT_MASK                 0xff
 120#define XILINX_DP_TX_INTR_STATUS                        0x140
 121#define XILINX_DP_TX_INTR_MASK                          0x144
 122#define XILINX_DP_TX_INTR_HPD_IRQ                       BIT(0)
 123#define XILINX_DP_TX_INTR_HPD_EVENT                     BIT(1)
 124#define XILINX_DP_TX_INTR_REPLY_RECV                    BIT(2)
 125#define XILINX_DP_TX_INTR_REPLY_TIMEOUT                 BIT(3)
 126#define XILINX_DP_TX_INTR_HPD_PULSE                     BIT(4)
 127#define XILINX_DP_TX_INTR_EXT_PKT_TXD                   BIT(5)
 128#define XILINX_DP_TX_INTR_LIV_ABUF_UNDRFLW              BIT(12)
 129#define XILINX_DP_TX_INTR_VBLANK_START                  BIT(13)
 130#define XILINX_DP_TX_INTR_PIXEL0_MATCH                  BIT(14)
 131#define XILINX_DP_TX_INTR_PIXEL1_MATCH                  BIT(15)
 132#define XILINX_DP_TX_INTR_CHBUF_UNDERFLW_MASK           0x3f0000
 133#define XILINX_DP_TX_INTR_CHBUF_OVERFLW_MASK            0xfc00000
 134#define XILINX_DP_TX_INTR_CUST_TS_2                     BIT(28)
 135#define XILINX_DP_TX_INTR_CUST_TS                       BIT(29)
 136#define XILINX_DP_TX_INTR_EXT_VSYNC_TS                  BIT(30)
 137#define XILINX_DP_TX_INTR_VSYNC_TS                      BIT(31)
 138#define XILINX_DP_TX_INTR_ALL                           (XILINX_DP_TX_INTR_HPD_IRQ | \
 139                                                         XILINX_DP_TX_INTR_HPD_EVENT | \
 140                                                         XILINX_DP_TX_INTR_REPLY_RECV | \
 141                                                         XILINX_DP_TX_INTR_REPLY_TIMEOUT | \
 142                                                         XILINX_DP_TX_INTR_HPD_PULSE | \
 143                                                         XILINX_DP_TX_INTR_EXT_PKT_TXD | \
 144                                                         XILINX_DP_TX_INTR_LIV_ABUF_UNDRFLW | \
 145                                                         XILINX_DP_TX_INTR_VBLANK_START | \
 146                                                         XILINX_DP_TX_INTR_CHBUF_UNDERFLW_MASK | \
 147                                                         XILINX_DP_TX_INTR_CHBUF_OVERFLW_MASK)
 148#define XILINX_DP_TX_REPLY_DATA_CNT                     0x148
 149#define XILINX_DP_SUB_TX_INTR_STATUS                    0x3a0
 150#define XILINX_DP_SUB_TX_INTR_MASK                      0x3a4
 151#define XILINX_DP_SUB_TX_INTR_EN                        0x3a8
 152#define XILINX_DP_SUB_TX_INTR_DS                        0x3ac
 153
 154/* Main stream attribute registers */
 155#define XILINX_DP_TX_MAIN_STREAM_HTOTAL                 0x180
 156#define XILINX_DP_TX_MAIN_STREAM_VTOTAL                 0x184
 157#define XILINX_DP_TX_MAIN_STREAM_POLARITY               0x188
 158#define XILINX_DP_TX_MAIN_STREAM_POLARITY_HSYNC_SHIFT   0
 159#define XILINX_DP_TX_MAIN_STREAM_POLARITY_VSYNC_SHIFT   1
 160#define XILINX_DP_TX_MAIN_STREAM_HSWIDTH                0x18c
 161#define XILINX_DP_TX_MAIN_STREAM_VSWIDTH                0x190
 162#define XILINX_DP_TX_MAIN_STREAM_HRES                   0x194
 163#define XILINX_DP_TX_MAIN_STREAM_VRES                   0x198
 164#define XILINX_DP_TX_MAIN_STREAM_HSTART                 0x19c
 165#define XILINX_DP_TX_MAIN_STREAM_VSTART                 0x1a0
 166#define XILINX_DP_TX_MAIN_STREAM_MISC0                  0x1a4
 167#define XILINX_DP_TX_MAIN_STREAM_MISC0_SYNC             BIT(0)
 168#define XILINX_DP_TX_MAIN_STREAM_MISC0_FORMAT_SHIFT     1
 169#define XILINX_DP_TX_MAIN_STREAM_MISC0_DYNAMIC_RANGE    BIT(3)
 170#define XILINX_DP_TX_MAIN_STREAM_MISC0_YCBCR_COLRIMETRY BIT(4)
 171#define XILINX_DP_TX_MAIN_STREAM_MISC0_BPC_SHIFT        5
 172#define XILINX_DP_TX_MAIN_STREAM_MISC1                  0x1a8
 173#define XILINX_DP_TX_MAIN_STREAM_MISC0_INTERLACED_VERT  BIT(0)
 174#define XILINX_DP_TX_MAIN_STREAM_MISC0_STEREO_VID_SHIFT 1
 175#define XILINX_DP_TX_M_VID                              0x1ac
 176#define XILINX_DP_TX_TRANSFER_UNIT_SIZE                 0x1b0
 177#define XILINX_DP_TX_DEF_TRANSFER_UNIT_SIZE             64
 178#define XILINX_DP_TX_N_VID                              0x1b4
 179#define XILINX_DP_TX_USER_PIXEL_WIDTH                   0x1b8
 180#define XILINX_DP_TX_USER_DATA_CNT_PER_LANE             0x1bc
 181#define XILINX_DP_TX_MIN_BYTES_PER_TU                   0x1c4
 182#define XILINX_DP_TX_FRAC_BYTES_PER_TU                  0x1c8
 183#define XILINX_DP_TX_INIT_WAIT                          0x1cc
 184
 185/* PHY configuration and status registers */
 186#define XILINX_DP_TX_PHY_CONFIG                         0x200
 187#define XILINX_DP_TX_PHY_CONFIG_PHY_RESET               BIT(0)
 188#define XILINX_DP_TX_PHY_CONFIG_GTTX_RESET              BIT(1)
 189#define XILINX_DP_TX_PHY_CONFIG_PHY_PMA_RESET           BIT(8)
 190#define XILINX_DP_TX_PHY_CONFIG_PHY_PCS_RESET           BIT(9)
 191#define XILINX_DP_TX_PHY_CONFIG_ALL_RESET               (XILINX_DP_TX_PHY_CONFIG_PHY_RESET | \
 192                                                         XILINX_DP_TX_PHY_CONFIG_GTTX_RESET | \
 193                                                         XILINX_DP_TX_PHY_CONFIG_PHY_PMA_RESET | \
 194                                                         XILINX_DP_TX_PHY_CONFIG_PHY_PCS_RESET)
 195#define XILINX_DP_TX_PHY_PREEMPHASIS_LANE_0             0x210
 196#define XILINX_DP_TX_PHY_PREEMPHASIS_LANE_1             0x214
 197#define XILINX_DP_TX_PHY_PREEMPHASIS_LANE_2             0x218
 198#define XILINX_DP_TX_PHY_PREEMPHASIS_LANE_3             0x21c
 199#define XILINX_DP_TX_PHY_VOLTAGE_DIFF_LANE_0            0x220
 200#define XILINX_DP_TX_PHY_VOLTAGE_DIFF_LANE_1            0x224
 201#define XILINX_DP_TX_PHY_VOLTAGE_DIFF_LANE_2            0x228
 202#define XILINX_DP_TX_PHY_VOLTAGE_DIFF_LANE_3            0x22c
 203#define XILINX_DP_TX_PHY_CLOCK_FEEDBACK_SETTING         0x234
 204#define XILINX_DP_TX_PHY_CLOCK_FEEDBACK_SETTING_162     0x1
 205#define XILINX_DP_TX_PHY_CLOCK_FEEDBACK_SETTING_270     0x3
 206#define XILINX_DP_TX_PHY_CLOCK_FEEDBACK_SETTING_540     0x5
 207#define XILINX_DP_TX_PHY_POWER_DOWN                     0x238
 208#define XILINX_DP_TX_PHY_POWER_DOWN_LANE_0              BIT(0)
 209#define XILINX_DP_TX_PHY_POWER_DOWN_LANE_1              BIT(1)
 210#define XILINX_DP_TX_PHY_POWER_DOWN_LANE_2              BIT(2)
 211#define XILINX_DP_TX_PHY_POWER_DOWN_LANE_3              BIT(3)
 212#define XILINX_DP_TX_PHY_POWER_DOWN_ALL                 0xf
 213#define XILINX_DP_TX_PHY_PRECURSOR_LANE_0               0x23c
 214#define XILINX_DP_TX_PHY_PRECURSOR_LANE_1               0x240
 215#define XILINX_DP_TX_PHY_PRECURSOR_LANE_2               0x244
 216#define XILINX_DP_TX_PHY_PRECURSOR_LANE_3               0x248
 217#define XILINX_DP_TX_PHY_POSTCURSOR_LANE_0              0x24c
 218#define XILINX_DP_TX_PHY_POSTCURSOR_LANE_1              0x250
 219#define XILINX_DP_TX_PHY_POSTCURSOR_LANE_2              0x254
 220#define XILINX_DP_TX_PHY_POSTCURSOR_LANE_3              0x258
 221#define XILINX_DP_SUB_TX_PHY_PRECURSOR_LANE_0           0x24c
 222#define XILINX_DP_SUB_TX_PHY_PRECURSOR_LANE_1           0x250
 223#define XILINX_DP_TX_PHY_STATUS                         0x280
 224#define XILINX_DP_TX_PHY_STATUS_PLL_LOCKED_SHIFT        4
 225#define XILINX_DP_TX_PHY_STATUS_FPGA_PLL_LOCKED         BIT(6)
 226
 227/* Audio registers */
 228#define XILINX_DP_TX_AUDIO_CONTROL                      0x300
 229#define XILINX_DP_TX_AUDIO_CHANNELS                     0x304
 230#define XILINX_DP_TX_AUDIO_INFO_DATA                    0x308
 231#define XILINX_DP_TX_AUDIO_M_AUD                        0x328
 232#define XILINX_DP_TX_AUDIO_N_AUD                        0x32c
 233#define XILINX_DP_TX_AUDIO_EXT_DATA                     0x330
 234
 235#define XILINX_DP_MISC0_RGB                             (0)
 236#define XILINX_DP_MISC0_YCRCB_422                       (5 << 1)
 237#define XILINX_DP_MISC0_YCRCB_444                       (6 << 1)
 238#define XILINX_DP_MISC0_FORMAT_MASK                     0xe
 239#define XILINX_DP_MISC0_BPC_6                           (0 << 5)
 240#define XILINX_DP_MISC0_BPC_8                           (1 << 5)
 241#define XILINX_DP_MISC0_BPC_10                          (2 << 5)
 242#define XILINX_DP_MISC0_BPC_12                          (3 << 5)
 243#define XILINX_DP_MISC0_BPC_16                          (4 << 5)
 244#define XILINX_DP_MISC0_BPC_MASK                        0xe0
 245#define XILINX_DP_MISC1_Y_ONLY                          (1 << 7)
 246
 247#define DP_REDUCED_BIT_RATE                             162000
 248#define DP_HIGH_BIT_RATE                                270000
 249#define DP_HIGH_BIT_RATE2                               540000
 250#define DP_MAX_TRAINING_TRIES                           5
 251#define DP_MAX_LANES                                    4
 252
 253enum dp_version {
 254        DP_V1_1A = 0x11,
 255        DP_V1_2 = 0x12
 256};
 257
 258/**
 259 * struct xilinx_drm_dp_link_config - Common link config between source and sink
 260 * @max_rate: maximum link rate
 261 * @max_lanes: maximum number of lanes
 262 */
 263struct xilinx_drm_dp_link_config {
 264        int max_rate;
 265        u8 max_lanes;
 266};
 267
 268/**
 269 * struct xilinx_drm_dp_mode - Configured mode of DisplayPort
 270 * @bw_code: code for bandwidth(link rate)
 271 * @lane_cnt: number of lanes
 272 * @pclock: pixel clock frequency of current mode
 273 */
 274struct xilinx_drm_dp_mode {
 275        u8 bw_code;
 276        u8 lane_cnt;
 277        int pclock;
 278};
 279
 280/**
 281 * struct xilinx_drm_dp_config - Configuration of DisplayPort from DTS
 282 * @dp_version: DisplayPort protocol version
 283 * @max_lanes: max number of lanes
 284 * @max_link_rate: max link rate
 285 * @max_bpc: maximum bits-per-color
 286 * @max_pclock: maximum pixel clock rate
 287 * @enable_yonly: enable yonly color space logic
 288 * @enable_ycrcb: enable ycrcb color space logic
 289 * @misc0: misc0 configuration (per DP v1.2 spec)
 290 * @misc1: misc1 configuration (per DP v1.2 spec)
 291 * @bpp: bits per pixel
 292 */
 293struct xilinx_drm_dp_config {
 294        enum dp_version dp_version;
 295        u32 max_lanes;
 296        u32 max_link_rate;
 297        u32 max_bpc;
 298        u32 max_pclock;
 299        bool enable_yonly;
 300        bool enable_ycrcb;
 301
 302        u8 misc0;
 303        u8 misc1;
 304        u8 bpp;
 305};
 306
 307/**
 308 * struct xilinx_drm_dp - Xilinx DisplayPort core
 309 * @encoder: pointer to the drm encoder structure
 310 * @dev: device structure
 311 * @iomem: device I/O memory for register access
 312 * @config: IP core configuration from DTS
 313 * @aux: aux channel
 314 * @dp_sub: DisplayPort subsystem
 315 * @phy: PHY handles for DP lanes
 316 * @aclk: clock source device for internal axi4-lite clock
 317 * @aud_clk: clock source device for audio clock
 318 * @aud_clk_enabled: if audio clock is enabled
 319 * @dpms: current dpms state
 320 * @status: the connection status
 321 * @dpcd: DP configuration data from currently connected sink device
 322 * @link_config: common link configuration between IP core and sink device
 323 * @mode: current mode between IP core and sink device
 324 * @train_set: set of training data
 325 */
 326struct xilinx_drm_dp {
 327        struct drm_encoder *encoder;
 328        struct device *dev;
 329        void __iomem *iomem;
 330
 331        struct xilinx_drm_dp_config config;
 332        struct drm_dp_aux aux;
 333        struct xilinx_drm_dp_sub *dp_sub;
 334        struct phy *phy[DP_MAX_LANES];
 335        struct clk *aclk;
 336        struct clk *aud_clk;
 337        bool aud_clk_enabled;
 338
 339        int dpms;
 340        enum drm_connector_status status;
 341        u8 dpcd[DP_RECEIVER_CAP_SIZE];
 342        struct xilinx_drm_dp_link_config link_config;
 343        struct xilinx_drm_dp_mode mode;
 344        u8 train_set[DP_MAX_LANES];
 345};
 346
 347static inline struct xilinx_drm_dp *to_dp(struct drm_encoder *encoder)
 348{
 349        return to_encoder_slave(encoder)->slave_priv;
 350}
 351
 352#define AUX_READ_BIT    0x1
 353
 354#ifdef CONFIG_DRM_XILINX_DP_DEBUG_FS
 355#define XILINX_DP_DEBUGFS_READ_MAX_SIZE 32
 356#define XILINX_DP_DEBUGFS_UINT8_MAX_STR "255"
 357#define IN_RANGE(x, min, max) ({        \
 358                typeof(x) _x = (x);     \
 359                _x >= (min) && _x <= (max); })
 360
 361static inline int xilinx_drm_dp_max_rate(int link_rate, u8 lane_num, u8 bpp);
 362/* Match xilinx_dp_testcases vs dp_debugfs_reqs[] entry */
 363enum xilinx_dp_testcases {
 364        DP_TC_LINK_RATE,
 365        DP_TC_LANE_COUNT,
 366        DP_TC_OUTPUT_FMT,
 367        DP_TC_NONE
 368};
 369
 370struct xilinx_dp_debugfs {
 371        enum xilinx_dp_testcases testcase;
 372        u8 link_rate;
 373        u8 lane_cnt;
 374        u8 old_output_fmt;
 375        struct xilinx_drm_dp *dp;
 376};
 377
 378struct xilinx_dp_debugfs dp_debugfs;
 379struct xilinx_dp_debugfs_request {
 380        const char *req;
 381        enum xilinx_dp_testcases tc;
 382        ssize_t (*read_handler)(char **kern_buff);
 383        ssize_t (*write_handler)(char **cmd);
 384};
 385
 386static s64 xilinx_dp_debugfs_argument_value(char *arg)
 387{
 388        s64 value;
 389
 390        if (!arg)
 391                return -1;
 392
 393        if (!kstrtos64(arg, 0, &value))
 394                return value;
 395
 396        return -1;
 397}
 398
 399static int xilinx_dp_update_output_format(u8 output_fmt, u32 num_colors)
 400{
 401        struct xilinx_drm_dp *dp = dp_debugfs.dp;
 402        struct xilinx_drm_dp_config *config = &dp->config;
 403        u32 bpc;
 404        u8 bpc_bits = (config->misc0 & XILINX_DP_MISC0_BPC_MASK);
 405        bool misc1 = output_fmt & XILINX_DP_MISC1_Y_ONLY ? true : false;
 406
 407        switch (bpc_bits) {
 408        case XILINX_DP_MISC0_BPC_6:
 409                bpc = 6;
 410                break;
 411        case XILINX_DP_MISC0_BPC_8:
 412                bpc = 8;
 413                break;
 414        case XILINX_DP_MISC0_BPC_10:
 415                bpc = 10;
 416                break;
 417        case XILINX_DP_MISC0_BPC_12:
 418                bpc = 12;
 419                break;
 420        case XILINX_DP_MISC0_BPC_16:
 421                bpc = 16;
 422                break;
 423        default:
 424                dev_err(dp->dev, "Invalid bpc count for misc0\n");
 425                return -EINVAL;
 426        }
 427
 428        /* clear old format */
 429        config->misc0 &= ~XILINX_DP_MISC0_FORMAT_MASK;
 430        config->misc1 &= ~XILINX_DP_MISC1_Y_ONLY;
 431
 432        if (misc1) {
 433                config->misc1 |= output_fmt;
 434                xilinx_drm_writel(dp->iomem, XILINX_DP_TX_MAIN_STREAM_MISC1,
 435                                  config->misc1);
 436        } else {
 437                config->misc0 |= output_fmt;
 438                xilinx_drm_writel(dp->iomem, XILINX_DP_TX_MAIN_STREAM_MISC0,
 439                                  config->misc0);
 440        }
 441        config->bpp = num_colors * bpc;
 442
 443        return 0;
 444}
 445
 446static ssize_t xilinx_dp_debugfs_max_linkrate_write(char **dp_test_arg)
 447{
 448        char *link_rate_arg;
 449        s64 link_rate;
 450
 451        link_rate_arg = strsep(dp_test_arg, " ");
 452        link_rate = xilinx_dp_debugfs_argument_value(link_rate_arg);
 453        if (link_rate < 0 || (link_rate != DP_HIGH_BIT_RATE2 &&
 454                              link_rate != DP_HIGH_BIT_RATE &&
 455                              link_rate != DP_REDUCED_BIT_RATE))
 456                return -EINVAL;
 457
 458        dp_debugfs.link_rate = drm_dp_link_rate_to_bw_code(link_rate);
 459        dp_debugfs.testcase = DP_TC_LINK_RATE;
 460
 461        return 0;
 462}
 463
 464static ssize_t xilinx_dp_debugfs_max_lanecnt_write(char **dp_test_arg)
 465{
 466        char *lane_cnt_arg;
 467        s64 lane_count;
 468
 469        lane_cnt_arg = strsep(dp_test_arg, " ");
 470        lane_count = xilinx_dp_debugfs_argument_value(lane_cnt_arg);
 471        if (lane_count < 0 || !IN_RANGE(lane_count, 1,
 472                                        dp_debugfs.dp->config.max_lanes))
 473                return -EINVAL;
 474
 475        dp_debugfs.lane_cnt = lane_count;
 476        dp_debugfs.testcase = DP_TC_LANE_COUNT;
 477
 478        return 0;
 479}
 480
 481static ssize_t xilinx_dp_debugfs_output_display_format_write(char **dp_test_arg)
 482{
 483        int ret;
 484        struct xilinx_drm_dp *dp = dp_debugfs.dp;
 485        char *output_format;
 486        u8 output_fmt;
 487        u32 num_colors;
 488
 489        /* Read the value from an user value */
 490        output_format = strsep(dp_test_arg, " ");
 491
 492        if (strncmp(output_format, "rgb", 3) == 0) {
 493                output_fmt = XILINX_DP_MISC0_RGB;
 494                num_colors = 3;
 495        } else if (strncmp(output_format, "ycbcr422", 8) == 0) {
 496                output_fmt = XILINX_DP_MISC0_YCRCB_422;
 497                num_colors = 2;
 498        } else if (strncmp(output_format, "ycbcr444", 8) == 0) {
 499                output_fmt = XILINX_DP_MISC0_YCRCB_444;
 500                num_colors = 3;
 501        } else if (strncmp(output_format, "yonly", 5) == 0) {
 502                output_fmt = XILINX_DP_MISC1_Y_ONLY;
 503                num_colors = 1;
 504        } else {
 505                dev_err(dp->dev, "Invalid output format\n");
 506                return -EINVAL;
 507        }
 508
 509        if (dp->config.misc1 & XILINX_DP_MISC1_Y_ONLY)
 510                dp_debugfs.old_output_fmt = XILINX_DP_MISC1_Y_ONLY;
 511        else
 512                dp_debugfs.old_output_fmt = dp->config.misc0 &
 513                                            XILINX_DP_MISC0_FORMAT_MASK;
 514
 515        ret = xilinx_dp_update_output_format(output_fmt, num_colors);
 516        if (!ret)
 517                dp_debugfs.testcase = DP_TC_OUTPUT_FMT;
 518        return ret;
 519}
 520
 521static ssize_t xilinx_dp_debugfs_max_linkrate_read(char **kern_buff)
 522{
 523        struct xilinx_drm_dp *dp = dp_debugfs.dp;
 524        size_t output_str_len;
 525        u8 dpcd_link_bw;
 526        int ret;
 527
 528        dp_debugfs.testcase = DP_TC_NONE;
 529        dp_debugfs.link_rate = 0;
 530
 531        /* Getting Sink Side Link Rate */
 532        ret = drm_dp_dpcd_readb(&dp->aux, DP_LINK_BW_SET, &dpcd_link_bw);
 533        if (ret < 0) {
 534                dev_err(dp->dev, "Failed to read link rate via AUX.\n");
 535                kfree(*kern_buff);
 536                return ret;
 537        }
 538
 539        output_str_len = strlen(XILINX_DP_DEBUGFS_UINT8_MAX_STR);
 540        output_str_len = min_t(size_t, XILINX_DP_DEBUGFS_READ_MAX_SIZE,
 541                               output_str_len);
 542        snprintf(*kern_buff, output_str_len, "%u", dpcd_link_bw);
 543
 544        return 0;
 545}
 546
 547static ssize_t xilinx_dp_debugfs_max_lanecnt_read(char **kern_buff)
 548{
 549        struct xilinx_drm_dp *dp = dp_debugfs.dp;
 550        size_t output_str_len;
 551        u8 dpcd_lane_cnt;
 552        int ret;
 553
 554        dp_debugfs.testcase = DP_TC_NONE;
 555        dp_debugfs.lane_cnt = 0;
 556
 557        /* Getting Sink Side Lane Count */
 558        ret = drm_dp_dpcd_readb(&dp->aux, DP_LANE_COUNT_SET, &dpcd_lane_cnt);
 559        if (ret < 0) {
 560                dev_err(dp->dev, "Failed to read link rate via AUX.\n");
 561                kfree(*kern_buff);
 562                return ret;
 563        }
 564
 565        dpcd_lane_cnt &= DP_LANE_COUNT_MASK;
 566        output_str_len = strlen(XILINX_DP_DEBUGFS_UINT8_MAX_STR);
 567        output_str_len = min_t(size_t, XILINX_DP_DEBUGFS_READ_MAX_SIZE,
 568                               output_str_len);
 569        snprintf(*kern_buff, output_str_len, "%u", dpcd_lane_cnt);
 570
 571        return 0;
 572}
 573
 574static ssize_t
 575xilinx_dp_debugfs_output_display_format_read(char **kern_buff)
 576{
 577        int ret;
 578        struct xilinx_drm_dp *dp = dp_debugfs.dp;
 579        u8 old_output_fmt = dp_debugfs.old_output_fmt;
 580        size_t output_str_len;
 581        u32 num_colors;
 582
 583        dp_debugfs.testcase = DP_TC_NONE;
 584
 585        if (old_output_fmt == XILINX_DP_MISC0_RGB) {
 586                num_colors = 3;
 587        } else if (old_output_fmt == XILINX_DP_MISC0_YCRCB_422) {
 588                num_colors = 2;
 589        } else if (old_output_fmt == XILINX_DP_MISC0_YCRCB_444) {
 590                num_colors = 3;
 591        } else if (old_output_fmt == XILINX_DP_MISC1_Y_ONLY) {
 592                num_colors = 1;
 593        } else {
 594                dev_err(dp->dev, "Invalid output format in misc0\n");
 595                return -EINVAL;
 596        }
 597
 598        ret = xilinx_dp_update_output_format(old_output_fmt, num_colors);
 599        if (ret)
 600                return ret;
 601
 602        output_str_len = strlen("Success");
 603        output_str_len = min_t(size_t, XILINX_DP_DEBUGFS_READ_MAX_SIZE,
 604                               output_str_len);
 605        snprintf(*kern_buff, output_str_len, "%s", "Success");
 606
 607        return 0;
 608}
 609
 610/* Match xilinx_dp_testcases vs dp_debugfs_reqs[] entry */
 611struct xilinx_dp_debugfs_request dp_debugfs_reqs[] = {
 612        {"LINK_RATE", DP_TC_LINK_RATE,
 613                        xilinx_dp_debugfs_max_linkrate_read,
 614                        xilinx_dp_debugfs_max_linkrate_write},
 615        {"LANE_COUNT", DP_TC_LANE_COUNT,
 616                        xilinx_dp_debugfs_max_lanecnt_read,
 617                        xilinx_dp_debugfs_max_lanecnt_write},
 618        {"OUTPUT_DISPLAY_FORMAT", DP_TC_OUTPUT_FMT,
 619                        xilinx_dp_debugfs_output_display_format_read,
 620                        xilinx_dp_debugfs_output_display_format_write},
 621};
 622
 623static ssize_t xilinx_dp_debugfs_read(struct file *f, char __user *buf,
 624                                      size_t size, loff_t *pos)
 625{
 626        char *kern_buff = NULL;
 627        size_t kern_buff_len, out_str_len;
 628        int ret;
 629
 630        if (size <= 0)
 631                return -EINVAL;
 632
 633        if (*pos != 0)
 634                return 0;
 635
 636        kern_buff = kzalloc(XILINX_DP_DEBUGFS_READ_MAX_SIZE, GFP_KERNEL);
 637        if (!kern_buff) {
 638                dp_debugfs.testcase = DP_TC_NONE;
 639                return -ENOMEM;
 640        }
 641
 642        if (dp_debugfs.testcase == DP_TC_NONE) {
 643                out_str_len = strlen("No testcase executed");
 644                out_str_len = min_t(size_t, XILINX_DP_DEBUGFS_READ_MAX_SIZE,
 645                                    out_str_len);
 646                snprintf(kern_buff, out_str_len, "%s", "No testcase executed");
 647        } else {
 648                ret = dp_debugfs_reqs[dp_debugfs.testcase].read_handler(
 649                                &kern_buff);
 650                if (ret) {
 651                        kfree(kern_buff);
 652                        return ret;
 653                }
 654        }
 655
 656        kern_buff_len = strlen(kern_buff);
 657        size = min(size, kern_buff_len);
 658
 659        ret = copy_to_user(buf, kern_buff, size);
 660
 661        kfree(kern_buff);
 662        if (ret)
 663                return ret;
 664
 665        *pos = size + 1;
 666        return size;
 667}
 668
 669static ssize_t
 670xilinx_dp_debugfs_write(struct file *f, const char __user *buf,
 671                        size_t size, loff_t *pos)
 672{
 673        char *kern_buff, *kern_buff_start;
 674        char *dp_test_req;
 675        int ret;
 676        int i;
 677
 678        if (*pos != 0 || size <= 0)
 679                return -EINVAL;
 680
 681        if (dp_debugfs.testcase != DP_TC_NONE)
 682                return -EBUSY;
 683
 684        kern_buff = kzalloc(size, GFP_KERNEL);
 685        if (!kern_buff)
 686                return -ENOMEM;
 687        kern_buff_start = kern_buff;
 688
 689        ret = strncpy_from_user(kern_buff, buf, size);
 690        if (ret < 0) {
 691                kfree(kern_buff_start);
 692                return ret;
 693        }
 694
 695        /* Read the testcase name and argument from an user request */
 696        dp_test_req = strsep(&kern_buff, " ");
 697
 698        for (i = 0; i < ARRAY_SIZE(dp_debugfs_reqs); i++) {
 699                if (!strcasecmp(dp_test_req, dp_debugfs_reqs[i].req))
 700                        if (!dp_debugfs_reqs[i].write_handler(&kern_buff)) {
 701                                kfree(kern_buff_start);
 702                                return size;
 703                        }
 704        }
 705
 706        kfree(kern_buff_start);
 707        return -EINVAL;
 708}
 709
 710static const struct file_operations fops_xilinx_dp_dbgfs = {
 711        .owner = THIS_MODULE,
 712        .read = xilinx_dp_debugfs_read,
 713        .write = xilinx_dp_debugfs_write,
 714};
 715
 716static int xilinx_dp_debugfs_init(struct xilinx_drm_dp *dp)
 717{
 718        int err;
 719        struct dentry *xilinx_dp_debugfs_dir, *xilinx_dp_debugfs_file;
 720
 721        dp_debugfs.testcase = DP_TC_NONE;
 722        dp_debugfs.dp = dp;
 723
 724        xilinx_dp_debugfs_dir = debugfs_create_dir("dp", NULL);
 725        if (!xilinx_dp_debugfs_dir) {
 726                dev_err(dp->dev, "debugfs_create_dir failed\n");
 727                return -ENODEV;
 728        }
 729
 730        xilinx_dp_debugfs_file =
 731                debugfs_create_file("testcase", 0444, xilinx_dp_debugfs_dir,
 732                                    NULL, &fops_xilinx_dp_dbgfs);
 733        if (!xilinx_dp_debugfs_file) {
 734                dev_err(dp->dev, "debugfs_create_file testcase failed\n");
 735                err = -ENODEV;
 736                goto err_dbgfs;
 737        }
 738        return 0;
 739
 740err_dbgfs:
 741        debugfs_remove_recursive(xilinx_dp_debugfs_dir);
 742        xilinx_dp_debugfs_dir = NULL;
 743        return err;
 744}
 745
 746static void xilinx_dp_debugfs_mode_config(struct xilinx_drm_dp *dp, u8 *lanes,
 747                                          u8 *rate_code, int pclock)
 748{
 749        int debugfs_rate = 0;
 750        u8 bpp = dp->config.bpp;
 751
 752        if (!dp_debugfs.link_rate && !dp_debugfs.lane_cnt)
 753                return;
 754
 755        if (dp_debugfs.link_rate) {
 756                debugfs_rate = min(dp_debugfs.link_rate, *rate_code);
 757                debugfs_rate =
 758                        drm_dp_bw_code_to_link_rate(debugfs_rate);
 759                debugfs_rate =
 760                        xilinx_drm_dp_max_rate(debugfs_rate, *lanes, bpp);
 761        }
 762
 763        if (dp_debugfs.lane_cnt) {
 764                u8 lane;
 765
 766                lane = min(dp_debugfs.lane_cnt, *lanes);
 767                debugfs_rate =
 768                        xilinx_drm_dp_max_rate(debugfs_rate, lane, bpp);
 769        }
 770
 771        if (pclock > debugfs_rate) {
 772                dev_dbg(dp->dev, "debugfs could't configure link values\n");
 773                return;
 774        }
 775
 776        if (dp_debugfs.link_rate)
 777                *rate_code = dp_debugfs.link_rate;
 778        if (dp_debugfs.lane_cnt)
 779                *lanes = dp_debugfs.lane_cnt;
 780
 781}
 782#else
 783static int xilinx_dp_debugfs_init(struct xilinx_drm_dp *dp)
 784{
 785        return 0;
 786}
 787
 788static void xilinx_dp_debugfs_mode_config(struct xilinx_drm_dp *dp, u8 *lanes,
 789                                          u8 *rate_code, int pclock)
 790{
 791}
 792#endif /* DRM_XILINX_DP_DEBUG_FS */
 793
 794/**
 795 * xilinx_drm_dp_aux_cmd_submit - Submit aux command
 796 * @dp: DisplayPort IP core structure
 797 * @cmd: aux command
 798 * @addr: aux address
 799 * @buf: buffer for command data
 800 * @bytes: number of bytes for @buf
 801 * @reply: reply code to be returned
 802 *
 803 * Submit an aux command. All aux related commands, native or i2c aux
 804 * read/write, are submitted through this function. The function is mapped to
 805 * the transfer function of struct drm_dp_aux. This function involves in
 806 * multiple register reads/writes, thus synchronization is needed, and it is
 807 * done by drm_dp_helper using @hw_mutex. The calling thread goes into sleep
 808 * if there's no immediate reply to the command submission. The reply code is
 809 * returned at @reply if @reply != NULL.
 810 *
 811 * Return: 0 if the command is submitted properly, or corresponding error code:
 812 * -EBUSY when there is any request already being processed
 813 * -ETIMEDOUT when receiving reply is timed out
 814 * -EIO when received bytes are less than requested
 815 */
 816static int xilinx_drm_dp_aux_cmd_submit(struct xilinx_drm_dp *dp, u32 cmd,
 817                                        u16 addr, u8 *buf, u8 bytes, u8 *reply)
 818{
 819        bool is_read = (cmd & AUX_READ_BIT) ? true : false;
 820        void __iomem *iomem = dp->iomem;
 821        u32 reg, i;
 822
 823        reg = xilinx_drm_readl(iomem, XILINX_DP_TX_INTR_SIGNAL_STATE);
 824        if (reg & XILINX_DP_TX_INTR_SIGNAL_STATE_REQUEST)
 825                return -EBUSY;
 826
 827        xilinx_drm_writel(iomem, XILINX_DP_TX_AUX_ADDRESS, addr);
 828
 829        if (!is_read)
 830                for (i = 0; i < bytes; i++)
 831                        xilinx_drm_writel(iomem, XILINX_DP_TX_AUX_WRITE_FIFO,
 832                                          buf[i]);
 833
 834        reg = cmd << XILINX_DP_TX_AUX_COMMAND_CMD_SHIFT;
 835        if (!buf || !bytes)
 836                reg |= XILINX_DP_TX_AUX_COMMAND_ADDRESS_ONLY;
 837        else
 838                reg |= (bytes - 1) << XILINX_DP_TX_AUX_COMMAND_BYTES_SHIFT;
 839        xilinx_drm_writel(iomem, XILINX_DP_TX_AUX_COMMAND, reg);
 840
 841        /* Wait for reply to be delivered upto 2ms */
 842        for (i = 0; ; i++) {
 843                reg = xilinx_drm_readl(iomem, XILINX_DP_TX_INTR_SIGNAL_STATE);
 844
 845                if (reg & XILINX_DP_TX_INTR_SIGNAL_STATE_REPLY)
 846                        break;
 847
 848                if (reg & XILINX_DP_TX_INTR_SIGNAL_STATE_REPLY_TIMEOUT ||
 849                    i == 2)
 850                        return -ETIMEDOUT;
 851
 852                usleep_range(1000, 1100);
 853        }
 854
 855        reg = xilinx_drm_readl(iomem, XILINX_DP_TX_AUX_REPLY_CODE);
 856        if (reply)
 857                *reply = reg;
 858
 859        if (is_read &&
 860            (reg == XILINX_DP_TX_AUX_REPLY_CODE_AUX_ACK ||
 861             reg == XILINX_DP_TX_AUX_REPLY_CODE_I2C_ACK)) {
 862                reg = xilinx_drm_readl(iomem, XILINX_DP_TX_REPLY_DATA_CNT);
 863                if ((reg & XILINX_DP_TX_AUX_REPLY_CNT_MASK) != bytes)
 864                        return -EIO;
 865
 866                for (i = 0; i < bytes; i++)
 867                        buf[i] = xilinx_drm_readl(iomem,
 868                                                  XILINX_DP_TX_AUX_REPLY_DATA);
 869        }
 870
 871        return 0;
 872}
 873
 874/**
 875 * xilinx_drm_dp_phy_ready - Check if PHY is ready
 876 * @dp: DisplayPort IP core structure
 877 *
 878 * Check if PHY is ready. If PHY is not ready, wait 1ms to check for 100 times.
 879 * This amount of delay was suggested by IP designer.
 880 *
 881 * Return: 0 if PHY is ready, or -ENODEV if PHY is not ready.
 882 */
 883static int xilinx_drm_dp_phy_ready(struct xilinx_drm_dp *dp)
 884{
 885        u32 i, reg, ready, lane;
 886
 887        lane = dp->config.max_lanes;
 888        ready = (1 << lane) - 1;
 889        if (!dp->dp_sub)
 890                ready |= XILINX_DP_TX_PHY_STATUS_FPGA_PLL_LOCKED;
 891
 892        /* Wait for 100 * 1ms. This should be enough time for PHY to be ready */
 893        for (i = 0; ; i++) {
 894                reg = xilinx_drm_readl(dp->iomem, XILINX_DP_TX_PHY_STATUS);
 895                if ((reg & ready) == ready)
 896                        return 0;
 897
 898                if (i == 100) {
 899                        DRM_ERROR("PHY isn't ready\n");
 900                        return -ENODEV;
 901                }
 902
 903                usleep_range(1000, 1100);
 904        }
 905
 906        return 0;
 907}
 908
 909/**
 910 * xilinx_drm_dp_max_rate - Calculate and return available max pixel clock
 911 * @link_rate: link rate (Kilo-bytes / sec)
 912 * @lane_num: number of lanes
 913 * @bpp: bits per pixel
 914 *
 915 * Return: max pixel clock (KHz) supported by current link config.
 916 */
 917static inline int xilinx_drm_dp_max_rate(int link_rate, u8 lane_num, u8 bpp)
 918{
 919        return link_rate * lane_num * 8 / bpp;
 920}
 921
 922/**
 923 * xilinx_drm_dp_mode_configure - Configure the link values
 924 * @dp: DisplayPort IP core structure
 925 * @pclock: pixel clock for requested display mode
 926 * @current_bw: current link rate
 927 *
 928 * Find the link configuration values, rate and lane count for requested pixel
 929 * clock @pclock. The @pclock is stored in the mode to be used in other
 930 * functions later. The returned rate is downshifted from the current rate
 931 * @current_bw.
 932 *
 933 * Return: Current link rate code, or -EINVAL.
 934 */
 935static int xilinx_drm_dp_mode_configure(struct xilinx_drm_dp *dp, int pclock,
 936                                        u8 current_bw)
 937{
 938        int max_rate = dp->link_config.max_rate;
 939        u8 bws[3] = { DP_LINK_BW_1_62, DP_LINK_BW_2_7, DP_LINK_BW_5_4 };
 940        u8 max_lanes = dp->link_config.max_lanes;
 941        u8 max_link_rate_code = drm_dp_link_rate_to_bw_code(max_rate);
 942        u8 bpp = dp->config.bpp;
 943        u8 lane_cnt;
 944        s8 i;
 945
 946        if (current_bw == DP_LINK_BW_1_62)
 947                return -EINVAL;
 948
 949        xilinx_dp_debugfs_mode_config(dp, &max_lanes, &max_link_rate_code,
 950                                      pclock);
 951
 952        for (i = ARRAY_SIZE(bws) - 1; i >= 0; i--) {
 953                if (current_bw && bws[i] >= current_bw)
 954                        continue;
 955
 956                if (bws[i] <= max_link_rate_code)
 957                        break;
 958        }
 959
 960        for (lane_cnt = 1; lane_cnt <= max_lanes; lane_cnt <<= 1) {
 961                int bw;
 962                u32 rate;
 963
 964                bw = drm_dp_bw_code_to_link_rate(bws[i]);
 965                rate = xilinx_drm_dp_max_rate(bw, lane_cnt, bpp);
 966                if (pclock <= rate) {
 967                        dp->mode.bw_code = bws[i];
 968                        dp->mode.lane_cnt = lane_cnt;
 969                        dp->mode.pclock = pclock;
 970                        return dp->mode.bw_code;
 971                }
 972        }
 973
 974        dev_dbg(dp->dev, "failed to configure link values\n");
 975
 976        return -EINVAL;
 977}
 978
 979/**
 980 * xilinx_drm_dp_adjust_train - Adjust train values
 981 * @dp: DisplayPort IP core structure
 982 * @link_status: link status from sink which contains requested training values
 983 */
 984static void xilinx_drm_dp_adjust_train(struct xilinx_drm_dp *dp,
 985                                       u8 link_status[DP_LINK_STATUS_SIZE])
 986{
 987        u8 *train_set = dp->train_set;
 988        u8 voltage = 0, preemphasis = 0;
 989        u8 max_preemphasis;
 990        u8 i;
 991
 992        for (i = 0; i < dp->mode.lane_cnt; i++) {
 993                u8 v = drm_dp_get_adjust_request_voltage(link_status, i);
 994                u8 p = drm_dp_get_adjust_request_pre_emphasis(link_status, i);
 995
 996                if (v > voltage)
 997                        voltage = v;
 998
 999                if (p > preemphasis)
1000                        preemphasis = p;
1001        }
1002
1003        if (voltage >= DP_TRAIN_VOLTAGE_SWING_LEVEL_3)
1004                voltage |= DP_TRAIN_MAX_SWING_REACHED;
1005
1006        max_preemphasis = (dp->dp_sub) ? DP_TRAIN_PRE_EMPH_LEVEL_2 :
1007                                         DP_TRAIN_PRE_EMPH_LEVEL_3;
1008
1009        if (preemphasis >= max_preemphasis)
1010                preemphasis |= DP_TRAIN_MAX_PRE_EMPHASIS_REACHED;
1011
1012        for (i = 0; i < dp->mode.lane_cnt; i++)
1013                train_set[i] = voltage | preemphasis;
1014}
1015
1016/**
1017 * xilinx_drm_dp_update_vs_emph - Update the training values
1018 * @dp: DisplayPort IP core structure
1019 *
1020 * Update the training values based on the request from sink. The mapped values
1021 * are predefined, and values(vs, pe, pc) are from the device manual.
1022 *
1023 * Return: 0 if vs and emph are updated successfully, or the error code returned
1024 * by drm_dp_dpcd_write().
1025 */
1026static int xilinx_drm_dp_update_vs_emph(struct xilinx_drm_dp *dp)
1027{
1028        u8 *train_set = dp->train_set;
1029        u8 i, v_level, p_level;
1030        int ret;
1031        static u8 vs[4][4] = { { 0x2a, 0x27, 0x24, 0x20 },
1032                               { 0x27, 0x23, 0x20, 0xff },
1033                               { 0x24, 0x20, 0xff, 0xff },
1034                               { 0xff, 0xff, 0xff, 0xff } };
1035        static u8 pe[4][4] = { { 0x2, 0x2, 0x2, 0x2 },
1036                               { 0x1, 0x1, 0x1, 0xff },
1037                               { 0x0, 0x0, 0xff, 0xff },
1038                               { 0xff, 0xff, 0xff, 0xff } };
1039
1040        ret = drm_dp_dpcd_write(&dp->aux, DP_TRAINING_LANE0_SET, train_set,
1041                                dp->mode.lane_cnt);
1042        if (ret < 0)
1043                return ret;
1044
1045        for (i = 0; i < dp->mode.lane_cnt; i++) {
1046                v_level = (train_set[i] & DP_TRAIN_VOLTAGE_SWING_MASK) >>
1047                          DP_TRAIN_VOLTAGE_SWING_SHIFT;
1048                p_level = (train_set[i] & DP_TRAIN_PRE_EMPHASIS_MASK) >>
1049                          DP_TRAIN_PRE_EMPHASIS_SHIFT;
1050
1051                if (dp->phy[i]) {
1052                        u32 reg = XILINX_DP_SUB_TX_PHY_PRECURSOR_LANE_0 + i * 4;
1053
1054                        xpsgtr_margining_factor(dp->phy[i], p_level, v_level);
1055                        xpsgtr_override_deemph(dp->phy[i], p_level, v_level);
1056                        xilinx_drm_writel(dp->iomem, reg, 0x2);
1057                } else {
1058                        u32 reg;
1059
1060                        reg = XILINX_DP_TX_PHY_VOLTAGE_DIFF_LANE_0 + i + 4;
1061                        xilinx_drm_writel(dp->iomem, reg, vs[p_level][v_level]);
1062                        reg = XILINX_DP_TX_PHY_PRECURSOR_LANE_0 + i + 4;
1063                        xilinx_drm_writel(dp->iomem, reg, pe[p_level][v_level]);
1064                        reg = XILINX_DP_TX_PHY_POSTCURSOR_LANE_0 + i + 4;
1065                        xilinx_drm_writel(dp->iomem, reg, 0);
1066                }
1067        }
1068
1069        return 0;
1070}
1071
1072/**
1073 * xilinx_drm_dp_link_train_cr - Train clock recovery
1074 * @dp: DisplayPort IP core structure
1075 *
1076 * Return: 0 if clock recovery train is done successfully, or corresponding
1077 * error code.
1078 */
1079static int xilinx_drm_dp_link_train_cr(struct xilinx_drm_dp *dp)
1080{
1081        u8 link_status[DP_LINK_STATUS_SIZE];
1082        u8 lane_cnt = dp->mode.lane_cnt;
1083        u8 vs = 0, tries = 0;
1084        u16 max_tries, i;
1085        bool cr_done;
1086        int ret;
1087
1088        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_TRAINING_PATTERN_SET,
1089                          DP_TRAINING_PATTERN_1);
1090        ret = drm_dp_dpcd_writeb(&dp->aux, DP_TRAINING_PATTERN_SET,
1091                                 DP_TRAINING_PATTERN_1 |
1092                                 DP_LINK_SCRAMBLING_DISABLE);
1093        if (ret < 0)
1094                return ret;
1095
1096        /* 256 loops should be maximum iterations for 4 lanes and 4 values.
1097         * So, This loop should exit before 512 iterations
1098         */
1099        for (max_tries = 0; max_tries < 512; max_tries++) {
1100                ret = xilinx_drm_dp_update_vs_emph(dp);
1101                if (ret)
1102                        return ret;
1103
1104                drm_dp_link_train_clock_recovery_delay(dp->dpcd);
1105
1106                ret = drm_dp_dpcd_read_link_status(&dp->aux, link_status);
1107                if (ret < 0)
1108                        return ret;
1109
1110                cr_done = drm_dp_clock_recovery_ok(link_status, lane_cnt);
1111                if (cr_done)
1112                        break;
1113
1114                for (i = 0; i < lane_cnt; i++)
1115                        if (!(dp->train_set[i] & DP_TRAIN_MAX_SWING_REACHED))
1116                                break;
1117
1118                if (i == lane_cnt)
1119                        break;
1120
1121                if ((dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK) == vs)
1122                        tries++;
1123                else
1124                        tries = 0;
1125
1126                if (tries == DP_MAX_TRAINING_TRIES)
1127                        break;
1128
1129                vs = dp->train_set[0] & DP_TRAIN_VOLTAGE_SWING_MASK;
1130
1131                xilinx_drm_dp_adjust_train(dp, link_status);
1132        }
1133
1134        if (!cr_done)
1135                return -EIO;
1136
1137        return 0;
1138}
1139
1140/**
1141 * xilinx_drm_dp_link_train_ce - Train channel equalization
1142 * @dp: DisplayPort IP core structure
1143 *
1144 * Return: 0 if channel equalization train is done successfully, or
1145 * corresponding error code.
1146 */
1147static int xilinx_drm_dp_link_train_ce(struct xilinx_drm_dp *dp)
1148{
1149        u8 link_status[DP_LINK_STATUS_SIZE];
1150        u8 lane_cnt = dp->mode.lane_cnt;
1151        u32 pat, tries;
1152        int ret;
1153        bool ce_done;
1154
1155        if (dp->config.dp_version == DP_V1_2 &&
1156            dp->dpcd[DP_DPCD_REV] >= DP_V1_2 &&
1157            dp->dpcd[DP_MAX_LANE_COUNT] & DP_TPS3_SUPPORTED)
1158                pat = DP_TRAINING_PATTERN_3;
1159        else
1160                pat = DP_TRAINING_PATTERN_2;
1161        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_TRAINING_PATTERN_SET, pat);
1162        ret = drm_dp_dpcd_writeb(&dp->aux, DP_TRAINING_PATTERN_SET,
1163                                 pat | DP_LINK_SCRAMBLING_DISABLE);
1164        if (ret < 0)
1165                return ret;
1166
1167        for (tries = 0; tries < DP_MAX_TRAINING_TRIES; tries++) {
1168                ret = xilinx_drm_dp_update_vs_emph(dp);
1169                if (ret)
1170                        return ret;
1171
1172                drm_dp_link_train_channel_eq_delay(dp->dpcd);
1173
1174                ret = drm_dp_dpcd_read_link_status(&dp->aux, link_status);
1175                if (ret < 0)
1176                        return ret;
1177
1178                ce_done = drm_dp_channel_eq_ok(link_status, lane_cnt);
1179                if (ce_done)
1180                        break;
1181
1182                xilinx_drm_dp_adjust_train(dp, link_status);
1183        }
1184
1185        if (!ce_done)
1186                return -EIO;
1187
1188        return 0;
1189}
1190
1191/**
1192 * xilinx_drm_dp_link_train - Train the link
1193 * @dp: DisplayPort IP core structure
1194 *
1195 * Return: 0 if all trains are done successfully, or corresponding error code.
1196 */
1197static int xilinx_drm_dp_train(struct xilinx_drm_dp *dp)
1198{
1199        u32 reg;
1200        u8 bw_code = dp->mode.bw_code;
1201        u8 lane_cnt = dp->mode.lane_cnt;
1202        u8 aux_lane_cnt = lane_cnt;
1203        bool enhanced;
1204        int ret;
1205
1206        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_LANE_CNT_SET, lane_cnt);
1207
1208        enhanced = drm_dp_enhanced_frame_cap(dp->dpcd);
1209        if (enhanced) {
1210                xilinx_drm_writel(dp->iomem, XILINX_DP_TX_ENHANCED_FRAME_EN, 1);
1211                aux_lane_cnt |= DP_LANE_COUNT_ENHANCED_FRAME_EN;
1212        }
1213
1214        if (dp->dpcd[3] & 0x1) {
1215                xilinx_drm_writel(dp->iomem, XILINX_DP_TX_DOWNSPREAD_CTL, 1);
1216                drm_dp_dpcd_writeb(&dp->aux, DP_DOWNSPREAD_CTRL,
1217                                   DP_SPREAD_AMP_0_5);
1218        } else {
1219                xilinx_drm_writel(dp->iomem, XILINX_DP_TX_DOWNSPREAD_CTL, 0);
1220                drm_dp_dpcd_writeb(&dp->aux, DP_DOWNSPREAD_CTRL, 0);
1221        }
1222
1223        ret = drm_dp_dpcd_writeb(&dp->aux, DP_LANE_COUNT_SET, aux_lane_cnt);
1224        if (ret < 0) {
1225                DRM_ERROR("failed to set lane count\n");
1226                return ret;
1227        }
1228
1229        ret = drm_dp_dpcd_writeb(&dp->aux, DP_MAIN_LINK_CHANNEL_CODING_SET,
1230                                 DP_SET_ANSI_8B10B);
1231        if (ret < 0) {
1232                DRM_ERROR("failed to set ANSI 8B/10B encoding\n");
1233                return ret;
1234        }
1235
1236        ret = drm_dp_dpcd_writeb(&dp->aux, DP_LINK_BW_SET, bw_code);
1237        if (ret < 0) {
1238                DRM_ERROR("failed to set DP bandwidth\n");
1239                return ret;
1240        }
1241
1242        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_LINK_BW_SET, bw_code);
1243
1244        switch (bw_code) {
1245        case DP_LINK_BW_1_62:
1246                reg = XILINX_DP_TX_PHY_CLOCK_FEEDBACK_SETTING_162;
1247                break;
1248        case DP_LINK_BW_2_7:
1249                reg = XILINX_DP_TX_PHY_CLOCK_FEEDBACK_SETTING_270;
1250                break;
1251        case DP_LINK_BW_5_4:
1252        default:
1253                reg = XILINX_DP_TX_PHY_CLOCK_FEEDBACK_SETTING_540;
1254                break;
1255        }
1256
1257        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_PHY_CLOCK_FEEDBACK_SETTING,
1258                          reg);
1259        ret = xilinx_drm_dp_phy_ready(dp);
1260        if (ret < 0)
1261                return ret;
1262
1263        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_SCRAMBLING_DISABLE, 1);
1264
1265        memset(dp->train_set, 0, 4);
1266
1267        ret = xilinx_drm_dp_link_train_cr(dp);
1268        if (ret)
1269                return ret;
1270
1271        ret = xilinx_drm_dp_link_train_ce(dp);
1272        if (ret)
1273                return ret;
1274
1275        ret = drm_dp_dpcd_writeb(&dp->aux, DP_TRAINING_PATTERN_SET,
1276                                 DP_TRAINING_PATTERN_DISABLE);
1277        if (ret < 0) {
1278                DRM_ERROR("failed to disable training pattern\n");
1279                return ret;
1280        }
1281        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_TRAINING_PATTERN_SET,
1282                          DP_TRAINING_PATTERN_DISABLE);
1283
1284        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_SCRAMBLING_DISABLE, 0);
1285
1286        return 0;
1287}
1288
1289/**
1290 * xilinx_drm_dp_train_loop - Downshift the link rate during training
1291 * @dp: DisplayPort IP core structure
1292 *
1293 * Train the link by downshifting the link rate if training is not successful.
1294 */
1295static void xilinx_drm_dp_train_loop(struct xilinx_drm_dp *dp)
1296{
1297        struct xilinx_drm_dp_mode *mode = &dp->mode;
1298        u8 bw = mode->bw_code;
1299        int ret;
1300
1301        do {
1302                if (dp->status == connector_status_disconnected)
1303                        return;
1304
1305                ret = xilinx_drm_dp_train(dp);
1306                if (!ret)
1307                        return;
1308
1309                ret = xilinx_drm_dp_mode_configure(dp, mode->pclock, bw);
1310                if (ret < 0)
1311                        return;
1312                bw = ret;
1313        } while (bw >= DP_LINK_BW_1_62);
1314
1315        DRM_ERROR("failed to train the DP link\n");
1316}
1317
1318/**
1319 * xilinx_drm_dp_init_aux - Initialize the DP aux
1320 * @dp: DisplayPort IP core structure
1321 *
1322 * Initialize the DP aux. The aux clock is derived from the axi clock, so
1323 * this function gets the axi clock frequency and calculates the filter
1324 * value. Additionally, the interrupts and transmitter are enabled.
1325 *
1326 * Return: 0 on success, error value otherwise
1327 */
1328static int xilinx_drm_dp_init_aux(struct xilinx_drm_dp *dp)
1329{
1330        int clock_rate;
1331        u32 reg, w;
1332
1333        clock_rate = clk_get_rate(dp->aclk);
1334        if (clock_rate < XILINX_DP_TX_CLK_DIVIDER_MHZ) {
1335                DRM_ERROR("aclk should be higher than 1MHz\n");
1336                return -EINVAL;
1337        }
1338
1339        /* Allowable values for this register are: 8, 16, 24, 32, 40, 48 */
1340        for (w = 8; w <= 48; w += 8) {
1341                /* AUX pulse width should be between 0.4 to 0.6 usec */
1342                if (w >= (4 * clock_rate / 10000000) &&
1343                    w <= (6 * clock_rate / 10000000))
1344                        break;
1345        }
1346
1347        if (w > 48) {
1348                DRM_ERROR("aclk frequency too high\n");
1349                return -EINVAL;
1350        }
1351        reg = w << XILINX_DP_TX_CLK_DIVIDER_AUX_FILTER_SHIFT;
1352
1353        reg |= clock_rate / XILINX_DP_TX_CLK_DIVIDER_MHZ;
1354        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_CLK_DIVIDER, reg);
1355
1356        if (dp->dp_sub)
1357                xilinx_drm_writel(dp->iomem, XILINX_DP_SUB_TX_INTR_EN,
1358                                  XILINX_DP_TX_INTR_ALL);
1359        else
1360                xilinx_drm_writel(dp->iomem, XILINX_DP_TX_INTR_MASK,
1361                                  (u32)~XILINX_DP_TX_INTR_ALL);
1362        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_ENABLE, 1);
1363
1364        return 0;
1365}
1366
1367/**
1368 * xilinx_drm_dp_init_phy - Initialize the phy
1369 * @dp: DisplayPort IP core structure
1370 *
1371 * Initialize the phy.
1372 *
1373 * Return: 0 if the phy instances are initialized correctly, or the error code
1374 * returned from the callee functions.
1375 */
1376static int xilinx_drm_dp_init_phy(struct xilinx_drm_dp *dp)
1377{
1378        unsigned int i;
1379        int ret;
1380
1381        for (i = 0; i < dp->config.max_lanes; i++) {
1382                ret = phy_init(dp->phy[i]);
1383                if (ret) {
1384                        dev_err(dp->dev, "failed to init phy lane %d\n", i);
1385                        return ret;
1386                }
1387        }
1388
1389        if (dp->dp_sub)
1390                xilinx_drm_writel(dp->iomem, XILINX_DP_SUB_TX_INTR_DS,
1391                                  XILINX_DP_TX_INTR_ALL);
1392        else
1393                xilinx_drm_writel(dp->iomem, XILINX_DP_TX_INTR_MASK,
1394                                  XILINX_DP_TX_INTR_ALL);
1395
1396        xilinx_drm_clr(dp->iomem, XILINX_DP_TX_PHY_CONFIG,
1397                       XILINX_DP_TX_PHY_CONFIG_ALL_RESET);
1398
1399        /* Wait for PLL to be locked for the primary (1st) */
1400        if (dp->phy[0]) {
1401                ret = xpsgtr_wait_pll_lock(dp->phy[0]);
1402                if (ret) {
1403                        dev_err(dp->dev, "failed to lock pll\n");
1404                        return ret;
1405                }
1406        }
1407
1408        return 0;
1409}
1410
1411/**
1412 * xilinx_drm_dp_exit_phy - Exit the phy
1413 * @dp: DisplayPort IP core structure
1414 *
1415 * Exit the phy.
1416 */
1417static void xilinx_drm_dp_exit_phy(struct xilinx_drm_dp *dp)
1418{
1419        unsigned int i;
1420        int ret;
1421
1422        for (i = 0; i < dp->config.max_lanes; i++) {
1423                ret = phy_exit(dp->phy[i]);
1424                if (ret) {
1425                        dev_err(dp->dev,
1426                                "failed to exit phy (%d) %d\n", i, ret);
1427                }
1428        }
1429}
1430
1431static void xilinx_drm_dp_dpms(struct drm_encoder *encoder, int dpms)
1432{
1433        struct xilinx_drm_dp *dp = to_dp(encoder);
1434        void __iomem *iomem = dp->iomem;
1435        unsigned int i;
1436        int ret;
1437
1438        if (dp->dpms == dpms)
1439                return;
1440
1441        dp->dpms = dpms;
1442
1443        switch (dpms) {
1444        case DRM_MODE_DPMS_ON:
1445                pm_runtime_get_sync(dp->dev);
1446
1447                if (dp->aud_clk && !dp->aud_clk_enabled) {
1448                        ret = clk_prepare_enable(dp->aud_clk);
1449                        if (ret) {
1450                                dev_err(dp->dev, "failed to enable aud_clk\n");
1451                        } else {
1452                                xilinx_drm_writel(iomem,
1453                                                  XILINX_DP_TX_AUDIO_CONTROL,
1454                                                  1);
1455                                dp->aud_clk_enabled = true;
1456                        }
1457                }
1458                xilinx_drm_writel(iomem, XILINX_DP_TX_PHY_POWER_DOWN, 0);
1459
1460                if (dp->status == connector_status_connected) {
1461                        for (i = 0; i < 3; i++) {
1462                                ret = drm_dp_dpcd_writeb(&dp->aux, DP_SET_POWER,
1463                                                         DP_SET_POWER_D0);
1464                                if (ret == 1)
1465                                        break;
1466                                usleep_range(300, 500);
1467                        }
1468                        /* Some monitors take time to wake up properly */
1469                        msleep(xilinx_drm_dp_power_on_delay_ms);
1470                        if (ret != 1)
1471                                dev_dbg(dp->dev, "DP aux failed\n");
1472                        else
1473                                xilinx_drm_dp_train_loop(dp);
1474                }
1475                xilinx_drm_writel(dp->iomem, XILINX_DP_TX_SW_RESET,
1476                                  XILINX_DP_TX_SW_RESET_ALL);
1477                xilinx_drm_writel(iomem, XILINX_DP_TX_ENABLE_MAIN_STREAM, 1);
1478
1479                return;
1480        default:
1481                xilinx_drm_writel(iomem, XILINX_DP_TX_ENABLE_MAIN_STREAM, 0);
1482                if (dp->status == connector_status_connected) {
1483                        drm_dp_dpcd_writeb(&dp->aux, DP_SET_POWER,
1484                                           DP_SET_POWER_D3);
1485                }
1486                xilinx_drm_writel(iomem, XILINX_DP_TX_PHY_POWER_DOWN,
1487                                  XILINX_DP_TX_PHY_POWER_DOWN_ALL);
1488                if (dp->aud_clk && dp->aud_clk_enabled) {
1489                        xilinx_drm_writel(iomem, XILINX_DP_TX_AUDIO_CONTROL, 0);
1490                        clk_disable_unprepare(dp->aud_clk);
1491                        dp->aud_clk_enabled = false;
1492                }
1493                pm_runtime_put_sync(dp->dev);
1494
1495                return;
1496        }
1497}
1498
1499static void xilinx_drm_dp_save(struct drm_encoder *encoder)
1500{
1501        /* no op */
1502}
1503
1504static void xilinx_drm_dp_restore(struct drm_encoder *encoder)
1505{
1506        /* no op */
1507}
1508
1509#define XILINX_DP_SUB_TX_MIN_H_BACKPORCH        20
1510
1511static bool xilinx_drm_dp_mode_fixup(struct drm_encoder *encoder,
1512                                     const struct drm_display_mode *mode,
1513                                     struct drm_display_mode *adjusted_mode)
1514{
1515        struct xilinx_drm_dp *dp = to_dp(encoder);
1516        int diff = mode->htotal - mode->hsync_end;
1517
1518        /*
1519         * ZynqMP DP requires horizontal backporch to be greater than 12.
1520         * This limitation may conflict with the sink device.
1521         */
1522        if (dp->dp_sub && diff < XILINX_DP_SUB_TX_MIN_H_BACKPORCH) {
1523                int vrefresh = (adjusted_mode->clock * 1000) /
1524                               (adjusted_mode->vtotal * adjusted_mode->htotal);
1525
1526                diff = XILINX_DP_SUB_TX_MIN_H_BACKPORCH - diff;
1527                adjusted_mode->htotal += diff;
1528                adjusted_mode->clock = adjusted_mode->vtotal *
1529                                       adjusted_mode->htotal * vrefresh / 1000;
1530        }
1531
1532        return true;
1533}
1534
1535static int xilinx_drm_dp_mode_valid(struct drm_encoder *encoder,
1536                                    struct drm_display_mode *mode)
1537{
1538        struct xilinx_drm_dp *dp = to_dp(encoder);
1539        u8 max_lanes = dp->link_config.max_lanes;
1540        u8 bpp = dp->config.bpp;
1541        u32 max_pclock = dp->config.max_pclock;
1542        int max_rate = dp->link_config.max_rate;
1543        int rate;
1544
1545        if (max_pclock && mode->clock > max_pclock)
1546                return MODE_CLOCK_HIGH;
1547
1548        rate = xilinx_drm_dp_max_rate(max_rate, max_lanes, bpp);
1549        if (mode->clock > rate)
1550                return MODE_CLOCK_HIGH;
1551
1552        return MODE_OK;
1553}
1554
1555/**
1556 * xilinx_drm_dp_mode_set_transfer_unit - Set the transfer unit values
1557 * @dp: DisplayPort IP core structure
1558 * @mode: requested display mode
1559 *
1560 * Set the transfer unit, and caculate all transfer unit size related values.
1561 * Calculation is based on DP and IP core specification.
1562 */
1563static void xilinx_drm_dp_mode_set_transfer_unit(struct xilinx_drm_dp *dp,
1564                                                 struct drm_display_mode *mode)
1565{
1566        u32 tu = XILINX_DP_TX_DEF_TRANSFER_UNIT_SIZE;
1567        u32 bw, vid_kbytes, avg_bytes_per_tu, init_wait;
1568
1569        /* Use the max transfer unit size (default) */
1570        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_TRANSFER_UNIT_SIZE, tu);
1571
1572        vid_kbytes = mode->clock * (dp->config.bpp / 8);
1573        bw = drm_dp_bw_code_to_link_rate(dp->mode.bw_code);
1574        avg_bytes_per_tu = vid_kbytes * tu / (dp->mode.lane_cnt * bw / 1000);
1575
1576        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_MIN_BYTES_PER_TU,
1577                          avg_bytes_per_tu / 1000);
1578        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_FRAC_BYTES_PER_TU,
1579                          avg_bytes_per_tu % 1000);
1580
1581        /* Configure the initial wait cycle based on transfer unit size */
1582        if (tu < (avg_bytes_per_tu / 1000))
1583                init_wait = 0;
1584        else if ((avg_bytes_per_tu / 1000) <= 4)
1585                init_wait = tu;
1586        else
1587                init_wait = tu - avg_bytes_per_tu / 1000;
1588
1589        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_INIT_WAIT, init_wait);
1590}
1591
1592/**
1593 * xilinx_drm_dp_mode_set_stream - Configure the main stream
1594 * @dp: DisplayPort IP core structure
1595 * @mode: requested display mode
1596 *
1597 * Configure the main stream based on the requested mode @mode. Calculation is
1598 * based on IP core specification.
1599 */
1600static void xilinx_drm_dp_mode_set_stream(struct xilinx_drm_dp *dp,
1601                                          struct drm_display_mode *mode)
1602{
1603        u8 lane_cnt = dp->mode.lane_cnt;
1604        u32 reg, wpl;
1605
1606        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_MAIN_STREAM_HTOTAL,
1607                          mode->htotal);
1608        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_MAIN_STREAM_VTOTAL,
1609                          mode->vtotal);
1610
1611        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_MAIN_STREAM_POLARITY,
1612                          (!!(mode->flags & DRM_MODE_FLAG_PVSYNC) <<
1613                           XILINX_DP_TX_MAIN_STREAM_POLARITY_VSYNC_SHIFT) |
1614                          (!!(mode->flags & DRM_MODE_FLAG_PHSYNC) <<
1615                           XILINX_DP_TX_MAIN_STREAM_POLARITY_HSYNC_SHIFT));
1616
1617        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_MAIN_STREAM_HSWIDTH,
1618                          mode->hsync_end - mode->hsync_start);
1619        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_MAIN_STREAM_VSWIDTH,
1620                          mode->vsync_end - mode->vsync_start);
1621
1622        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_MAIN_STREAM_HRES,
1623                          mode->hdisplay);
1624        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_MAIN_STREAM_VRES,
1625                          mode->vdisplay);
1626
1627        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_MAIN_STREAM_HSTART,
1628                          mode->htotal - mode->hsync_start);
1629        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_MAIN_STREAM_VSTART,
1630                          mode->vtotal - mode->vsync_start);
1631
1632        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_MAIN_STREAM_MISC0,
1633                          dp->config.misc0);
1634        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_MAIN_STREAM_MISC1,
1635                          dp->config.misc1);
1636
1637        /* In synchronous mode, set the diviers */
1638        if (dp->config.misc0 & XILINX_DP_TX_MAIN_STREAM_MISC0_SYNC) {
1639                reg = drm_dp_bw_code_to_link_rate(dp->mode.bw_code);
1640                xilinx_drm_writel(dp->iomem, XILINX_DP_TX_N_VID, reg);
1641                xilinx_drm_writel(dp->iomem, XILINX_DP_TX_M_VID, mode->clock);
1642                if (dp->aud_clk) {
1643                        int aud_rate = clk_get_rate(dp->aud_clk);
1644
1645                        dev_dbg(dp->dev, "Audio rate: %d\n", aud_rate / 512);
1646
1647                        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_AUDIO_N_AUD,
1648                                          reg);
1649                        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_AUDIO_M_AUD,
1650                                          aud_rate / 1000);
1651                }
1652        }
1653
1654        /* Only 2 channel is supported now */
1655        if (dp->aud_clk)
1656                xilinx_drm_writel(dp->iomem, XILINX_DP_TX_AUDIO_CHANNELS, 1);
1657
1658        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_USER_PIXEL_WIDTH, 1);
1659
1660        /* Translate to the native 16 bit datapath based on IP core spec */
1661        wpl = (mode->hdisplay * dp->config.bpp + 15) / 16;
1662        reg = wpl + wpl % lane_cnt - lane_cnt;
1663        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_USER_DATA_CNT_PER_LANE, reg);
1664}
1665
1666static void xilinx_drm_dp_mode_set(struct drm_encoder *encoder,
1667                                   struct drm_display_mode *mode,
1668                                   struct drm_display_mode *adjusted_mode)
1669{
1670        struct xilinx_drm_dp *dp = to_dp(encoder);
1671        int ret;
1672
1673        ret = xilinx_drm_dp_mode_configure(dp, adjusted_mode->clock, 0);
1674        if (ret < 0)
1675                return;
1676
1677        xilinx_drm_dp_mode_set_stream(dp, adjusted_mode);
1678        xilinx_drm_dp_mode_set_transfer_unit(dp, adjusted_mode);
1679}
1680
1681static enum drm_connector_status
1682xilinx_drm_dp_detect(struct drm_encoder *encoder,
1683                     struct drm_connector *connector)
1684{
1685        struct xilinx_drm_dp *dp = to_dp(encoder);
1686        struct xilinx_drm_dp_link_config *link_config = &dp->link_config;
1687        u32 state;
1688        int ret;
1689
1690        state = xilinx_drm_readl(dp->iomem, XILINX_DP_TX_INTR_SIGNAL_STATE);
1691        if (state & XILINX_DP_TX_INTR_SIGNAL_STATE_HPD) {
1692                dp->status = connector_status_connected;
1693                ret = drm_dp_dpcd_read(&dp->aux, 0x0, dp->dpcd,
1694                                       sizeof(dp->dpcd));
1695                if (ret < 0)
1696                        goto disconnected;
1697
1698                link_config->max_rate = min_t(int,
1699                                              drm_dp_max_link_rate(dp->dpcd),
1700                                              dp->config.max_link_rate);
1701                link_config->max_lanes = min_t(u8,
1702                                               drm_dp_max_lane_count(dp->dpcd),
1703                                               dp->config.max_lanes);
1704                return dp->status;
1705        }
1706
1707disconnected:
1708        dp->status = connector_status_disconnected;
1709        return dp->status;
1710}
1711
1712static int xilinx_drm_dp_get_modes(struct drm_encoder *encoder,
1713                                   struct drm_connector *connector)
1714{
1715        struct xilinx_drm_dp *dp = to_dp(encoder);
1716        struct edid *edid;
1717        int ret;
1718
1719        edid = drm_get_edid(connector, &dp->aux.ddc);
1720        if (!edid)
1721                return 0;
1722
1723        drm_mode_connector_update_edid_property(connector, edid);
1724        ret = drm_add_edid_modes(connector, edid);
1725
1726        kfree(edid);
1727
1728        return ret;
1729}
1730
1731static struct drm_encoder_slave_funcs xilinx_drm_dp_encoder_funcs = {
1732        .dpms                   = xilinx_drm_dp_dpms,
1733        .save                   = xilinx_drm_dp_save,
1734        .restore                = xilinx_drm_dp_restore,
1735        .mode_fixup             = xilinx_drm_dp_mode_fixup,
1736        .mode_valid             = xilinx_drm_dp_mode_valid,
1737        .mode_set               = xilinx_drm_dp_mode_set,
1738        .detect                 = xilinx_drm_dp_detect,
1739        .get_modes              = xilinx_drm_dp_get_modes,
1740};
1741
1742static int xilinx_drm_dp_encoder_init(struct platform_device *pdev,
1743                                      struct drm_device *dev,
1744                                      struct drm_encoder_slave *encoder)
1745{
1746        struct xilinx_drm_dp *dp = platform_get_drvdata(pdev);
1747
1748        encoder->slave_priv = dp;
1749        encoder->slave_funcs = &xilinx_drm_dp_encoder_funcs;
1750
1751        dp->encoder = &encoder->base;
1752
1753        return xilinx_drm_dp_init_aux(dp);
1754}
1755
1756static irqreturn_t xilinx_drm_dp_irq_handler(int irq, void *data)
1757{
1758        struct xilinx_drm_dp *dp = (struct xilinx_drm_dp *)data;
1759        u32 reg, status;
1760
1761        reg = dp->dp_sub ?
1762              XILINX_DP_SUB_TX_INTR_STATUS : XILINX_DP_TX_INTR_STATUS;
1763        status = xilinx_drm_readl(dp->iomem, reg);
1764        if (!status)
1765                return IRQ_NONE;
1766
1767        if (status & XILINX_DP_TX_INTR_CHBUF_UNDERFLW_MASK)
1768                dev_dbg(dp->dev, "underflow interrupt\n");
1769        if (status & XILINX_DP_TX_INTR_CHBUF_OVERFLW_MASK)
1770                dev_dbg(dp->dev, "overflow interrupt\n");
1771
1772        xilinx_drm_writel(dp->iomem, reg, status);
1773
1774        if (status & XILINX_DP_TX_INTR_VBLANK_START)
1775                xilinx_drm_dp_sub_handle_vblank(dp->dp_sub);
1776
1777        if (status & XILINX_DP_TX_INTR_HPD_EVENT)
1778                drm_helper_hpd_irq_event(dp->encoder->dev);
1779
1780        if (status & XILINX_DP_TX_INTR_HPD_IRQ) {
1781                u8 status[DP_LINK_STATUS_SIZE + 2];
1782                int ret;
1783
1784                ret = drm_dp_dpcd_read(&dp->aux, DP_SINK_COUNT, status,
1785                                       DP_LINK_STATUS_SIZE + 2);
1786                if (ret < 0)
1787                        goto handled;
1788
1789                if (status[4] & DP_LINK_STATUS_UPDATED ||
1790                    !drm_dp_clock_recovery_ok(&status[2], dp->mode.lane_cnt) ||
1791                    !drm_dp_channel_eq_ok(&status[2], dp->mode.lane_cnt))
1792                        xilinx_drm_dp_train_loop(dp);
1793        }
1794
1795handled:
1796        return IRQ_HANDLED;
1797}
1798
1799static ssize_t
1800xilinx_drm_dp_aux_transfer(struct drm_dp_aux *aux, struct drm_dp_aux_msg *msg)
1801{
1802        struct xilinx_drm_dp *dp = container_of(aux, struct xilinx_drm_dp, aux);
1803        int ret;
1804        unsigned int i, iter;
1805
1806        /* Number of loops = timeout in msec / aux delay (400 usec) */
1807        iter = xilinx_drm_dp_aux_timeout_ms * 1000 / 400;
1808        iter = iter ? iter : 1;
1809
1810        for (i = 0; i < iter; i++) {
1811                ret = xilinx_drm_dp_aux_cmd_submit(dp, msg->request,
1812                                                   msg->address, msg->buffer,
1813                                                   msg->size, &msg->reply);
1814                if (!ret) {
1815                        dev_dbg(dp->dev, "aux %d retries\n", i);
1816                        return msg->size;
1817                }
1818
1819                if (dp->status == connector_status_disconnected) {
1820                        dev_dbg(dp->dev, "no aux dev\n");
1821                        return -ENODEV;
1822                }
1823
1824                usleep_range(400, 500);
1825        }
1826
1827        dev_dbg(dp->dev, "failed to do aux transfer (%d)\n", ret);
1828
1829        return ret;
1830}
1831
1832static int xilinx_drm_dp_parse_of(struct xilinx_drm_dp *dp)
1833{
1834        struct device_node *node = dp->dev->of_node;
1835        struct xilinx_drm_dp_config *config = &dp->config;
1836        const char *string;
1837        u32 num_colors, bpc;
1838        bool sync;
1839        int ret;
1840
1841        ret = of_property_read_string(node, "xlnx,dp-version", &string);
1842        if (ret < 0) {
1843                dev_err(dp->dev, "No DP version in DT\n");
1844                return ret;
1845        }
1846
1847        if (strcmp(string, "v1.1a") == 0) {
1848                config->dp_version = DP_V1_1A;
1849        } else if (strcmp(string, "v1.2") == 0) {
1850                config->dp_version = DP_V1_2;
1851        } else {
1852                dev_err(dp->dev, "Invalid DP version in DT\n");
1853                return -EINVAL;
1854        }
1855
1856        ret = of_property_read_u32(node, "xlnx,max-lanes", &config->max_lanes);
1857        if (ret < 0) {
1858                dev_err(dp->dev, "No lane count in DT\n");
1859                return ret;
1860        }
1861
1862        if (config->max_lanes != 1 && config->max_lanes != 2 &&
1863            config->max_lanes != 4) {
1864                dev_err(dp->dev, "Invalid max lanes in DT\n");
1865                return -EINVAL;
1866        }
1867
1868        ret = of_property_read_u32(node, "xlnx,max-link-rate",
1869                                   &config->max_link_rate);
1870        if (ret < 0) {
1871                dev_err(dp->dev, "No link rate in DT\n");
1872                return ret;
1873        }
1874
1875        if (config->max_link_rate != DP_REDUCED_BIT_RATE &&
1876            config->max_link_rate != DP_HIGH_BIT_RATE &&
1877            config->max_link_rate != DP_HIGH_BIT_RATE2) {
1878                dev_err(dp->dev, "Invalid link rate in DT\n");
1879                return -EINVAL;
1880        }
1881
1882        config->enable_yonly = of_property_read_bool(node, "xlnx,enable-yonly");
1883        config->enable_ycrcb = of_property_read_bool(node, "xlnx,enable-ycrcb");
1884
1885        sync = of_property_read_bool(node, "xlnx,sync");
1886        if (sync)
1887                config->misc0 |= XILINX_DP_TX_MAIN_STREAM_MISC0_SYNC;
1888
1889        ret = of_property_read_string(node, "xlnx,colormetry", &string);
1890        if (ret < 0) {
1891                dev_err(dp->dev, "No colormetry in DT\n");
1892                return ret;
1893        }
1894
1895        if (strcmp(string, "rgb") == 0) {
1896                config->misc0 |= XILINX_DP_MISC0_RGB;
1897                num_colors = 3;
1898        } else if (config->enable_ycrcb && strcmp(string, "ycrcb422") == 0) {
1899                config->misc0 |= XILINX_DP_MISC0_YCRCB_422;
1900                num_colors = 2;
1901        } else if (config->enable_ycrcb && strcmp(string, "ycrcb444") == 0) {
1902                config->misc0 |= XILINX_DP_MISC0_YCRCB_444;
1903                num_colors = 3;
1904        } else if (config->enable_yonly && strcmp(string, "yonly") == 0) {
1905                config->misc1 |= XILINX_DP_MISC1_Y_ONLY;
1906                num_colors = 1;
1907        } else {
1908                dev_err(dp->dev, "Invalid colormetry in DT\n");
1909                return -EINVAL;
1910        }
1911
1912        ret = of_property_read_u32(node, "xlnx,max-bpc", &config->max_bpc);
1913        if (ret < 0) {
1914                dev_err(dp->dev, "No max bpc in DT\n");
1915                return ret;
1916        }
1917
1918        if (config->max_bpc != 8 && config->max_bpc != 10 &&
1919            config->max_bpc != 12 && config->max_bpc != 16) {
1920                dev_err(dp->dev, "Invalid max bpc in DT\n");
1921                return -EINVAL;
1922        }
1923
1924        ret = of_property_read_u32(node, "xlnx,bpc", &bpc);
1925        if (ret < 0) {
1926                dev_err(dp->dev, "No color depth(bpc) in DT\n");
1927                return ret;
1928        }
1929
1930        if (bpc > config->max_bpc) {
1931                dev_err(dp->dev, "Invalid color depth(bpc) in DT\n");
1932                return -EINVAL;
1933        }
1934
1935        switch (bpc) {
1936        case 6:
1937                config->misc0 |= XILINX_DP_MISC0_BPC_6;
1938                break;
1939        case 8:
1940                config->misc0 |= XILINX_DP_MISC0_BPC_8;
1941                break;
1942        case 10:
1943                config->misc0 |= XILINX_DP_MISC0_BPC_10;
1944                break;
1945        case 12:
1946                config->misc0 |= XILINX_DP_MISC0_BPC_12;
1947                break;
1948        case 16:
1949                config->misc0 |= XILINX_DP_MISC0_BPC_16;
1950                break;
1951        default:
1952                dev_err(dp->dev, "Not supported color depth in DT\n");
1953                return -EINVAL;
1954        }
1955
1956        config->bpp = num_colors * bpc;
1957
1958        of_property_read_u32(node, "xlnx,max-pclock-frequency",
1959                             &config->max_pclock);
1960
1961        return 0;
1962}
1963
1964static int __maybe_unused xilinx_drm_dp_pm_suspend(struct device *dev)
1965{
1966        struct xilinx_drm_dp *dp = dev_get_drvdata(dev);
1967
1968        xilinx_drm_dp_exit_phy(dp);
1969
1970        return 0;
1971}
1972
1973static int __maybe_unused xilinx_drm_dp_pm_resume(struct device *dev)
1974{
1975        struct xilinx_drm_dp *dp = dev_get_drvdata(dev);
1976
1977        xilinx_drm_dp_init_phy(dp);
1978        xilinx_drm_dp_init_aux(dp);
1979        drm_helper_hpd_irq_event(dp->encoder->dev);
1980
1981        return 0;
1982}
1983
1984static const struct dev_pm_ops xilinx_drm_dp_pm_ops = {
1985        SET_SYSTEM_SLEEP_PM_OPS(xilinx_drm_dp_pm_suspend,
1986                                xilinx_drm_dp_pm_resume)
1987};
1988
1989static int xilinx_drm_dp_probe(struct platform_device *pdev)
1990{
1991        struct xilinx_drm_dp *dp;
1992        struct resource *res;
1993        u32 version, i;
1994        int irq, ret;
1995
1996        dp = devm_kzalloc(&pdev->dev, sizeof(*dp), GFP_KERNEL);
1997        if (!dp)
1998                return -ENOMEM;
1999
2000        dp->dpms = DRM_MODE_DPMS_OFF;
2001        dp->status = connector_status_disconnected;
2002        dp->dev = &pdev->dev;
2003
2004        ret = xilinx_drm_dp_parse_of(dp);
2005        if (ret < 0)
2006                return ret;
2007
2008        dp->aclk = devm_clk_get(dp->dev, "aclk");
2009        if (IS_ERR(dp->aclk))
2010                return PTR_ERR(dp->aclk);
2011
2012        ret = clk_prepare_enable(dp->aclk);
2013        if (ret) {
2014                dev_err(dp->dev, "failed to enable the aclk\n");
2015                return ret;
2016        }
2017
2018        dp->aud_clk = devm_clk_get(dp->dev, "aud_clk");
2019        if (IS_ERR(dp->aud_clk)) {
2020                ret = PTR_ERR(dp->aud_clk);
2021                if (ret == -EPROBE_DEFER)
2022                        goto error_aclk;
2023                dp->aud_clk = NULL;
2024                dev_dbg(dp->dev, "failed to get the aud_clk:\n");
2025        }
2026
2027        dp->dp_sub = xilinx_drm_dp_sub_of_get(pdev->dev.of_node);
2028        if (IS_ERR(dp->dp_sub)) {
2029                ret = PTR_ERR(dp->dp_sub);
2030                goto error_aclk;
2031        }
2032
2033        res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2034        dp->iomem = devm_ioremap_resource(dp->dev, res);
2035        if (IS_ERR(dp->iomem)) {
2036                ret = PTR_ERR(dp->iomem);
2037                goto error_dp_sub;
2038        }
2039
2040        platform_set_drvdata(pdev, dp);
2041
2042        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_PHY_POWER_DOWN,
2043                          XILINX_DP_TX_PHY_POWER_DOWN_ALL);
2044        xilinx_drm_set(dp->iomem, XILINX_DP_TX_PHY_CONFIG,
2045                       XILINX_DP_TX_PHY_CONFIG_ALL_RESET);
2046        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_FORCE_SCRAMBLER_RESET, 1);
2047        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_ENABLE, 0);
2048
2049        if (dp->dp_sub) {
2050                for (i = 0; i < dp->config.max_lanes; i++) {
2051                        char phy_name[16];
2052
2053                        snprintf(phy_name, sizeof(phy_name), "dp-phy%d", i);
2054                        dp->phy[i] = devm_phy_get(dp->dev, phy_name);
2055                        if (IS_ERR(dp->phy[i])) {
2056                                dev_err(dp->dev, "failed to get phy lane\n");
2057                                ret = PTR_ERR(dp->phy[i]);
2058                                dp->phy[i] = NULL;
2059                                goto error_dp_sub;
2060                        }
2061                }
2062        }
2063
2064        ret = xilinx_drm_dp_init_phy(dp);
2065        if (ret)
2066                goto error_phy;
2067
2068        dp->aux.name = "Xilinx DP AUX";
2069        dp->aux.dev = dp->dev;
2070        dp->aux.transfer = xilinx_drm_dp_aux_transfer;
2071        ret = drm_dp_aux_register(&dp->aux);
2072        if (ret < 0) {
2073                dev_err(dp->dev, "failed to initialize DP aux\n");
2074                goto error;
2075        }
2076
2077        irq = platform_get_irq(pdev, 0);
2078        if (irq < 0) {
2079                ret = irq;
2080                goto error;
2081        }
2082
2083        ret = devm_request_threaded_irq(dp->dev, irq, NULL,
2084                                        xilinx_drm_dp_irq_handler, IRQF_ONESHOT,
2085                                        dev_name(dp->dev), dp);
2086        if (ret < 0)
2087                goto error;
2088
2089        version = xilinx_drm_readl(dp->iomem, XILINX_DP_TX_VERSION);
2090
2091        dev_info(dp->dev, "device found, version %u.%02x%x\n",
2092                 ((version & XILINX_DP_TX_VERSION_MAJOR_MASK) >>
2093                  XILINX_DP_TX_VERSION_MAJOR_SHIFT),
2094                 ((version & XILINX_DP_TX_VERSION_MINOR_MASK) >>
2095                  XILINX_DP_TX_VERSION_MINOR_SHIFT),
2096                 ((version & XILINX_DP_TX_VERSION_REVISION_MASK) >>
2097                  XILINX_DP_TX_VERSION_REVISION_SHIFT));
2098
2099        version = xilinx_drm_readl(dp->iomem, XILINX_DP_TX_CORE_ID);
2100        if (version & XILINX_DP_TX_CORE_ID_DIRECTION) {
2101                dev_err(dp->dev, "Receiver is not supported\n");
2102                ret = -ENODEV;
2103                goto error;
2104        }
2105
2106        dev_info(dp->dev, "Display Port, version %u.%02x%02x (tx)\n",
2107                 ((version & XILINX_DP_TX_CORE_ID_MAJOR_MASK) >>
2108                  XILINX_DP_TX_CORE_ID_MAJOR_SHIFT),
2109                 ((version & XILINX_DP_TX_CORE_ID_MINOR_MASK) >>
2110                  XILINX_DP_TX_CORE_ID_MINOR_SHIFT),
2111                 ((version & XILINX_DP_TX_CORE_ID_REVISION_MASK) >>
2112                  XILINX_DP_TX_CORE_ID_REVISION_SHIFT));
2113
2114        pm_runtime_enable(dp->dev);
2115
2116        xilinx_dp_debugfs_init(dp);
2117
2118        return 0;
2119
2120error:
2121        drm_dp_aux_unregister(&dp->aux);
2122error_dp_sub:
2123        xilinx_drm_dp_sub_put(dp->dp_sub);
2124error_phy:
2125        xilinx_drm_dp_exit_phy(dp);
2126error_aclk:
2127        clk_disable_unprepare(dp->aclk);
2128        return ret;
2129}
2130
2131static int xilinx_drm_dp_remove(struct platform_device *pdev)
2132{
2133        struct xilinx_drm_dp *dp = platform_get_drvdata(pdev);
2134
2135        pm_runtime_disable(dp->dev);
2136        xilinx_drm_writel(dp->iomem, XILINX_DP_TX_ENABLE, 0);
2137
2138        drm_dp_aux_unregister(&dp->aux);
2139        xilinx_drm_dp_exit_phy(dp);
2140        xilinx_drm_dp_sub_put(dp->dp_sub);
2141
2142        if (dp->aud_clk && dp->aud_clk_enabled)
2143                clk_disable_unprepare(dp->aud_clk);
2144        clk_disable_unprepare(dp->aclk);
2145
2146        return 0;
2147}
2148
2149static const struct of_device_id xilinx_drm_dp_of_match[] = {
2150        { .compatible = "xlnx,v-dp", },
2151        { /* end of table */ },
2152};
2153MODULE_DEVICE_TABLE(of, xilinx_drm_dp_of_match);
2154
2155static struct drm_platform_encoder_driver xilinx_drm_dp_driver = {
2156        .platform_driver = {
2157                .probe                  = xilinx_drm_dp_probe,
2158                .remove                 = xilinx_drm_dp_remove,
2159                .driver                 = {
2160                        .owner          = THIS_MODULE,
2161                        .name           = "xilinx-drm-dp",
2162                        .of_match_table = xilinx_drm_dp_of_match,
2163                        .pm             = &xilinx_drm_dp_pm_ops,
2164                },
2165        },
2166
2167        .encoder_init = xilinx_drm_dp_encoder_init,
2168};
2169
2170static int __init xilinx_drm_dp_init(void)
2171{
2172        return platform_driver_register(&xilinx_drm_dp_driver.platform_driver);
2173}
2174
2175static void __exit xilinx_drm_dp_exit(void)
2176{
2177        platform_driver_unregister(&xilinx_drm_dp_driver.platform_driver);
2178}
2179
2180module_init(xilinx_drm_dp_init);
2181module_exit(xilinx_drm_dp_exit);
2182
2183MODULE_AUTHOR("Xilinx, Inc.");
2184MODULE_DESCRIPTION("Xilinx DRM KMS DiplayPort Driver");
2185MODULE_LICENSE("GPL v2");
2186