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 * Copyright (c) 2011 The FreeBSD Foundation 30 * All rights reserved. 31 * 32 * This software was developed by Konstantin Belousov under sponsorship from 33 * the FreeBSD Foundation. 34 * 35 * Redistribution and use in source and binary forms, with or without 36 * modification, are permitted provided that the following conditions 37 * are met: 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 47 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 54 * SUCH DAMAGE. 55 */ 56 57 #include <sys/mplock2.h> 58 59 #include <linux/i2c.h> 60 #include <linux/export.h> 61 #include <drm/drmP.h> 62 #include "intel_drv.h" 63 #include <drm/i915_drm.h> 64 #include "i915_drv.h" 65 66 #include <bus/iicbus/iic.h> 67 #include <bus/iicbus/iiconf.h> 68 #include <bus/iicbus/iicbus.h> 69 #include "iicbus_if.h" 70 #include "iicbb_if.h" 71 72 struct gmbus_pin { 73 const char *name; 74 int reg; 75 }; 76 77 /* Map gmbus pin pairs to names and registers. */ 78 static const struct gmbus_pin gmbus_pins[] = { 79 [GMBUS_PIN_SSC] = { "ssc", GPIOB }, 80 [GMBUS_PIN_VGADDC] = { "vga", GPIOA }, 81 [GMBUS_PIN_PANEL] = { "panel", GPIOC }, 82 [GMBUS_PIN_DPC] = { "dpc", GPIOD }, 83 [GMBUS_PIN_DPB] = { "dpb", GPIOE }, 84 [GMBUS_PIN_DPD] = { "dpd", GPIOF }, 85 }; 86 87 static const struct gmbus_pin gmbus_pins_bdw[] = { 88 [GMBUS_PIN_VGADDC] = { "vga", GPIOA }, 89 [GMBUS_PIN_DPC] = { "dpc", GPIOD }, 90 [GMBUS_PIN_DPB] = { "dpb", GPIOE }, 91 [GMBUS_PIN_DPD] = { "dpd", GPIOF }, 92 }; 93 94 static const struct gmbus_pin gmbus_pins_skl[] = { 95 [GMBUS_PIN_DPC] = { "dpc", GPIOD }, 96 [GMBUS_PIN_DPB] = { "dpb", GPIOE }, 97 [GMBUS_PIN_DPD] = { "dpd", GPIOF }, 98 }; 99 100 static const struct gmbus_pin gmbus_pins_bxt[] = { 101 [GMBUS_PIN_1_BXT] = { "dpb", PCH_GPIOB }, 102 [GMBUS_PIN_2_BXT] = { "dpc", PCH_GPIOC }, 103 [GMBUS_PIN_3_BXT] = { "misc", PCH_GPIOD }, 104 }; 105 106 /* pin is expected to be valid */ 107 static const struct gmbus_pin *get_gmbus_pin(struct drm_i915_private *dev_priv, 108 unsigned int pin) 109 { 110 if (IS_BROXTON(dev_priv)) 111 return &gmbus_pins_bxt[pin]; 112 else if (IS_SKYLAKE(dev_priv)) 113 return &gmbus_pins_skl[pin]; 114 else if (IS_BROADWELL(dev_priv)) 115 return &gmbus_pins_bdw[pin]; 116 else 117 return &gmbus_pins[pin]; 118 } 119 120 bool intel_gmbus_is_valid_pin(struct drm_i915_private *dev_priv, 121 unsigned int pin) 122 { 123 unsigned int size; 124 125 if (IS_BROXTON(dev_priv)) 126 size = ARRAY_SIZE(gmbus_pins_bxt); 127 else if (IS_SKYLAKE(dev_priv)) 128 size = ARRAY_SIZE(gmbus_pins_skl); 129 else if (IS_BROADWELL(dev_priv)) 130 size = ARRAY_SIZE(gmbus_pins_bdw); 131 else 132 size = ARRAY_SIZE(gmbus_pins); 133 134 return pin < size && get_gmbus_pin(dev_priv, pin)->reg; 135 } 136 137 /* Intel GPIO access functions */ 138 139 #define I2C_RISEFALL_TIME 10 140 141 void 142 intel_i2c_reset(struct drm_device *dev) 143 { 144 struct drm_i915_private *dev_priv = dev->dev_private; 145 146 I915_WRITE(dev_priv->gpio_mmio_base + GMBUS0, 0); 147 I915_WRITE(dev_priv->gpio_mmio_base + GMBUS4, 0); 148 } 149 150 static void intel_i2c_quirk_set(struct drm_i915_private *dev_priv, bool enable) 151 { 152 u32 val; 153 154 /* When using bit bashing for I2C, this bit needs to be set to 1 */ 155 if (!IS_PINEVIEW(dev_priv->dev)) 156 return; 157 158 val = I915_READ(DSPCLK_GATE_D); 159 if (enable) 160 val |= DPCUNIT_CLOCK_GATE_DISABLE; 161 else 162 val &= ~DPCUNIT_CLOCK_GATE_DISABLE; 163 I915_WRITE(DSPCLK_GATE_D, val); 164 } 165 166 static u32 get_reserved(device_t idev) 167 { 168 struct intel_iic_softc *sc = device_get_softc(idev); 169 struct drm_device *dev = sc->drm_dev; 170 struct drm_i915_private *dev_priv; 171 u32 reserved = 0; 172 173 dev_priv = dev->dev_private; 174 175 /* On most chips, these bits must be preserved in software. */ 176 if (!IS_I830(dev) && !IS_845G(dev)) 177 reserved = I915_READ_NOTRACE(sc->reg) & 178 (GPIO_DATA_PULLUP_DISABLE | 179 GPIO_CLOCK_PULLUP_DISABLE); 180 181 return reserved; 182 } 183 184 static int get_clock(device_t idev) 185 { 186 struct intel_iic_softc *sc; 187 struct drm_i915_private *dev_priv; 188 u32 reserved; 189 190 sc = device_get_softc(idev); 191 dev_priv = sc->drm_dev->dev_private; 192 193 reserved = get_reserved(idev); 194 195 I915_WRITE_NOTRACE(sc->reg, reserved | GPIO_CLOCK_DIR_MASK); 196 I915_WRITE_NOTRACE(sc->reg, reserved); 197 return ((I915_READ_NOTRACE(sc->reg) & GPIO_CLOCK_VAL_IN) != 0); 198 } 199 200 static int get_data(device_t idev) 201 { 202 struct intel_iic_softc *sc; 203 struct drm_i915_private *dev_priv; 204 u32 reserved; 205 206 sc = device_get_softc(idev); 207 dev_priv = sc->drm_dev->dev_private; 208 209 reserved = get_reserved(idev); 210 211 I915_WRITE_NOTRACE(sc->reg, reserved | GPIO_DATA_DIR_MASK); 212 I915_WRITE_NOTRACE(sc->reg, reserved); 213 return ((I915_READ_NOTRACE(sc->reg) & GPIO_DATA_VAL_IN) != 0); 214 } 215 216 static int 217 intel_iicbus_reset(device_t idev, u_char speed, u_char addr, u_char *oldaddr) 218 { 219 struct intel_iic_softc *sc; 220 struct drm_device *dev; 221 222 sc = device_get_softc(idev); 223 dev = sc->drm_dev; 224 225 intel_i2c_reset(dev); 226 return (0); 227 } 228 229 static void set_clock(device_t idev, int val) 230 { 231 struct intel_iic_softc *sc; 232 struct drm_i915_private *dev_priv; 233 u32 clock_bits, reserved; 234 235 sc = device_get_softc(idev); 236 dev_priv = sc->drm_dev->dev_private; 237 238 reserved = get_reserved(idev); 239 if (val) 240 clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK; 241 else 242 clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK | 243 GPIO_CLOCK_VAL_MASK; 244 245 I915_WRITE_NOTRACE(sc->reg, reserved | clock_bits); 246 POSTING_READ(sc->reg); 247 } 248 249 static void set_data(device_t idev, int val) 250 { 251 struct intel_iic_softc *sc; 252 struct drm_i915_private *dev_priv; 253 u32 reserved; 254 u32 data_bits; 255 256 sc = device_get_softc(idev); 257 dev_priv = sc->drm_dev->dev_private; 258 259 reserved = get_reserved(idev); 260 261 if (val) 262 data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK; 263 else 264 data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK | 265 GPIO_DATA_VAL_MASK; 266 267 I915_WRITE_NOTRACE(sc->reg, reserved | data_bits); 268 POSTING_READ(sc->reg); 269 } 270 271 static const char *gpio_names[GMBUS_NUM_PINS] = { 272 "ssc", 273 "vga", 274 "panel", 275 "dpc", 276 "dpb", 277 "dpd", 278 }; 279 280 static int 281 intel_gpio_setup(device_t idev) 282 { 283 static const int map_pin_to_reg[] = { 284 0, 285 GPIOB, 286 GPIOA, 287 GPIOC, 288 GPIOD, 289 GPIOE, 290 GPIOF, 291 0 292 }; 293 294 struct intel_iic_softc *sc; 295 struct drm_i915_private *dev_priv; 296 int pin; 297 298 sc = device_get_softc(idev); 299 sc->drm_dev = device_get_softc(device_get_parent(idev)); 300 dev_priv = sc->drm_dev->dev_private; 301 pin = device_get_unit(idev); 302 303 ksnprintf(sc->name, sizeof(sc->name), "i915 iicbb %s", gpio_names[pin]); 304 device_set_desc(idev, sc->name); 305 306 sc->reg0 = pin | GMBUS_RATE_100KHZ; 307 sc->reg = dev_priv->gpio_mmio_base + map_pin_to_reg[pin]; 308 309 /* add generic bit-banging code */ 310 sc->iic_dev = device_add_child(idev, "iicbb", -1); 311 if (sc->iic_dev == NULL) 312 return (ENXIO); 313 device_quiet(sc->iic_dev); 314 bus_generic_attach(idev); 315 316 return (0); 317 } 318 319 static int 320 intel_i2c_quirk_xfer(device_t idev, struct iic_msg *msgs, int nmsgs) 321 { 322 device_t bridge_dev; 323 struct intel_iic_softc *sc; 324 struct drm_i915_private *dev_priv; 325 int ret; 326 int i; 327 328 bridge_dev = device_get_parent(device_get_parent(idev)); 329 sc = device_get_softc(bridge_dev); 330 dev_priv = sc->drm_dev->dev_private; 331 332 intel_i2c_reset(sc->drm_dev); 333 intel_i2c_quirk_set(dev_priv, true); 334 IICBB_SETSDA(bridge_dev, 1); 335 IICBB_SETSCL(bridge_dev, 1); 336 DELAY(I2C_RISEFALL_TIME); 337 338 for (i = 0; i < nmsgs - 1; i++) { 339 /* force use of repeated start instead of default stop+start */ 340 msgs[i].flags |= IIC_M_NOSTOP; 341 } 342 ret = iicbus_transfer(idev, msgs, nmsgs); 343 IICBB_SETSDA(bridge_dev, 1); 344 IICBB_SETSCL(bridge_dev, 1); 345 intel_i2c_quirk_set(dev_priv, false); 346 347 return (ret); 348 } 349 350 static int 351 gmbus_wait_hw_status(struct drm_i915_private *dev_priv, 352 u32 gmbus2_status, 353 u32 gmbus4_irq_en) 354 { 355 int i; 356 int reg_offset = dev_priv->gpio_mmio_base; 357 u32 gmbus2 = 0; 358 DEFINE_WAIT(wait); 359 360 if (!HAS_GMBUS_IRQ(dev_priv->dev)) 361 gmbus4_irq_en = 0; 362 363 /* Important: The hw handles only the first bit, so set only one! Since 364 * we also need to check for NAKs besides the hw ready/idle signal, we 365 * need to wake up periodically and check that ourselves. */ 366 I915_WRITE(GMBUS4 + reg_offset, gmbus4_irq_en); 367 368 for (i = 0; i < msecs_to_jiffies_timeout(50); i++) { 369 prepare_to_wait(&dev_priv->gmbus_wait_queue, &wait, 370 TASK_UNINTERRUPTIBLE); 371 372 gmbus2 = I915_READ_NOTRACE(GMBUS2 + reg_offset); 373 if (gmbus2 & (GMBUS_SATOER | gmbus2_status)) 374 break; 375 376 schedule_timeout(1); 377 } 378 finish_wait(&dev_priv->gmbus_wait_queue, &wait); 379 380 I915_WRITE(GMBUS4 + reg_offset, 0); 381 382 if (gmbus2 & GMBUS_SATOER) 383 return -ENXIO; 384 if (gmbus2 & gmbus2_status) 385 return 0; 386 return -ETIMEDOUT; 387 } 388 389 static int 390 gmbus_wait_idle(struct drm_i915_private *dev_priv) 391 { 392 int ret; 393 int reg_offset = dev_priv->gpio_mmio_base; 394 395 #define C ((I915_READ_NOTRACE(GMBUS2 + reg_offset) & GMBUS_ACTIVE) == 0) 396 397 if (!HAS_GMBUS_IRQ(dev_priv->dev)) 398 return wait_for(C, 10); 399 400 /* Important: The hw handles only the first bit, so set only one! */ 401 I915_WRITE(GMBUS4 + reg_offset, GMBUS_IDLE_EN); 402 403 ret = wait_event_timeout(dev_priv->gmbus_wait_queue, C, 404 msecs_to_jiffies_timeout(10)); 405 406 I915_WRITE(GMBUS4 + reg_offset, 0); 407 408 if (ret) 409 return 0; 410 else 411 return -ETIMEDOUT; 412 #undef C 413 } 414 415 static int 416 gmbus_xfer_read_chunk(struct drm_i915_private *dev_priv, 417 unsigned short addr, u8 *buf, unsigned int len, 418 u32 gmbus1_index) 419 { 420 int reg_offset = dev_priv->gpio_mmio_base; 421 422 I915_WRITE(GMBUS1 + reg_offset, 423 gmbus1_index | 424 GMBUS_CYCLE_WAIT | 425 (len << GMBUS_BYTE_COUNT_SHIFT) | 426 (addr << GMBUS_SLAVE_ADDR_SHIFT) | 427 GMBUS_SLAVE_READ | GMBUS_SW_RDY); 428 while (len) { 429 int ret; 430 u32 val, loop = 0; 431 432 ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_RDY, 433 GMBUS_HW_RDY_EN); 434 if (ret) 435 return ret; 436 437 val = I915_READ(GMBUS3 + reg_offset); 438 do { 439 *buf++ = val & 0xff; 440 val >>= 8; 441 } while (--len && ++loop < 4); 442 } 443 444 return 0; 445 } 446 447 static int 448 gmbus_xfer_read(struct drm_i915_private *dev_priv, struct i2c_msg *msg, 449 u32 gmbus1_index) 450 { 451 u8 *buf = msg->buf; 452 unsigned int rx_size = msg->len; 453 unsigned int len; 454 int ret; 455 456 do { 457 len = min(rx_size, GMBUS_BYTE_COUNT_MAX); 458 459 ret = gmbus_xfer_read_chunk(dev_priv, msg->slave >> 1, 460 buf, len, gmbus1_index); 461 if (ret) 462 return ret; 463 464 rx_size -= len; 465 buf += len; 466 } while (rx_size != 0); 467 468 return 0; 469 } 470 471 static int 472 gmbus_xfer_write_chunk(struct drm_i915_private *dev_priv, 473 unsigned short addr, u8 *buf, unsigned int len) 474 { 475 int reg_offset = dev_priv->gpio_mmio_base; 476 unsigned int chunk_size = len; 477 u32 val, loop; 478 479 val = loop = 0; 480 while (len && loop < 4) { 481 val |= *buf++ << (8 * loop++); 482 len -= 1; 483 } 484 485 I915_WRITE(GMBUS3 + reg_offset, val); 486 I915_WRITE(GMBUS1 + reg_offset, 487 GMBUS_CYCLE_WAIT | 488 (chunk_size << GMBUS_BYTE_COUNT_SHIFT) | 489 (addr << GMBUS_SLAVE_ADDR_SHIFT) | 490 GMBUS_SLAVE_WRITE | GMBUS_SW_RDY); 491 while (len) { 492 int ret; 493 494 val = loop = 0; 495 do { 496 val |= *buf++ << (8 * loop); 497 } while (--len && ++loop < 4); 498 499 I915_WRITE(GMBUS3 + reg_offset, val); 500 501 ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_RDY, 502 GMBUS_HW_RDY_EN); 503 if (ret) 504 return ret; 505 } 506 507 return 0; 508 } 509 510 static int 511 gmbus_xfer_write(struct drm_i915_private *dev_priv, struct i2c_msg *msg) 512 { 513 u8 *buf = msg->buf; 514 unsigned int tx_size = msg->len; 515 unsigned int len; 516 int ret; 517 518 do { 519 len = min(tx_size, GMBUS_BYTE_COUNT_MAX); 520 521 ret = gmbus_xfer_write_chunk(dev_priv, msg->slave >> 1, buf, len); 522 if (ret) 523 return ret; 524 525 buf += len; 526 tx_size -= len; 527 } while (tx_size != 0); 528 529 return 0; 530 } 531 532 /* 533 * The gmbus controller can combine a 1 or 2 byte write with a read that 534 * immediately follows it by using an "INDEX" cycle. 535 */ 536 static bool 537 gmbus_is_index_read(struct i2c_msg *msgs, int i, int num) 538 { 539 return (i + 1 < num && 540 !(msgs[i].flags & I2C_M_RD) && msgs[i].len <= 2 && 541 (msgs[i + 1].flags & I2C_M_RD)); 542 } 543 544 static int 545 gmbus_xfer_index_read(struct drm_i915_private *dev_priv, struct i2c_msg *msgs) 546 { 547 int reg_offset = dev_priv->gpio_mmio_base; 548 u32 gmbus1_index = 0; 549 u32 gmbus5 = 0; 550 int ret; 551 552 if (msgs[0].len == 2) 553 gmbus5 = GMBUS_2BYTE_INDEX_EN | 554 msgs[0].buf[1] | (msgs[0].buf[0] << 8); 555 if (msgs[0].len == 1) 556 gmbus1_index = GMBUS_CYCLE_INDEX | 557 (msgs[0].buf[0] << GMBUS_SLAVE_INDEX_SHIFT); 558 559 /* GMBUS5 holds 16-bit index */ 560 if (gmbus5) 561 I915_WRITE(GMBUS5 + reg_offset, gmbus5); 562 563 ret = gmbus_xfer_read(dev_priv, &msgs[1], gmbus1_index); 564 565 /* Clear GMBUS5 after each index transfer */ 566 if (gmbus5) 567 I915_WRITE(GMBUS5 + reg_offset, 0); 568 569 return ret; 570 } 571 572 static int 573 gmbus_xfer(struct device *adapter, 574 struct i2c_msg *msgs, 575 int num) 576 { 577 struct intel_iic_softc *sc; 578 struct drm_i915_private *dev_priv; 579 int i = 0, inc, try = 0, reg_offset; 580 int unit; 581 int ret = 0; 582 583 sc = device_get_softc(adapter); 584 dev_priv = sc->drm_dev->dev_private; 585 unit = device_get_unit(adapter); 586 587 mutex_lock(&dev_priv->gmbus_mutex); 588 589 if (sc->force_bit_dev) { 590 ret = intel_i2c_quirk_xfer(dev_priv->bbbus[unit], msgs, num); 591 goto out; 592 } 593 594 reg_offset = HAS_PCH_SPLIT(dev_priv->dev) ? PCH_GMBUS0 - GMBUS0 : 0; 595 596 retry: 597 I915_WRITE(GMBUS0 + reg_offset, sc->reg0); 598 599 for (; i < num; i += inc) { 600 inc = 1; 601 if (gmbus_is_index_read(msgs, i, num)) { 602 ret = gmbus_xfer_index_read(dev_priv, &msgs[i]); 603 inc = 2; /* an index read is two msgs */ 604 } else if (msgs[i].flags & I2C_M_RD) { 605 ret = gmbus_xfer_read(dev_priv, &msgs[i], 0); 606 } else { 607 ret = gmbus_xfer_write(dev_priv, &msgs[i]); 608 } 609 610 if (ret == -ETIMEDOUT) 611 goto timeout; 612 if (ret == -ENXIO) 613 goto clear_err; 614 615 ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_WAIT_PHASE, 616 GMBUS_HW_WAIT_EN); 617 if (ret == -ENXIO) 618 goto clear_err; 619 if (ret) 620 goto timeout; 621 } 622 623 /* Generate a STOP condition on the bus. Note that gmbus can't generata 624 * a STOP on the very first cycle. To simplify the code we 625 * unconditionally generate the STOP condition with an additional gmbus 626 * cycle. */ 627 I915_WRITE(GMBUS1 + reg_offset, GMBUS_CYCLE_STOP | GMBUS_SW_RDY); 628 629 /* Mark the GMBUS interface as disabled after waiting for idle. 630 * We will re-enable it at the start of the next xfer, 631 * till then let it sleep. 632 */ 633 if (gmbus_wait_idle(dev_priv)) { 634 DRM_DEBUG_KMS("GMBUS [%s] timed out waiting for idle\n", 635 sc->name); 636 ret = -ETIMEDOUT; 637 } 638 I915_WRITE(GMBUS0 + reg_offset, 0); 639 ret = ret ?: i; 640 goto timeout; /* XXX: should be out */ 641 642 clear_err: 643 /* 644 * Wait for bus to IDLE before clearing NAK. 645 * If we clear the NAK while bus is still active, then it will stay 646 * active and the next transaction may fail. 647 * 648 * If no ACK is received during the address phase of a transaction, the 649 * adapter must report -ENXIO. It is not clear what to return if no ACK 650 * is received at other times. But we have to be careful to not return 651 * spurious -ENXIO because that will prevent i2c and drm edid functions 652 * from retrying. So return -ENXIO only when gmbus properly quiescents - 653 * timing out seems to happen when there _is_ a ddc chip present, but 654 * it's slow responding and only answers on the 2nd retry. 655 */ 656 ret = -ENXIO; 657 if (gmbus_wait_idle(dev_priv)) { 658 DRM_DEBUG_KMS("GMBUS [%s] timed out after NAK\n", 659 sc->name); 660 ret = -ETIMEDOUT; 661 } 662 663 /* Toggle the Software Clear Interrupt bit. This has the effect 664 * of resetting the GMBUS controller and so clearing the 665 * BUS_ERROR raised by the slave's NAK. 666 */ 667 I915_WRITE(GMBUS1 + reg_offset, GMBUS_SW_CLR_INT); 668 I915_WRITE(GMBUS1 + reg_offset, 0); 669 I915_WRITE(GMBUS0 + reg_offset, 0); 670 671 DRM_DEBUG_KMS("GMBUS [%s] NAK for addr: %04x %c(%d)\n", 672 sc->name, msgs[i].slave, 673 (msgs[i].flags & I2C_M_RD) ? 'r' : 'w', msgs[i].len); 674 675 /* 676 * Passive adapters sometimes NAK the first probe. Retry the first 677 * message once on -ENXIO for GMBUS transfers; the bit banging algorithm 678 * has retries internally. See also the retry loop in 679 * drm_do_probe_ddc_edid, which bails out on the first -ENXIO. 680 */ 681 if (ret == -ENXIO && i == 0 && try++ == 0) { 682 DRM_DEBUG_KMS("GMBUS [%s] NAK on first message, retry\n", 683 sc->name); 684 goto retry; 685 } 686 687 goto out; 688 689 timeout: 690 DRM_INFO("GMBUS [%s] timed out, falling back to bit banging on pin %d\n", 691 sc->name, sc->reg0 & 0xff); 692 I915_WRITE(GMBUS0 + reg_offset, 0); 693 694 /* Hardware may not support GMBUS over these pins? Try GPIO bitbanging instead. */ 695 sc->force_bit_dev = true; 696 ret = intel_i2c_quirk_xfer(dev_priv->bbbus[unit], msgs, num); 697 698 out: 699 mutex_unlock(&dev_priv->gmbus_mutex); 700 return ret; 701 } 702 703 struct i2c_adapter *intel_gmbus_get_adapter(struct drm_i915_private *dev_priv, 704 unsigned int pin) 705 { 706 if (WARN_ON(!intel_gmbus_is_valid_pin(dev_priv, pin))) 707 return NULL; 708 709 return dev_priv->gmbus[pin]; 710 } 711 712 void 713 intel_gmbus_set_speed(device_t idev, int speed) 714 { 715 struct intel_iic_softc *sc; 716 717 sc = device_get_softc(device_get_parent(idev)); 718 719 sc->reg0 = (sc->reg0 & ~(0x3 << 8)) | speed; 720 } 721 722 void 723 intel_gmbus_force_bit(device_t idev, bool force_bit) 724 { 725 struct intel_iic_softc *sc; 726 727 sc = device_get_softc(device_get_parent(idev)); 728 sc->force_bit_dev += force_bit ? 1 : -1; 729 DRM_DEBUG_KMS("%sabling bit-banging on %s. force bit now %d\n", 730 force_bit ? "en" : "dis", sc->name, 731 sc->force_bit_dev); 732 } 733 734 static int 735 intel_gmbus_probe(device_t dev) 736 { 737 738 return (BUS_PROBE_SPECIFIC); 739 } 740 741 static int 742 intel_gmbus_attach(device_t idev) 743 { 744 struct drm_i915_private *dev_priv; 745 struct intel_iic_softc *sc; 746 int pin; 747 748 sc = device_get_softc(idev); 749 sc->drm_dev = device_get_softc(device_get_parent(idev)); 750 dev_priv = sc->drm_dev->dev_private; 751 pin = device_get_unit(idev); 752 753 ksnprintf(sc->name, sizeof(sc->name), "gmbus bus %s", gpio_names[pin]); 754 device_set_desc(idev, sc->name); 755 756 /* By default use a conservative clock rate */ 757 sc->reg0 = pin | GMBUS_RATE_100KHZ; 758 759 /* XXX force bit banging until GMBUS is fully debugged */ 760 if (IS_GEN2(sc->drm_dev)) { 761 sc->force_bit_dev = true; 762 } 763 764 /* add bus interface device */ 765 sc->iic_dev = device_add_child(idev, "iicbus", -1); 766 if (sc->iic_dev == NULL) 767 return (ENXIO); 768 device_quiet(sc->iic_dev); 769 bus_generic_attach(idev); 770 771 return (0); 772 } 773 774 static int 775 intel_gmbus_detach(device_t idev) 776 { 777 struct intel_iic_softc *sc; 778 struct drm_i915_private *dev_priv; 779 device_t child; 780 int u; 781 782 sc = device_get_softc(idev); 783 u = device_get_unit(idev); 784 dev_priv = sc->drm_dev->dev_private; 785 786 child = sc->iic_dev; 787 bus_generic_detach(idev); 788 if (child != NULL) 789 device_delete_child(idev, child); 790 791 return (0); 792 } 793 794 static int 795 intel_iicbb_probe(device_t dev) 796 { 797 798 return (BUS_PROBE_DEFAULT); 799 } 800 801 static int 802 intel_iicbb_detach(device_t idev) 803 { 804 struct intel_iic_softc *sc; 805 device_t child; 806 807 sc = device_get_softc(idev); 808 child = sc->iic_dev; 809 bus_generic_detach(idev); 810 if (child) 811 device_delete_child(idev, child); 812 return (0); 813 } 814 815 static device_method_t intel_gmbus_methods[] = { 816 DEVMETHOD(device_probe, intel_gmbus_probe), 817 DEVMETHOD(device_attach, intel_gmbus_attach), 818 DEVMETHOD(device_detach, intel_gmbus_detach), 819 DEVMETHOD(iicbus_reset, intel_iicbus_reset), 820 DEVMETHOD(iicbus_transfer, gmbus_xfer), 821 DEVMETHOD_END 822 }; 823 static driver_t intel_gmbus_driver = { 824 "intel_gmbus", 825 intel_gmbus_methods, 826 sizeof(struct intel_iic_softc) 827 }; 828 static devclass_t intel_gmbus_devclass; 829 DRIVER_MODULE_ORDERED(intel_gmbus, drm, intel_gmbus_driver, 830 intel_gmbus_devclass, NULL, NULL, SI_ORDER_FIRST); 831 DRIVER_MODULE(iicbus, intel_gmbus, iicbus_driver, iicbus_devclass, NULL, NULL); 832 833 static device_method_t intel_iicbb_methods[] = { 834 DEVMETHOD(device_probe, intel_iicbb_probe), 835 DEVMETHOD(device_attach, intel_gpio_setup), 836 DEVMETHOD(device_detach, intel_iicbb_detach), 837 838 DEVMETHOD(bus_add_child, bus_generic_add_child), 839 DEVMETHOD(bus_print_child, bus_generic_print_child), 840 841 DEVMETHOD(iicbb_callback, iicbus_null_callback), 842 DEVMETHOD(iicbb_reset, intel_iicbus_reset), 843 DEVMETHOD(iicbb_setsda, set_data), 844 DEVMETHOD(iicbb_setscl, set_clock), 845 DEVMETHOD(iicbb_getsda, get_data), 846 DEVMETHOD(iicbb_getscl, get_clock), 847 DEVMETHOD_END 848 }; 849 static driver_t intel_iicbb_driver = { 850 "intel_iicbb", 851 intel_iicbb_methods, 852 sizeof(struct intel_iic_softc) 853 }; 854 static devclass_t intel_iicbb_devclass; 855 DRIVER_MODULE_ORDERED(intel_iicbb, drm, intel_iicbb_driver, 856 intel_iicbb_devclass, NULL, NULL, SI_ORDER_FIRST); 857 DRIVER_MODULE(iicbb, intel_iicbb, iicbb_driver, iicbb_devclass, NULL, NULL); 858 859 static void intel_teardown_gmbus_m(struct drm_device *dev, int m); 860 861 /** 862 * intel_gmbus_setup - instantiate all Intel i2c GMBuses 863 * @dev: DRM device 864 */ 865 int intel_setup_gmbus(struct drm_device *dev) 866 { 867 struct drm_i915_private *dev_priv = dev->dev_private; 868 device_t iic_dev; 869 unsigned int pin; 870 int ret; 871 872 if (HAS_PCH_NOP(dev)) 873 return 0; 874 else if (HAS_PCH_SPLIT(dev)) 875 dev_priv->gpio_mmio_base = PCH_GPIOA - GPIOA; 876 else if (IS_VALLEYVIEW(dev)) 877 dev_priv->gpio_mmio_base = VLV_DISPLAY_BASE; 878 else 879 dev_priv->gpio_mmio_base = 0; 880 881 lockinit(&dev_priv->gmbus_mutex, "gmbus", 0, LK_CANRECURSE); 882 init_waitqueue_head(&dev_priv->gmbus_wait_queue); 883 884 dev_priv->gmbus_bridge = kmalloc(sizeof(device_t) * GMBUS_NUM_PINS, 885 M_DRM, M_WAITOK | M_ZERO); 886 dev_priv->bbbus_bridge = kmalloc(sizeof(device_t) * GMBUS_NUM_PINS, 887 M_DRM, M_WAITOK | M_ZERO); 888 dev_priv->gmbus = kmalloc(sizeof(device_t) * GMBUS_NUM_PINS, 889 M_DRM, M_WAITOK | M_ZERO); 890 dev_priv->bbbus = kmalloc(sizeof(device_t) * GMBUS_NUM_PINS, 891 M_DRM, M_WAITOK | M_ZERO); 892 893 for (pin = 0; pin < GMBUS_NUM_PINS; pin++) { 894 if (!intel_gmbus_is_valid_pin(dev_priv, pin)) 895 continue; 896 897 /* 898 * Initialized bbbus_bridge before gmbus_bridge, since 899 * gmbus may decide to force quirk transfer in the 900 * attachment code. 901 */ 902 dev_priv->bbbus_bridge[pin] = device_add_child(dev->dev, 903 "intel_iicbb", pin); 904 if (dev_priv->bbbus_bridge[pin] == NULL) { 905 DRM_ERROR("bbbus bridge %d creation failed\n", pin); 906 ret = ENXIO; 907 goto err; 908 } 909 device_quiet(dev_priv->bbbus_bridge[pin]); 910 ret = device_probe_and_attach(dev_priv->bbbus_bridge[pin]); 911 if (ret != 0) { 912 DRM_ERROR("bbbus bridge %d attach failed, %d\n", pin, ret); 913 goto err; 914 } 915 916 iic_dev = device_find_child(dev_priv->bbbus_bridge[pin], "iicbb", 917 -1); 918 if (iic_dev == NULL) { 919 DRM_ERROR("bbbus bridge doesn't have iicbb child\n"); 920 goto err; 921 } 922 iic_dev = device_find_child(iic_dev, "iicbus", -1); 923 if (iic_dev == NULL) { 924 DRM_ERROR( 925 "bbbus bridge doesn't have iicbus grandchild\n"); 926 goto err; 927 } 928 929 dev_priv->bbbus[pin] = iic_dev; 930 931 dev_priv->gmbus_bridge[pin] = device_add_child(dev->dev, 932 "intel_gmbus", pin); 933 if (dev_priv->gmbus_bridge[pin] == NULL) { 934 DRM_ERROR("gmbus bridge %d creation failed\n", pin); 935 ret = ENXIO; 936 goto err; 937 } 938 device_quiet(dev_priv->gmbus_bridge[pin]); 939 ret = device_probe_and_attach(dev_priv->gmbus_bridge[pin]); 940 if (ret != 0) { 941 DRM_ERROR("gmbus bridge %d attach failed, %d\n", pin, 942 ret); 943 ret = ENXIO; 944 goto err; 945 } 946 947 iic_dev = device_find_child(dev_priv->gmbus_bridge[pin], 948 "iicbus", -1); 949 if (iic_dev == NULL) { 950 DRM_ERROR("gmbus bridge doesn't have iicbus child\n"); 951 goto err; 952 } 953 dev_priv->gmbus[pin] = iic_dev; 954 955 intel_i2c_reset(dev); 956 } 957 958 return (0); 959 960 err: 961 intel_teardown_gmbus_m(dev, pin); 962 return (ret); 963 } 964 965 static void 966 intel_teardown_gmbus_m(struct drm_device *dev, int m) 967 { 968 struct drm_i915_private *dev_priv; 969 970 dev_priv = dev->dev_private; 971 972 kfree(dev_priv->gmbus); 973 dev_priv->gmbus = NULL; 974 kfree(dev_priv->bbbus); 975 dev_priv->bbbus = NULL; 976 kfree(dev_priv->gmbus_bridge); 977 dev_priv->gmbus_bridge = NULL; 978 kfree(dev_priv->bbbus_bridge); 979 dev_priv->bbbus_bridge = NULL; 980 lockuninit(&dev_priv->gmbus_mutex); 981 } 982 983 void 984 intel_teardown_gmbus(struct drm_device *dev) 985 { 986 987 get_mplock(); 988 intel_teardown_gmbus_m(dev, GMBUS_NUM_PINS); 989 rel_mplock(); 990 } 991