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