Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2006 Dave Airlie <airlied@linux.ie> |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 3 | * Copyright © 2006-2008,2010 Intel Corporation |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 4 | * Jesse Barnes <jesse.barnes@intel.com> |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the "Software"), |
| 8 | * to deal in the Software without restriction, including without limitation |
| 9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 10 | * and/or sell copies of the Software, and to permit persons to whom the |
| 11 | * Software is furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice (including the next |
| 14 | * paragraph) shall be included in all copies or substantial portions of the |
| 15 | * Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 23 | * DEALINGS IN THE SOFTWARE. |
| 24 | * |
| 25 | * Authors: |
| 26 | * Eric Anholt <eric@anholt.net> |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 27 | * Chris Wilson <chris@chris-wilson.co.uk> |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 28 | */ |
| 29 | #include <linux/i2c.h> |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 30 | #include <linux/i2c-algo-bit.h> |
Paul Gortmaker | 2d1a8a4 | 2011-08-30 18:16:33 -0400 | [diff] [blame] | 31 | #include <linux/export.h> |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 32 | #include "drmP.h" |
| 33 | #include "drm.h" |
| 34 | #include "intel_drv.h" |
| 35 | #include "i915_drm.h" |
| 36 | #include "i915_drv.h" |
| 37 | |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 38 | /* Intel GPIO access functions */ |
| 39 | |
Jean Delvare | 1849ecb | 2012-01-28 11:07:09 +0100 | [diff] [blame] | 40 | #define I2C_RISEFALL_TIME 10 |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 41 | |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 42 | static inline struct intel_gmbus * |
| 43 | to_intel_gmbus(struct i2c_adapter *i2c) |
| 44 | { |
| 45 | return container_of(i2c, struct intel_gmbus, adapter); |
| 46 | } |
| 47 | |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 48 | struct intel_gpio { |
| 49 | struct i2c_adapter adapter; |
| 50 | struct i2c_algo_bit_data algo; |
| 51 | struct drm_i915_private *dev_priv; |
| 52 | u32 reg; |
| 53 | }; |
| 54 | |
| 55 | void |
| 56 | intel_i2c_reset(struct drm_device *dev) |
Shaohua Li | 0ba0e9e | 2009-04-07 11:02:28 +0800 | [diff] [blame] | 57 | { |
| 58 | struct drm_i915_private *dev_priv = dev->dev_private; |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 59 | if (HAS_PCH_SPLIT(dev)) |
| 60 | I915_WRITE(PCH_GMBUS0, 0); |
| 61 | else |
| 62 | I915_WRITE(GMBUS0, 0); |
| 63 | } |
| 64 | |
| 65 | static void intel_i2c_quirk_set(struct drm_i915_private *dev_priv, bool enable) |
| 66 | { |
Chris Wilson | b222f26 | 2010-09-11 21:48:25 +0100 | [diff] [blame] | 67 | u32 val; |
Shaohua Li | 0ba0e9e | 2009-04-07 11:02:28 +0800 | [diff] [blame] | 68 | |
| 69 | /* When using bit bashing for I2C, this bit needs to be set to 1 */ |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 70 | if (!IS_PINEVIEW(dev_priv->dev)) |
Shaohua Li | 0ba0e9e | 2009-04-07 11:02:28 +0800 | [diff] [blame] | 71 | return; |
Chris Wilson | b222f26 | 2010-09-11 21:48:25 +0100 | [diff] [blame] | 72 | |
| 73 | val = I915_READ(DSPCLK_GATE_D); |
Shaohua Li | 0ba0e9e | 2009-04-07 11:02:28 +0800 | [diff] [blame] | 74 | if (enable) |
Chris Wilson | b222f26 | 2010-09-11 21:48:25 +0100 | [diff] [blame] | 75 | val |= DPCUNIT_CLOCK_GATE_DISABLE; |
Shaohua Li | 0ba0e9e | 2009-04-07 11:02:28 +0800 | [diff] [blame] | 76 | else |
Chris Wilson | b222f26 | 2010-09-11 21:48:25 +0100 | [diff] [blame] | 77 | val &= ~DPCUNIT_CLOCK_GATE_DISABLE; |
| 78 | I915_WRITE(DSPCLK_GATE_D, val); |
Shaohua Li | 0ba0e9e | 2009-04-07 11:02:28 +0800 | [diff] [blame] | 79 | } |
| 80 | |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 81 | static u32 get_reserved(struct intel_gpio *gpio) |
| 82 | { |
| 83 | struct drm_i915_private *dev_priv = gpio->dev_priv; |
| 84 | struct drm_device *dev = dev_priv->dev; |
| 85 | u32 reserved = 0; |
| 86 | |
| 87 | /* On most chips, these bits must be preserved in software. */ |
| 88 | if (!IS_I830(dev) && !IS_845G(dev)) |
Yuanhan Liu | db5e417 | 2010-11-08 09:58:16 +0000 | [diff] [blame] | 89 | reserved = I915_READ_NOTRACE(gpio->reg) & |
| 90 | (GPIO_DATA_PULLUP_DISABLE | |
| 91 | GPIO_CLOCK_PULLUP_DISABLE); |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 92 | |
| 93 | return reserved; |
| 94 | } |
| 95 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 96 | static int get_clock(void *data) |
| 97 | { |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 98 | struct intel_gpio *gpio = data; |
| 99 | struct drm_i915_private *dev_priv = gpio->dev_priv; |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 100 | u32 reserved = get_reserved(gpio); |
Yuanhan Liu | db5e417 | 2010-11-08 09:58:16 +0000 | [diff] [blame] | 101 | I915_WRITE_NOTRACE(gpio->reg, reserved | GPIO_CLOCK_DIR_MASK); |
| 102 | I915_WRITE_NOTRACE(gpio->reg, reserved); |
| 103 | return (I915_READ_NOTRACE(gpio->reg) & GPIO_CLOCK_VAL_IN) != 0; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static int get_data(void *data) |
| 107 | { |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 108 | struct intel_gpio *gpio = data; |
| 109 | struct drm_i915_private *dev_priv = gpio->dev_priv; |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 110 | u32 reserved = get_reserved(gpio); |
Yuanhan Liu | db5e417 | 2010-11-08 09:58:16 +0000 | [diff] [blame] | 111 | I915_WRITE_NOTRACE(gpio->reg, reserved | GPIO_DATA_DIR_MASK); |
| 112 | I915_WRITE_NOTRACE(gpio->reg, reserved); |
| 113 | return (I915_READ_NOTRACE(gpio->reg) & GPIO_DATA_VAL_IN) != 0; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static void set_clock(void *data, int state_high) |
| 117 | { |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 118 | struct intel_gpio *gpio = data; |
| 119 | struct drm_i915_private *dev_priv = gpio->dev_priv; |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 120 | u32 reserved = get_reserved(gpio); |
| 121 | u32 clock_bits; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 122 | |
| 123 | if (state_high) |
| 124 | clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK; |
| 125 | else |
| 126 | clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK | |
| 127 | GPIO_CLOCK_VAL_MASK; |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 128 | |
Yuanhan Liu | db5e417 | 2010-11-08 09:58:16 +0000 | [diff] [blame] | 129 | I915_WRITE_NOTRACE(gpio->reg, reserved | clock_bits); |
Chris Wilson | 374c479 | 2010-11-08 21:07:24 +0000 | [diff] [blame] | 130 | POSTING_READ(gpio->reg); |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | static void set_data(void *data, int state_high) |
| 134 | { |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 135 | struct intel_gpio *gpio = data; |
| 136 | struct drm_i915_private *dev_priv = gpio->dev_priv; |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 137 | u32 reserved = get_reserved(gpio); |
| 138 | u32 data_bits; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 139 | |
| 140 | if (state_high) |
| 141 | data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK; |
| 142 | else |
| 143 | data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK | |
| 144 | GPIO_DATA_VAL_MASK; |
| 145 | |
Yuanhan Liu | db5e417 | 2010-11-08 09:58:16 +0000 | [diff] [blame] | 146 | I915_WRITE_NOTRACE(gpio->reg, reserved | data_bits); |
Chris Wilson | 374c479 | 2010-11-08 21:07:24 +0000 | [diff] [blame] | 147 | POSTING_READ(gpio->reg); |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 150 | static struct i2c_adapter * |
| 151 | intel_gpio_create(struct drm_i915_private *dev_priv, u32 pin) |
Eric Anholt | f0217c4 | 2009-12-01 11:56:30 -0800 | [diff] [blame] | 152 | { |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 153 | static const int map_pin_to_reg[] = { |
| 154 | 0, |
| 155 | GPIOB, |
| 156 | GPIOA, |
| 157 | GPIOC, |
| 158 | GPIOD, |
| 159 | GPIOE, |
Zhenyu Wang | 7b5337d | 2010-10-13 16:40:12 +0800 | [diff] [blame] | 160 | 0, |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 161 | GPIOF, |
| 162 | }; |
| 163 | struct intel_gpio *gpio; |
Eric Anholt | f0217c4 | 2009-12-01 11:56:30 -0800 | [diff] [blame] | 164 | |
Jean Delvare | 6966945 | 2010-11-05 18:51:34 +0100 | [diff] [blame] | 165 | if (pin >= ARRAY_SIZE(map_pin_to_reg) || !map_pin_to_reg[pin]) |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 166 | return NULL; |
Eric Anholt | f0217c4 | 2009-12-01 11:56:30 -0800 | [diff] [blame] | 167 | |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 168 | gpio = kzalloc(sizeof(struct intel_gpio), GFP_KERNEL); |
| 169 | if (gpio == NULL) |
| 170 | return NULL; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 171 | |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 172 | gpio->reg = map_pin_to_reg[pin]; |
| 173 | if (HAS_PCH_SPLIT(dev_priv->dev)) |
| 174 | gpio->reg += PCH_GPIOA - GPIOA; |
| 175 | gpio->dev_priv = dev_priv; |
| 176 | |
Jean Delvare | 6966945 | 2010-11-05 18:51:34 +0100 | [diff] [blame] | 177 | snprintf(gpio->adapter.name, sizeof(gpio->adapter.name), |
| 178 | "i915 GPIO%c", "?BACDE?F"[pin]); |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 179 | gpio->adapter.owner = THIS_MODULE; |
| 180 | gpio->adapter.algo_data = &gpio->algo; |
| 181 | gpio->adapter.dev.parent = &dev_priv->dev->pdev->dev; |
| 182 | gpio->algo.setsda = set_data; |
| 183 | gpio->algo.setscl = set_clock; |
| 184 | gpio->algo.getsda = get_data; |
| 185 | gpio->algo.getscl = get_clock; |
| 186 | gpio->algo.udelay = I2C_RISEFALL_TIME; |
| 187 | gpio->algo.timeout = usecs_to_jiffies(2200); |
| 188 | gpio->algo.data = gpio; |
| 189 | |
| 190 | if (i2c_bit_add_bus(&gpio->adapter)) |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 191 | goto out_free; |
| 192 | |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 193 | return &gpio->adapter; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 194 | |
| 195 | out_free: |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 196 | kfree(gpio); |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 197 | return NULL; |
| 198 | } |
| 199 | |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 200 | static int |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 201 | intel_i2c_quirk_xfer(struct drm_i915_private *dev_priv, |
| 202 | struct i2c_adapter *adapter, |
| 203 | struct i2c_msg *msgs, |
| 204 | int num) |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 205 | { |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 206 | struct intel_gpio *gpio = container_of(adapter, |
| 207 | struct intel_gpio, |
| 208 | adapter); |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 209 | int ret; |
Keith Packard | f9c10a9 | 2009-05-30 12:16:25 -0700 | [diff] [blame] | 210 | |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 211 | intel_i2c_reset(dev_priv->dev); |
| 212 | |
| 213 | intel_i2c_quirk_set(dev_priv, true); |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 214 | set_data(gpio, 1); |
| 215 | set_clock(gpio, 1); |
| 216 | udelay(I2C_RISEFALL_TIME); |
| 217 | |
| 218 | ret = adapter->algo->master_xfer(adapter, msgs, num); |
| 219 | |
| 220 | set_data(gpio, 1); |
| 221 | set_clock(gpio, 1); |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 222 | intel_i2c_quirk_set(dev_priv, false); |
| 223 | |
| 224 | return ret; |
| 225 | } |
| 226 | |
| 227 | static int |
| 228 | gmbus_xfer(struct i2c_adapter *adapter, |
| 229 | struct i2c_msg *msgs, |
| 230 | int num) |
| 231 | { |
| 232 | struct intel_gmbus *bus = container_of(adapter, |
| 233 | struct intel_gmbus, |
| 234 | adapter); |
| 235 | struct drm_i915_private *dev_priv = adapter->algo_data; |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 236 | int i, reg_offset; |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 237 | |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 238 | if (bus->force_bit) |
| 239 | return intel_i2c_quirk_xfer(dev_priv, |
| 240 | bus->force_bit, msgs, num); |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 241 | |
| 242 | reg_offset = HAS_PCH_SPLIT(dev_priv->dev) ? PCH_GMBUS0 - GMBUS0 : 0; |
| 243 | |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 244 | I915_WRITE(GMBUS0 + reg_offset, bus->reg0); |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 245 | |
| 246 | for (i = 0; i < num; i++) { |
| 247 | u16 len = msgs[i].len; |
| 248 | u8 *buf = msgs[i].buf; |
| 249 | |
| 250 | if (msgs[i].flags & I2C_M_RD) { |
| 251 | I915_WRITE(GMBUS1 + reg_offset, |
| 252 | GMBUS_CYCLE_WAIT | (i + 1 == num ? GMBUS_CYCLE_STOP : 0) | |
| 253 | (len << GMBUS_BYTE_COUNT_SHIFT) | |
| 254 | (msgs[i].addr << GMBUS_SLAVE_ADDR_SHIFT) | |
| 255 | GMBUS_SLAVE_READ | GMBUS_SW_RDY); |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 256 | POSTING_READ(GMBUS2+reg_offset); |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 257 | do { |
| 258 | u32 val, loop = 0; |
| 259 | |
| 260 | if (wait_for(I915_READ(GMBUS2 + reg_offset) & (GMBUS_SATOER | GMBUS_HW_RDY), 50)) |
| 261 | goto timeout; |
| 262 | if (I915_READ(GMBUS2 + reg_offset) & GMBUS_SATOER) |
Chris Wilson | 7f58aab | 2011-03-30 16:20:43 +0100 | [diff] [blame] | 263 | goto clear_err; |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 264 | |
| 265 | val = I915_READ(GMBUS3 + reg_offset); |
| 266 | do { |
| 267 | *buf++ = val & 0xff; |
| 268 | val >>= 8; |
| 269 | } while (--len && ++loop < 4); |
| 270 | } while (len); |
| 271 | } else { |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 272 | u32 val, loop; |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 273 | |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 274 | val = loop = 0; |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 275 | do { |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 276 | val |= *buf++ << (8 * loop); |
| 277 | } while (--len && ++loop < 4); |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 278 | |
| 279 | I915_WRITE(GMBUS3 + reg_offset, val); |
| 280 | I915_WRITE(GMBUS1 + reg_offset, |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 281 | (i + 1 == num ? GMBUS_CYCLE_STOP : GMBUS_CYCLE_WAIT) | |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 282 | (msgs[i].len << GMBUS_BYTE_COUNT_SHIFT) | |
| 283 | (msgs[i].addr << GMBUS_SLAVE_ADDR_SHIFT) | |
| 284 | GMBUS_SLAVE_WRITE | GMBUS_SW_RDY); |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 285 | POSTING_READ(GMBUS2+reg_offset); |
| 286 | |
| 287 | while (len) { |
| 288 | if (wait_for(I915_READ(GMBUS2 + reg_offset) & (GMBUS_SATOER | GMBUS_HW_RDY), 50)) |
| 289 | goto timeout; |
| 290 | if (I915_READ(GMBUS2 + reg_offset) & GMBUS_SATOER) |
Chris Wilson | 7f58aab | 2011-03-30 16:20:43 +0100 | [diff] [blame] | 291 | goto clear_err; |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 292 | |
| 293 | val = loop = 0; |
| 294 | do { |
| 295 | val |= *buf++ << (8 * loop); |
| 296 | } while (--len && ++loop < 4); |
| 297 | |
| 298 | I915_WRITE(GMBUS3 + reg_offset, val); |
| 299 | POSTING_READ(GMBUS2+reg_offset); |
| 300 | } |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | if (i + 1 < num && wait_for(I915_READ(GMBUS2 + reg_offset) & (GMBUS_SATOER | GMBUS_HW_WAIT_PHASE), 50)) |
| 304 | goto timeout; |
| 305 | if (I915_READ(GMBUS2 + reg_offset) & GMBUS_SATOER) |
Chris Wilson | 7f58aab | 2011-03-30 16:20:43 +0100 | [diff] [blame] | 306 | goto clear_err; |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Chris Wilson | 7f58aab | 2011-03-30 16:20:43 +0100 | [diff] [blame] | 309 | goto done; |
| 310 | |
| 311 | clear_err: |
| 312 | /* Toggle the Software Clear Interrupt bit. This has the effect |
| 313 | * of resetting the GMBUS controller and so clearing the |
| 314 | * BUS_ERROR raised by the slave's NAK. |
| 315 | */ |
| 316 | I915_WRITE(GMBUS1 + reg_offset, GMBUS_SW_CLR_INT); |
| 317 | I915_WRITE(GMBUS1 + reg_offset, 0); |
| 318 | |
| 319 | done: |
| 320 | /* Mark the GMBUS interface as disabled. We will re-enable it at the |
| 321 | * start of the next xfer, till then let it sleep. |
| 322 | */ |
| 323 | I915_WRITE(GMBUS0 + reg_offset, 0); |
| 324 | return i; |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 325 | |
| 326 | timeout: |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 327 | DRM_INFO("GMBUS timed out, falling back to bit banging on pin %d [%s]\n", |
| 328 | bus->reg0 & 0xff, bus->adapter.name); |
Chris Wilson | 7f58aab | 2011-03-30 16:20:43 +0100 | [diff] [blame] | 329 | I915_WRITE(GMBUS0 + reg_offset, 0); |
| 330 | |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 331 | /* Hardware may not support GMBUS over these pins? Try GPIO bitbanging instead. */ |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 332 | bus->force_bit = intel_gpio_create(dev_priv, bus->reg0 & 0xff); |
| 333 | if (!bus->force_bit) |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 334 | return -ENOMEM; |
| 335 | |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 336 | return intel_i2c_quirk_xfer(dev_priv, bus->force_bit, msgs, num); |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | static u32 gmbus_func(struct i2c_adapter *adapter) |
| 340 | { |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 341 | struct intel_gmbus *bus = container_of(adapter, |
| 342 | struct intel_gmbus, |
| 343 | adapter); |
| 344 | |
| 345 | if (bus->force_bit) |
| 346 | bus->force_bit->algo->functionality(bus->force_bit); |
| 347 | |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 348 | return (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | |
| 349 | /* I2C_FUNC_10BIT_ADDR | */ |
| 350 | I2C_FUNC_SMBUS_READ_BLOCK_DATA | |
| 351 | I2C_FUNC_SMBUS_BLOCK_PROC_CALL); |
| 352 | } |
| 353 | |
| 354 | static const struct i2c_algorithm gmbus_algorithm = { |
| 355 | .master_xfer = gmbus_xfer, |
| 356 | .functionality = gmbus_func |
| 357 | }; |
| 358 | |
| 359 | /** |
| 360 | * intel_gmbus_setup - instantiate all Intel i2c GMBuses |
| 361 | * @dev: DRM device |
| 362 | */ |
| 363 | int intel_setup_gmbus(struct drm_device *dev) |
| 364 | { |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 365 | static const char *names[GMBUS_NUM_PORTS] = { |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 366 | "disabled", |
| 367 | "ssc", |
| 368 | "vga", |
| 369 | "panel", |
| 370 | "dpc", |
| 371 | "dpb", |
Jean Delvare | 6966945 | 2010-11-05 18:51:34 +0100 | [diff] [blame] | 372 | "reserved", |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 373 | "dpd", |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 374 | }; |
| 375 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 376 | int ret, i; |
| 377 | |
| 378 | dev_priv->gmbus = kcalloc(sizeof(struct intel_gmbus), GMBUS_NUM_PORTS, |
| 379 | GFP_KERNEL); |
| 380 | if (dev_priv->gmbus == NULL) |
| 381 | return -ENOMEM; |
| 382 | |
| 383 | for (i = 0; i < GMBUS_NUM_PORTS; i++) { |
| 384 | struct intel_gmbus *bus = &dev_priv->gmbus[i]; |
| 385 | |
| 386 | bus->adapter.owner = THIS_MODULE; |
| 387 | bus->adapter.class = I2C_CLASS_DDC; |
| 388 | snprintf(bus->adapter.name, |
Jean Delvare | 6966945 | 2010-11-05 18:51:34 +0100 | [diff] [blame] | 389 | sizeof(bus->adapter.name), |
| 390 | "i915 gmbus %s", |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 391 | names[i]); |
| 392 | |
| 393 | bus->adapter.dev.parent = &dev->pdev->dev; |
| 394 | bus->adapter.algo_data = dev_priv; |
| 395 | |
| 396 | bus->adapter.algo = &gmbus_algorithm; |
| 397 | ret = i2c_add_adapter(&bus->adapter); |
| 398 | if (ret) |
| 399 | goto err; |
| 400 | |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 401 | /* By default use a conservative clock rate */ |
| 402 | bus->reg0 = i | GMBUS_RATE_100KHZ; |
Chris Wilson | cb8ea75 | 2010-09-28 13:35:47 +0100 | [diff] [blame] | 403 | |
| 404 | /* XXX force bit banging until GMBUS is fully debugged */ |
Jean Delvare | 826c7e4 | 2011-06-04 19:34:56 +0000 | [diff] [blame] | 405 | bus->force_bit = intel_gpio_create(dev_priv, i); |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | intel_i2c_reset(dev_priv->dev); |
| 409 | |
| 410 | return 0; |
| 411 | |
| 412 | err: |
| 413 | while (--i) { |
| 414 | struct intel_gmbus *bus = &dev_priv->gmbus[i]; |
| 415 | i2c_del_adapter(&bus->adapter); |
| 416 | } |
| 417 | kfree(dev_priv->gmbus); |
| 418 | dev_priv->gmbus = NULL; |
| 419 | return ret; |
| 420 | } |
| 421 | |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 422 | void intel_gmbus_set_speed(struct i2c_adapter *adapter, int speed) |
| 423 | { |
| 424 | struct intel_gmbus *bus = to_intel_gmbus(adapter); |
| 425 | |
Adam Jackson | d5090b9 | 2011-06-16 16:36:28 -0400 | [diff] [blame] | 426 | bus->reg0 = (bus->reg0 & ~(0x3 << 8)) | speed; |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | void intel_gmbus_force_bit(struct i2c_adapter *adapter, bool force_bit) |
| 430 | { |
| 431 | struct intel_gmbus *bus = to_intel_gmbus(adapter); |
| 432 | |
| 433 | if (force_bit) { |
| 434 | if (bus->force_bit == NULL) { |
| 435 | struct drm_i915_private *dev_priv = adapter->algo_data; |
| 436 | bus->force_bit = intel_gpio_create(dev_priv, |
| 437 | bus->reg0 & 0xff); |
| 438 | } |
| 439 | } else { |
| 440 | if (bus->force_bit) { |
| 441 | i2c_del_adapter(bus->force_bit); |
| 442 | kfree(bus->force_bit); |
| 443 | bus->force_bit = NULL; |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 448 | void intel_teardown_gmbus(struct drm_device *dev) |
| 449 | { |
| 450 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 451 | int i; |
| 452 | |
| 453 | if (dev_priv->gmbus == NULL) |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 454 | return; |
| 455 | |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 456 | for (i = 0; i < GMBUS_NUM_PORTS; i++) { |
| 457 | struct intel_gmbus *bus = &dev_priv->gmbus[i]; |
Chris Wilson | e957d77 | 2010-09-24 12:52:03 +0100 | [diff] [blame] | 458 | if (bus->force_bit) { |
| 459 | i2c_del_adapter(bus->force_bit); |
| 460 | kfree(bus->force_bit); |
Chris Wilson | f899fc6 | 2010-07-20 15:44:45 -0700 | [diff] [blame] | 461 | } |
| 462 | i2c_del_adapter(&bus->adapter); |
| 463 | } |
| 464 | |
| 465 | kfree(dev_priv->gmbus); |
| 466 | dev_priv->gmbus = NULL; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 467 | } |