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