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; 39*aee94f86SFranç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[] = { 66*aee94f86SFrançois Tigeot [GMBUS_PIN_1_BXT] = { "dpb", GPIOB }, 67*aee94f86SFrançois Tigeot [GMBUS_PIN_2_BXT] = { "dpc", GPIOC }, 68*aee94f86SFranç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]; 77*aee94f86SFranç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); 92*aee94f86SFranç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 99*aee94f86SFrançois Tigeot return pin < size && 100*aee94f86SFranç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 { 116a2fdbec6SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 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 */ 127bad0eccaSFrançois Tigeot if (!IS_PINEVIEW(dev_priv->dev)) 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; 1419f4ca867SFrançois Tigeot struct drm_device *dev = dev_priv->dev; 142a2fdbec6SFrançois Tigeot u32 reserved = 0; 143a2fdbec6SFrançois Tigeot 144a2fdbec6SFrançois Tigeot /* On most chips, these bits must be preserved in software. */ 145a2fdbec6SFrançois Tigeot if (!IS_I830(dev) && !IS_845G(dev)) 1469f4ca867SFrançois Tigeot reserved = I915_READ_NOTRACE(bus->gpio_reg) & 147a2fdbec6SFrançois Tigeot (GPIO_DATA_PULLUP_DISABLE | 148a2fdbec6SFrançois Tigeot GPIO_CLOCK_PULLUP_DISABLE); 149a2fdbec6SFrançois Tigeot 150a2fdbec6SFrançois Tigeot return reserved; 151a2fdbec6SFrançois Tigeot } 152a2fdbec6SFrançois Tigeot 1539f4ca867SFrançois Tigeot static int get_clock(void *data) 154bad0eccaSFrançois Tigeot { 1559f4ca867SFrançois Tigeot struct intel_gmbus *bus = data; 1569f4ca867SFrançois Tigeot struct drm_i915_private *dev_priv = bus->dev_priv; 1579f4ca867SFrançois Tigeot u32 reserved = get_reserved(bus); 1589f4ca867SFrançois Tigeot I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_CLOCK_DIR_MASK); 1599f4ca867SFrançois Tigeot I915_WRITE_NOTRACE(bus->gpio_reg, reserved); 1609f4ca867SFrançois Tigeot return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_CLOCK_VAL_IN) != 0; 161bad0eccaSFrançois Tigeot } 162bad0eccaSFrançois Tigeot 1639f4ca867SFrançois Tigeot static int get_data(void *data) 164bad0eccaSFrançois Tigeot { 1659f4ca867SFrançois Tigeot struct intel_gmbus *bus = data; 1669f4ca867SFrançois Tigeot struct drm_i915_private *dev_priv = bus->dev_priv; 1679f4ca867SFrançois Tigeot u32 reserved = get_reserved(bus); 1689f4ca867SFrançois Tigeot I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_DATA_DIR_MASK); 1699f4ca867SFrançois Tigeot I915_WRITE_NOTRACE(bus->gpio_reg, reserved); 1709f4ca867SFrançois Tigeot return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_DATA_VAL_IN) != 0; 171bad0eccaSFrançois Tigeot } 172bad0eccaSFrançois Tigeot 1739f4ca867SFrançois Tigeot static void set_clock(void *data, int state_high) 174bad0eccaSFrançois Tigeot { 1759f4ca867SFrançois Tigeot struct intel_gmbus *bus = data; 1769f4ca867SFrançois Tigeot struct drm_i915_private *dev_priv = bus->dev_priv; 1779f4ca867SFrançois Tigeot u32 reserved = get_reserved(bus); 1789f4ca867SFrançois Tigeot u32 clock_bits; 179bad0eccaSFrançois Tigeot 1809f4ca867SFrançois Tigeot if (state_high) 181bad0eccaSFrançois Tigeot clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK; 182bad0eccaSFrançois Tigeot else 183bad0eccaSFrançois Tigeot clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK | 184bad0eccaSFrançois Tigeot GPIO_CLOCK_VAL_MASK; 185bad0eccaSFrançois Tigeot 1869f4ca867SFrançois Tigeot I915_WRITE_NOTRACE(bus->gpio_reg, reserved | clock_bits); 1879f4ca867SFrançois Tigeot POSTING_READ(bus->gpio_reg); 188bad0eccaSFrançois Tigeot } 189bad0eccaSFrançois Tigeot 1909f4ca867SFrançois Tigeot static void set_data(void *data, int state_high) 191bad0eccaSFrançois Tigeot { 1929f4ca867SFrançois Tigeot struct intel_gmbus *bus = data; 1939f4ca867SFrançois Tigeot struct drm_i915_private *dev_priv = bus->dev_priv; 1949f4ca867SFrançois Tigeot u32 reserved = get_reserved(bus); 195a2fdbec6SFrançois Tigeot u32 data_bits; 196bad0eccaSFrançois Tigeot 1979f4ca867SFrançois Tigeot if (state_high) 198a2fdbec6SFrançois Tigeot data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK; 199a2fdbec6SFrançois Tigeot else 200a2fdbec6SFrançois Tigeot data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK | 201a2fdbec6SFrançois Tigeot GPIO_DATA_VAL_MASK; 202bad0eccaSFrançois Tigeot 2039f4ca867SFrançois Tigeot I915_WRITE_NOTRACE(bus->gpio_reg, reserved | data_bits); 2049f4ca867SFrançois Tigeot POSTING_READ(bus->gpio_reg); 205bad0eccaSFrançois Tigeot } 206bad0eccaSFrançois Tigeot 207bad0eccaSFrançois Tigeot static int 2089f4ca867SFrançois Tigeot intel_gpio_pre_xfer(struct i2c_adapter *adapter) 209bad0eccaSFrançois Tigeot { 2109f4ca867SFrançois Tigeot struct intel_gmbus *bus = container_of(adapter, 2119f4ca867SFrançois Tigeot struct intel_gmbus, 2129f4ca867SFrançois Tigeot adapter); 2139f4ca867SFrançois Tigeot struct drm_i915_private *dev_priv = bus->dev_priv; 214a2fdbec6SFrançois Tigeot 2159f4ca867SFrançois Tigeot intel_i2c_reset(dev_priv->dev); 216a2fdbec6SFrançois Tigeot intel_i2c_quirk_set(dev_priv, true); 2179f4ca867SFrançois Tigeot set_data(bus, 1); 2189f4ca867SFrançois Tigeot set_clock(bus, 1); 2199f4ca867SFrançois Tigeot udelay(I2C_RISEFALL_TIME); 2209f4ca867SFrançois Tigeot return 0; 221a2fdbec6SFrançois Tigeot } 222a2fdbec6SFrançois Tigeot 2239f4ca867SFrançois Tigeot static void 2249f4ca867SFrançois Tigeot intel_gpio_post_xfer(struct i2c_adapter *adapter) 2259f4ca867SFrançois Tigeot { 2269f4ca867SFrançois Tigeot struct intel_gmbus *bus = container_of(adapter, 2279f4ca867SFrançois Tigeot struct intel_gmbus, 2289f4ca867SFrançois Tigeot adapter); 2299f4ca867SFrançois Tigeot struct drm_i915_private *dev_priv = bus->dev_priv; 2309f4ca867SFrançois Tigeot 2319f4ca867SFrançois Tigeot set_data(bus, 1); 2329f4ca867SFrançois Tigeot set_clock(bus, 1); 2339f4ca867SFrançois Tigeot intel_i2c_quirk_set(dev_priv, false); 2349f4ca867SFrançois Tigeot } 2359f4ca867SFrançois Tigeot 2369f4ca867SFrançois Tigeot static void 2379f4ca867SFrançois Tigeot intel_gpio_setup(struct intel_gmbus *bus, unsigned int pin) 2389f4ca867SFrançois Tigeot { 2399f4ca867SFrançois Tigeot struct drm_i915_private *dev_priv = bus->dev_priv; 2409f4ca867SFrançois Tigeot struct i2c_algo_bit_data *algo; 2419f4ca867SFrançois Tigeot 2429f4ca867SFrançois Tigeot algo = &bus->bit_algo; 2439f4ca867SFrançois Tigeot 244*aee94f86SFrançois Tigeot bus->gpio_reg = _MMIO(dev_priv->gpio_mmio_base + 245*aee94f86SFrançois Tigeot i915_mmio_reg_offset(get_gmbus_pin(dev_priv, pin)->reg)); 2469f4ca867SFrançois Tigeot bus->adapter.algo_data = algo; 2479f4ca867SFrançois Tigeot algo->setsda = set_data; 2489f4ca867SFrançois Tigeot algo->setscl = set_clock; 2499f4ca867SFrançois Tigeot algo->getsda = get_data; 2509f4ca867SFrançois Tigeot algo->getscl = get_clock; 2519f4ca867SFrançois Tigeot algo->pre_xfer = intel_gpio_pre_xfer; 2529f4ca867SFrançois Tigeot algo->post_xfer = intel_gpio_post_xfer; 2539f4ca867SFrançois Tigeot algo->udelay = I2C_RISEFALL_TIME; 2549f4ca867SFrançois Tigeot algo->timeout = usecs_to_jiffies(2200); 2559f4ca867SFrançois Tigeot algo->data = bus; 256a2fdbec6SFrançois Tigeot } 257a2fdbec6SFrançois Tigeot 258a2fdbec6SFrançois Tigeot static int 259a2fdbec6SFrançois Tigeot gmbus_wait_hw_status(struct drm_i915_private *dev_priv, 260a2fdbec6SFrançois Tigeot u32 gmbus2_status, 261a2fdbec6SFrançois Tigeot u32 gmbus4_irq_en) 262a2fdbec6SFrançois Tigeot { 263a2fdbec6SFrançois Tigeot int i; 264a2fdbec6SFrançois Tigeot u32 gmbus2 = 0; 265a2fdbec6SFrançois Tigeot DEFINE_WAIT(wait); 266a2fdbec6SFrançois Tigeot 267a2fdbec6SFrançois Tigeot if (!HAS_GMBUS_IRQ(dev_priv->dev)) 268a2fdbec6SFrançois Tigeot gmbus4_irq_en = 0; 269a2fdbec6SFrançois Tigeot 270a2fdbec6SFrançois Tigeot /* Important: The hw handles only the first bit, so set only one! Since 271a2fdbec6SFrançois Tigeot * we also need to check for NAKs besides the hw ready/idle signal, we 272a2fdbec6SFrançois Tigeot * need to wake up periodically and check that ourselves. */ 273352ff8bdSFrançois Tigeot I915_WRITE(GMBUS4, gmbus4_irq_en); 274a2fdbec6SFrançois Tigeot 2758e26cdf6SFrançois Tigeot for (i = 0; i < msecs_to_jiffies_timeout(50); i++) { 276a2fdbec6SFrançois Tigeot prepare_to_wait(&dev_priv->gmbus_wait_queue, &wait, 277a2fdbec6SFrançois Tigeot TASK_UNINTERRUPTIBLE); 278a2fdbec6SFrançois Tigeot 279352ff8bdSFrançois Tigeot gmbus2 = I915_READ_NOTRACE(GMBUS2); 280a2fdbec6SFrançois Tigeot if (gmbus2 & (GMBUS_SATOER | gmbus2_status)) 281a2fdbec6SFrançois Tigeot break; 282a2fdbec6SFrançois Tigeot 283a2fdbec6SFrançois Tigeot schedule_timeout(1); 284a2fdbec6SFrançois Tigeot } 285a2fdbec6SFrançois Tigeot finish_wait(&dev_priv->gmbus_wait_queue, &wait); 286a2fdbec6SFrançois Tigeot 287352ff8bdSFrançois Tigeot I915_WRITE(GMBUS4, 0); 288a2fdbec6SFrançois Tigeot 289a2fdbec6SFrançois Tigeot if (gmbus2 & GMBUS_SATOER) 290a2fdbec6SFrançois Tigeot return -ENXIO; 291a2fdbec6SFrançois Tigeot if (gmbus2 & gmbus2_status) 292a2fdbec6SFrançois Tigeot return 0; 293a2fdbec6SFrançois Tigeot return -ETIMEDOUT; 294a2fdbec6SFrançois Tigeot } 295a2fdbec6SFrançois Tigeot 296a2fdbec6SFrançois Tigeot static int 297a2fdbec6SFrançois Tigeot gmbus_wait_idle(struct drm_i915_private *dev_priv) 298a2fdbec6SFrançois Tigeot { 299a2fdbec6SFrançois Tigeot int ret; 300a2fdbec6SFrançois Tigeot 301352ff8bdSFrançois Tigeot #define C ((I915_READ_NOTRACE(GMBUS2) & GMBUS_ACTIVE) == 0) 302a2fdbec6SFrançois Tigeot 303a2fdbec6SFrançois Tigeot if (!HAS_GMBUS_IRQ(dev_priv->dev)) 304a2fdbec6SFrançois Tigeot return wait_for(C, 10); 305a2fdbec6SFrançois Tigeot 306a2fdbec6SFrançois Tigeot /* Important: The hw handles only the first bit, so set only one! */ 307352ff8bdSFrançois Tigeot I915_WRITE(GMBUS4, GMBUS_IDLE_EN); 308a2fdbec6SFrançois Tigeot 3098e26cdf6SFrançois Tigeot ret = wait_event_timeout(dev_priv->gmbus_wait_queue, C, 3108e26cdf6SFrançois Tigeot msecs_to_jiffies_timeout(10)); 311a2fdbec6SFrançois Tigeot 312352ff8bdSFrançois Tigeot I915_WRITE(GMBUS4, 0); 313a2fdbec6SFrançois Tigeot 314a2fdbec6SFrançois Tigeot if (ret) 315a2fdbec6SFrançois Tigeot return 0; 316a2fdbec6SFrançois Tigeot else 317a2fdbec6SFrançois Tigeot return -ETIMEDOUT; 318a2fdbec6SFrançois Tigeot #undef C 319a2fdbec6SFrançois Tigeot } 320a2fdbec6SFrançois Tigeot 321a2fdbec6SFrançois Tigeot static int 322477eb7f9SFrançois Tigeot gmbus_xfer_read_chunk(struct drm_i915_private *dev_priv, 323477eb7f9SFrançois Tigeot unsigned short addr, u8 *buf, unsigned int len, 324a2fdbec6SFrançois Tigeot u32 gmbus1_index) 325a2fdbec6SFrançois Tigeot { 326352ff8bdSFrançois Tigeot I915_WRITE(GMBUS1, 327a2fdbec6SFrançois Tigeot gmbus1_index | 328a2fdbec6SFrançois Tigeot GMBUS_CYCLE_WAIT | 329a2fdbec6SFrançois Tigeot (len << GMBUS_BYTE_COUNT_SHIFT) | 330477eb7f9SFrançois Tigeot (addr << GMBUS_SLAVE_ADDR_SHIFT) | 331a2fdbec6SFrançois Tigeot GMBUS_SLAVE_READ | GMBUS_SW_RDY); 332a2fdbec6SFrançois Tigeot while (len) { 333a2fdbec6SFrançois Tigeot int ret; 334a2fdbec6SFrançois Tigeot u32 val, loop = 0; 335a2fdbec6SFrançois Tigeot 336a2fdbec6SFrançois Tigeot ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_RDY, 337a2fdbec6SFrançois Tigeot GMBUS_HW_RDY_EN); 338a2fdbec6SFrançois Tigeot if (ret) 339a2fdbec6SFrançois Tigeot return ret; 340a2fdbec6SFrançois Tigeot 341352ff8bdSFrançois Tigeot val = I915_READ(GMBUS3); 342a2fdbec6SFrançois Tigeot do { 343a2fdbec6SFrançois Tigeot *buf++ = val & 0xff; 344a2fdbec6SFrançois Tigeot val >>= 8; 345a2fdbec6SFrançois Tigeot } while (--len && ++loop < 4); 346a2fdbec6SFrançois Tigeot } 347a2fdbec6SFrançois Tigeot 348a2fdbec6SFrançois Tigeot return 0; 349a2fdbec6SFrançois Tigeot } 350a2fdbec6SFrançois Tigeot 351a2fdbec6SFrançois Tigeot static int 352477eb7f9SFrançois Tigeot gmbus_xfer_read(struct drm_i915_private *dev_priv, struct i2c_msg *msg, 353477eb7f9SFrançois Tigeot u32 gmbus1_index) 354477eb7f9SFrançois Tigeot { 355477eb7f9SFrançois Tigeot u8 *buf = msg->buf; 356477eb7f9SFrançois Tigeot unsigned int rx_size = msg->len; 357477eb7f9SFrançois Tigeot unsigned int len; 358477eb7f9SFrançois Tigeot int ret; 359477eb7f9SFrançois Tigeot 360477eb7f9SFrançois Tigeot do { 361477eb7f9SFrançois Tigeot len = min(rx_size, GMBUS_BYTE_COUNT_MAX); 362477eb7f9SFrançois Tigeot 3639f4ca867SFrançois Tigeot ret = gmbus_xfer_read_chunk(dev_priv, msg->addr, 364477eb7f9SFrançois Tigeot buf, len, gmbus1_index); 365477eb7f9SFrançois Tigeot if (ret) 366477eb7f9SFrançois Tigeot return ret; 367477eb7f9SFrançois Tigeot 368477eb7f9SFrançois Tigeot rx_size -= len; 369477eb7f9SFrançois Tigeot buf += len; 370477eb7f9SFrançois Tigeot } while (rx_size != 0); 371477eb7f9SFrançois Tigeot 372477eb7f9SFrançois Tigeot return 0; 373477eb7f9SFrançois Tigeot } 374477eb7f9SFrançois Tigeot 375477eb7f9SFrançois Tigeot static int 376477eb7f9SFrançois Tigeot gmbus_xfer_write_chunk(struct drm_i915_private *dev_priv, 377477eb7f9SFrançois Tigeot unsigned short addr, u8 *buf, unsigned int len) 378a2fdbec6SFrançois Tigeot { 379477eb7f9SFrançois Tigeot unsigned int chunk_size = len; 380bad0eccaSFrançois Tigeot u32 val, loop; 381bad0eccaSFrançois Tigeot 382a2fdbec6SFrançois Tigeot val = loop = 0; 383a2fdbec6SFrançois Tigeot while (len && loop < 4) { 384a2fdbec6SFrançois Tigeot val |= *buf++ << (8 * loop++); 385a2fdbec6SFrançois Tigeot len -= 1; 386a2fdbec6SFrançois Tigeot } 387a2fdbec6SFrançois Tigeot 388352ff8bdSFrançois Tigeot I915_WRITE(GMBUS3, val); 389352ff8bdSFrançois Tigeot I915_WRITE(GMBUS1, 390a2fdbec6SFrançois Tigeot GMBUS_CYCLE_WAIT | 391477eb7f9SFrançois Tigeot (chunk_size << GMBUS_BYTE_COUNT_SHIFT) | 392477eb7f9SFrançois Tigeot (addr << GMBUS_SLAVE_ADDR_SHIFT) | 393a2fdbec6SFrançois Tigeot GMBUS_SLAVE_WRITE | GMBUS_SW_RDY); 394a2fdbec6SFrançois Tigeot while (len) { 395a2fdbec6SFrançois Tigeot int ret; 396a2fdbec6SFrançois Tigeot 397a2fdbec6SFrançois Tigeot val = loop = 0; 398a2fdbec6SFrançois Tigeot do { 399a2fdbec6SFrançois Tigeot val |= *buf++ << (8 * loop); 400a2fdbec6SFrançois Tigeot } while (--len && ++loop < 4); 401a2fdbec6SFrançois Tigeot 402352ff8bdSFrançois Tigeot I915_WRITE(GMBUS3, val); 403a2fdbec6SFrançois Tigeot 404a2fdbec6SFrançois Tigeot ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_RDY, 405a2fdbec6SFrançois Tigeot GMBUS_HW_RDY_EN); 406a2fdbec6SFrançois Tigeot if (ret) 407a2fdbec6SFrançois Tigeot return ret; 408a2fdbec6SFrançois Tigeot } 409477eb7f9SFrançois Tigeot 410477eb7f9SFrançois Tigeot return 0; 411477eb7f9SFrançois Tigeot } 412477eb7f9SFrançois Tigeot 413477eb7f9SFrançois Tigeot static int 414477eb7f9SFrançois Tigeot gmbus_xfer_write(struct drm_i915_private *dev_priv, struct i2c_msg *msg) 415477eb7f9SFrançois Tigeot { 416477eb7f9SFrançois Tigeot u8 *buf = msg->buf; 417477eb7f9SFrançois Tigeot unsigned int tx_size = msg->len; 418477eb7f9SFrançois Tigeot unsigned int len; 419477eb7f9SFrançois Tigeot int ret; 420477eb7f9SFrançois Tigeot 421477eb7f9SFrançois Tigeot do { 422477eb7f9SFrançois Tigeot len = min(tx_size, GMBUS_BYTE_COUNT_MAX); 423477eb7f9SFrançois Tigeot 4249f4ca867SFrançois Tigeot ret = gmbus_xfer_write_chunk(dev_priv, msg->addr, buf, len); 425477eb7f9SFrançois Tigeot if (ret) 426477eb7f9SFrançois Tigeot return ret; 427477eb7f9SFrançois Tigeot 428477eb7f9SFrançois Tigeot buf += len; 429477eb7f9SFrançois Tigeot tx_size -= len; 430477eb7f9SFrançois Tigeot } while (tx_size != 0); 431477eb7f9SFrançois Tigeot 432a2fdbec6SFrançois Tigeot return 0; 433a2fdbec6SFrançois Tigeot } 434a2fdbec6SFrançois Tigeot 435a2fdbec6SFrançois Tigeot /* 436a2fdbec6SFrançois Tigeot * The gmbus controller can combine a 1 or 2 byte write with a read that 437a2fdbec6SFrançois Tigeot * immediately follows it by using an "INDEX" cycle. 438a2fdbec6SFrançois Tigeot */ 439a2fdbec6SFrançois Tigeot static bool 440a2fdbec6SFrançois Tigeot gmbus_is_index_read(struct i2c_msg *msgs, int i, int num) 441a2fdbec6SFrançois Tigeot { 442a2fdbec6SFrançois Tigeot return (i + 1 < num && 443a2fdbec6SFrançois Tigeot !(msgs[i].flags & I2C_M_RD) && msgs[i].len <= 2 && 444a2fdbec6SFrançois Tigeot (msgs[i + 1].flags & I2C_M_RD)); 445a2fdbec6SFrançois Tigeot } 446a2fdbec6SFrançois Tigeot 447a2fdbec6SFrançois Tigeot static int 448a2fdbec6SFrançois Tigeot gmbus_xfer_index_read(struct drm_i915_private *dev_priv, struct i2c_msg *msgs) 449a2fdbec6SFrançois Tigeot { 450a2fdbec6SFrançois Tigeot u32 gmbus1_index = 0; 451a2fdbec6SFrançois Tigeot u32 gmbus5 = 0; 452a2fdbec6SFrançois Tigeot int ret; 453a2fdbec6SFrançois Tigeot 454a2fdbec6SFrançois Tigeot if (msgs[0].len == 2) 455a2fdbec6SFrançois Tigeot gmbus5 = GMBUS_2BYTE_INDEX_EN | 456a2fdbec6SFrançois Tigeot msgs[0].buf[1] | (msgs[0].buf[0] << 8); 457a2fdbec6SFrançois Tigeot if (msgs[0].len == 1) 458a2fdbec6SFrançois Tigeot gmbus1_index = GMBUS_CYCLE_INDEX | 459a2fdbec6SFrançois Tigeot (msgs[0].buf[0] << GMBUS_SLAVE_INDEX_SHIFT); 460a2fdbec6SFrançois Tigeot 461a2fdbec6SFrançois Tigeot /* GMBUS5 holds 16-bit index */ 462a2fdbec6SFrançois Tigeot if (gmbus5) 463352ff8bdSFrançois Tigeot I915_WRITE(GMBUS5, gmbus5); 464a2fdbec6SFrançois Tigeot 465a2fdbec6SFrançois Tigeot ret = gmbus_xfer_read(dev_priv, &msgs[1], gmbus1_index); 466a2fdbec6SFrançois Tigeot 467a2fdbec6SFrançois Tigeot /* Clear GMBUS5 after each index transfer */ 468a2fdbec6SFrançois Tigeot if (gmbus5) 469352ff8bdSFrançois Tigeot I915_WRITE(GMBUS5, 0); 470a2fdbec6SFrançois Tigeot 471a2fdbec6SFrançois Tigeot return ret; 472a2fdbec6SFrançois Tigeot } 473a2fdbec6SFrançois Tigeot 474a2fdbec6SFrançois Tigeot static int 475*aee94f86SFrançois Tigeot do_gmbus_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num) 476a2fdbec6SFrançois Tigeot { 4779f4ca867SFrançois Tigeot struct intel_gmbus *bus = container_of(adapter, 4789f4ca867SFrançois Tigeot struct intel_gmbus, 4799f4ca867SFrançois Tigeot adapter); 4809f4ca867SFrançois Tigeot struct drm_i915_private *dev_priv = bus->dev_priv; 481352ff8bdSFrançois Tigeot int i = 0, inc, try = 0; 482a2fdbec6SFrançois Tigeot int ret = 0; 483a2fdbec6SFrançois Tigeot 484477eb7f9SFrançois Tigeot retry: 4859f4ca867SFrançois Tigeot I915_WRITE(GMBUS0, bus->reg0); 486bad0eccaSFrançois Tigeot 487477eb7f9SFrançois Tigeot for (; i < num; i += inc) { 488477eb7f9SFrançois Tigeot inc = 1; 489a2fdbec6SFrançois Tigeot if (gmbus_is_index_read(msgs, i, num)) { 490a2fdbec6SFrançois Tigeot ret = gmbus_xfer_index_read(dev_priv, &msgs[i]); 491477eb7f9SFrançois Tigeot inc = 2; /* an index read is two msgs */ 492a2fdbec6SFrançois Tigeot } else if (msgs[i].flags & I2C_M_RD) { 493a2fdbec6SFrançois Tigeot ret = gmbus_xfer_read(dev_priv, &msgs[i], 0); 494bad0eccaSFrançois Tigeot } else { 495a2fdbec6SFrançois Tigeot ret = gmbus_xfer_write(dev_priv, &msgs[i]); 496a2fdbec6SFrançois Tigeot } 497bad0eccaSFrançois Tigeot 498*aee94f86SFrançois Tigeot if (!ret) 499a2fdbec6SFrançois Tigeot ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_WAIT_PHASE, 500a2fdbec6SFrançois Tigeot GMBUS_HW_WAIT_EN); 501*aee94f86SFrançois Tigeot if (ret == -ETIMEDOUT) 502a2fdbec6SFrançois Tigeot goto timeout; 503*aee94f86SFrançois Tigeot else if (ret) 504*aee94f86SFrançois Tigeot goto clear_err; 505bad0eccaSFrançois Tigeot } 506bad0eccaSFrançois Tigeot 507a2fdbec6SFrançois Tigeot /* Generate a STOP condition on the bus. Note that gmbus can't generata 508a2fdbec6SFrançois Tigeot * a STOP on the very first cycle. To simplify the code we 509a2fdbec6SFrançois Tigeot * unconditionally generate the STOP condition with an additional gmbus 510a2fdbec6SFrançois Tigeot * cycle. */ 511352ff8bdSFrançois Tigeot I915_WRITE(GMBUS1, GMBUS_CYCLE_STOP | GMBUS_SW_RDY); 512a2fdbec6SFrançois Tigeot 513bad0eccaSFrançois Tigeot /* Mark the GMBUS interface as disabled after waiting for idle. 514bad0eccaSFrançois Tigeot * We will re-enable it at the start of the next xfer, 515bad0eccaSFrançois Tigeot * till then let it sleep. 516bad0eccaSFrançois Tigeot */ 517a2fdbec6SFrançois Tigeot if (gmbus_wait_idle(dev_priv)) { 518a2fdbec6SFrançois Tigeot DRM_DEBUG_KMS("GMBUS [%s] timed out waiting for idle\n", 5199f4ca867SFrançois Tigeot adapter->name); 520a2fdbec6SFrançois Tigeot ret = -ETIMEDOUT; 521a2fdbec6SFrançois Tigeot } 522352ff8bdSFrançois Tigeot I915_WRITE(GMBUS0, 0); 523a2fdbec6SFrançois Tigeot ret = ret ?: i; 5249f4ca867SFrançois Tigeot goto out; 525bad0eccaSFrançois Tigeot 526bad0eccaSFrançois Tigeot clear_err: 527a2fdbec6SFrançois Tigeot /* 528a2fdbec6SFrançois Tigeot * Wait for bus to IDLE before clearing NAK. 529a2fdbec6SFrançois Tigeot * If we clear the NAK while bus is still active, then it will stay 530a2fdbec6SFrançois Tigeot * active and the next transaction may fail. 531a2fdbec6SFrançois Tigeot * 532a2fdbec6SFrançois Tigeot * If no ACK is received during the address phase of a transaction, the 533a2fdbec6SFrançois Tigeot * adapter must report -ENXIO. It is not clear what to return if no ACK 534a2fdbec6SFrançois Tigeot * is received at other times. But we have to be careful to not return 535a2fdbec6SFrançois Tigeot * spurious -ENXIO because that will prevent i2c and drm edid functions 536a2fdbec6SFrançois Tigeot * from retrying. So return -ENXIO only when gmbus properly quiescents - 537a2fdbec6SFrançois Tigeot * timing out seems to happen when there _is_ a ddc chip present, but 538a2fdbec6SFrançois Tigeot * it's slow responding and only answers on the 2nd retry. 539a2fdbec6SFrançois Tigeot */ 540a2fdbec6SFrançois Tigeot ret = -ENXIO; 541a2fdbec6SFrançois Tigeot if (gmbus_wait_idle(dev_priv)) { 542a2fdbec6SFrançois Tigeot DRM_DEBUG_KMS("GMBUS [%s] timed out after NAK\n", 5439f4ca867SFrançois Tigeot adapter->name); 544a2fdbec6SFrançois Tigeot ret = -ETIMEDOUT; 545a2fdbec6SFrançois Tigeot } 546a2fdbec6SFrançois Tigeot 547bad0eccaSFrançois Tigeot /* Toggle the Software Clear Interrupt bit. This has the effect 548bad0eccaSFrançois Tigeot * of resetting the GMBUS controller and so clearing the 549bad0eccaSFrançois Tigeot * BUS_ERROR raised by the slave's NAK. 550bad0eccaSFrançois Tigeot */ 551352ff8bdSFrançois Tigeot I915_WRITE(GMBUS1, GMBUS_SW_CLR_INT); 552352ff8bdSFrançois Tigeot I915_WRITE(GMBUS1, 0); 553352ff8bdSFrançois Tigeot I915_WRITE(GMBUS0, 0); 554bad0eccaSFrançois Tigeot 555a2fdbec6SFrançois Tigeot DRM_DEBUG_KMS("GMBUS [%s] NAK for addr: %04x %c(%d)\n", 5569f4ca867SFrançois Tigeot adapter->name, msgs[i].addr, 557a2fdbec6SFrançois Tigeot (msgs[i].flags & I2C_M_RD) ? 'r' : 'w', msgs[i].len); 558bad0eccaSFrançois Tigeot 559477eb7f9SFrançois Tigeot /* 560477eb7f9SFrançois Tigeot * Passive adapters sometimes NAK the first probe. Retry the first 561477eb7f9SFrançois Tigeot * message once on -ENXIO for GMBUS transfers; the bit banging algorithm 562477eb7f9SFrançois Tigeot * has retries internally. See also the retry loop in 563477eb7f9SFrançois Tigeot * drm_do_probe_ddc_edid, which bails out on the first -ENXIO. 564477eb7f9SFrançois Tigeot */ 565477eb7f9SFrançois Tigeot if (ret == -ENXIO && i == 0 && try++ == 0) { 566477eb7f9SFrançois Tigeot DRM_DEBUG_KMS("GMBUS [%s] NAK on first message, retry\n", 5679f4ca867SFrançois Tigeot adapter->name); 568477eb7f9SFrançois Tigeot goto retry; 569477eb7f9SFrançois Tigeot } 570477eb7f9SFrançois Tigeot 571bad0eccaSFrançois Tigeot goto out; 572a2fdbec6SFrançois Tigeot 573a2fdbec6SFrançois Tigeot timeout: 574a2fdbec6SFrançois Tigeot DRM_INFO("GMBUS [%s] timed out, falling back to bit banging on pin %d\n", 5759f4ca867SFrançois Tigeot bus->adapter.name, bus->reg0 & 0xff); 576352ff8bdSFrançois Tigeot I915_WRITE(GMBUS0, 0); 577a2fdbec6SFrançois Tigeot 578*aee94f86SFrançois Tigeot /* 579*aee94f86SFrançois Tigeot * Hardware may not support GMBUS over these pins? Try GPIO bitbanging 580*aee94f86SFrançois Tigeot * instead. Use EAGAIN to have i2c core retry. 581*aee94f86SFrançois Tigeot */ 5829f4ca867SFrançois Tigeot bus->force_bit = 1; 583*aee94f86SFrançois Tigeot ret = -EAGAIN; 584a2fdbec6SFrançois Tigeot 585a2fdbec6SFrançois Tigeot out: 586*aee94f86SFrançois Tigeot return ret; 587*aee94f86SFrançois Tigeot } 588352ff8bdSFrançois Tigeot 589*aee94f86SFrançois Tigeot static int 590*aee94f86SFrançois Tigeot gmbus_xfer(struct i2c_adapter *adapter, struct i2c_msg *msgs, int num) 591*aee94f86SFrançois Tigeot { 592*aee94f86SFrançois Tigeot struct intel_gmbus *bus = container_of(adapter, struct intel_gmbus, 593*aee94f86SFrançois Tigeot adapter); 594*aee94f86SFrançois Tigeot struct drm_i915_private *dev_priv = bus->dev_priv; 595*aee94f86SFrançois Tigeot int ret; 596*aee94f86SFrançois Tigeot 597*aee94f86SFrançois Tigeot intel_display_power_get(dev_priv, POWER_DOMAIN_GMBUS); 598*aee94f86SFrançois Tigeot mutex_lock(&dev_priv->gmbus_mutex); 599*aee94f86SFrançois Tigeot 600*aee94f86SFrançois Tigeot if (bus->force_bit) 601*aee94f86SFrançois Tigeot ret = i2c_bit_algo.master_xfer(adapter, msgs, num); 602*aee94f86SFrançois Tigeot else 603*aee94f86SFrançois Tigeot ret = do_gmbus_xfer(adapter, msgs, num); 604*aee94f86SFrançois Tigeot 605*aee94f86SFrançois Tigeot mutex_unlock(&dev_priv->gmbus_mutex); 606352ff8bdSFrançois Tigeot intel_display_power_put(dev_priv, POWER_DOMAIN_GMBUS); 607352ff8bdSFrançois Tigeot 608a2fdbec6SFrançois Tigeot return ret; 609bad0eccaSFrançois Tigeot } 610bad0eccaSFrançois Tigeot 6119f4ca867SFrançois Tigeot static u32 gmbus_func(struct i2c_adapter *adapter) 61219df918dSFrançois Tigeot { 6139f4ca867SFrançois Tigeot return i2c_bit_algo.functionality(adapter) & 6149f4ca867SFrançois Tigeot (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | 6159f4ca867SFrançois Tigeot /* I2C_FUNC_10BIT_ADDR | */ 6169f4ca867SFrançois Tigeot I2C_FUNC_SMBUS_READ_BLOCK_DATA | 6179f4ca867SFrançois Tigeot I2C_FUNC_SMBUS_BLOCK_PROC_CALL); 61819df918dSFrançois Tigeot } 61919df918dSFrançois Tigeot 6209f4ca867SFrançois Tigeot static const struct i2c_algorithm gmbus_algorithm = { 6219f4ca867SFrançois Tigeot .master_xfer = gmbus_xfer, 6229f4ca867SFrançois Tigeot .functionality = gmbus_func 623bad0eccaSFrançois Tigeot }; 624a2fdbec6SFrançois Tigeot 62519c468b4SFrançois Tigeot /** 62619c468b4SFrançois Tigeot * intel_gmbus_setup - instantiate all Intel i2c GMBuses 62719c468b4SFrançois Tigeot * @dev: DRM device 62819c468b4SFrançois Tigeot */ 62919c468b4SFrançois Tigeot int intel_setup_gmbus(struct drm_device *dev) 630bad0eccaSFrançois Tigeot { 631a2fdbec6SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 6329f4ca867SFrançois Tigeot struct intel_gmbus *bus; 63319c468b4SFrançois Tigeot unsigned int pin; 63419c468b4SFrançois Tigeot int ret; 635bad0eccaSFrançois Tigeot 6368e26cdf6SFrançois Tigeot if (HAS_PCH_NOP(dev)) 6378e26cdf6SFrançois Tigeot return 0; 638*aee94f86SFrançois Tigeot 639*aee94f86SFrançois Tigeot if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) 640a2fdbec6SFrançois Tigeot dev_priv->gpio_mmio_base = VLV_DISPLAY_BASE; 641*aee94f86SFrançois Tigeot else if (!HAS_GMCH_DISPLAY(dev_priv)) 642*aee94f86SFrançois Tigeot dev_priv->gpio_mmio_base = 643*aee94f86SFrançois Tigeot i915_mmio_reg_offset(PCH_GPIOA) - 644*aee94f86SFrançois Tigeot i915_mmio_reg_offset(GPIOA); 645a2fdbec6SFrançois Tigeot 64619df918dSFrançois Tigeot lockinit(&dev_priv->gmbus_mutex, "gmbus", 0, LK_CANRECURSE); 647a2fdbec6SFrançois Tigeot init_waitqueue_head(&dev_priv->gmbus_wait_queue); 648a2fdbec6SFrançois Tigeot 6499f4ca867SFrançois Tigeot for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) { 65019c468b4SFrançois Tigeot if (!intel_gmbus_is_valid_pin(dev_priv, pin)) 65119c468b4SFrançois Tigeot continue; 65219c468b4SFrançois Tigeot 6539f4ca867SFrançois Tigeot bus = &dev_priv->gmbus[pin]; 6549f4ca867SFrançois Tigeot 6559f4ca867SFrançois Tigeot #if 0 6569f4ca867SFrançois Tigeot bus->adapter.owner = THIS_MODULE; 6579f4ca867SFrançois Tigeot bus->adapter.class = I2C_CLASS_DDC; 6589f4ca867SFrançois Tigeot #endif 6599f4ca867SFrançois Tigeot ksnprintf(bus->adapter.name, 6609f4ca867SFrançois Tigeot sizeof(bus->adapter.name), 6619f4ca867SFrançois Tigeot "i915 gmbus %s", 6629f4ca867SFrançois Tigeot get_gmbus_pin(dev_priv, pin)->name); 6639f4ca867SFrançois Tigeot 6649f4ca867SFrançois Tigeot bus->adapter.dev.parent = &dev->pdev->dev; 6659f4ca867SFrançois Tigeot bus->dev_priv = dev_priv; 6669f4ca867SFrançois Tigeot 6679f4ca867SFrançois Tigeot bus->adapter.algo = &gmbus_algorithm; 6689f4ca867SFrançois Tigeot 669*aee94f86SFrançois Tigeot /* 670*aee94f86SFrançois Tigeot * We wish to retry with bit banging 671*aee94f86SFrançois Tigeot * after a timed out GMBUS attempt. 672*aee94f86SFrançois Tigeot */ 673*aee94f86SFrançois Tigeot bus->adapter.retries = 1; 674*aee94f86SFrançois Tigeot 6759f4ca867SFrançois Tigeot /* By default use a conservative clock rate */ 6769f4ca867SFrançois Tigeot bus->reg0 = pin | GMBUS_RATE_100KHZ; 6779f4ca867SFrançois Tigeot 6789f4ca867SFrançois Tigeot /* gmbus seems to be broken on i830 */ 6799f4ca867SFrançois Tigeot if (IS_I830(dev)) 6809f4ca867SFrançois Tigeot bus->force_bit = 1; 6819f4ca867SFrançois Tigeot 6829f4ca867SFrançois Tigeot intel_gpio_setup(bus, pin); 6839f4ca867SFrançois Tigeot 6849f4ca867SFrançois Tigeot ret = i2c_add_adapter(&bus->adapter); 6859f4ca867SFrançois Tigeot if (ret) 686bad0eccaSFrançois Tigeot goto err; 687bad0eccaSFrançois Tigeot } 688bad0eccaSFrançois Tigeot 6899f4ca867SFrançois Tigeot intel_i2c_reset(dev_priv->dev); 690bad0eccaSFrançois Tigeot 6919f4ca867SFrançois Tigeot return 0; 692bad0eccaSFrançois Tigeot 693bad0eccaSFrançois Tigeot err: 694*aee94f86SFrançois Tigeot while (pin--) { 6959f4ca867SFrançois Tigeot if (!intel_gmbus_is_valid_pin(dev_priv, pin)) 6969f4ca867SFrançois Tigeot continue; 6979f4ca867SFrançois Tigeot 6989f4ca867SFrançois Tigeot bus = &dev_priv->gmbus[pin]; 6999f4ca867SFrançois Tigeot i2c_del_adapter(&bus->adapter); 7009f4ca867SFrançois Tigeot } 7019f4ca867SFrançois Tigeot return ret; 702bad0eccaSFrançois Tigeot } 703bad0eccaSFrançois Tigeot 7049f4ca867SFrançois Tigeot struct i2c_adapter *intel_gmbus_get_adapter(struct drm_i915_private *dev_priv, 7059f4ca867SFrançois Tigeot unsigned int pin) 706bad0eccaSFrançois Tigeot { 7079f4ca867SFrançois Tigeot if (WARN_ON(!intel_gmbus_is_valid_pin(dev_priv, pin))) 7089f4ca867SFrançois Tigeot return NULL; 709bad0eccaSFrançois Tigeot 7109f4ca867SFrançois Tigeot return &dev_priv->gmbus[pin].adapter; 711bad0eccaSFrançois Tigeot } 712bad0eccaSFrançois Tigeot 7139f4ca867SFrançois Tigeot void intel_gmbus_set_speed(struct i2c_adapter *adapter, int speed) 714bad0eccaSFrançois Tigeot { 7159f4ca867SFrançois Tigeot struct intel_gmbus *bus = to_intel_gmbus(adapter); 716bad0eccaSFrançois Tigeot 7179f4ca867SFrançois Tigeot bus->reg0 = (bus->reg0 & ~(0x3 << 8)) | speed; 7189f4ca867SFrançois Tigeot } 7199f4ca867SFrançois Tigeot 7209f4ca867SFrançois Tigeot void intel_gmbus_force_bit(struct i2c_adapter *adapter, bool force_bit) 7219f4ca867SFrançois Tigeot { 7229f4ca867SFrançois Tigeot struct intel_gmbus *bus = to_intel_gmbus(adapter); 7239f4ca867SFrançois Tigeot 7249f4ca867SFrançois Tigeot bus->force_bit += force_bit ? 1 : -1; 7259f4ca867SFrançois Tigeot DRM_DEBUG_KMS("%sabling bit-banging on %s. force bit now %d\n", 7269f4ca867SFrançois Tigeot force_bit ? "en" : "dis", adapter->name, 7279f4ca867SFrançois Tigeot bus->force_bit); 7289f4ca867SFrançois Tigeot } 7299f4ca867SFrançois Tigeot 7309f4ca867SFrançois Tigeot void intel_teardown_gmbus(struct drm_device *dev) 7319f4ca867SFrançois Tigeot { 7329f4ca867SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 7339f4ca867SFrançois Tigeot struct intel_gmbus *bus; 7349f4ca867SFrançois Tigeot unsigned int pin; 7359f4ca867SFrançois Tigeot 7369f4ca867SFrançois Tigeot for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) { 7379f4ca867SFrançois Tigeot if (!intel_gmbus_is_valid_pin(dev_priv, pin)) 7389f4ca867SFrançois Tigeot continue; 7399f4ca867SFrançois Tigeot 7409f4ca867SFrançois Tigeot bus = &dev_priv->gmbus[pin]; 7419f4ca867SFrançois Tigeot i2c_del_adapter(&bus->adapter); 7429f4ca867SFrançois Tigeot } 743bad0eccaSFrançois Tigeot } 744