1 /* 2 * Copyright (c) 2006 Dave Airlie <airlied@linux.ie> 3 * Copyright © 2006-2008,2010 Intel Corporation 4 * Jesse Barnes <jesse.barnes@intel.com> 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the next 14 * paragraph) shall be included in all copies or substantial portions of the 15 * Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23 * DEALINGS IN THE SOFTWARE. 24 * 25 * Authors: 26 * Eric Anholt <eric@anholt.net> 27 * Chris Wilson <chris@chris-wilson.co.uk> 28 */ 29 30 #include <linux/export.h> 31 #include <linux/i2c-algo-bit.h> 32 #include <linux/i2c.h> 33 #include <linux/iopoll.h> 34 35 #include <drm/drm_print.h> 36 #include <drm/display/drm_hdcp_helper.h> 37 38 #include "i915_reg.h" 39 #include "intel_de.h" 40 #include "intel_display_regs.h" 41 #include "intel_display_types.h" 42 #include "intel_display_wa.h" 43 #include "intel_gmbus.h" 44 #include "intel_gmbus_regs.h" 45 #include "intel_parent.h" 46 47 struct intel_gmbus { 48 struct i2c_adapter adapter; 49 #define GMBUS_FORCE_BIT_RETRY (1U << 31) 50 u32 force_bit; 51 u32 reg0; 52 i915_reg_t gpio_reg; 53 struct i2c_algo_bit_data bit_algo; 54 struct intel_display *display; 55 }; 56 57 enum gmbus_gpio { 58 GPIOA, 59 GPIOB, 60 GPIOC, 61 GPIOD, 62 GPIOE, 63 GPIOF, 64 GPIOG, 65 GPIOH, 66 __GPIOI_UNUSED, 67 GPIOJ, 68 GPIOK, 69 GPIOL, 70 GPIOM, 71 GPION, 72 GPIOO, 73 }; 74 75 struct gmbus_pin { 76 const char *name; 77 enum gmbus_gpio gpio; 78 }; 79 80 /* Map gmbus pin pairs to names and registers. */ 81 static const struct gmbus_pin gmbus_pins[] = { 82 [GMBUS_PIN_SSC] = { "ssc", GPIOB }, 83 [GMBUS_PIN_VGADDC] = { "vga", GPIOA }, 84 [GMBUS_PIN_PANEL] = { "panel", GPIOC }, 85 [GMBUS_PIN_DPC] = { "dpc", GPIOD }, 86 [GMBUS_PIN_DPB] = { "dpb", GPIOE }, 87 [GMBUS_PIN_DPD] = { "dpd", GPIOF }, 88 }; 89 90 static const struct gmbus_pin gmbus_pins_bdw[] = { 91 [GMBUS_PIN_VGADDC] = { "vga", GPIOA }, 92 [GMBUS_PIN_DPC] = { "dpc", GPIOD }, 93 [GMBUS_PIN_DPB] = { "dpb", GPIOE }, 94 [GMBUS_PIN_DPD] = { "dpd", GPIOF }, 95 }; 96 97 static const struct gmbus_pin gmbus_pins_skl[] = { 98 [GMBUS_PIN_DPC] = { "dpc", GPIOD }, 99 [GMBUS_PIN_DPB] = { "dpb", GPIOE }, 100 [GMBUS_PIN_DPD] = { "dpd", GPIOF }, 101 }; 102 103 static const struct gmbus_pin gmbus_pins_bxt[] = { 104 [GMBUS_PIN_1_BXT] = { "dpb", GPIOB }, 105 [GMBUS_PIN_2_BXT] = { "dpc", GPIOC }, 106 [GMBUS_PIN_3_BXT] = { "misc", GPIOD }, 107 }; 108 109 static const struct gmbus_pin gmbus_pins_cnp[] = { 110 [GMBUS_PIN_1_BXT] = { "dpb", GPIOB }, 111 [GMBUS_PIN_2_BXT] = { "dpc", GPIOC }, 112 [GMBUS_PIN_3_BXT] = { "misc", GPIOD }, 113 [GMBUS_PIN_4_CNP] = { "dpd", GPIOE }, 114 }; 115 116 static const struct gmbus_pin gmbus_pins_icp[] = { 117 [GMBUS_PIN_1_BXT] = { "dpa", GPIOB }, 118 [GMBUS_PIN_2_BXT] = { "dpb", GPIOC }, 119 [GMBUS_PIN_3_BXT] = { "dpc", GPIOD }, 120 [GMBUS_PIN_9_TC1_ICP] = { "tc1", GPIOJ }, 121 [GMBUS_PIN_10_TC2_ICP] = { "tc2", GPIOK }, 122 [GMBUS_PIN_11_TC3_ICP] = { "tc3", GPIOL }, 123 [GMBUS_PIN_12_TC4_ICP] = { "tc4", GPIOM }, 124 [GMBUS_PIN_13_TC5_TGP] = { "tc5", GPION }, 125 [GMBUS_PIN_14_TC6_TGP] = { "tc6", GPIOO }, 126 }; 127 128 static const struct gmbus_pin gmbus_pins_dg1[] = { 129 [GMBUS_PIN_1_BXT] = { "dpa", GPIOB }, 130 [GMBUS_PIN_2_BXT] = { "dpb", GPIOC }, 131 [GMBUS_PIN_3_BXT] = { "dpc", GPIOD }, 132 [GMBUS_PIN_4_CNP] = { "dpd", GPIOE }, 133 }; 134 135 static const struct gmbus_pin gmbus_pins_dg2[] = { 136 [GMBUS_PIN_1_BXT] = { "dpa", GPIOB }, 137 [GMBUS_PIN_2_BXT] = { "dpb", GPIOC }, 138 [GMBUS_PIN_3_BXT] = { "dpc", GPIOD }, 139 [GMBUS_PIN_4_CNP] = { "dpd", GPIOE }, 140 [GMBUS_PIN_9_TC1_ICP] = { "tc1", GPIOJ }, 141 }; 142 143 static const struct gmbus_pin gmbus_pins_mtp[] = { 144 [GMBUS_PIN_1_BXT] = { "dpa", GPIOB }, 145 [GMBUS_PIN_2_BXT] = { "dpb", GPIOC }, 146 [GMBUS_PIN_3_BXT] = { "dpc", GPIOD }, 147 [GMBUS_PIN_4_CNP] = { "dpd", GPIOE }, 148 [GMBUS_PIN_5_MTP] = { "dpe", GPIOF }, 149 [GMBUS_PIN_9_TC1_ICP] = { "tc1", GPIOJ }, 150 [GMBUS_PIN_10_TC2_ICP] = { "tc2", GPIOK }, 151 [GMBUS_PIN_11_TC3_ICP] = { "tc3", GPIOL }, 152 [GMBUS_PIN_12_TC4_ICP] = { "tc4", GPIOM }, 153 }; 154 155 static const struct gmbus_pin *get_gmbus_pin(struct intel_display *display, 156 unsigned int pin) 157 { 158 const struct gmbus_pin *pins; 159 size_t size; 160 161 if (INTEL_PCH_TYPE(display) >= PCH_MTL) { 162 pins = gmbus_pins_mtp; 163 size = ARRAY_SIZE(gmbus_pins_mtp); 164 } else if (INTEL_PCH_TYPE(display) >= PCH_DG2) { 165 pins = gmbus_pins_dg2; 166 size = ARRAY_SIZE(gmbus_pins_dg2); 167 } else if (INTEL_PCH_TYPE(display) >= PCH_DG1) { 168 pins = gmbus_pins_dg1; 169 size = ARRAY_SIZE(gmbus_pins_dg1); 170 } else if (INTEL_PCH_TYPE(display) >= PCH_ICP) { 171 pins = gmbus_pins_icp; 172 size = ARRAY_SIZE(gmbus_pins_icp); 173 } else if (HAS_PCH_CNP(display)) { 174 pins = gmbus_pins_cnp; 175 size = ARRAY_SIZE(gmbus_pins_cnp); 176 } else if (display->platform.geminilake || display->platform.broxton) { 177 pins = gmbus_pins_bxt; 178 size = ARRAY_SIZE(gmbus_pins_bxt); 179 } else if (DISPLAY_VER(display) == 9) { 180 pins = gmbus_pins_skl; 181 size = ARRAY_SIZE(gmbus_pins_skl); 182 } else if (display->platform.broadwell) { 183 pins = gmbus_pins_bdw; 184 size = ARRAY_SIZE(gmbus_pins_bdw); 185 } else { 186 pins = gmbus_pins; 187 size = ARRAY_SIZE(gmbus_pins); 188 } 189 190 if (pin >= size || !pins[pin].name) 191 return NULL; 192 193 return &pins[pin]; 194 } 195 196 bool intel_gmbus_is_valid_pin(struct intel_display *display, unsigned int pin) 197 { 198 return get_gmbus_pin(display, pin); 199 } 200 201 /* Intel GPIO access functions */ 202 203 #define I2C_RISEFALL_TIME 10 204 205 static inline struct intel_gmbus * 206 to_intel_gmbus(struct i2c_adapter *i2c) 207 { 208 return container_of(i2c, struct intel_gmbus, adapter); 209 } 210 211 void 212 intel_gmbus_reset(struct intel_display *display) 213 { 214 intel_de_write(display, GMBUS0(display), 0); 215 intel_de_write(display, GMBUS4(display), 0); 216 } 217 218 static void pnv_gmbus_clock_gating(struct intel_display *display, 219 bool enable) 220 { 221 /* When using bit bashing for I2C, this bit needs to be set to 1 */ 222 intel_de_rmw(display, DSPCLK_GATE_D, 223 PNV_GMBUSUNIT_CLOCK_GATE_DISABLE, 224 !enable ? PNV_GMBUSUNIT_CLOCK_GATE_DISABLE : 0); 225 } 226 227 static void pch_gmbus_clock_gating(struct intel_display *display, 228 bool enable) 229 { 230 intel_de_rmw(display, SOUTH_DSPCLK_GATE_D, 231 PCH_GMBUSUNIT_CLOCK_GATE_DISABLE, 232 !enable ? PCH_GMBUSUNIT_CLOCK_GATE_DISABLE : 0); 233 } 234 235 static void bxt_gmbus_clock_gating(struct intel_display *display, 236 bool enable) 237 { 238 intel_de_rmw(display, GEN9_CLKGATE_DIS_4, BXT_GMBUS_GATING_DIS, 239 !enable ? BXT_GMBUS_GATING_DIS : 0); 240 } 241 242 static u32 get_reserved(struct intel_gmbus *bus) 243 { 244 struct intel_display *display = bus->display; 245 u32 preserve_bits = 0; 246 247 if (display->platform.i830 || display->platform.i845g) 248 return 0; 249 250 /* On most chips, these bits must be preserved in software. */ 251 preserve_bits |= GPIO_DATA_PULLUP_DISABLE | GPIO_CLOCK_PULLUP_DISABLE; 252 253 /* Wa_16025573575: the masks bits need to be preserved through out */ 254 if (intel_display_wa(display, 16025573575)) 255 preserve_bits |= GPIO_CLOCK_DIR_MASK | GPIO_CLOCK_VAL_MASK | 256 GPIO_DATA_DIR_MASK | GPIO_DATA_VAL_MASK; 257 258 return intel_de_read_notrace(display, bus->gpio_reg) & preserve_bits; 259 } 260 261 static int get_clock(void *data) 262 { 263 struct intel_gmbus *bus = data; 264 struct intel_display *display = bus->display; 265 u32 reserved = get_reserved(bus); 266 267 intel_de_write_notrace(display, bus->gpio_reg, reserved | GPIO_CLOCK_DIR_MASK); 268 intel_de_write_notrace(display, bus->gpio_reg, reserved); 269 270 return (intel_de_read_notrace(display, bus->gpio_reg) & GPIO_CLOCK_VAL_IN) != 0; 271 } 272 273 static int get_data(void *data) 274 { 275 struct intel_gmbus *bus = data; 276 struct intel_display *display = bus->display; 277 u32 reserved = get_reserved(bus); 278 279 intel_de_write_notrace(display, bus->gpio_reg, reserved | GPIO_DATA_DIR_MASK); 280 intel_de_write_notrace(display, bus->gpio_reg, reserved); 281 282 return (intel_de_read_notrace(display, bus->gpio_reg) & GPIO_DATA_VAL_IN) != 0; 283 } 284 285 static void set_clock(void *data, int state_high) 286 { 287 struct intel_gmbus *bus = data; 288 struct intel_display *display = bus->display; 289 u32 reserved = get_reserved(bus); 290 u32 clock_bits; 291 292 if (state_high) 293 clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK; 294 else 295 clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK | 296 GPIO_CLOCK_VAL_MASK; 297 298 intel_de_write_notrace(display, bus->gpio_reg, reserved | clock_bits); 299 intel_de_posting_read(display, bus->gpio_reg); 300 } 301 302 static void set_data(void *data, int state_high) 303 { 304 struct intel_gmbus *bus = data; 305 struct intel_display *display = bus->display; 306 u32 reserved = get_reserved(bus); 307 u32 data_bits; 308 309 if (state_high) 310 data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK; 311 else 312 data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK | 313 GPIO_DATA_VAL_MASK; 314 315 intel_de_write_notrace(display, bus->gpio_reg, reserved | data_bits); 316 intel_de_posting_read(display, bus->gpio_reg); 317 } 318 319 static void 320 ptl_handle_mask_bits(struct intel_gmbus *bus, bool set) 321 { 322 struct intel_display *display = bus->display; 323 u32 reg_val = intel_de_read_notrace(display, bus->gpio_reg); 324 u32 mask_bits = GPIO_CLOCK_DIR_MASK | GPIO_CLOCK_VAL_MASK | 325 GPIO_DATA_DIR_MASK | GPIO_DATA_VAL_MASK; 326 if (set) 327 reg_val |= mask_bits; 328 else 329 reg_val &= ~mask_bits; 330 331 intel_de_write_notrace(display, bus->gpio_reg, reg_val); 332 intel_de_posting_read(display, bus->gpio_reg); 333 } 334 335 static int 336 intel_gpio_pre_xfer(struct i2c_adapter *adapter) 337 { 338 struct intel_gmbus *bus = to_intel_gmbus(adapter); 339 struct intel_display *display = bus->display; 340 341 intel_gmbus_reset(display); 342 343 if (display->platform.pineview) 344 pnv_gmbus_clock_gating(display, false); 345 346 if (intel_display_wa(display, 16025573575)) 347 ptl_handle_mask_bits(bus, true); 348 349 set_data(bus, 1); 350 set_clock(bus, 1); 351 udelay(I2C_RISEFALL_TIME); 352 return 0; 353 } 354 355 static void 356 intel_gpio_post_xfer(struct i2c_adapter *adapter) 357 { 358 struct intel_gmbus *bus = to_intel_gmbus(adapter); 359 struct intel_display *display = bus->display; 360 361 set_data(bus, 1); 362 set_clock(bus, 1); 363 364 if (display->platform.pineview) 365 pnv_gmbus_clock_gating(display, true); 366 367 if (intel_display_wa(display, 16025573575)) 368 ptl_handle_mask_bits(bus, false); 369 } 370 371 static void 372 intel_gpio_setup(struct intel_gmbus *bus, i915_reg_t gpio_reg) 373 { 374 struct i2c_algo_bit_data *algo; 375 376 algo = &bus->bit_algo; 377 378 bus->gpio_reg = gpio_reg; 379 bus->adapter.algo_data = algo; 380 algo->setsda = set_data; 381 algo->setscl = set_clock; 382 algo->getsda = get_data; 383 algo->getscl = get_clock; 384 algo->pre_xfer = intel_gpio_pre_xfer; 385 algo->post_xfer = intel_gpio_post_xfer; 386 algo->udelay = I2C_RISEFALL_TIME; 387 algo->timeout = usecs_to_jiffies(2200); 388 algo->data = bus; 389 } 390 391 static bool has_gmbus_irq(struct intel_display *display) 392 { 393 /* 394 * encoder->shutdown() may want to use GMBUS 395 * after irqs have already been disabled. 396 */ 397 return HAS_GMBUS_IRQ(display) && intel_parent_irq_enabled(display); 398 } 399 400 static int gmbus_wait(struct intel_display *display, u32 status, u32 irq_en) 401 { 402 DEFINE_WAIT(wait); 403 u32 gmbus2; 404 int ret; 405 406 /* Important: The hw handles only the first bit, so set only one! Since 407 * we also need to check for NAKs besides the hw ready/idle signal, we 408 * need to wake up periodically and check that ourselves. 409 */ 410 if (!has_gmbus_irq(display)) 411 irq_en = 0; 412 413 add_wait_queue(&display->gmbus.wait_queue, &wait); 414 intel_de_write_fw(display, GMBUS4(display), irq_en); 415 416 status |= GMBUS_SATOER; 417 418 ret = poll_timeout_us_atomic(gmbus2 = intel_de_read_fw(display, GMBUS2(display)), 419 gmbus2 & status, 420 0, 2, false); 421 if (ret) 422 ret = poll_timeout_us(gmbus2 = intel_de_read_fw(display, GMBUS2(display)), 423 gmbus2 & status, 424 500, 50 * 1000, false); 425 426 intel_de_write_fw(display, GMBUS4(display), 0); 427 remove_wait_queue(&display->gmbus.wait_queue, &wait); 428 429 if (gmbus2 & GMBUS_SATOER) 430 return -ENXIO; 431 432 return ret; 433 } 434 435 static int 436 gmbus_wait_idle(struct intel_display *display) 437 { 438 DEFINE_WAIT(wait); 439 u32 irq_enable; 440 int ret; 441 442 /* Important: The hw handles only the first bit, so set only one! */ 443 irq_enable = 0; 444 if (has_gmbus_irq(display)) 445 irq_enable = GMBUS_IDLE_EN; 446 447 add_wait_queue(&display->gmbus.wait_queue, &wait); 448 intel_de_write_fw(display, GMBUS4(display), irq_enable); 449 450 ret = intel_de_wait_fw_ms(display, GMBUS2(display), GMBUS_ACTIVE, 0, 10, NULL); 451 452 intel_de_write_fw(display, GMBUS4(display), 0); 453 remove_wait_queue(&display->gmbus.wait_queue, &wait); 454 455 return ret; 456 } 457 458 static unsigned int gmbus_max_xfer_size(struct intel_display *display) 459 { 460 return DISPLAY_VER(display) >= 9 ? GEN9_GMBUS_BYTE_COUNT_MAX : 461 GMBUS_BYTE_COUNT_MAX; 462 } 463 464 static int 465 gmbus_xfer_read_chunk(struct intel_display *display, 466 unsigned short addr, u8 *buf, unsigned int len, 467 u32 gmbus0_reg, u32 gmbus1_index) 468 { 469 unsigned int size = len; 470 bool burst_read = len > gmbus_max_xfer_size(display); 471 bool extra_byte_added = false; 472 473 if (burst_read) { 474 /* 475 * As per HW Spec, for 512Bytes need to read extra Byte and 476 * Ignore the extra byte read. 477 */ 478 if (len == 512) { 479 extra_byte_added = true; 480 len++; 481 } 482 size = len % 256 + 256; 483 intel_de_write_fw(display, GMBUS0(display), 484 gmbus0_reg | GMBUS_BYTE_CNT_OVERRIDE); 485 } 486 487 intel_de_write_fw(display, GMBUS1(display), 488 gmbus1_index | GMBUS_CYCLE_WAIT | (size << GMBUS_BYTE_COUNT_SHIFT) | (addr << GMBUS_SLAVE_ADDR_SHIFT) | GMBUS_SLAVE_READ | GMBUS_SW_RDY); 489 while (len) { 490 int ret; 491 u32 val, loop = 0; 492 493 ret = gmbus_wait(display, GMBUS_HW_RDY, GMBUS_HW_RDY_EN); 494 if (ret) 495 return ret; 496 497 val = intel_de_read_fw(display, GMBUS3(display)); 498 do { 499 if (extra_byte_added && len == 1) 500 break; 501 502 *buf++ = val & 0xff; 503 val >>= 8; 504 } while (--len && ++loop < 4); 505 506 if (burst_read && len == size - 4) 507 /* Reset the override bit */ 508 intel_de_write_fw(display, GMBUS0(display), gmbus0_reg); 509 } 510 511 return 0; 512 } 513 514 /* 515 * HW spec says that 512Bytes in Burst read need special treatment. 516 * But it doesn't talk about other multiple of 256Bytes. And couldn't locate 517 * an I2C target, which supports such a lengthy burst read too for experiments. 518 * 519 * So until things get clarified on HW support, to avoid the burst read length 520 * in fold of 256Bytes except 512, max burst read length is fixed at 767Bytes. 521 */ 522 #define INTEL_GMBUS_BURST_READ_MAX_LEN 767U 523 524 static int 525 gmbus_xfer_read(struct intel_display *display, struct i2c_msg *msg, 526 u32 gmbus0_reg, u32 gmbus1_index) 527 { 528 u8 *buf = msg->buf; 529 unsigned int rx_size = msg->len; 530 unsigned int len; 531 int ret; 532 533 do { 534 if (HAS_GMBUS_BURST_READ(display)) 535 len = min(rx_size, INTEL_GMBUS_BURST_READ_MAX_LEN); 536 else 537 len = min(rx_size, gmbus_max_xfer_size(display)); 538 539 ret = gmbus_xfer_read_chunk(display, msg->addr, buf, len, 540 gmbus0_reg, gmbus1_index); 541 if (ret) 542 return ret; 543 544 rx_size -= len; 545 buf += len; 546 } while (rx_size != 0); 547 548 return 0; 549 } 550 551 static int 552 gmbus_xfer_write_chunk(struct intel_display *display, 553 unsigned short addr, u8 *buf, unsigned int len, 554 u32 gmbus1_index) 555 { 556 unsigned int chunk_size = len; 557 u32 val, loop; 558 559 val = loop = 0; 560 while (len && loop < 4) { 561 val |= *buf++ << (8 * loop++); 562 len -= 1; 563 } 564 565 intel_de_write_fw(display, GMBUS3(display), val); 566 intel_de_write_fw(display, GMBUS1(display), 567 gmbus1_index | GMBUS_CYCLE_WAIT | (chunk_size << GMBUS_BYTE_COUNT_SHIFT) | (addr << GMBUS_SLAVE_ADDR_SHIFT) | GMBUS_SLAVE_WRITE | GMBUS_SW_RDY); 568 while (len) { 569 int ret; 570 571 val = loop = 0; 572 do { 573 val |= *buf++ << (8 * loop); 574 } while (--len && ++loop < 4); 575 576 intel_de_write_fw(display, GMBUS3(display), val); 577 578 ret = gmbus_wait(display, GMBUS_HW_RDY, GMBUS_HW_RDY_EN); 579 if (ret) 580 return ret; 581 } 582 583 return 0; 584 } 585 586 static int 587 gmbus_xfer_write(struct intel_display *display, struct i2c_msg *msg, 588 u32 gmbus1_index) 589 { 590 u8 *buf = msg->buf; 591 unsigned int tx_size = msg->len; 592 unsigned int len; 593 int ret; 594 595 do { 596 len = min(tx_size, gmbus_max_xfer_size(display)); 597 598 ret = gmbus_xfer_write_chunk(display, msg->addr, buf, len, 599 gmbus1_index); 600 if (ret) 601 return ret; 602 603 buf += len; 604 tx_size -= len; 605 } while (tx_size != 0); 606 607 return 0; 608 } 609 610 /* 611 * The gmbus controller can combine a 1 or 2 byte write with another read/write 612 * that immediately follows it by using an "INDEX" cycle. 613 */ 614 static bool 615 gmbus_is_index_xfer(struct i2c_msg *msgs, int i, int num) 616 { 617 return (i + 1 < num && 618 msgs[i].addr == msgs[i + 1].addr && 619 !(msgs[i].flags & I2C_M_RD) && 620 (msgs[i].len == 1 || msgs[i].len == 2) && 621 msgs[i + 1].len > 0); 622 } 623 624 static int 625 gmbus_index_xfer(struct intel_display *display, struct i2c_msg *msgs, 626 u32 gmbus0_reg) 627 { 628 u32 gmbus1_index = 0; 629 u32 gmbus5 = 0; 630 int ret; 631 632 if (msgs[0].len == 2) 633 gmbus5 = GMBUS_2BYTE_INDEX_EN | 634 msgs[0].buf[1] | (msgs[0].buf[0] << 8); 635 if (msgs[0].len == 1) 636 gmbus1_index = GMBUS_CYCLE_INDEX | 637 (msgs[0].buf[0] << GMBUS_SLAVE_INDEX_SHIFT); 638 639 /* GMBUS5 holds 16-bit index */ 640 if (gmbus5) 641 intel_de_write_fw(display, GMBUS5(display), gmbus5); 642 643 if (msgs[1].flags & I2C_M_RD) 644 ret = gmbus_xfer_read(display, &msgs[1], gmbus0_reg, 645 gmbus1_index); 646 else 647 ret = gmbus_xfer_write(display, &msgs[1], gmbus1_index); 648 649 /* Clear GMBUS5 after each index transfer */ 650 if (gmbus5) 651 intel_de_write_fw(display, GMBUS5(display), 0); 652 653 return ret; 654 } 655 656 static int 657 do_gmbus_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num, 658 u32 gmbus0_source) 659 { 660 struct intel_gmbus *bus = to_intel_gmbus(adapter); 661 struct intel_display *display = bus->display; 662 int i = 0, inc, try = 0; 663 int ret = 0; 664 665 /* Display WA #0868: skl,bxt,kbl,cfl,glk */ 666 if (display->platform.geminilake || display->platform.broxton) 667 bxt_gmbus_clock_gating(display, false); 668 else if (HAS_PCH_SPT(display) || HAS_PCH_CNP(display)) 669 pch_gmbus_clock_gating(display, false); 670 671 retry: 672 intel_de_write_fw(display, GMBUS0(display), gmbus0_source | bus->reg0); 673 674 for (; i < num; i += inc) { 675 inc = 1; 676 if (gmbus_is_index_xfer(msgs, i, num)) { 677 ret = gmbus_index_xfer(display, &msgs[i], 678 gmbus0_source | bus->reg0); 679 inc = 2; /* an index transmission is two msgs */ 680 } else if (msgs[i].flags & I2C_M_RD) { 681 ret = gmbus_xfer_read(display, &msgs[i], 682 gmbus0_source | bus->reg0, 0); 683 } else { 684 ret = gmbus_xfer_write(display, &msgs[i], 0); 685 } 686 687 if (!ret) 688 ret = gmbus_wait(display, 689 GMBUS_HW_WAIT_PHASE, GMBUS_HW_WAIT_EN); 690 if (ret == -ETIMEDOUT) 691 goto timeout; 692 else if (ret) 693 goto clear_err; 694 } 695 696 /* Generate a STOP condition on the bus. Note that gmbus can't generata 697 * a STOP on the very first cycle. To simplify the code we 698 * unconditionally generate the STOP condition with an additional gmbus 699 * cycle. */ 700 intel_de_write_fw(display, GMBUS1(display), GMBUS_CYCLE_STOP | GMBUS_SW_RDY); 701 702 /* Mark the GMBUS interface as disabled after waiting for idle. 703 * We will re-enable it at the start of the next xfer, 704 * till then let it sleep. 705 */ 706 if (gmbus_wait_idle(display)) { 707 drm_dbg_kms(display->drm, 708 "GMBUS [%s] timed out waiting for idle\n", 709 adapter->name); 710 ret = -ETIMEDOUT; 711 } 712 intel_de_write_fw(display, GMBUS0(display), 0); 713 ret = ret ?: i; 714 goto out; 715 716 clear_err: 717 /* 718 * Wait for bus to IDLE before clearing NAK. 719 * If we clear the NAK while bus is still active, then it will stay 720 * active and the next transaction may fail. 721 * 722 * If no ACK is received during the address phase of a transaction, the 723 * adapter must report -ENXIO. It is not clear what to return if no ACK 724 * is received at other times. But we have to be careful to not return 725 * spurious -ENXIO because that will prevent i2c and drm edid functions 726 * from retrying. So return -ENXIO only when gmbus properly quiescents - 727 * timing out seems to happen when there _is_ a ddc chip present, but 728 * it's slow responding and only answers on the 2nd retry. 729 */ 730 ret = -ENXIO; 731 if (gmbus_wait_idle(display)) { 732 drm_dbg_kms(display->drm, 733 "GMBUS [%s] timed out after NAK\n", 734 adapter->name); 735 ret = -ETIMEDOUT; 736 } 737 738 /* Toggle the Software Clear Interrupt bit. This has the effect 739 * of resetting the GMBUS controller and so clearing the 740 * BUS_ERROR raised by the target's NAK. 741 */ 742 intel_de_write_fw(display, GMBUS1(display), GMBUS_SW_CLR_INT); 743 intel_de_write_fw(display, GMBUS1(display), 0); 744 intel_de_write_fw(display, GMBUS0(display), 0); 745 746 drm_dbg_kms(display->drm, "GMBUS [%s] NAK for addr: %04x %c(%d)\n", 747 adapter->name, msgs[i].addr, 748 (msgs[i].flags & I2C_M_RD) ? 'r' : 'w', msgs[i].len); 749 750 /* 751 * Passive adapters sometimes NAK the first probe. Retry the first 752 * message once on -ENXIO for GMBUS transfers; the bit banging algorithm 753 * has retries internally. See also the retry loop in 754 * drm_do_probe_ddc_edid, which bails out on the first -ENXIO. 755 */ 756 if (ret == -ENXIO && i == 0 && try++ == 0) { 757 drm_dbg_kms(display->drm, 758 "GMBUS [%s] NAK on first message, retry\n", 759 adapter->name); 760 goto retry; 761 } 762 763 goto out; 764 765 timeout: 766 drm_dbg_kms(display->drm, 767 "GMBUS [%s] timed out, falling back to bit banging on pin %d\n", 768 bus->adapter.name, bus->reg0 & 0xff); 769 intel_de_write_fw(display, GMBUS0(display), 0); 770 771 /* 772 * Hardware may not support GMBUS over these pins? Try GPIO bitbanging 773 * instead. Use EAGAIN to have i2c core retry. 774 */ 775 ret = -EAGAIN; 776 777 out: 778 /* Display WA #0868: skl,bxt,kbl,cfl,glk */ 779 if (display->platform.geminilake || display->platform.broxton) 780 bxt_gmbus_clock_gating(display, true); 781 else if (HAS_PCH_SPT(display) || HAS_PCH_CNP(display)) 782 pch_gmbus_clock_gating(display, true); 783 784 return ret; 785 } 786 787 static int 788 gmbus_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num) 789 { 790 struct intel_gmbus *bus = to_intel_gmbus(adapter); 791 struct intel_display *display = bus->display; 792 struct ref_tracker *wakeref; 793 int ret; 794 795 wakeref = intel_display_power_get(display, POWER_DOMAIN_GMBUS); 796 797 if (bus->force_bit) { 798 ret = i2c_bit_algo.master_xfer(adapter, msgs, num); 799 if (ret < 0) 800 bus->force_bit &= ~GMBUS_FORCE_BIT_RETRY; 801 } else { 802 ret = do_gmbus_xfer(adapter, msgs, num, 0); 803 if (ret == -EAGAIN) 804 bus->force_bit |= GMBUS_FORCE_BIT_RETRY; 805 } 806 807 intel_display_power_put(display, POWER_DOMAIN_GMBUS, wakeref); 808 809 return ret; 810 } 811 812 int intel_gmbus_output_aksv(struct i2c_adapter *adapter) 813 { 814 struct intel_gmbus *bus = to_intel_gmbus(adapter); 815 struct intel_display *display = bus->display; 816 u8 cmd = DRM_HDCP_DDC_AKSV; 817 u8 buf[DRM_HDCP_KSV_LEN] = {}; 818 struct i2c_msg msgs[] = { 819 { 820 .addr = DRM_HDCP_DDC_ADDR, 821 .flags = 0, 822 .len = sizeof(cmd), 823 .buf = &cmd, 824 }, 825 { 826 .addr = DRM_HDCP_DDC_ADDR, 827 .flags = 0, 828 .len = sizeof(buf), 829 .buf = buf, 830 } 831 }; 832 struct ref_tracker *wakeref; 833 int ret; 834 835 wakeref = intel_display_power_get(display, POWER_DOMAIN_GMBUS); 836 mutex_lock(&display->gmbus.mutex); 837 838 /* 839 * In order to output Aksv to the receiver, use an indexed write to 840 * pass the i2c command, and tell GMBUS to use the HW-provided value 841 * instead of sourcing GMBUS3 for the data. 842 */ 843 ret = do_gmbus_xfer(adapter, msgs, ARRAY_SIZE(msgs), GMBUS_AKSV_SELECT); 844 845 mutex_unlock(&display->gmbus.mutex); 846 intel_display_power_put(display, POWER_DOMAIN_GMBUS, wakeref); 847 848 return ret; 849 } 850 851 static u32 gmbus_func(struct i2c_adapter *adapter) 852 { 853 return i2c_bit_algo.functionality(adapter) & 854 (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | 855 /* I2C_FUNC_10BIT_ADDR | */ 856 I2C_FUNC_SMBUS_READ_BLOCK_DATA | 857 I2C_FUNC_SMBUS_BLOCK_PROC_CALL); 858 } 859 860 static const struct i2c_algorithm gmbus_algorithm = { 861 .master_xfer = gmbus_xfer, 862 .functionality = gmbus_func 863 }; 864 865 static void gmbus_lock_bus(struct i2c_adapter *adapter, 866 unsigned int flags) 867 { 868 struct intel_gmbus *bus = to_intel_gmbus(adapter); 869 struct intel_display *display = bus->display; 870 871 mutex_lock(&display->gmbus.mutex); 872 } 873 874 static int gmbus_trylock_bus(struct i2c_adapter *adapter, 875 unsigned int flags) 876 { 877 struct intel_gmbus *bus = to_intel_gmbus(adapter); 878 struct intel_display *display = bus->display; 879 880 return mutex_trylock(&display->gmbus.mutex); 881 } 882 883 static void gmbus_unlock_bus(struct i2c_adapter *adapter, 884 unsigned int flags) 885 { 886 struct intel_gmbus *bus = to_intel_gmbus(adapter); 887 struct intel_display *display = bus->display; 888 889 mutex_unlock(&display->gmbus.mutex); 890 } 891 892 static const struct i2c_lock_operations gmbus_lock_ops = { 893 .lock_bus = gmbus_lock_bus, 894 .trylock_bus = gmbus_trylock_bus, 895 .unlock_bus = gmbus_unlock_bus, 896 }; 897 898 /** 899 * intel_gmbus_setup - instantiate all Intel i2c GMBuses 900 * @display: display device 901 */ 902 int intel_gmbus_setup(struct intel_display *display) 903 { 904 struct pci_dev *pdev = to_pci_dev(display->drm->dev); 905 unsigned int pin; 906 int ret; 907 908 if (display->platform.valleyview || display->platform.cherryview) 909 display->gmbus.mmio_base = VLV_DISPLAY_BASE; 910 else if (!HAS_GMCH(display)) 911 /* 912 * Broxton uses the same PCH offsets for South Display Engine, 913 * even though it doesn't have a PCH. 914 */ 915 display->gmbus.mmio_base = PCH_DISPLAY_BASE; 916 917 mutex_init(&display->gmbus.mutex); 918 init_waitqueue_head(&display->gmbus.wait_queue); 919 920 for (pin = 0; pin < ARRAY_SIZE(display->gmbus.bus); pin++) { 921 const struct gmbus_pin *gmbus_pin; 922 struct intel_gmbus *bus; 923 924 gmbus_pin = get_gmbus_pin(display, pin); 925 if (!gmbus_pin) 926 continue; 927 928 bus = kzalloc_obj(*bus); 929 if (!bus) { 930 ret = -ENOMEM; 931 goto err; 932 } 933 934 bus->adapter.owner = THIS_MODULE; 935 snprintf(bus->adapter.name, 936 sizeof(bus->adapter.name), 937 "i915 gmbus %s", gmbus_pin->name); 938 939 bus->adapter.dev.parent = &pdev->dev; 940 bus->display = display; 941 942 bus->adapter.algo = &gmbus_algorithm; 943 bus->adapter.lock_ops = &gmbus_lock_ops; 944 945 /* 946 * We wish to retry with bit banging 947 * after a timed out GMBUS attempt. 948 */ 949 bus->adapter.retries = 1; 950 951 /* By default use a conservative clock rate */ 952 bus->reg0 = pin | GMBUS_RATE_100KHZ; 953 954 /* gmbus seems to be broken on i830 */ 955 if (display->platform.i830) 956 bus->force_bit = 1; 957 958 intel_gpio_setup(bus, GPIO(display, gmbus_pin->gpio)); 959 960 ret = i2c_add_adapter(&bus->adapter); 961 if (ret) { 962 kfree(bus); 963 goto err; 964 } 965 966 display->gmbus.bus[pin] = bus; 967 } 968 969 intel_gmbus_reset(display); 970 971 return 0; 972 973 err: 974 intel_gmbus_teardown(display); 975 976 return ret; 977 } 978 979 struct i2c_adapter *intel_gmbus_get_adapter(struct intel_display *display, 980 unsigned int pin) 981 { 982 if (drm_WARN_ON(display->drm, pin >= ARRAY_SIZE(display->gmbus.bus) || 983 !display->gmbus.bus[pin])) 984 return NULL; 985 986 return &display->gmbus.bus[pin]->adapter; 987 } 988 989 void intel_gmbus_force_bit(struct i2c_adapter *adapter, bool force_bit) 990 { 991 struct intel_gmbus *bus = to_intel_gmbus(adapter); 992 struct intel_display *display = bus->display; 993 994 mutex_lock(&display->gmbus.mutex); 995 996 bus->force_bit += force_bit ? 1 : -1; 997 drm_dbg_kms(display->drm, 998 "%sabling bit-banging on %s. force bit now %d\n", 999 force_bit ? "en" : "dis", adapter->name, 1000 bus->force_bit); 1001 1002 mutex_unlock(&display->gmbus.mutex); 1003 } 1004 1005 bool intel_gmbus_is_forced_bit(struct i2c_adapter *adapter) 1006 { 1007 struct intel_gmbus *bus = to_intel_gmbus(adapter); 1008 1009 return bus->force_bit; 1010 } 1011 1012 void intel_gmbus_teardown(struct intel_display *display) 1013 { 1014 unsigned int pin; 1015 1016 for (pin = 0; pin < ARRAY_SIZE(display->gmbus.bus); pin++) { 1017 struct intel_gmbus *bus; 1018 1019 bus = display->gmbus.bus[pin]; 1020 if (!bus) 1021 continue; 1022 1023 i2c_del_adapter(&bus->adapter); 1024 1025 kfree(bus); 1026 display->gmbus.bus[pin] = NULL; 1027 } 1028 } 1029 1030 void intel_gmbus_irq_handler(struct intel_display *display) 1031 { 1032 wake_up_all(&display->gmbus.wait_queue); 1033 } 1034