1bad0eccaSFrançois Tigeot /* 2bad0eccaSFrançois Tigeot * Copyright (c) 2006 Dave Airlie <airlied@linux.ie> 3bad0eccaSFrançois Tigeot * Copyright © 2006-2008,2010 Intel Corporation 4bad0eccaSFrançois Tigeot * Jesse Barnes <jesse.barnes@intel.com> 5bad0eccaSFrançois Tigeot * 6bad0eccaSFrançois Tigeot * Permission is hereby granted, free of charge, to any person obtaining a 7bad0eccaSFrançois Tigeot * copy of this software and associated documentation files (the "Software"), 8bad0eccaSFrançois Tigeot * to deal in the Software without restriction, including without limitation 9bad0eccaSFrançois Tigeot * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10bad0eccaSFrançois Tigeot * and/or sell copies of the Software, and to permit persons to whom the 11bad0eccaSFrançois Tigeot * Software is furnished to do so, subject to the following conditions: 12bad0eccaSFrançois Tigeot * 13bad0eccaSFrançois Tigeot * The above copyright notice and this permission notice (including the next 14bad0eccaSFrançois Tigeot * paragraph) shall be included in all copies or substantial portions of the 15bad0eccaSFrançois Tigeot * Software. 16bad0eccaSFrançois Tigeot * 17bad0eccaSFrançois Tigeot * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18bad0eccaSFrançois Tigeot * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19bad0eccaSFrançois Tigeot * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20bad0eccaSFrançois Tigeot * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21bad0eccaSFrançois Tigeot * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22bad0eccaSFrançois Tigeot * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 23bad0eccaSFrançois Tigeot * DEALINGS IN THE SOFTWARE. 24bad0eccaSFrançois Tigeot * 25bad0eccaSFrançois Tigeot * Authors: 26bad0eccaSFrançois Tigeot * Eric Anholt <eric@anholt.net> 27bad0eccaSFrançois Tigeot * Chris Wilson <chris@chris-wilson.co.uk> 28bad0eccaSFrançois Tigeot */ 29a2fdbec6SFrançois Tigeot #include <linux/i2c.h> 309f4ca867SFrançois Tigeot #include <linux/i2c-algo-bit.h> 31a2fdbec6SFrançois Tigeot #include <linux/export.h> 32bad0eccaSFrançois Tigeot #include <drm/drmP.h> 33a2fdbec6SFrançois Tigeot #include "intel_drv.h" 34bad0eccaSFrançois Tigeot #include <drm/i915_drm.h> 35bad0eccaSFrançois Tigeot #include "i915_drv.h" 36a2fdbec6SFrançois Tigeot 3719c468b4SFrançois Tigeot struct gmbus_pin { 38a2fdbec6SFrançois Tigeot const char *name; 39aee94f86SFrançois Tigeot i915_reg_t reg; 40a2fdbec6SFrançois Tigeot }; 41a2fdbec6SFrançois Tigeot 4219c468b4SFrançois Tigeot /* Map gmbus pin pairs to names and registers. */ 4319c468b4SFrançois Tigeot static const struct gmbus_pin gmbus_pins[] = { 4419c468b4SFrançois Tigeot [GMBUS_PIN_SSC] = { "ssc", GPIOB }, 4519c468b4SFrançois Tigeot [GMBUS_PIN_VGADDC] = { "vga", GPIOA }, 4619c468b4SFrançois Tigeot [GMBUS_PIN_PANEL] = { "panel", GPIOC }, 4719c468b4SFrançois Tigeot [GMBUS_PIN_DPC] = { "dpc", GPIOD }, 4819c468b4SFrançois Tigeot [GMBUS_PIN_DPB] = { "dpb", GPIOE }, 4919c468b4SFrançois Tigeot [GMBUS_PIN_DPD] = { "dpd", GPIOF }, 50a2fdbec6SFrançois Tigeot }; 51bad0eccaSFrançois Tigeot 5219c468b4SFrançois Tigeot static const struct gmbus_pin gmbus_pins_bdw[] = { 5319c468b4SFrançois Tigeot [GMBUS_PIN_VGADDC] = { "vga", GPIOA }, 5419c468b4SFrançois Tigeot [GMBUS_PIN_DPC] = { "dpc", GPIOD }, 5519c468b4SFrançois Tigeot [GMBUS_PIN_DPB] = { "dpb", GPIOE }, 5619c468b4SFrançois Tigeot [GMBUS_PIN_DPD] = { "dpd", GPIOF }, 5719c468b4SFrançois Tigeot }; 5819c468b4SFrançois Tigeot 5919c468b4SFrançois Tigeot static const struct gmbus_pin gmbus_pins_skl[] = { 6019c468b4SFrançois Tigeot [GMBUS_PIN_DPC] = { "dpc", GPIOD }, 6119c468b4SFrançois Tigeot [GMBUS_PIN_DPB] = { "dpb", GPIOE }, 6219c468b4SFrançois Tigeot [GMBUS_PIN_DPD] = { "dpd", GPIOF }, 6319c468b4SFrançois Tigeot }; 6419c468b4SFrançois Tigeot 6519c468b4SFrançois Tigeot static const struct gmbus_pin gmbus_pins_bxt[] = { 66aee94f86SFrançois Tigeot [GMBUS_PIN_1_BXT] = { "dpb", GPIOB }, 67aee94f86SFrançois Tigeot [GMBUS_PIN_2_BXT] = { "dpc", GPIOC }, 68aee94f86SFrançois Tigeot [GMBUS_PIN_3_BXT] = { "misc", GPIOD }, 6919c468b4SFrançois Tigeot }; 7019c468b4SFrançois Tigeot 7119c468b4SFrançois Tigeot /* pin is expected to be valid */ 7219c468b4SFrançois Tigeot static const struct gmbus_pin *get_gmbus_pin(struct drm_i915_private *dev_priv, 7319c468b4SFrançois Tigeot unsigned int pin) 7419c468b4SFrançois Tigeot { 7519c468b4SFrançois Tigeot if (IS_BROXTON(dev_priv)) 7619c468b4SFrançois Tigeot return &gmbus_pins_bxt[pin]; 77aee94f86SFrançois Tigeot else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) 7819c468b4SFrançois Tigeot return &gmbus_pins_skl[pin]; 7919c468b4SFrançois Tigeot else if (IS_BROADWELL(dev_priv)) 8019c468b4SFrançois Tigeot return &gmbus_pins_bdw[pin]; 8119c468b4SFrançois Tigeot else 8219c468b4SFrançois Tigeot return &gmbus_pins[pin]; 8319c468b4SFrançois Tigeot } 8419c468b4SFrançois Tigeot 8519c468b4SFrançois Tigeot bool intel_gmbus_is_valid_pin(struct drm_i915_private *dev_priv, 8619c468b4SFrançois Tigeot unsigned int pin) 8719c468b4SFrançois Tigeot { 8819c468b4SFrançois Tigeot unsigned int size; 8919c468b4SFrançois Tigeot 9019c468b4SFrançois Tigeot if (IS_BROXTON(dev_priv)) 9119c468b4SFrançois Tigeot size = ARRAY_SIZE(gmbus_pins_bxt); 92aee94f86SFrançois Tigeot else if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) 9319c468b4SFrançois Tigeot size = ARRAY_SIZE(gmbus_pins_skl); 9419c468b4SFrançois Tigeot else if (IS_BROADWELL(dev_priv)) 9519c468b4SFrançois Tigeot size = ARRAY_SIZE(gmbus_pins_bdw); 9619c468b4SFrançois Tigeot else 9719c468b4SFrançois Tigeot size = ARRAY_SIZE(gmbus_pins); 9819c468b4SFrançois Tigeot 99aee94f86SFrançois Tigeot return pin < size && 100aee94f86SFrançois Tigeot i915_mmio_reg_valid(get_gmbus_pin(dev_priv, pin)->reg); 10119c468b4SFrançois Tigeot } 10219c468b4SFrançois Tigeot 103bad0eccaSFrançois Tigeot /* Intel GPIO access functions */ 104bad0eccaSFrançois Tigeot 105bad0eccaSFrançois Tigeot #define I2C_RISEFALL_TIME 10 106bad0eccaSFrançois Tigeot 1079f4ca867SFrançois Tigeot static inline struct intel_gmbus * 1089f4ca867SFrançois Tigeot to_intel_gmbus(struct i2c_adapter *i2c) 1099f4ca867SFrançois Tigeot { 1109f4ca867SFrançois Tigeot return container_of(i2c, struct intel_gmbus, adapter); 1119f4ca867SFrançois Tigeot } 1129f4ca867SFrançois Tigeot 113a2fdbec6SFrançois Tigeot void 114a2fdbec6SFrançois Tigeot intel_i2c_reset(struct drm_device *dev) 115a2fdbec6SFrançois Tigeot { 116bf017597SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(dev); 1179edbd4a0SFrançois Tigeot 118352ff8bdSFrançois Tigeot I915_WRITE(GMBUS0, 0); 119352ff8bdSFrançois Tigeot I915_WRITE(GMBUS4, 0); 120a2fdbec6SFrançois Tigeot } 121a2fdbec6SFrançois Tigeot 122a2fdbec6SFrançois Tigeot static void intel_i2c_quirk_set(struct drm_i915_private *dev_priv, bool enable) 123bad0eccaSFrançois Tigeot { 124bad0eccaSFrançois Tigeot u32 val; 125bad0eccaSFrançois Tigeot 126bad0eccaSFrançois Tigeot /* When using bit bashing for I2C, this bit needs to be set to 1 */ 1278621f407SFrançois Tigeot if (!IS_PINEVIEW(dev_priv)) 128bad0eccaSFrançois Tigeot return; 129bad0eccaSFrançois Tigeot 130bad0eccaSFrançois Tigeot val = I915_READ(DSPCLK_GATE_D); 131bad0eccaSFrançois Tigeot if (enable) 132bad0eccaSFrançois Tigeot val |= DPCUNIT_CLOCK_GATE_DISABLE; 133bad0eccaSFrançois Tigeot else 134bad0eccaSFrançois Tigeot val &= ~DPCUNIT_CLOCK_GATE_DISABLE; 135bad0eccaSFrançois Tigeot I915_WRITE(DSPCLK_GATE_D, val); 136bad0eccaSFrançois Tigeot } 137bad0eccaSFrançois Tigeot 1389f4ca867SFrançois Tigeot static u32 get_reserved(struct intel_gmbus *bus) 139a2fdbec6SFrançois Tigeot { 1409f4ca867SFrançois Tigeot struct drm_i915_private *dev_priv = bus->dev_priv; 141a2fdbec6SFrançois Tigeot u32 reserved = 0; 142a2fdbec6SFrançois Tigeot 143a2fdbec6SFrançois Tigeot /* On most chips, these bits must be preserved in software. */ 144*1e12ee3bSFrançois Tigeot if (!IS_I830(dev_priv) && !IS_845G(dev_priv)) 1459f4ca867SFrançois Tigeot reserved = I915_READ_NOTRACE(bus->gpio_reg) & 146a2fdbec6SFrançois Tigeot (GPIO_DATA_PULLUP_DISABLE | 147a2fdbec6SFrançois Tigeot GPIO_CLOCK_PULLUP_DISABLE); 148a2fdbec6SFrançois Tigeot 149a2fdbec6SFrançois Tigeot return reserved; 150a2fdbec6SFrançois Tigeot } 151a2fdbec6SFrançois Tigeot 1529f4ca867SFrançois Tigeot static int get_clock(void *data) 153bad0eccaSFrançois Tigeot { 1549f4ca867SFrançois Tigeot struct intel_gmbus *bus = data; 1559f4ca867SFrançois Tigeot struct drm_i915_private *dev_priv = bus->dev_priv; 1569f4ca867SFrançois Tigeot u32 reserved = get_reserved(bus); 1579f4ca867SFrançois Tigeot I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_CLOCK_DIR_MASK); 1589f4ca867SFrançois Tigeot I915_WRITE_NOTRACE(bus->gpio_reg, reserved); 1599f4ca867SFrançois Tigeot return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_CLOCK_VAL_IN) != 0; 160bad0eccaSFrançois Tigeot } 161bad0eccaSFrançois Tigeot 1629f4ca867SFrançois Tigeot static int get_data(void *data) 163bad0eccaSFrançois Tigeot { 1649f4ca867SFrançois Tigeot struct intel_gmbus *bus = data; 1659f4ca867SFrançois Tigeot struct drm_i915_private *dev_priv = bus->dev_priv; 1669f4ca867SFrançois Tigeot u32 reserved = get_reserved(bus); 1679f4ca867SFrançois Tigeot I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_DATA_DIR_MASK); 1689f4ca867SFrançois Tigeot I915_WRITE_NOTRACE(bus->gpio_reg, reserved); 1699f4ca867SFrançois Tigeot return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_DATA_VAL_IN) != 0; 170bad0eccaSFrançois Tigeot } 171bad0eccaSFrançois Tigeot 1729f4ca867SFrançois Tigeot static void set_clock(void *data, int state_high) 173bad0eccaSFrançois Tigeot { 1749f4ca867SFrançois Tigeot struct intel_gmbus *bus = data; 1759f4ca867SFrançois Tigeot struct drm_i915_private *dev_priv = bus->dev_priv; 1769f4ca867SFrançois Tigeot u32 reserved = get_reserved(bus); 1779f4ca867SFrançois Tigeot u32 clock_bits; 178bad0eccaSFrançois Tigeot 1799f4ca867SFrançois Tigeot if (state_high) 180bad0eccaSFrançois Tigeot clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK; 181bad0eccaSFrançois Tigeot else 182bad0eccaSFrançois Tigeot clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK | 183bad0eccaSFrançois Tigeot GPIO_CLOCK_VAL_MASK; 184bad0eccaSFrançois Tigeot 1859f4ca867SFrançois Tigeot I915_WRITE_NOTRACE(bus->gpio_reg, reserved | clock_bits); 1869f4ca867SFrançois Tigeot POSTING_READ(bus->gpio_reg); 187bad0eccaSFrançois Tigeot } 188bad0eccaSFrançois Tigeot 1899f4ca867SFrançois Tigeot static void set_data(void *data, int state_high) 190bad0eccaSFrançois Tigeot { 1919f4ca867SFrançois Tigeot struct intel_gmbus *bus = data; 1929f4ca867SFrançois Tigeot struct drm_i915_private *dev_priv = bus->dev_priv; 1939f4ca867SFrançois Tigeot u32 reserved = get_reserved(bus); 194a2fdbec6SFrançois Tigeot u32 data_bits; 195bad0eccaSFrançois Tigeot 1969f4ca867SFrançois Tigeot if (state_high) 197a2fdbec6SFrançois Tigeot data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK; 198a2fdbec6SFrançois Tigeot else 199a2fdbec6SFrançois Tigeot data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK | 200a2fdbec6SFrançois Tigeot GPIO_DATA_VAL_MASK; 201bad0eccaSFrançois Tigeot 2029f4ca867SFrançois Tigeot I915_WRITE_NOTRACE(bus->gpio_reg, reserved | data_bits); 2039f4ca867SFrançois Tigeot POSTING_READ(bus->gpio_reg); 204bad0eccaSFrançois Tigeot } 205bad0eccaSFrançois Tigeot 206bad0eccaSFrançois Tigeot static int 2079f4ca867SFrançois Tigeot intel_gpio_pre_xfer(struct i2c_adapter *adapter) 208bad0eccaSFrançois Tigeot { 2099f4ca867SFrançois Tigeot struct intel_gmbus *bus = container_of(adapter, 2109f4ca867SFrançois Tigeot struct intel_gmbus, 2119f4ca867SFrançois Tigeot adapter); 2129f4ca867SFrançois Tigeot struct drm_i915_private *dev_priv = bus->dev_priv; 213a2fdbec6SFrançois Tigeot 214303bf270SFrançois Tigeot intel_i2c_reset(&dev_priv->drm); 215a2fdbec6SFrançois Tigeot intel_i2c_quirk_set(dev_priv, true); 2169f4ca867SFrançois Tigeot set_data(bus, 1); 2179f4ca867SFrançois Tigeot set_clock(bus, 1); 2189f4ca867SFrançois Tigeot udelay(I2C_RISEFALL_TIME); 2199f4ca867SFrançois Tigeot return 0; 220a2fdbec6SFrançois Tigeot } 221a2fdbec6SFrançois Tigeot 2229f4ca867SFrançois Tigeot static void 2239f4ca867SFrançois Tigeot intel_gpio_post_xfer(struct i2c_adapter *adapter) 2249f4ca867SFrançois Tigeot { 2259f4ca867SFrançois Tigeot struct intel_gmbus *bus = container_of(adapter, 2269f4ca867SFrançois Tigeot struct intel_gmbus, 2279f4ca867SFrançois Tigeot adapter); 2289f4ca867SFrançois Tigeot struct drm_i915_private *dev_priv = bus->dev_priv; 2299f4ca867SFrançois Tigeot 2309f4ca867SFrançois Tigeot set_data(bus, 1); 2319f4ca867SFrançois Tigeot set_clock(bus, 1); 2329f4ca867SFrançois Tigeot intel_i2c_quirk_set(dev_priv, false); 2339f4ca867SFrançois Tigeot } 2349f4ca867SFrançois Tigeot 2359f4ca867SFrançois Tigeot static void 2369f4ca867SFrançois Tigeot intel_gpio_setup(struct intel_gmbus *bus, unsigned int pin) 2379f4ca867SFrançois Tigeot { 2389f4ca867SFrançois Tigeot struct drm_i915_private *dev_priv = bus->dev_priv; 2399f4ca867SFrançois Tigeot struct i2c_algo_bit_data *algo; 2409f4ca867SFrançois Tigeot 2419f4ca867SFrançois Tigeot algo = &bus->bit_algo; 2429f4ca867SFrançois Tigeot 243aee94f86SFrançois Tigeot bus->gpio_reg = _MMIO(dev_priv->gpio_mmio_base + 244aee94f86SFrançois Tigeot i915_mmio_reg_offset(get_gmbus_pin(dev_priv, pin)->reg)); 2459f4ca867SFrançois Tigeot bus->adapter.algo_data = algo; 2469f4ca867SFrançois Tigeot algo->setsda = set_data; 2479f4ca867SFrançois Tigeot algo->setscl = set_clock; 2489f4ca867SFrançois Tigeot algo->getsda = get_data; 2499f4ca867SFrançois Tigeot algo->getscl = get_clock; 2509f4ca867SFrançois Tigeot algo->pre_xfer = intel_gpio_pre_xfer; 2519f4ca867SFrançois Tigeot algo->post_xfer = intel_gpio_post_xfer; 2529f4ca867SFrançois Tigeot algo->udelay = I2C_RISEFALL_TIME; 2539f4ca867SFrançois Tigeot algo->timeout = usecs_to_jiffies(2200); 2549f4ca867SFrançois Tigeot algo->data = bus; 255a2fdbec6SFrançois Tigeot } 256a2fdbec6SFrançois Tigeot 257*1e12ee3bSFrançois Tigeot static int gmbus_wait(struct drm_i915_private *dev_priv, u32 status, u32 irq_en) 258a2fdbec6SFrançois Tigeot { 259a2fdbec6SFrançois Tigeot DEFINE_WAIT(wait); 260*1e12ee3bSFrançois Tigeot u32 gmbus2; 261*1e12ee3bSFrançois Tigeot int ret; 262a2fdbec6SFrançois Tigeot 263a2fdbec6SFrançois Tigeot /* Important: The hw handles only the first bit, so set only one! Since 264a2fdbec6SFrançois Tigeot * we also need to check for NAKs besides the hw ready/idle signal, we 265*1e12ee3bSFrançois Tigeot * need to wake up periodically and check that ourselves. 266*1e12ee3bSFrançois Tigeot */ 267*1e12ee3bSFrançois Tigeot if (!HAS_GMBUS_IRQ(dev_priv)) 268*1e12ee3bSFrançois Tigeot irq_en = 0; 269a2fdbec6SFrançois Tigeot 270*1e12ee3bSFrançois Tigeot add_wait_queue(&dev_priv->gmbus_wait_queue, &wait); 271*1e12ee3bSFrançois Tigeot I915_WRITE_FW(GMBUS4, irq_en); 272a2fdbec6SFrançois Tigeot 273*1e12ee3bSFrançois Tigeot status |= GMBUS_SATOER; 274*1e12ee3bSFrançois Tigeot ret = wait_for_us((gmbus2 = I915_READ_FW(GMBUS2)) & status, 2); 275*1e12ee3bSFrançois Tigeot if (ret) 276*1e12ee3bSFrançois Tigeot ret = wait_for((gmbus2 = I915_READ_FW(GMBUS2)) & status, 50); 277a2fdbec6SFrançois Tigeot 278*1e12ee3bSFrançois Tigeot I915_WRITE_FW(GMBUS4, 0); 279*1e12ee3bSFrançois Tigeot remove_wait_queue(&dev_priv->gmbus_wait_queue, &wait); 280a2fdbec6SFrançois Tigeot 281a2fdbec6SFrançois Tigeot if (gmbus2 & GMBUS_SATOER) 282a2fdbec6SFrançois Tigeot return -ENXIO; 283*1e12ee3bSFrançois Tigeot 284*1e12ee3bSFrançois Tigeot return ret; 285a2fdbec6SFrançois Tigeot } 286a2fdbec6SFrançois Tigeot 287a2fdbec6SFrançois Tigeot static int 288a2fdbec6SFrançois Tigeot gmbus_wait_idle(struct drm_i915_private *dev_priv) 289a2fdbec6SFrançois Tigeot { 290*1e12ee3bSFrançois Tigeot DEFINE_WAIT(wait); 291*1e12ee3bSFrançois Tigeot u32 irq_enable; 292a2fdbec6SFrançois Tigeot int ret; 293a2fdbec6SFrançois Tigeot 294*1e12ee3bSFrançois Tigeot /* Important: The hw handles only the first bit, so set only one! */ 295*1e12ee3bSFrançois Tigeot irq_enable = 0; 296*1e12ee3bSFrançois Tigeot if (HAS_GMBUS_IRQ(dev_priv)) 297*1e12ee3bSFrançois Tigeot irq_enable = GMBUS_IDLE_EN; 298*1e12ee3bSFrançois Tigeot 299*1e12ee3bSFrançois Tigeot add_wait_queue(&dev_priv->gmbus_wait_queue, &wait); 300*1e12ee3bSFrançois Tigeot I915_WRITE_FW(GMBUS4, irq_enable); 301*1e12ee3bSFrançois Tigeot 302*1e12ee3bSFrançois Tigeot ret = intel_wait_for_register_fw(dev_priv, 3031487f786SFrançois Tigeot GMBUS2, GMBUS_ACTIVE, 0, 3041487f786SFrançois Tigeot 10); 305a2fdbec6SFrançois Tigeot 306*1e12ee3bSFrançois Tigeot I915_WRITE_FW(GMBUS4, 0); 307*1e12ee3bSFrançois Tigeot remove_wait_queue(&dev_priv->gmbus_wait_queue, &wait); 308a2fdbec6SFrançois Tigeot 309*1e12ee3bSFrançois Tigeot return ret; 310a2fdbec6SFrançois Tigeot } 311a2fdbec6SFrançois Tigeot 312a2fdbec6SFrançois Tigeot static int 313477eb7f9SFrançois Tigeot gmbus_xfer_read_chunk(struct drm_i915_private *dev_priv, 314477eb7f9SFrançois Tigeot unsigned short addr, u8 *buf, unsigned int len, 315a2fdbec6SFrançois Tigeot u32 gmbus1_index) 316a2fdbec6SFrançois Tigeot { 317*1e12ee3bSFrançois Tigeot I915_WRITE_FW(GMBUS1, 318a2fdbec6SFrançois Tigeot gmbus1_index | 319a2fdbec6SFrançois Tigeot GMBUS_CYCLE_WAIT | 320a2fdbec6SFrançois Tigeot (len << GMBUS_BYTE_COUNT_SHIFT) | 321477eb7f9SFrançois Tigeot (addr << GMBUS_SLAVE_ADDR_SHIFT) | 322a2fdbec6SFrançois Tigeot GMBUS_SLAVE_READ | GMBUS_SW_RDY); 323a2fdbec6SFrançois Tigeot while (len) { 324a2fdbec6SFrançois Tigeot int ret; 325a2fdbec6SFrançois Tigeot u32 val, loop = 0; 326a2fdbec6SFrançois Tigeot 327*1e12ee3bSFrançois Tigeot ret = gmbus_wait(dev_priv, GMBUS_HW_RDY, GMBUS_HW_RDY_EN); 328a2fdbec6SFrançois Tigeot if (ret) 329a2fdbec6SFrançois Tigeot return ret; 330a2fdbec6SFrançois Tigeot 331*1e12ee3bSFrançois Tigeot val = I915_READ_FW(GMBUS3); 332a2fdbec6SFrançois Tigeot do { 333a2fdbec6SFrançois Tigeot *buf++ = val & 0xff; 334a2fdbec6SFrançois Tigeot val >>= 8; 335a2fdbec6SFrançois Tigeot } while (--len && ++loop < 4); 336a2fdbec6SFrançois Tigeot } 337a2fdbec6SFrançois Tigeot 338a2fdbec6SFrançois Tigeot return 0; 339a2fdbec6SFrançois Tigeot } 340a2fdbec6SFrançois Tigeot 341a2fdbec6SFrançois Tigeot static int 342477eb7f9SFrançois Tigeot gmbus_xfer_read(struct drm_i915_private *dev_priv, struct i2c_msg *msg, 343477eb7f9SFrançois Tigeot u32 gmbus1_index) 344477eb7f9SFrançois Tigeot { 345477eb7f9SFrançois Tigeot u8 *buf = msg->buf; 346477eb7f9SFrançois Tigeot unsigned int rx_size = msg->len; 347477eb7f9SFrançois Tigeot unsigned int len; 348477eb7f9SFrançois Tigeot int ret; 349477eb7f9SFrançois Tigeot 350477eb7f9SFrançois Tigeot do { 351477eb7f9SFrançois Tigeot len = min(rx_size, GMBUS_BYTE_COUNT_MAX); 352477eb7f9SFrançois Tigeot 3539f4ca867SFrançois Tigeot ret = gmbus_xfer_read_chunk(dev_priv, msg->addr, 354477eb7f9SFrançois Tigeot buf, len, gmbus1_index); 355477eb7f9SFrançois Tigeot if (ret) 356477eb7f9SFrançois Tigeot return ret; 357477eb7f9SFrançois Tigeot 358477eb7f9SFrançois Tigeot rx_size -= len; 359477eb7f9SFrançois Tigeot buf += len; 360477eb7f9SFrançois Tigeot } while (rx_size != 0); 361477eb7f9SFrançois Tigeot 362477eb7f9SFrançois Tigeot return 0; 363477eb7f9SFrançois Tigeot } 364477eb7f9SFrançois Tigeot 365477eb7f9SFrançois Tigeot static int 366477eb7f9SFrançois Tigeot gmbus_xfer_write_chunk(struct drm_i915_private *dev_priv, 367477eb7f9SFrançois Tigeot unsigned short addr, u8 *buf, unsigned int len) 368a2fdbec6SFrançois Tigeot { 369477eb7f9SFrançois Tigeot unsigned int chunk_size = len; 370bad0eccaSFrançois Tigeot u32 val, loop; 371bad0eccaSFrançois Tigeot 372a2fdbec6SFrançois Tigeot val = loop = 0; 373a2fdbec6SFrançois Tigeot while (len && loop < 4) { 374a2fdbec6SFrançois Tigeot val |= *buf++ << (8 * loop++); 375a2fdbec6SFrançois Tigeot len -= 1; 376a2fdbec6SFrançois Tigeot } 377a2fdbec6SFrançois Tigeot 378*1e12ee3bSFrançois Tigeot I915_WRITE_FW(GMBUS3, val); 379*1e12ee3bSFrançois Tigeot I915_WRITE_FW(GMBUS1, 380a2fdbec6SFrançois Tigeot GMBUS_CYCLE_WAIT | 381477eb7f9SFrançois Tigeot (chunk_size << GMBUS_BYTE_COUNT_SHIFT) | 382477eb7f9SFrançois Tigeot (addr << GMBUS_SLAVE_ADDR_SHIFT) | 383a2fdbec6SFrançois Tigeot GMBUS_SLAVE_WRITE | GMBUS_SW_RDY); 384a2fdbec6SFrançois Tigeot while (len) { 385a2fdbec6SFrançois Tigeot int ret; 386a2fdbec6SFrançois Tigeot 387a2fdbec6SFrançois Tigeot val = loop = 0; 388a2fdbec6SFrançois Tigeot do { 389a2fdbec6SFrançois Tigeot val |= *buf++ << (8 * loop); 390a2fdbec6SFrançois Tigeot } while (--len && ++loop < 4); 391a2fdbec6SFrançois Tigeot 392*1e12ee3bSFrançois Tigeot I915_WRITE_FW(GMBUS3, val); 393a2fdbec6SFrançois Tigeot 394*1e12ee3bSFrançois Tigeot ret = gmbus_wait(dev_priv, GMBUS_HW_RDY, GMBUS_HW_RDY_EN); 395a2fdbec6SFrançois Tigeot if (ret) 396a2fdbec6SFrançois Tigeot return ret; 397a2fdbec6SFrançois Tigeot } 398477eb7f9SFrançois Tigeot 399477eb7f9SFrançois Tigeot return 0; 400477eb7f9SFrançois Tigeot } 401477eb7f9SFrançois Tigeot 402477eb7f9SFrançois Tigeot static int 403477eb7f9SFrançois Tigeot gmbus_xfer_write(struct drm_i915_private *dev_priv, struct i2c_msg *msg) 404477eb7f9SFrançois Tigeot { 405477eb7f9SFrançois Tigeot u8 *buf = msg->buf; 406477eb7f9SFrançois Tigeot unsigned int tx_size = msg->len; 407477eb7f9SFrançois Tigeot unsigned int len; 408477eb7f9SFrançois Tigeot int ret; 409477eb7f9SFrançois Tigeot 410477eb7f9SFrançois Tigeot do { 411477eb7f9SFrançois Tigeot len = min(tx_size, GMBUS_BYTE_COUNT_MAX); 412477eb7f9SFrançois Tigeot 4139f4ca867SFrançois Tigeot ret = gmbus_xfer_write_chunk(dev_priv, msg->addr, buf, len); 414477eb7f9SFrançois Tigeot if (ret) 415477eb7f9SFrançois Tigeot return ret; 416477eb7f9SFrançois Tigeot 417477eb7f9SFrançois Tigeot buf += len; 418477eb7f9SFrançois Tigeot tx_size -= len; 419477eb7f9SFrançois Tigeot } while (tx_size != 0); 420477eb7f9SFrançois Tigeot 421a2fdbec6SFrançois Tigeot return 0; 422a2fdbec6SFrançois Tigeot } 423a2fdbec6SFrançois Tigeot 424a2fdbec6SFrançois Tigeot /* 425a2fdbec6SFrançois Tigeot * The gmbus controller can combine a 1 or 2 byte write with a read that 426a2fdbec6SFrançois Tigeot * immediately follows it by using an "INDEX" cycle. 427a2fdbec6SFrançois Tigeot */ 428a2fdbec6SFrançois Tigeot static bool 429a2fdbec6SFrançois Tigeot gmbus_is_index_read(struct i2c_msg *msgs, int i, int num) 430a2fdbec6SFrançois Tigeot { 431a2fdbec6SFrançois Tigeot return (i + 1 < num && 432a2fdbec6SFrançois Tigeot !(msgs[i].flags & I2C_M_RD) && msgs[i].len <= 2 && 433a2fdbec6SFrançois Tigeot (msgs[i + 1].flags & I2C_M_RD)); 434a2fdbec6SFrançois Tigeot } 435a2fdbec6SFrançois Tigeot 436a2fdbec6SFrançois Tigeot static int 437a2fdbec6SFrançois Tigeot gmbus_xfer_index_read(struct drm_i915_private *dev_priv, struct i2c_msg *msgs) 438a2fdbec6SFrançois Tigeot { 439a2fdbec6SFrançois Tigeot u32 gmbus1_index = 0; 440a2fdbec6SFrançois Tigeot u32 gmbus5 = 0; 441a2fdbec6SFrançois Tigeot int ret; 442a2fdbec6SFrançois Tigeot 443a2fdbec6SFrançois Tigeot if (msgs[0].len == 2) 444a2fdbec6SFrançois Tigeot gmbus5 = GMBUS_2BYTE_INDEX_EN | 445a2fdbec6SFrançois Tigeot msgs[0].buf[1] | (msgs[0].buf[0] << 8); 446a2fdbec6SFrançois Tigeot if (msgs[0].len == 1) 447a2fdbec6SFrançois Tigeot gmbus1_index = GMBUS_CYCLE_INDEX | 448a2fdbec6SFrançois Tigeot (msgs[0].buf[0] << GMBUS_SLAVE_INDEX_SHIFT); 449a2fdbec6SFrançois Tigeot 450a2fdbec6SFrançois Tigeot /* GMBUS5 holds 16-bit index */ 451a2fdbec6SFrançois Tigeot if (gmbus5) 452*1e12ee3bSFrançois Tigeot I915_WRITE_FW(GMBUS5, gmbus5); 453a2fdbec6SFrançois Tigeot 454a2fdbec6SFrançois Tigeot ret = gmbus_xfer_read(dev_priv, &msgs[1], gmbus1_index); 455a2fdbec6SFrançois Tigeot 456a2fdbec6SFrançois Tigeot /* Clear GMBUS5 after each index transfer */ 457a2fdbec6SFrançois Tigeot if (gmbus5) 458*1e12ee3bSFrançois Tigeot I915_WRITE_FW(GMBUS5, 0); 459a2fdbec6SFrançois Tigeot 460a2fdbec6SFrançois Tigeot return ret; 461a2fdbec6SFrançois Tigeot } 462a2fdbec6SFrançois Tigeot 463a2fdbec6SFrançois Tigeot static int 464aee94f86SFrançois Tigeot do_gmbus_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num) 465a2fdbec6SFrançois Tigeot { 4669f4ca867SFrançois Tigeot struct intel_gmbus *bus = container_of(adapter, 4679f4ca867SFrançois Tigeot struct intel_gmbus, 4689f4ca867SFrançois Tigeot adapter); 4699f4ca867SFrançois Tigeot struct drm_i915_private *dev_priv = bus->dev_priv; 470352ff8bdSFrançois Tigeot int i = 0, inc, try = 0; 471a2fdbec6SFrançois Tigeot int ret = 0; 472a2fdbec6SFrançois Tigeot 473477eb7f9SFrançois Tigeot retry: 474*1e12ee3bSFrançois Tigeot I915_WRITE_FW(GMBUS0, bus->reg0); 475bad0eccaSFrançois Tigeot 476477eb7f9SFrançois Tigeot for (; i < num; i += inc) { 477477eb7f9SFrançois Tigeot inc = 1; 478a2fdbec6SFrançois Tigeot if (gmbus_is_index_read(msgs, i, num)) { 479a2fdbec6SFrançois Tigeot ret = gmbus_xfer_index_read(dev_priv, &msgs[i]); 480477eb7f9SFrançois Tigeot inc = 2; /* an index read is two msgs */ 481a2fdbec6SFrançois Tigeot } else if (msgs[i].flags & I2C_M_RD) { 482a2fdbec6SFrançois Tigeot ret = gmbus_xfer_read(dev_priv, &msgs[i], 0); 483bad0eccaSFrançois Tigeot } else { 484a2fdbec6SFrançois Tigeot ret = gmbus_xfer_write(dev_priv, &msgs[i]); 485a2fdbec6SFrançois Tigeot } 486bad0eccaSFrançois Tigeot 487aee94f86SFrançois Tigeot if (!ret) 488*1e12ee3bSFrançois Tigeot ret = gmbus_wait(dev_priv, 489*1e12ee3bSFrançois Tigeot GMBUS_HW_WAIT_PHASE, GMBUS_HW_WAIT_EN); 490aee94f86SFrançois Tigeot if (ret == -ETIMEDOUT) 491a2fdbec6SFrançois Tigeot goto timeout; 492aee94f86SFrançois Tigeot else if (ret) 493aee94f86SFrançois Tigeot goto clear_err; 494bad0eccaSFrançois Tigeot } 495bad0eccaSFrançois Tigeot 496a2fdbec6SFrançois Tigeot /* Generate a STOP condition on the bus. Note that gmbus can't generata 497a2fdbec6SFrançois Tigeot * a STOP on the very first cycle. To simplify the code we 498a2fdbec6SFrançois Tigeot * unconditionally generate the STOP condition with an additional gmbus 499a2fdbec6SFrançois Tigeot * cycle. */ 500*1e12ee3bSFrançois Tigeot I915_WRITE_FW(GMBUS1, GMBUS_CYCLE_STOP | GMBUS_SW_RDY); 501a2fdbec6SFrançois Tigeot 502bad0eccaSFrançois Tigeot /* Mark the GMBUS interface as disabled after waiting for idle. 503bad0eccaSFrançois Tigeot * We will re-enable it at the start of the next xfer, 504bad0eccaSFrançois Tigeot * till then let it sleep. 505bad0eccaSFrançois Tigeot */ 506a2fdbec6SFrançois Tigeot if (gmbus_wait_idle(dev_priv)) { 507a2fdbec6SFrançois Tigeot DRM_DEBUG_KMS("GMBUS [%s] timed out waiting for idle\n", 5089f4ca867SFrançois Tigeot adapter->name); 509a2fdbec6SFrançois Tigeot ret = -ETIMEDOUT; 510a2fdbec6SFrançois Tigeot } 511*1e12ee3bSFrançois Tigeot I915_WRITE_FW(GMBUS0, 0); 512a2fdbec6SFrançois Tigeot ret = ret ?: i; 5139f4ca867SFrançois Tigeot goto out; 514bad0eccaSFrançois Tigeot 515bad0eccaSFrançois Tigeot clear_err: 516a2fdbec6SFrançois Tigeot /* 517a2fdbec6SFrançois Tigeot * Wait for bus to IDLE before clearing NAK. 518a2fdbec6SFrançois Tigeot * If we clear the NAK while bus is still active, then it will stay 519a2fdbec6SFrançois Tigeot * active and the next transaction may fail. 520a2fdbec6SFrançois Tigeot * 521a2fdbec6SFrançois Tigeot * If no ACK is received during the address phase of a transaction, the 522a2fdbec6SFrançois Tigeot * adapter must report -ENXIO. It is not clear what to return if no ACK 523a2fdbec6SFrançois Tigeot * is received at other times. But we have to be careful to not return 524a2fdbec6SFrançois Tigeot * spurious -ENXIO because that will prevent i2c and drm edid functions 525a2fdbec6SFrançois Tigeot * from retrying. So return -ENXIO only when gmbus properly quiescents - 526a2fdbec6SFrançois Tigeot * timing out seems to happen when there _is_ a ddc chip present, but 527a2fdbec6SFrançois Tigeot * it's slow responding and only answers on the 2nd retry. 528a2fdbec6SFrançois Tigeot */ 529a2fdbec6SFrançois Tigeot ret = -ENXIO; 530a2fdbec6SFrançois Tigeot if (gmbus_wait_idle(dev_priv)) { 531a2fdbec6SFrançois Tigeot DRM_DEBUG_KMS("GMBUS [%s] timed out after NAK\n", 5329f4ca867SFrançois Tigeot adapter->name); 533a2fdbec6SFrançois Tigeot ret = -ETIMEDOUT; 534a2fdbec6SFrançois Tigeot } 535a2fdbec6SFrançois Tigeot 536bad0eccaSFrançois Tigeot /* Toggle the Software Clear Interrupt bit. This has the effect 537bad0eccaSFrançois Tigeot * of resetting the GMBUS controller and so clearing the 538bad0eccaSFrançois Tigeot * BUS_ERROR raised by the slave's NAK. 539bad0eccaSFrançois Tigeot */ 540*1e12ee3bSFrançois Tigeot I915_WRITE_FW(GMBUS1, GMBUS_SW_CLR_INT); 541*1e12ee3bSFrançois Tigeot I915_WRITE_FW(GMBUS1, 0); 542*1e12ee3bSFrançois Tigeot I915_WRITE_FW(GMBUS0, 0); 543bad0eccaSFrançois Tigeot 544a2fdbec6SFrançois Tigeot DRM_DEBUG_KMS("GMBUS [%s] NAK for addr: %04x %c(%d)\n", 5459f4ca867SFrançois Tigeot adapter->name, msgs[i].addr, 546a2fdbec6SFrançois Tigeot (msgs[i].flags & I2C_M_RD) ? 'r' : 'w', msgs[i].len); 547bad0eccaSFrançois Tigeot 548477eb7f9SFrançois Tigeot /* 549477eb7f9SFrançois Tigeot * Passive adapters sometimes NAK the first probe. Retry the first 550477eb7f9SFrançois Tigeot * message once on -ENXIO for GMBUS transfers; the bit banging algorithm 551477eb7f9SFrançois Tigeot * has retries internally. See also the retry loop in 552477eb7f9SFrançois Tigeot * drm_do_probe_ddc_edid, which bails out on the first -ENXIO. 553477eb7f9SFrançois Tigeot */ 554477eb7f9SFrançois Tigeot if (ret == -ENXIO && i == 0 && try++ == 0) { 555477eb7f9SFrançois Tigeot DRM_DEBUG_KMS("GMBUS [%s] NAK on first message, retry\n", 5569f4ca867SFrançois Tigeot adapter->name); 557477eb7f9SFrançois Tigeot goto retry; 558477eb7f9SFrançois Tigeot } 559477eb7f9SFrançois Tigeot 560bad0eccaSFrançois Tigeot goto out; 561a2fdbec6SFrançois Tigeot 562a2fdbec6SFrançois Tigeot timeout: 5638621f407SFrançois Tigeot DRM_DEBUG_KMS("GMBUS [%s] timed out, falling back to bit banging on pin %d\n", 5649f4ca867SFrançois Tigeot bus->adapter.name, bus->reg0 & 0xff); 565*1e12ee3bSFrançois Tigeot I915_WRITE_FW(GMBUS0, 0); 566a2fdbec6SFrançois Tigeot 567aee94f86SFrançois Tigeot /* 568aee94f86SFrançois Tigeot * Hardware may not support GMBUS over these pins? Try GPIO bitbanging 569aee94f86SFrançois Tigeot * instead. Use EAGAIN to have i2c core retry. 570aee94f86SFrançois Tigeot */ 571aee94f86SFrançois Tigeot ret = -EAGAIN; 572a2fdbec6SFrançois Tigeot 573a2fdbec6SFrançois Tigeot out: 574aee94f86SFrançois Tigeot return ret; 575aee94f86SFrançois Tigeot } 576352ff8bdSFrançois Tigeot 577aee94f86SFrançois Tigeot static int 578aee94f86SFrançois Tigeot gmbus_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num) 579aee94f86SFrançois Tigeot { 580aee94f86SFrançois Tigeot struct intel_gmbus *bus = container_of(adapter, struct intel_gmbus, 581aee94f86SFrançois Tigeot adapter); 582aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = bus->dev_priv; 583aee94f86SFrançois Tigeot int ret; 584aee94f86SFrançois Tigeot 585aee94f86SFrançois Tigeot intel_display_power_get(dev_priv, POWER_DOMAIN_GMBUS); 586aee94f86SFrançois Tigeot mutex_lock(&dev_priv->gmbus_mutex); 587aee94f86SFrançois Tigeot 5888621f407SFrançois Tigeot if (bus->force_bit) { 589aee94f86SFrançois Tigeot ret = i2c_bit_algo.master_xfer(adapter, msgs, num); 5908621f407SFrançois Tigeot if (ret < 0) 5918621f407SFrançois Tigeot bus->force_bit &= ~GMBUS_FORCE_BIT_RETRY; 5928621f407SFrançois Tigeot } else { 593aee94f86SFrançois Tigeot ret = do_gmbus_xfer(adapter, msgs, num); 5948621f407SFrançois Tigeot if (ret == -EAGAIN) 5958621f407SFrançois Tigeot bus->force_bit |= GMBUS_FORCE_BIT_RETRY; 5968621f407SFrançois Tigeot } 597aee94f86SFrançois Tigeot 598aee94f86SFrançois Tigeot mutex_unlock(&dev_priv->gmbus_mutex); 599352ff8bdSFrançois Tigeot intel_display_power_put(dev_priv, POWER_DOMAIN_GMBUS); 600352ff8bdSFrançois Tigeot 601a2fdbec6SFrançois Tigeot return ret; 602bad0eccaSFrançois Tigeot } 603bad0eccaSFrançois Tigeot 6049f4ca867SFrançois Tigeot static u32 gmbus_func(struct i2c_adapter *adapter) 60519df918dSFrançois Tigeot { 6069f4ca867SFrançois Tigeot return i2c_bit_algo.functionality(adapter) & 6079f4ca867SFrançois Tigeot (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | 6089f4ca867SFrançois Tigeot /* I2C_FUNC_10BIT_ADDR | */ 6099f4ca867SFrançois Tigeot I2C_FUNC_SMBUS_READ_BLOCK_DATA | 6109f4ca867SFrançois Tigeot I2C_FUNC_SMBUS_BLOCK_PROC_CALL); 61119df918dSFrançois Tigeot } 61219df918dSFrançois Tigeot 6139f4ca867SFrançois Tigeot static const struct i2c_algorithm gmbus_algorithm = { 6149f4ca867SFrançois Tigeot .master_xfer = gmbus_xfer, 6159f4ca867SFrançois Tigeot .functionality = gmbus_func 616bad0eccaSFrançois Tigeot }; 617a2fdbec6SFrançois Tigeot 61819c468b4SFrançois Tigeot /** 61919c468b4SFrançois Tigeot * intel_gmbus_setup - instantiate all Intel i2c GMBuses 62019c468b4SFrançois Tigeot * @dev: DRM device 62119c468b4SFrançois Tigeot */ 62219c468b4SFrançois Tigeot int intel_setup_gmbus(struct drm_device *dev) 623bad0eccaSFrançois Tigeot { 624bf017597SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(dev); 625*1e12ee3bSFrançois Tigeot struct pci_dev *pdev = dev_priv->drm.pdev; 6269f4ca867SFrançois Tigeot struct intel_gmbus *bus; 62719c468b4SFrançois Tigeot unsigned int pin; 62819c468b4SFrançois Tigeot int ret; 629bad0eccaSFrançois Tigeot 630*1e12ee3bSFrançois Tigeot if (HAS_PCH_NOP(dev_priv)) 6318e26cdf6SFrançois Tigeot return 0; 632aee94f86SFrançois Tigeot 633*1e12ee3bSFrançois Tigeot if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) 634a2fdbec6SFrançois Tigeot dev_priv->gpio_mmio_base = VLV_DISPLAY_BASE; 635aee94f86SFrançois Tigeot else if (!HAS_GMCH_DISPLAY(dev_priv)) 636aee94f86SFrançois Tigeot dev_priv->gpio_mmio_base = 637aee94f86SFrançois Tigeot i915_mmio_reg_offset(PCH_GPIOA) - 638aee94f86SFrançois Tigeot i915_mmio_reg_offset(GPIOA); 639a2fdbec6SFrançois Tigeot 64019df918dSFrançois Tigeot lockinit(&dev_priv->gmbus_mutex, "gmbus", 0, LK_CANRECURSE); 641a2fdbec6SFrançois Tigeot init_waitqueue_head(&dev_priv->gmbus_wait_queue); 642a2fdbec6SFrançois Tigeot 6439f4ca867SFrançois Tigeot for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) { 64419c468b4SFrançois Tigeot if (!intel_gmbus_is_valid_pin(dev_priv, pin)) 64519c468b4SFrançois Tigeot continue; 64619c468b4SFrançois Tigeot 6479f4ca867SFrançois Tigeot bus = &dev_priv->gmbus[pin]; 6489f4ca867SFrançois Tigeot 6499f4ca867SFrançois Tigeot #if 0 6509f4ca867SFrançois Tigeot bus->adapter.owner = THIS_MODULE; 6519f4ca867SFrançois Tigeot bus->adapter.class = I2C_CLASS_DDC; 6529f4ca867SFrançois Tigeot #endif 6539f4ca867SFrançois Tigeot ksnprintf(bus->adapter.name, 6549f4ca867SFrançois Tigeot sizeof(bus->adapter.name), 6559f4ca867SFrançois Tigeot "i915 gmbus %s", 6569f4ca867SFrançois Tigeot get_gmbus_pin(dev_priv, pin)->name); 6579f4ca867SFrançois Tigeot 658*1e12ee3bSFrançois Tigeot bus->adapter.dev.parent = &pdev->dev; 6599f4ca867SFrançois Tigeot bus->dev_priv = dev_priv; 6609f4ca867SFrançois Tigeot 6619f4ca867SFrançois Tigeot bus->adapter.algo = &gmbus_algorithm; 6629f4ca867SFrançois Tigeot 663aee94f86SFrançois Tigeot /* 664aee94f86SFrançois Tigeot * We wish to retry with bit banging 665aee94f86SFrançois Tigeot * after a timed out GMBUS attempt. 666aee94f86SFrançois Tigeot */ 667aee94f86SFrançois Tigeot bus->adapter.retries = 1; 668aee94f86SFrançois Tigeot 6699f4ca867SFrançois Tigeot /* By default use a conservative clock rate */ 6709f4ca867SFrançois Tigeot bus->reg0 = pin | GMBUS_RATE_100KHZ; 6719f4ca867SFrançois Tigeot 6729f4ca867SFrançois Tigeot /* gmbus seems to be broken on i830 */ 673*1e12ee3bSFrançois Tigeot if (IS_I830(dev_priv)) 6749f4ca867SFrançois Tigeot bus->force_bit = 1; 6759f4ca867SFrançois Tigeot 6769f4ca867SFrançois Tigeot intel_gpio_setup(bus, pin); 6779f4ca867SFrançois Tigeot 6789f4ca867SFrançois Tigeot ret = i2c_add_adapter(&bus->adapter); 6799f4ca867SFrançois Tigeot if (ret) 680bad0eccaSFrançois Tigeot goto err; 681bad0eccaSFrançois Tigeot } 682bad0eccaSFrançois Tigeot 683303bf270SFrançois Tigeot intel_i2c_reset(&dev_priv->drm); 684bad0eccaSFrançois Tigeot 6859f4ca867SFrançois Tigeot return 0; 686bad0eccaSFrançois Tigeot 687bad0eccaSFrançois Tigeot err: 688aee94f86SFrançois Tigeot while (pin--) { 6899f4ca867SFrançois Tigeot if (!intel_gmbus_is_valid_pin(dev_priv, pin)) 6909f4ca867SFrançois Tigeot continue; 6919f4ca867SFrançois Tigeot 6929f4ca867SFrançois Tigeot bus = &dev_priv->gmbus[pin]; 6939f4ca867SFrançois Tigeot i2c_del_adapter(&bus->adapter); 6949f4ca867SFrançois Tigeot } 6959f4ca867SFrançois Tigeot return ret; 696bad0eccaSFrançois Tigeot } 697bad0eccaSFrançois Tigeot 6989f4ca867SFrançois Tigeot struct i2c_adapter *intel_gmbus_get_adapter(struct drm_i915_private *dev_priv, 6999f4ca867SFrançois Tigeot unsigned int pin) 700bad0eccaSFrançois Tigeot { 7019f4ca867SFrançois Tigeot if (WARN_ON(!intel_gmbus_is_valid_pin(dev_priv, pin))) 7029f4ca867SFrançois Tigeot return NULL; 703bad0eccaSFrançois Tigeot 7049f4ca867SFrançois Tigeot return &dev_priv->gmbus[pin].adapter; 705bad0eccaSFrançois Tigeot } 706bad0eccaSFrançois Tigeot 7079f4ca867SFrançois Tigeot void intel_gmbus_set_speed(struct i2c_adapter *adapter, int speed) 708bad0eccaSFrançois Tigeot { 7099f4ca867SFrançois Tigeot struct intel_gmbus *bus = to_intel_gmbus(adapter); 710bad0eccaSFrançois Tigeot 7119f4ca867SFrançois Tigeot bus->reg0 = (bus->reg0 & ~(0x3 << 8)) | speed; 7129f4ca867SFrançois Tigeot } 7139f4ca867SFrançois Tigeot 7149f4ca867SFrançois Tigeot void intel_gmbus_force_bit(struct i2c_adapter *adapter, bool force_bit) 7159f4ca867SFrançois Tigeot { 7169f4ca867SFrançois Tigeot struct intel_gmbus *bus = to_intel_gmbus(adapter); 7178621f407SFrançois Tigeot struct drm_i915_private *dev_priv = bus->dev_priv; 7188621f407SFrançois Tigeot 7198621f407SFrançois Tigeot mutex_lock(&dev_priv->gmbus_mutex); 7209f4ca867SFrançois Tigeot 7219f4ca867SFrançois Tigeot bus->force_bit += force_bit ? 1 : -1; 7229f4ca867SFrançois Tigeot DRM_DEBUG_KMS("%sabling bit-banging on %s. force bit now %d\n", 7239f4ca867SFrançois Tigeot force_bit ? "en" : "dis", adapter->name, 7249f4ca867SFrançois Tigeot bus->force_bit); 7258621f407SFrançois Tigeot 7268621f407SFrançois Tigeot mutex_unlock(&dev_priv->gmbus_mutex); 7279f4ca867SFrançois Tigeot } 7289f4ca867SFrançois Tigeot 7299f4ca867SFrançois Tigeot void intel_teardown_gmbus(struct drm_device *dev) 7309f4ca867SFrançois Tigeot { 731bf017597SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(dev); 7329f4ca867SFrançois Tigeot struct intel_gmbus *bus; 7339f4ca867SFrançois Tigeot unsigned int pin; 7349f4ca867SFrançois Tigeot 7359f4ca867SFrançois Tigeot for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) { 7369f4ca867SFrançois Tigeot if (!intel_gmbus_is_valid_pin(dev_priv, pin)) 7379f4ca867SFrançois Tigeot continue; 7389f4ca867SFrançois Tigeot 7399f4ca867SFrançois Tigeot bus = &dev_priv->gmbus[pin]; 7409f4ca867SFrançois Tigeot i2c_del_adapter(&bus->adapter); 7419f4ca867SFrançois Tigeot } 742bad0eccaSFrançois Tigeot } 743