Jitao Shi | 542919f | 2019-09-26 10:22:08 +0800 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2019 Analogix Semiconductor. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | #include <console/console.h> |
| 17 | #include <delay.h> |
| 18 | #include <device/i2c_simple.h> |
| 19 | #include <edid.h> |
| 20 | #include <gpio.h> |
| 21 | #include <timer.h> |
| 22 | #include <string.h> |
| 23 | |
| 24 | #include "anx7625.h" |
| 25 | |
| 26 | #define ANXERROR(format, ...) \ |
| 27 | printk(BIOS_ERR, "ERROR: %s: " format, __func__, ##__VA_ARGS__) |
| 28 | #define ANXINFO(format, ...) \ |
| 29 | printk(BIOS_INFO, "%s: " format, __func__, ##__VA_ARGS__) |
| 30 | #define ANXDEBUG(format, ...) \ |
| 31 | printk(BIOS_DEBUG, "%s: " format, __func__, ##__VA_ARGS__) |
| 32 | |
| 33 | /* |
| 34 | * There is a sync issue while accessing I2C register between AP(CPU) and |
| 35 | * internal firmware(OCM). To avoid the race condition, AP should access the |
| 36 | * reserved slave address before slave address changes. |
| 37 | */ |
| 38 | static int i2c_access_workaround(uint8_t bus, uint8_t saddr) |
| 39 | { |
| 40 | uint8_t offset; |
| 41 | static uint8_t saddr_backup = 0; |
| 42 | int ret = 0; |
| 43 | |
| 44 | if (saddr == saddr_backup) |
| 45 | return ret; |
| 46 | |
| 47 | saddr_backup = saddr; |
| 48 | |
| 49 | switch (saddr) { |
| 50 | case TCPC_INTERFACE_ADDR: |
| 51 | offset = RSVD_00_ADDR; |
| 52 | break; |
| 53 | case TX_P0_ADDR: |
| 54 | offset = RSVD_D1_ADDR; |
| 55 | break; |
| 56 | case TX_P1_ADDR: |
| 57 | offset = RSVD_60_ADDR; |
| 58 | break; |
| 59 | case RX_P0_ADDR: |
| 60 | offset = RSVD_39_ADDR; |
| 61 | break; |
| 62 | case RX_P1_ADDR: |
| 63 | offset = RSVD_7F_ADDR; |
| 64 | break; |
| 65 | default: |
| 66 | offset = RSVD_00_ADDR; |
| 67 | break; |
| 68 | } |
| 69 | |
| 70 | ret = i2c_writeb(bus, saddr, offset, 0x00); |
| 71 | if (ret < 0) |
| 72 | ANXERROR("Failed to access %#x:%#x\n", saddr, offset); |
| 73 | return ret; |
| 74 | } |
| 75 | |
| 76 | static int anx7625_reg_read(uint8_t bus, uint8_t saddr, uint8_t offset, |
| 77 | uint8_t *val) |
| 78 | { |
| 79 | int ret; |
| 80 | |
| 81 | i2c_access_workaround(bus, saddr); |
| 82 | ret = i2c_readb(bus, saddr, offset, val); |
| 83 | if (ret < 0) { |
| 84 | ANXERROR("Failed to read i2c reg=%#x:%#x\n", saddr, offset); |
| 85 | return ret; |
| 86 | } |
| 87 | return *val; |
| 88 | } |
| 89 | |
| 90 | static int anx7625_reg_block_read(uint8_t bus, uint8_t saddr, uint8_t reg_addr, |
| 91 | uint8_t len, uint8_t *buf) |
| 92 | { |
| 93 | int ret; |
| 94 | |
| 95 | i2c_access_workaround(bus, saddr); |
| 96 | ret = i2c_read_bytes(bus, saddr, reg_addr, buf, len); |
| 97 | if (ret < 0) |
| 98 | ANXERROR("Failed to read i2c block=%#x:%#x[len=%#x]\n", saddr, |
| 99 | reg_addr, len); |
| 100 | return ret; |
| 101 | } |
| 102 | |
| 103 | static int anx7625_reg_write(uint8_t bus, uint8_t saddr, uint8_t reg_addr, |
| 104 | uint8_t reg_val) |
| 105 | { |
| 106 | int ret; |
| 107 | |
| 108 | i2c_access_workaround(bus, saddr); |
| 109 | ret = i2c_writeb(bus, saddr, reg_addr, reg_val); |
| 110 | if (ret < 0) |
| 111 | ANXERROR("Failed to write i2c id=%#x:%#x\n", saddr, reg_addr); |
| 112 | |
| 113 | return ret; |
| 114 | } |
| 115 | |
| 116 | static int anx7625_write_or(uint8_t bus, uint8_t saddr, uint8_t offset, |
| 117 | uint8_t mask) |
| 118 | { |
| 119 | uint8_t val; |
| 120 | int ret; |
| 121 | |
| 122 | ret = anx7625_reg_read(bus, saddr, offset, &val); |
| 123 | if (ret < 0) |
| 124 | return ret; |
| 125 | |
| 126 | return anx7625_reg_write(bus, saddr, offset, val | mask); |
| 127 | } |
| 128 | |
| 129 | static int anx7625_write_and(uint8_t bus, uint8_t saddr, uint8_t offset, |
| 130 | uint8_t mask) |
| 131 | { |
| 132 | int ret; |
| 133 | uint8_t val; |
| 134 | |
| 135 | ret = anx7625_reg_read(bus, saddr, offset, &val); |
| 136 | if (ret < 0) |
| 137 | return ret; |
| 138 | |
| 139 | return anx7625_reg_write(bus, saddr, offset, val & mask); |
| 140 | } |
| 141 | |
| 142 | static int wait_aux_op_finish(uint8_t bus) |
| 143 | { |
| 144 | uint8_t val; |
| 145 | int ret = -1; |
| 146 | int loop; |
| 147 | |
| 148 | for (loop = 0; loop < 150; loop++) { |
| 149 | mdelay(2); |
| 150 | anx7625_reg_read(bus, RX_P0_ADDR, AP_AUX_CTRL_STATUS, &val); |
| 151 | if (!(val & AP_AUX_CTRL_OP_EN)) { |
| 152 | ret = 0; |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | if (ret != 0) { |
| 158 | ANXERROR("Timed out waiting aux operation.\n"); |
| 159 | return ret; |
| 160 | } |
| 161 | |
| 162 | ret = anx7625_reg_read(bus, RX_P0_ADDR, AP_AUX_CTRL_STATUS, &val); |
| 163 | if (ret < 0 || val & 0x0F) { |
| 164 | ANXDEBUG("aux status %02x\n", val); |
| 165 | ret = -1; |
| 166 | } |
| 167 | |
| 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | static unsigned long gcd(unsigned long a, unsigned long b) |
| 172 | { |
| 173 | if (a == 0) |
| 174 | return b; |
| 175 | |
| 176 | while (b != 0) { |
| 177 | if (a > b) |
| 178 | a = a - b; |
| 179 | else |
| 180 | b = b - a; |
| 181 | } |
| 182 | |
| 183 | return a; |
| 184 | } |
| 185 | |
| 186 | /* Reduce fraction a/b */ |
| 187 | static void anx7625_reduction_of_a_fraction(unsigned long *_a, |
| 188 | unsigned long *_b) |
| 189 | { |
| 190 | unsigned long gcd_num; |
| 191 | unsigned long a = *_a, b = *_b, old_a, old_b; |
| 192 | u32 denom = 1; |
| 193 | |
| 194 | gcd_num = gcd(a, b); |
| 195 | a /= gcd_num; |
| 196 | b /= gcd_num; |
| 197 | |
| 198 | old_a = a; |
| 199 | old_b = b; |
| 200 | |
| 201 | while (a > MAX_UNSIGNED_24BIT || b > MAX_UNSIGNED_24BIT) { |
| 202 | denom++; |
| 203 | a = old_a / denom; |
| 204 | b = old_b / denom; |
| 205 | } |
| 206 | |
| 207 | /* Increase a, b to have higher ODFC PLL output frequency accuracy. */ |
| 208 | while ((a << 1) < MAX_UNSIGNED_24BIT && (b << 1) < MAX_UNSIGNED_24BIT) { |
| 209 | a <<= 1; |
| 210 | b <<= 1; |
| 211 | } |
| 212 | |
| 213 | *_a = a; |
| 214 | *_b = b; |
| 215 | } |
| 216 | |
| 217 | static int anx7625_calculate_m_n(u32 pixelclock, |
| 218 | unsigned long *m, unsigned long *n, |
| 219 | uint8_t *pd) |
| 220 | { |
| 221 | uint8_t post_divider = *pd; |
| 222 | if (pixelclock > PLL_OUT_FREQ_ABS_MAX / POST_DIVIDER_MIN) { |
| 223 | /* pixel clock frequency is too high */ |
| 224 | ANXERROR("pixelclock %u higher than %lu, " |
| 225 | "output may be unstable\n", |
| 226 | pixelclock, PLL_OUT_FREQ_ABS_MAX / POST_DIVIDER_MIN); |
| 227 | return 1; |
| 228 | } |
| 229 | |
| 230 | if (pixelclock < PLL_OUT_FREQ_ABS_MIN / POST_DIVIDER_MAX) { |
| 231 | /* pixel clock frequency is too low */ |
| 232 | ANXERROR("pixelclock %u lower than %lu, " |
| 233 | "output may be unstable\n", |
| 234 | pixelclock, PLL_OUT_FREQ_ABS_MIN / POST_DIVIDER_MAX); |
| 235 | return 1; |
| 236 | } |
| 237 | |
| 238 | post_divider = 1; |
| 239 | |
| 240 | for (post_divider = 1; |
| 241 | pixelclock < PLL_OUT_FREQ_MIN / post_divider; |
| 242 | post_divider++) |
| 243 | ; |
| 244 | |
| 245 | if (post_divider > POST_DIVIDER_MAX) { |
| 246 | for (post_divider = 1; |
| 247 | pixelclock < PLL_OUT_FREQ_ABS_MIN / post_divider; |
| 248 | post_divider++) |
| 249 | ; |
| 250 | |
| 251 | if (post_divider > POST_DIVIDER_MAX) { |
| 252 | ANXERROR("cannot find property post_divider(%d)\n", |
| 253 | post_divider); |
| 254 | return 1; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /* Patch to improve the accuracy */ |
| 259 | if (post_divider == 7) { |
| 260 | /* 27,000,000 is not divisible by 7 */ |
| 261 | post_divider = 8; |
| 262 | } else if (post_divider == 11) { |
| 263 | /* 27,000,000 is not divisible by 11 */ |
| 264 | post_divider = 12; |
| 265 | } else if (post_divider == 13 || post_divider == 14) { |
| 266 | /*27,000,000 is not divisible by 13 or 14*/ |
| 267 | post_divider = 15; |
| 268 | } |
| 269 | |
| 270 | if (pixelclock * post_divider > PLL_OUT_FREQ_ABS_MAX) { |
| 271 | ANXINFO("act clock(%u) large than maximum(%lu)\n", |
| 272 | pixelclock * post_divider, PLL_OUT_FREQ_ABS_MAX); |
| 273 | return 1; |
| 274 | } |
| 275 | |
| 276 | *m = (unsigned long long)pixelclock * 599 / 600; |
| 277 | *n = XTAL_FRQ / post_divider; |
| 278 | *pd = post_divider; |
| 279 | |
| 280 | anx7625_reduction_of_a_fraction(m, n); |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | static int anx7625_odfc_config(uint8_t bus, uint8_t post_divider) |
| 286 | { |
| 287 | int ret; |
| 288 | |
| 289 | /* config input reference clock frequency 27MHz/19.2MHz */ |
| 290 | ret = anx7625_write_and(bus, RX_P1_ADDR, MIPI_DIGITAL_PLL_16, |
| 291 | ~(REF_CLK_27000kHz << MIPI_FREF_D_IND)); |
| 292 | ret |= anx7625_write_or(bus, RX_P1_ADDR, MIPI_DIGITAL_PLL_16, |
| 293 | (REF_CLK_27000kHz << MIPI_FREF_D_IND)); |
| 294 | /* post divider */ |
| 295 | ret |= anx7625_write_and(bus, RX_P1_ADDR, MIPI_DIGITAL_PLL_8, 0x0f); |
| 296 | ret |= anx7625_write_or(bus, RX_P1_ADDR, MIPI_DIGITAL_PLL_8, |
| 297 | post_divider << 4); |
| 298 | |
| 299 | /* add patch for MIS2-125 (5pcs ANX7625 fail ATE MBIST test) */ |
| 300 | ret |= anx7625_write_and(bus, RX_P1_ADDR, MIPI_DIGITAL_PLL_7, |
| 301 | ~MIPI_PLL_VCO_TUNE_REG_VAL); |
| 302 | |
| 303 | /* reset ODFC PLL */ |
| 304 | ret |= anx7625_write_and(bus, RX_P1_ADDR, MIPI_DIGITAL_PLL_7, |
| 305 | ~MIPI_PLL_RESET_N); |
| 306 | ret |= anx7625_write_or(bus, RX_P1_ADDR, MIPI_DIGITAL_PLL_7, |
| 307 | MIPI_PLL_RESET_N); |
| 308 | |
| 309 | if (ret < 0) |
| 310 | ANXERROR("IO error.\n"); |
| 311 | |
| 312 | return ret; |
| 313 | } |
| 314 | |
| 315 | static int anx7625_dsi_video_config(uint8_t bus, struct display_timing *dt) |
| 316 | { |
| 317 | unsigned long m, n; |
| 318 | u16 htotal; |
| 319 | int ret; |
| 320 | uint8_t post_divider = 0; |
| 321 | |
| 322 | ret = anx7625_calculate_m_n(dt->pixelclock * 1000, &m, &n, |
| 323 | &post_divider); |
| 324 | |
| 325 | if (ret != 0) { |
| 326 | ANXERROR("cannot get property m n value.\n"); |
| 327 | return -1; |
| 328 | } |
| 329 | |
| 330 | ANXINFO("compute M(%lu), N(%lu), divider(%d).\n", m, n, post_divider); |
| 331 | |
| 332 | /* configure pixel clock */ |
| 333 | ret = anx7625_reg_write(bus, RX_P0_ADDR, PIXEL_CLOCK_L, |
| 334 | (dt->pixelclock / 1000) & 0xFF); |
| 335 | ret |= anx7625_reg_write(bus, RX_P0_ADDR, PIXEL_CLOCK_H, |
| 336 | (dt->pixelclock / 1000) >> 8); |
| 337 | /* lane count */ |
| 338 | ret |= anx7625_write_and(bus, RX_P1_ADDR, MIPI_LANE_CTRL_0, 0xfc); |
| 339 | |
| 340 | ret |= anx7625_write_or(bus, RX_P1_ADDR, MIPI_LANE_CTRL_0, 3); |
| 341 | |
| 342 | /* Htotal */ |
| 343 | htotal = dt->hactive + dt->hfront_porch + |
| 344 | dt->hback_porch + dt->hsync_len; |
| 345 | ret |= anx7625_reg_write(bus, RX_P2_ADDR, |
| 346 | HORIZONTAL_TOTAL_PIXELS_L, htotal & 0xFF); |
| 347 | ret |= anx7625_reg_write(bus, RX_P2_ADDR, |
| 348 | HORIZONTAL_TOTAL_PIXELS_H, htotal >> 8); |
| 349 | /* Hactive */ |
| 350 | ret |= anx7625_reg_write(bus, RX_P2_ADDR, |
| 351 | HORIZONTAL_ACTIVE_PIXELS_L, dt->hactive & 0xFF); |
| 352 | ret |= anx7625_reg_write(bus, RX_P2_ADDR, |
| 353 | HORIZONTAL_ACTIVE_PIXELS_H, dt->hactive >> 8); |
| 354 | /* HFP */ |
| 355 | ret |= anx7625_reg_write(bus, RX_P2_ADDR, |
| 356 | HORIZONTAL_FRONT_PORCH_L, dt->hfront_porch); |
| 357 | ret |= anx7625_reg_write(bus, RX_P2_ADDR, |
| 358 | HORIZONTAL_FRONT_PORCH_H, |
| 359 | dt->hfront_porch >> 8); |
| 360 | /* HWS */ |
| 361 | ret |= anx7625_reg_write(bus, RX_P2_ADDR, |
| 362 | HORIZONTAL_SYNC_WIDTH_L, dt->hsync_len); |
| 363 | ret |= anx7625_reg_write(bus, RX_P2_ADDR, |
| 364 | HORIZONTAL_SYNC_WIDTH_H, dt->hsync_len >> 8); |
| 365 | /* HBP */ |
| 366 | ret |= anx7625_reg_write(bus, RX_P2_ADDR, |
| 367 | HORIZONTAL_BACK_PORCH_L, dt->hback_porch); |
| 368 | ret |= anx7625_reg_write(bus, RX_P2_ADDR, |
| 369 | HORIZONTAL_BACK_PORCH_H, dt->hback_porch >> 8); |
| 370 | /* Vactive */ |
| 371 | ret |= anx7625_reg_write(bus, RX_P2_ADDR, ACTIVE_LINES_L, dt->vactive); |
| 372 | ret |= anx7625_reg_write(bus, RX_P2_ADDR, ACTIVE_LINES_H, |
| 373 | dt->vactive >> 8); |
| 374 | /* VFP */ |
| 375 | ret |= anx7625_reg_write(bus, RX_P2_ADDR, |
| 376 | VERTICAL_FRONT_PORCH, dt->vfront_porch); |
| 377 | /* VWS */ |
| 378 | ret |= anx7625_reg_write(bus, RX_P2_ADDR, |
| 379 | VERTICAL_SYNC_WIDTH, dt->vsync_len); |
| 380 | /* VBP */ |
| 381 | ret |= anx7625_reg_write(bus, RX_P2_ADDR, |
| 382 | VERTICAL_BACK_PORCH, dt->vback_porch); |
| 383 | /* M value */ |
| 384 | ret |= anx7625_reg_write(bus, RX_P1_ADDR, |
| 385 | MIPI_PLL_M_NUM_23_16, (m >> 16) & 0xff); |
| 386 | ret |= anx7625_reg_write(bus, RX_P1_ADDR, |
| 387 | MIPI_PLL_M_NUM_15_8, (m >> 8) & 0xff); |
| 388 | ret |= anx7625_reg_write(bus, RX_P1_ADDR, |
| 389 | MIPI_PLL_M_NUM_7_0, (m & 0xff)); |
| 390 | /* N value */ |
| 391 | ret |= anx7625_reg_write(bus, RX_P1_ADDR, |
| 392 | MIPI_PLL_N_NUM_23_16, (n >> 16) & 0xff); |
| 393 | ret |= anx7625_reg_write(bus, RX_P1_ADDR, |
| 394 | MIPI_PLL_N_NUM_15_8, (n >> 8) & 0xff); |
| 395 | ret |= anx7625_reg_write(bus, RX_P1_ADDR, MIPI_PLL_N_NUM_7_0, |
| 396 | (n & 0xff)); |
| 397 | /* diff */ |
| 398 | ret |= anx7625_reg_write(bus, RX_P1_ADDR, MIPI_DIGITAL_ADJ_1, 0x37); |
| 399 | |
| 400 | ret |= anx7625_odfc_config(bus, post_divider - 1); |
| 401 | |
| 402 | if (ret < 0) |
| 403 | ANXERROR("mipi dsi setup IO error.\n"); |
| 404 | |
| 405 | return ret; |
| 406 | } |
| 407 | |
| 408 | static int anx7625_swap_dsi_lane3(uint8_t bus) |
| 409 | { |
| 410 | int ret; |
| 411 | uint8_t val; |
| 412 | |
| 413 | /* swap MIPI-DSI data lane 3 P and N */ |
| 414 | ret = anx7625_reg_read(bus, RX_P1_ADDR, MIPI_SWAP, &val); |
| 415 | if (ret < 0) { |
| 416 | ANXERROR("IO error: access MIPI_SWAP.\n"); |
| 417 | return -1; |
| 418 | } |
| 419 | |
| 420 | val |= (1 << MIPI_SWAP_CH3); |
| 421 | return anx7625_reg_write(bus, RX_P1_ADDR, MIPI_SWAP, val); |
| 422 | } |
| 423 | |
| 424 | static int anx7625_api_dsi_config(uint8_t bus, struct display_timing *dt) |
| 425 | |
| 426 | { |
| 427 | int val, ret; |
| 428 | |
| 429 | /* swap MIPI-DSI data lane 3 P and N */ |
| 430 | ret = anx7625_swap_dsi_lane3(bus); |
| 431 | if (ret < 0) { |
| 432 | ANXERROR("IO error: swap dsi lane 3 failed.\n"); |
| 433 | return ret; |
| 434 | } |
| 435 | |
| 436 | /* DSI clock settings */ |
| 437 | val = (0 << MIPI_HS_PWD_CLK) | |
| 438 | (0 << MIPI_HS_RT_CLK) | |
| 439 | (0 << MIPI_PD_CLK) | |
| 440 | (1 << MIPI_CLK_RT_MANUAL_PD_EN) | |
| 441 | (1 << MIPI_CLK_HS_MANUAL_PD_EN) | |
| 442 | (0 << MIPI_CLK_DET_DET_BYPASS) | |
| 443 | (0 << MIPI_CLK_MISS_CTRL) | |
| 444 | (0 << MIPI_PD_LPTX_CH_MANUAL_PD_EN); |
| 445 | ret = anx7625_reg_write(bus, RX_P1_ADDR, MIPI_PHY_CONTROL_3, val); |
| 446 | |
| 447 | /* |
| 448 | * Decreased HS prepare tg delay from 160ns to 80ns work with |
| 449 | * a) Dragon board 810 series (Qualcomm AP) |
| 450 | * b) Moving Pixel DSI source (PG3A pattern generator + |
| 451 | * P332 D-PHY Probe) default D-PHY tg 5ns/step |
| 452 | */ |
| 453 | ret |= anx7625_reg_write(bus, RX_P1_ADDR, MIPI_TIME_HS_PRPR, 0x10); |
| 454 | |
| 455 | /* enable DSI mode */ |
| 456 | ret |= anx7625_write_or(bus, RX_P1_ADDR, MIPI_DIGITAL_PLL_18, |
| 457 | SELECT_DSI << MIPI_DPI_SELECT); |
| 458 | |
| 459 | ret |= anx7625_dsi_video_config(bus, dt); |
| 460 | if (ret < 0) { |
| 461 | ANXERROR("dsi video tg config failed\n"); |
| 462 | return ret; |
| 463 | } |
| 464 | |
| 465 | /* toggle m, n ready */ |
| 466 | ret = anx7625_write_and(bus, RX_P1_ADDR, MIPI_DIGITAL_PLL_6, |
| 467 | ~(MIPI_M_NUM_READY | MIPI_N_NUM_READY)); |
| 468 | mdelay(1); |
| 469 | ret |= anx7625_write_or(bus, RX_P1_ADDR, MIPI_DIGITAL_PLL_6, |
| 470 | MIPI_M_NUM_READY | MIPI_N_NUM_READY); |
| 471 | |
| 472 | /* configure integer stable register */ |
| 473 | ret |= anx7625_reg_write(bus, RX_P1_ADDR, MIPI_VIDEO_STABLE_CNT, 0x02); |
| 474 | /* power on MIPI RX */ |
| 475 | ret |= anx7625_reg_write(bus, RX_P1_ADDR, MIPI_LANE_CTRL_10, 0x00); |
| 476 | ret |= anx7625_reg_write(bus, RX_P1_ADDR, MIPI_LANE_CTRL_10, 0x80); |
| 477 | |
| 478 | if (ret < 0) |
| 479 | ANXERROR("IO error: mipi dsi enable init failed.\n"); |
| 480 | |
| 481 | return ret; |
| 482 | } |
| 483 | |
| 484 | static int anx7625_dsi_config(uint8_t bus, struct display_timing *dt) |
| 485 | { |
| 486 | int ret; |
| 487 | |
| 488 | ANXINFO("config dsi.\n"); |
| 489 | |
| 490 | /* DSC disable */ |
| 491 | ret = anx7625_write_and(bus, RX_P0_ADDR, R_DSC_CTRL_0, ~DSC_EN); |
| 492 | ret |= anx7625_api_dsi_config(bus, dt); |
| 493 | |
| 494 | if (ret < 0) { |
| 495 | ANXERROR("IO error: api dsi config error.\n"); |
| 496 | return ret; |
| 497 | } |
| 498 | |
| 499 | /* set MIPI RX EN */ |
| 500 | ret = anx7625_write_or(bus, RX_P0_ADDR, AP_AV_STATUS, AP_MIPI_RX_EN); |
| 501 | /* clear mute flag */ |
| 502 | ret |= anx7625_write_and(bus, RX_P0_ADDR, AP_AV_STATUS, ~AP_MIPI_MUTE); |
| 503 | |
| 504 | if (ret < 0) |
| 505 | ANXERROR("IO error: enable mipi rx failed.\n"); |
| 506 | else |
| 507 | ANXINFO("success to config DSI\n"); |
| 508 | |
| 509 | return ret; |
| 510 | } |
| 511 | |
| 512 | static int sp_tx_rst_aux(uint8_t bus) |
| 513 | { |
| 514 | int ret; |
| 515 | |
| 516 | ret = anx7625_write_or(bus, TX_P2_ADDR, RST_CTRL2, AUX_RST); |
| 517 | ret |= anx7625_write_and(bus, TX_P2_ADDR, RST_CTRL2, ~AUX_RST); |
| 518 | return ret; |
| 519 | } |
| 520 | |
| 521 | static int sp_tx_aux_wr(uint8_t bus, uint8_t offset) |
| 522 | { |
| 523 | int ret; |
| 524 | |
| 525 | ret = anx7625_reg_write(bus, RX_P0_ADDR, AP_AUX_BUFF_START, offset); |
| 526 | ret |= anx7625_reg_write(bus, RX_P0_ADDR, AP_AUX_COMMAND, 0x04); |
| 527 | ret |= anx7625_write_or(bus, RX_P0_ADDR, |
| 528 | AP_AUX_CTRL_STATUS, AP_AUX_CTRL_OP_EN); |
| 529 | return ret | wait_aux_op_finish(bus); |
| 530 | } |
| 531 | |
| 532 | static int sp_tx_aux_rd(uint8_t bus, uint8_t len_cmd) |
| 533 | { |
| 534 | int ret; |
| 535 | |
| 536 | ret = anx7625_reg_write(bus, RX_P0_ADDR, AP_AUX_COMMAND, len_cmd); |
| 537 | ret |= anx7625_write_or(bus, RX_P0_ADDR, |
| 538 | AP_AUX_CTRL_STATUS, AP_AUX_CTRL_OP_EN); |
| 539 | return ret | wait_aux_op_finish(bus); |
| 540 | } |
| 541 | |
| 542 | static int sp_tx_get_edid_block(uint8_t bus) |
| 543 | { |
| 544 | int ret; |
| 545 | uint8_t val = 0; |
| 546 | |
| 547 | sp_tx_aux_wr(bus, 0x7e); |
| 548 | sp_tx_aux_rd(bus, 0x01); |
| 549 | ret = anx7625_reg_read(bus, RX_P0_ADDR, AP_AUX_BUFF_START, &val); |
| 550 | |
| 551 | if (ret < 0) { |
| 552 | ANXERROR("IO error: access AUX BUFF.\n"); |
| 553 | return -1; |
| 554 | } |
| 555 | |
| 556 | ANXINFO("EDID Block = %d\n", val + 1); |
| 557 | |
| 558 | if (val > 3) |
| 559 | val = 1; |
| 560 | |
| 561 | return val; |
| 562 | } |
| 563 | |
| 564 | static int edid_read(uint8_t bus, uint8_t offset, uint8_t *pblock_buf) |
| 565 | { |
| 566 | uint8_t c, cnt = 0; |
| 567 | |
| 568 | c = 0; |
| 569 | for (cnt = 0; cnt < 3; cnt++) { |
| 570 | sp_tx_aux_wr(bus, offset); |
| 571 | /* set I2C read com 0x01 mot = 0 and read 16 bytes */ |
| 572 | c = sp_tx_aux_rd(bus, 0xf1); |
| 573 | |
| 574 | if (c == 1) { |
| 575 | sp_tx_rst_aux(bus); |
| 576 | ANXERROR("edid read failed, reset!\n"); |
| 577 | cnt++; |
| 578 | } else { |
| 579 | anx7625_reg_block_read(bus, RX_P0_ADDR, |
| 580 | AP_AUX_BUFF_START, |
| 581 | MAX_DPCD_BUFFER_SIZE, pblock_buf); |
| 582 | return 0; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | return 1; |
| 587 | } |
| 588 | |
| 589 | static int segments_edid_read(uint8_t bus, uint8_t segment, uint8_t *buf, |
| 590 | uint8_t offset) |
| 591 | { |
| 592 | uint8_t c, cnt = 0; |
| 593 | int ret; |
| 594 | |
| 595 | /* write address only */ |
| 596 | ret = anx7625_reg_write(bus, RX_P0_ADDR, AP_AUX_ADDR_7_0, 0x30); |
| 597 | ret |= anx7625_reg_write(bus, RX_P0_ADDR, AP_AUX_COMMAND, 0x04); |
| 598 | ret |= anx7625_reg_write(bus, RX_P0_ADDR, AP_AUX_CTRL_STATUS, |
| 599 | AP_AUX_CTRL_ADDRONLY | AP_AUX_CTRL_OP_EN); |
| 600 | |
| 601 | ret |= wait_aux_op_finish(bus); |
| 602 | /* write segment address */ |
| 603 | ret |= sp_tx_aux_wr(bus, segment); |
| 604 | /* data read */ |
| 605 | ret |= anx7625_reg_write(bus, RX_P0_ADDR, AP_AUX_ADDR_7_0, 0x50); |
| 606 | |
| 607 | if (ret < 0) { |
| 608 | ANXERROR("IO error: aux initial failed.\n"); |
| 609 | return ret; |
| 610 | } |
| 611 | |
| 612 | for (cnt = 0; cnt < 3; cnt++) { |
| 613 | sp_tx_aux_wr(bus, offset); |
| 614 | /* set I2C read com 0x01 mot = 0 and read 16 bytes */ |
| 615 | c = sp_tx_aux_rd(bus, 0xf1); |
| 616 | |
| 617 | if (c == 1) { |
| 618 | ret = sp_tx_rst_aux(bus); |
| 619 | ANXERROR("segment read failed, reset!\n"); |
| 620 | cnt++; |
| 621 | } else { |
| 622 | ret = anx7625_reg_block_read(bus, RX_P0_ADDR, |
| 623 | AP_AUX_BUFF_START, |
| 624 | MAX_DPCD_BUFFER_SIZE, buf); |
| 625 | return ret; |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | return ret; |
| 630 | } |
| 631 | |
| 632 | static int sp_tx_edid_read(uint8_t bus, uint8_t *pedid_blocks_buf, |
| 633 | uint32_t size) |
| 634 | { |
| 635 | uint8_t offset, edid_pos; |
| 636 | int count, blocks_num; |
| 637 | uint8_t pblock_buf[MAX_DPCD_BUFFER_SIZE]; |
| 638 | uint8_t i; |
| 639 | uint8_t g_edid_break = 0; |
| 640 | int ret; |
| 641 | |
| 642 | /* address initial */ |
| 643 | ret = anx7625_reg_write(bus, RX_P0_ADDR, AP_AUX_ADDR_7_0, 0x50); |
| 644 | ret |= anx7625_reg_write(bus, RX_P0_ADDR, AP_AUX_ADDR_15_8, 0); |
| 645 | ret |= anx7625_write_and(bus, RX_P0_ADDR, AP_AUX_ADDR_19_16, 0xf0); |
| 646 | |
| 647 | if (ret < 0) { |
| 648 | ANXERROR("access aux channel IO error.\n"); |
| 649 | return -1; |
| 650 | } |
| 651 | |
| 652 | blocks_num = sp_tx_get_edid_block(bus); |
| 653 | if (blocks_num < 0) |
| 654 | return blocks_num; |
| 655 | |
| 656 | count = 0; |
| 657 | |
| 658 | do { |
| 659 | switch (count) { |
| 660 | case 0: |
| 661 | case 1: |
| 662 | for (i = 0; i < 8; i++) { |
| 663 | offset = (i + count * 8) * MAX_DPCD_BUFFER_SIZE; |
| 664 | g_edid_break = edid_read(bus, offset, |
| 665 | pblock_buf); |
| 666 | |
| 667 | if (g_edid_break == 1) |
| 668 | break; |
| 669 | |
| 670 | if (offset <= size - MAX_DPCD_BUFFER_SIZE) |
| 671 | memcpy(&pedid_blocks_buf[offset], |
| 672 | pblock_buf, |
| 673 | MAX_DPCD_BUFFER_SIZE); |
| 674 | } |
| 675 | |
| 676 | break; |
| 677 | case 2: |
| 678 | case 3: |
| 679 | offset = (count == 2) ? 0x00 : 0x80; |
| 680 | |
| 681 | for (i = 0; i < 8; i++) { |
| 682 | edid_pos = (i + count * 8) * |
| 683 | MAX_DPCD_BUFFER_SIZE; |
| 684 | |
| 685 | if (g_edid_break == 1) |
| 686 | break; |
| 687 | |
| 688 | segments_edid_read(bus, count / 2, |
| 689 | pblock_buf, offset); |
| 690 | if (edid_pos <= size - MAX_DPCD_BUFFER_SIZE) |
| 691 | memcpy(&pedid_blocks_buf[edid_pos], |
| 692 | pblock_buf, |
| 693 | MAX_DPCD_BUFFER_SIZE); |
| 694 | offset = offset + 0x10; |
| 695 | } |
| 696 | |
| 697 | break; |
| 698 | default: |
| 699 | die("%s: count should be <= 3", __func__); |
| 700 | break; |
| 701 | } |
| 702 | |
| 703 | count++; |
| 704 | |
| 705 | } while (blocks_num >= count); |
| 706 | |
| 707 | /* reset aux channel */ |
| 708 | sp_tx_rst_aux(bus); |
| 709 | |
| 710 | return blocks_num; |
| 711 | } |
| 712 | |
| 713 | static void anx7625_disable_pd_protocol(uint8_t bus) |
| 714 | { |
| 715 | int ret; |
| 716 | |
| 717 | /* reset main ocm */ |
| 718 | ret = anx7625_reg_write(bus, RX_P0_ADDR, 0x88, 0x40); |
| 719 | /* Disable PD */ |
| 720 | ret |= anx7625_reg_write(bus, RX_P0_ADDR, AP_AV_STATUS, AP_DISABLE_PD); |
| 721 | /* release main ocm */ |
| 722 | ret |= anx7625_reg_write(bus, RX_P0_ADDR, 0x88, 0x00); |
| 723 | |
| 724 | if (ret < 0) |
| 725 | ANXERROR("Failed to disable PD feature.\n"); |
| 726 | else |
| 727 | ANXINFO("Disabled PD feature.\n"); |
| 728 | } |
| 729 | |
| 730 | #define FLASH_LOAD_STA 0x05 |
| 731 | #define FLASH_LOAD_STA_CHK (1 << 7) |
| 732 | |
| 733 | static int anx7625_power_on_init(uint8_t bus) |
| 734 | { |
| 735 | int i, ret; |
| 736 | uint8_t val, version, revision; |
| 737 | |
| 738 | anx7625_reg_write(bus, RX_P0_ADDR, XTAL_FRQ_SEL, XTAL_FRQ_27M); |
| 739 | |
| 740 | for (i = 0; i < OCM_LOADING_TIME; i++) { |
| 741 | /* check interface */ |
| 742 | ret = anx7625_reg_read(bus, RX_P0_ADDR, FLASH_LOAD_STA, &val); |
| 743 | if (ret < 0) { |
| 744 | ANXERROR("Failed to load flash\n"); |
| 745 | return ret; |
| 746 | } |
| 747 | |
| 748 | if ((val & FLASH_LOAD_STA_CHK) != FLASH_LOAD_STA_CHK) { |
| 749 | mdelay(1); |
| 750 | continue; |
| 751 | } |
| 752 | ANXINFO("Init interface.\n"); |
| 753 | |
| 754 | |
| 755 | anx7625_disable_pd_protocol(bus); |
| 756 | anx7625_reg_read(bus, RX_P0_ADDR, OCM_FW_VERSION, &version); |
| 757 | anx7625_reg_read(bus, RX_P0_ADDR, OCM_FW_REVERSION, &revision); |
| 758 | ANXINFO("Firmware: ver %#02x, rev %#02x.\n", version, revision); |
| 759 | return 0; |
| 760 | } |
| 761 | return -1; |
| 762 | } |
| 763 | |
| 764 | static void anx7625_start_dp_work(uint8_t bus) |
| 765 | { |
| 766 | int ret; |
| 767 | uint8_t val; |
| 768 | |
| 769 | /* not support HDCP */ |
| 770 | ret = anx7625_write_and(bus, RX_P1_ADDR, 0xee, 0x9f); |
| 771 | |
| 772 | /* try auth flag */ |
| 773 | ret |= anx7625_write_or(bus, RX_P1_ADDR, 0xec, 0x10); |
| 774 | /* interrupt for DRM */ |
| 775 | ret |= anx7625_write_or(bus, RX_P1_ADDR, 0xff, 0x01); |
| 776 | if (ret < 0) |
| 777 | return; |
| 778 | |
| 779 | ret = anx7625_reg_read(bus, RX_P1_ADDR, 0x86, &val); |
| 780 | if (ret < 0) |
| 781 | return; |
| 782 | |
| 783 | ANXINFO("Secure OCM version=%02x\n", val); |
| 784 | } |
| 785 | |
| 786 | static int anx7625_hpd_change_detect(uint8_t bus) |
| 787 | { |
| 788 | int ret; |
| 789 | uint8_t status; |
| 790 | |
| 791 | ret = anx7625_reg_read(bus, RX_P0_ADDR, SYSTEM_STSTUS, &status); |
| 792 | if (ret < 0) { |
| 793 | ANXERROR("IO error: Failed to clear interrupt status.\n"); |
| 794 | return ret; |
| 795 | } |
| 796 | |
| 797 | if (status & HPD_STATUS) { |
| 798 | anx7625_start_dp_work(bus); |
| 799 | ANXINFO("HPD received 0x7e:0x45=%#x\n", status); |
| 800 | return 1; |
| 801 | } |
| 802 | return 0; |
| 803 | } |
| 804 | |
| 805 | static void anx7625_parse_edid(const struct edid *edid, |
| 806 | struct display_timing *dt) |
| 807 | { |
| 808 | dt->pixelclock = edid->mode.pixel_clock; |
| 809 | |
| 810 | dt->hactive = edid->mode.ha; |
| 811 | dt->hsync_len = edid->mode.hspw; |
| 812 | dt->hback_porch = (edid->mode.hbl - edid->mode.hso - |
| 813 | edid->mode.hborder - edid->mode.hspw); |
| 814 | dt->hfront_porch = edid->mode.hso - edid->mode.hborder; |
| 815 | |
| 816 | dt->vactive = edid->mode.va; |
| 817 | dt->vsync_len = edid->mode.vspw; |
| 818 | dt->vfront_porch = edid->mode.vso - edid->mode.vborder; |
| 819 | dt->vback_porch = (edid->mode.vbl - edid->mode.vso - |
| 820 | edid->mode.vspw - edid->mode.vborder); |
| 821 | |
| 822 | ANXINFO("pixelclock(%d).\n" |
| 823 | " hactive(%d), hsync(%d), hfp(%d), hbp(%d)\n" |
| 824 | " vactive(%d), vsync(%d), vfp(%d), vbp(%d)\n", |
| 825 | dt->pixelclock, |
| 826 | dt->hactive, dt->hsync_len, dt->hfront_porch, dt->hback_porch, |
| 827 | dt->vactive, dt->vsync_len, dt->vfront_porch, dt->vback_porch); |
| 828 | } |
| 829 | |
| 830 | int anx7625_dp_start(uint8_t bus, const struct edid *edid) |
| 831 | { |
| 832 | int ret; |
| 833 | struct display_timing dt; |
| 834 | |
| 835 | anx7625_parse_edid(edid, &dt); |
| 836 | |
| 837 | ret = anx7625_dsi_config(bus, &dt); |
| 838 | if (ret < 0) |
| 839 | ANXERROR("MIPI phy setup error.\n"); |
| 840 | else |
| 841 | ANXINFO("MIPI phy setup OK.\n"); |
| 842 | |
| 843 | return ret; |
| 844 | } |
| 845 | |
| 846 | int anx7625_dp_get_edid(uint8_t bus, struct edid *out) |
| 847 | { |
| 848 | int block_num; |
| 849 | int ret; |
| 850 | u8 edid[FOUR_BLOCK_SIZE]; |
| 851 | |
| 852 | block_num = sp_tx_edid_read(bus, edid, FOUR_BLOCK_SIZE); |
| 853 | if (block_num < 0) { |
| 854 | ANXERROR("Failed to get eDP EDID.\n"); |
| 855 | return -1; |
| 856 | } |
| 857 | |
| 858 | ret = decode_edid(edid, (block_num + 1) * ONE_BLOCK_SIZE, out); |
| 859 | if (ret != EDID_CONFORMANT) { |
| 860 | ANXERROR("Failed to decode EDID.\n"); |
| 861 | return -1; |
| 862 | } |
| 863 | |
| 864 | return 0; |
| 865 | } |
| 866 | |
| 867 | int anx7625_init(uint8_t bus) |
| 868 | { |
| 869 | int retry_hpd_change = 50; |
| 870 | int retry_power_on = 3; |
| 871 | |
| 872 | while (--retry_power_on) { |
| 873 | if (anx7625_power_on_init(bus) == 0) |
| 874 | break; |
| 875 | } |
| 876 | if (!retry_power_on) { |
| 877 | ANXERROR("Failed to power on.\n"); |
| 878 | return -1; |
| 879 | } |
| 880 | |
| 881 | while (--retry_hpd_change) { |
| 882 | mdelay(10); |
| 883 | int detected = anx7625_hpd_change_detect(bus); |
| 884 | if (detected < 0) |
| 885 | return -1; |
| 886 | if (detected > 0) |
| 887 | return 0; |
| 888 | } |
| 889 | |
| 890 | ANXERROR("Timed out to detect HPD change on bus %d.\n", bus); |
| 891 | return -1; |
| 892 | } |