blob: 3ef5af15b8124e0e672b62943b7ec26ed0022261 [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
Chris Wilsonf899fc62010-07-20 15:44:45 -07003 * Copyright © 2006-2008,2010 Intel Corporation
Jesse Barnes79e53942008-11-07 14:24:08 -08004 * 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 Wilsonf899fc62010-07-20 15:44:45 -070027 * Chris Wilson <chris@chris-wilson.co.uk>
Jesse Barnes79e53942008-11-07 14:24:08 -080028 */
29#include <linux/i2c.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c-algo-bit.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040031#include <linux/export.h>
David Howells760285e2012-10-02 18:01:07 +010032#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080033#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010034#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080035#include "i915_drv.h"
36
Daniel Kurtz2ed06c92012-03-28 02:36:15 +080037struct gmbus_port {
38 const char *name;
39 int reg;
40};
41
42static const struct gmbus_port gmbus_ports[] = {
43 { "ssc", GPIOB },
44 { "vga", GPIOA },
45 { "panel", GPIOC },
46 { "dpc", GPIOD },
47 { "dpb", GPIOE },
48 { "dpd", GPIOF },
49};
50
Chris Wilsonf899fc62010-07-20 15:44:45 -070051/* Intel GPIO access functions */
52
Jean Delvare1849ecb2012-01-28 11:07:09 +010053#define I2C_RISEFALL_TIME 10
Chris Wilsonf899fc62010-07-20 15:44:45 -070054
Chris Wilsone957d772010-09-24 12:52:03 +010055static inline struct intel_gmbus *
56to_intel_gmbus(struct i2c_adapter *i2c)
57{
58 return container_of(i2c, struct intel_gmbus, adapter);
59}
60
Chris Wilsonf899fc62010-07-20 15:44:45 -070061void
62intel_i2c_reset(struct drm_device *dev)
Shaohua Li0ba0e9e2009-04-07 11:02:28 +080063{
64 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter110447fc2012-03-23 23:43:36 +010065 I915_WRITE(dev_priv->gpio_mmio_base + GMBUS0, 0);
Chris Wilsonf899fc62010-07-20 15:44:45 -070066}
67
68static void intel_i2c_quirk_set(struct drm_i915_private *dev_priv, bool enable)
69{
Chris Wilsonb222f262010-09-11 21:48:25 +010070 u32 val;
Shaohua Li0ba0e9e2009-04-07 11:02:28 +080071
72 /* When using bit bashing for I2C, this bit needs to be set to 1 */
Chris Wilsonf899fc62010-07-20 15:44:45 -070073 if (!IS_PINEVIEW(dev_priv->dev))
Shaohua Li0ba0e9e2009-04-07 11:02:28 +080074 return;
Chris Wilsonb222f262010-09-11 21:48:25 +010075
76 val = I915_READ(DSPCLK_GATE_D);
Shaohua Li0ba0e9e2009-04-07 11:02:28 +080077 if (enable)
Chris Wilsonb222f262010-09-11 21:48:25 +010078 val |= DPCUNIT_CLOCK_GATE_DISABLE;
Shaohua Li0ba0e9e2009-04-07 11:02:28 +080079 else
Chris Wilsonb222f262010-09-11 21:48:25 +010080 val &= ~DPCUNIT_CLOCK_GATE_DISABLE;
81 I915_WRITE(DSPCLK_GATE_D, val);
Shaohua Li0ba0e9e2009-04-07 11:02:28 +080082}
83
Daniel Vetter36c785f2012-02-14 22:37:22 +010084static u32 get_reserved(struct intel_gmbus *bus)
Chris Wilsone957d772010-09-24 12:52:03 +010085{
Daniel Vetter36c785f2012-02-14 22:37:22 +010086 struct drm_i915_private *dev_priv = bus->dev_priv;
Chris Wilsone957d772010-09-24 12:52:03 +010087 struct drm_device *dev = dev_priv->dev;
88 u32 reserved = 0;
89
90 /* On most chips, these bits must be preserved in software. */
91 if (!IS_I830(dev) && !IS_845G(dev))
Daniel Vetter36c785f2012-02-14 22:37:22 +010092 reserved = I915_READ_NOTRACE(bus->gpio_reg) &
Yuanhan Liudb5e4172010-11-08 09:58:16 +000093 (GPIO_DATA_PULLUP_DISABLE |
94 GPIO_CLOCK_PULLUP_DISABLE);
Chris Wilsone957d772010-09-24 12:52:03 +010095
96 return reserved;
97}
98
Jesse Barnes79e53942008-11-07 14:24:08 -080099static int get_clock(void *data)
100{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100101 struct intel_gmbus *bus = data;
102 struct drm_i915_private *dev_priv = bus->dev_priv;
103 u32 reserved = get_reserved(bus);
104 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_CLOCK_DIR_MASK);
105 I915_WRITE_NOTRACE(bus->gpio_reg, reserved);
106 return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_CLOCK_VAL_IN) != 0;
Jesse Barnes79e53942008-11-07 14:24:08 -0800107}
108
109static int get_data(void *data)
110{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100111 struct intel_gmbus *bus = data;
112 struct drm_i915_private *dev_priv = bus->dev_priv;
113 u32 reserved = get_reserved(bus);
114 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_DATA_DIR_MASK);
115 I915_WRITE_NOTRACE(bus->gpio_reg, reserved);
116 return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_DATA_VAL_IN) != 0;
Jesse Barnes79e53942008-11-07 14:24:08 -0800117}
118
119static void set_clock(void *data, int state_high)
120{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100121 struct intel_gmbus *bus = data;
122 struct drm_i915_private *dev_priv = bus->dev_priv;
123 u32 reserved = get_reserved(bus);
Chris Wilsone957d772010-09-24 12:52:03 +0100124 u32 clock_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -0800125
126 if (state_high)
127 clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
128 else
129 clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
130 GPIO_CLOCK_VAL_MASK;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700131
Daniel Vetter36c785f2012-02-14 22:37:22 +0100132 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | clock_bits);
133 POSTING_READ(bus->gpio_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -0800134}
135
136static void set_data(void *data, int state_high)
137{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100138 struct intel_gmbus *bus = data;
139 struct drm_i915_private *dev_priv = bus->dev_priv;
140 u32 reserved = get_reserved(bus);
Chris Wilsone957d772010-09-24 12:52:03 +0100141 u32 data_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -0800142
143 if (state_high)
144 data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
145 else
146 data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
147 GPIO_DATA_VAL_MASK;
148
Daniel Vetter36c785f2012-02-14 22:37:22 +0100149 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | data_bits);
150 POSTING_READ(bus->gpio_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -0800151}
152
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800153static int
154intel_gpio_pre_xfer(struct i2c_adapter *adapter)
155{
156 struct intel_gmbus *bus = container_of(adapter,
157 struct intel_gmbus,
158 adapter);
159 struct drm_i915_private *dev_priv = bus->dev_priv;
160
161 intel_i2c_reset(dev_priv->dev);
162 intel_i2c_quirk_set(dev_priv, true);
163 set_data(bus, 1);
164 set_clock(bus, 1);
165 udelay(I2C_RISEFALL_TIME);
166 return 0;
167}
168
169static void
170intel_gpio_post_xfer(struct i2c_adapter *adapter)
171{
172 struct intel_gmbus *bus = container_of(adapter,
173 struct intel_gmbus,
174 adapter);
175 struct drm_i915_private *dev_priv = bus->dev_priv;
176
177 set_data(bus, 1);
178 set_clock(bus, 1);
179 intel_i2c_quirk_set(dev_priv, false);
180}
181
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800182static void
Daniel Vetterf6f808c2012-02-14 18:58:49 +0100183intel_gpio_setup(struct intel_gmbus *bus, u32 pin)
Eric Anholtf0217c42009-12-01 11:56:30 -0800184{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100185 struct drm_i915_private *dev_priv = bus->dev_priv;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100186 struct i2c_algo_bit_data *algo;
Eric Anholtf0217c42009-12-01 11:56:30 -0800187
Daniel Vetterc167a6f2012-02-28 00:43:09 +0100188 algo = &bus->bit_algo;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100189
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800190 /* -1 to map pin pair to gmbus index */
191 bus->gpio_reg = dev_priv->gpio_mmio_base + gmbus_ports[pin - 1].reg;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700192
Daniel Vetterc167a6f2012-02-28 00:43:09 +0100193 bus->adapter.algo_data = algo;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100194 algo->setsda = set_data;
195 algo->setscl = set_clock;
196 algo->getsda = get_data;
197 algo->getscl = get_clock;
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800198 algo->pre_xfer = intel_gpio_pre_xfer;
199 algo->post_xfer = intel_gpio_post_xfer;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100200 algo->udelay = I2C_RISEFALL_TIME;
201 algo->timeout = usecs_to_jiffies(2200);
202 algo->data = bus;
Jesse Barnes79e53942008-11-07 14:24:08 -0800203}
204
Chris Wilsonf899fc62010-07-20 15:44:45 -0700205static int
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800206gmbus_xfer_read(struct drm_i915_private *dev_priv, struct i2c_msg *msg,
207 u32 gmbus1_index)
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800208{
209 int reg_offset = dev_priv->gpio_mmio_base;
210 u16 len = msg->len;
211 u8 *buf = msg->buf;
212
213 I915_WRITE(GMBUS1 + reg_offset,
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800214 gmbus1_index |
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800215 GMBUS_CYCLE_WAIT |
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800216 (len << GMBUS_BYTE_COUNT_SHIFT) |
217 (msg->addr << GMBUS_SLAVE_ADDR_SHIFT) |
218 GMBUS_SLAVE_READ | GMBUS_SW_RDY);
Daniel Kurtz79985ee2012-04-13 19:47:53 +0800219 while (len) {
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800220 int ret;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800221 u32 val, loop = 0;
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800222 u32 gmbus2;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800223
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800224 ret = wait_for((gmbus2 = I915_READ(GMBUS2 + reg_offset)) &
225 (GMBUS_SATOER | GMBUS_HW_RDY),
226 50);
227 if (ret)
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800228 return -ETIMEDOUT;
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800229 if (gmbus2 & GMBUS_SATOER)
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800230 return -ENXIO;
231
232 val = I915_READ(GMBUS3 + reg_offset);
233 do {
234 *buf++ = val & 0xff;
235 val >>= 8;
236 } while (--len && ++loop < 4);
Daniel Kurtz79985ee2012-04-13 19:47:53 +0800237 }
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800238
239 return 0;
240}
241
242static int
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800243gmbus_xfer_write(struct drm_i915_private *dev_priv, struct i2c_msg *msg)
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800244{
245 int reg_offset = dev_priv->gpio_mmio_base;
246 u16 len = msg->len;
247 u8 *buf = msg->buf;
248 u32 val, loop;
249
250 val = loop = 0;
Daniel Kurtz26883c32012-03-30 19:46:36 +0800251 while (len && loop < 4) {
252 val |= *buf++ << (8 * loop++);
253 len -= 1;
254 }
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800255
256 I915_WRITE(GMBUS3 + reg_offset, val);
257 I915_WRITE(GMBUS1 + reg_offset,
258 GMBUS_CYCLE_WAIT |
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800259 (msg->len << GMBUS_BYTE_COUNT_SHIFT) |
260 (msg->addr << GMBUS_SLAVE_ADDR_SHIFT) |
261 GMBUS_SLAVE_WRITE | GMBUS_SW_RDY);
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800262 while (len) {
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800263 int ret;
264 u32 gmbus2;
265
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800266 val = loop = 0;
267 do {
268 val |= *buf++ << (8 * loop);
269 } while (--len && ++loop < 4);
270
271 I915_WRITE(GMBUS3 + reg_offset, val);
Daniel Kurtz7a39a9d2012-03-30 19:46:37 +0800272
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800273 ret = wait_for((gmbus2 = I915_READ(GMBUS2 + reg_offset)) &
274 (GMBUS_SATOER | GMBUS_HW_RDY),
275 50);
276 if (ret)
Daniel Kurtz7a39a9d2012-03-30 19:46:37 +0800277 return -ETIMEDOUT;
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800278 if (gmbus2 & GMBUS_SATOER)
Daniel Kurtz7a39a9d2012-03-30 19:46:37 +0800279 return -ENXIO;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800280 }
281 return 0;
282}
283
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800284/*
285 * The gmbus controller can combine a 1 or 2 byte write with a read that
286 * immediately follows it by using an "INDEX" cycle.
287 */
288static bool
289gmbus_is_index_read(struct i2c_msg *msgs, int i, int num)
290{
291 return (i + 1 < num &&
292 !(msgs[i].flags & I2C_M_RD) && msgs[i].len <= 2 &&
293 (msgs[i + 1].flags & I2C_M_RD));
294}
295
296static int
297gmbus_xfer_index_read(struct drm_i915_private *dev_priv, struct i2c_msg *msgs)
298{
299 int reg_offset = dev_priv->gpio_mmio_base;
300 u32 gmbus1_index = 0;
301 u32 gmbus5 = 0;
302 int ret;
303
304 if (msgs[0].len == 2)
305 gmbus5 = GMBUS_2BYTE_INDEX_EN |
306 msgs[0].buf[1] | (msgs[0].buf[0] << 8);
307 if (msgs[0].len == 1)
308 gmbus1_index = GMBUS_CYCLE_INDEX |
309 (msgs[0].buf[0] << GMBUS_SLAVE_INDEX_SHIFT);
310
311 /* GMBUS5 holds 16-bit index */
312 if (gmbus5)
313 I915_WRITE(GMBUS5 + reg_offset, gmbus5);
314
315 ret = gmbus_xfer_read(dev_priv, &msgs[1], gmbus1_index);
316
317 /* Clear GMBUS5 after each index transfer */
318 if (gmbus5)
319 I915_WRITE(GMBUS5 + reg_offset, 0);
320
321 return ret;
322}
323
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800324static int
Chris Wilsonf899fc62010-07-20 15:44:45 -0700325gmbus_xfer(struct i2c_adapter *adapter,
326 struct i2c_msg *msgs,
327 int num)
328{
329 struct intel_gmbus *bus = container_of(adapter,
330 struct intel_gmbus,
331 adapter);
Daniel Vetterc2b91522012-02-14 22:37:19 +0100332 struct drm_i915_private *dev_priv = bus->dev_priv;
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800333 int i, reg_offset;
334 int ret = 0;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700335
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500336 mutex_lock(&dev_priv->gmbus_mutex);
337
338 if (bus->force_bit) {
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800339 ret = i2c_bit_algo.master_xfer(adapter, msgs, num);
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500340 goto out;
341 }
Chris Wilsonf899fc62010-07-20 15:44:45 -0700342
Daniel Vetter110447fc2012-03-23 23:43:36 +0100343 reg_offset = dev_priv->gpio_mmio_base;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700344
Chris Wilsone957d772010-09-24 12:52:03 +0100345 I915_WRITE(GMBUS0 + reg_offset, bus->reg0);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700346
347 for (i = 0; i < num; i++) {
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800348 u32 gmbus2;
349
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800350 if (gmbus_is_index_read(msgs, i, num)) {
351 ret = gmbus_xfer_index_read(dev_priv, &msgs[i]);
352 i += 1; /* set i to the index of the read xfer */
353 } else if (msgs[i].flags & I2C_M_RD) {
354 ret = gmbus_xfer_read(dev_priv, &msgs[i], 0);
355 } else {
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800356 ret = gmbus_xfer_write(dev_priv, &msgs[i]);
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800357 }
Chris Wilsonf899fc62010-07-20 15:44:45 -0700358
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800359 if (ret == -ETIMEDOUT)
360 goto timeout;
361 if (ret == -ENXIO)
362 goto clear_err;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700363
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800364 ret = wait_for((gmbus2 = I915_READ(GMBUS2 + reg_offset)) &
365 (GMBUS_SATOER | GMBUS_HW_WAIT_PHASE),
366 50);
367 if (ret)
Chris Wilsonf899fc62010-07-20 15:44:45 -0700368 goto timeout;
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800369 if (gmbus2 & GMBUS_SATOER)
Chris Wilson7f58aab2011-03-30 16:20:43 +0100370 goto clear_err;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700371 }
372
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800373 /* Generate a STOP condition on the bus. Note that gmbus can't generata
374 * a STOP on the very first cycle. To simplify the code we
375 * unconditionally generate the STOP condition with an additional gmbus
376 * cycle. */
377 I915_WRITE(GMBUS1 + reg_offset, GMBUS_CYCLE_STOP | GMBUS_SW_RDY);
378
Benson Leungcaae7452012-02-09 12:03:17 -0800379 /* Mark the GMBUS interface as disabled after waiting for idle.
380 * We will re-enable it at the start of the next xfer,
381 * till then let it sleep.
Chris Wilson7f58aab2011-03-30 16:20:43 +0100382 */
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800383 if (wait_for((I915_READ(GMBUS2 + reg_offset) & GMBUS_ACTIVE) == 0,
384 10)) {
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800385 DRM_DEBUG_KMS("GMBUS [%s] timed out waiting for idle\n",
Daniel Kurtze646d572012-03-30 19:46:38 +0800386 adapter->name);
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800387 ret = -ETIMEDOUT;
388 }
Chris Wilson7f58aab2011-03-30 16:20:43 +0100389 I915_WRITE(GMBUS0 + reg_offset, 0);
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800390 ret = ret ?: i;
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500391 goto out;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700392
Daniel Kurtze646d572012-03-30 19:46:38 +0800393clear_err:
394 /*
395 * Wait for bus to IDLE before clearing NAK.
396 * If we clear the NAK while bus is still active, then it will stay
397 * active and the next transaction may fail.
Daniel Vetter65e81862012-05-21 20:19:48 +0200398 *
399 * If no ACK is received during the address phase of a transaction, the
400 * adapter must report -ENXIO. It is not clear what to return if no ACK
401 * is received at other times. But we have to be careful to not return
402 * spurious -ENXIO because that will prevent i2c and drm edid functions
403 * from retrying. So return -ENXIO only when gmbus properly quiescents -
404 * timing out seems to happen when there _is_ a ddc chip present, but
405 * it's slow responding and only answers on the 2nd retry.
Daniel Kurtze646d572012-03-30 19:46:38 +0800406 */
Daniel Vetter65e81862012-05-21 20:19:48 +0200407 ret = -ENXIO;
Daniel Kurtze646d572012-03-30 19:46:38 +0800408 if (wait_for((I915_READ(GMBUS2 + reg_offset) & GMBUS_ACTIVE) == 0,
Daniel Vetter65e81862012-05-21 20:19:48 +0200409 10)) {
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800410 DRM_DEBUG_KMS("GMBUS [%s] timed out after NAK\n",
411 adapter->name);
Daniel Vetter65e81862012-05-21 20:19:48 +0200412 ret = -ETIMEDOUT;
413 }
Daniel Kurtze646d572012-03-30 19:46:38 +0800414
415 /* Toggle the Software Clear Interrupt bit. This has the effect
416 * of resetting the GMBUS controller and so clearing the
417 * BUS_ERROR raised by the slave's NAK.
418 */
419 I915_WRITE(GMBUS1 + reg_offset, GMBUS_SW_CLR_INT);
420 I915_WRITE(GMBUS1 + reg_offset, 0);
421 I915_WRITE(GMBUS0 + reg_offset, 0);
422
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800423 DRM_DEBUG_KMS("GMBUS [%s] NAK for addr: %04x %c(%d)\n",
Daniel Kurtze646d572012-03-30 19:46:38 +0800424 adapter->name, msgs[i].addr,
425 (msgs[i].flags & I2C_M_RD) ? 'r' : 'w', msgs[i].len);
426
Daniel Kurtze646d572012-03-30 19:46:38 +0800427 goto out;
428
Chris Wilsonf899fc62010-07-20 15:44:45 -0700429timeout:
Daniel Kurtz874e3cc2012-03-28 02:36:11 +0800430 DRM_INFO("GMBUS [%s] timed out, falling back to bit banging on pin %d\n",
431 bus->adapter.name, bus->reg0 & 0xff);
Chris Wilson7f58aab2011-03-30 16:20:43 +0100432 I915_WRITE(GMBUS0 + reg_offset, 0);
433
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800434 /* Hardware may not support GMBUS over these pins? Try GPIO bitbanging instead. */
Chris Wilsonf2ce9fa2012-11-10 15:58:21 +0000435 bus->force_bit = 1;
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800436 ret = i2c_bit_algo.master_xfer(adapter, msgs, num);
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800437
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500438out:
439 mutex_unlock(&dev_priv->gmbus_mutex);
440 return ret;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700441}
442
443static u32 gmbus_func(struct i2c_adapter *adapter)
444{
Daniel Vetterf6f808c2012-02-14 18:58:49 +0100445 return i2c_bit_algo.functionality(adapter) &
446 (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
Chris Wilsonf899fc62010-07-20 15:44:45 -0700447 /* I2C_FUNC_10BIT_ADDR | */
448 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
449 I2C_FUNC_SMBUS_BLOCK_PROC_CALL);
450}
451
452static const struct i2c_algorithm gmbus_algorithm = {
453 .master_xfer = gmbus_xfer,
454 .functionality = gmbus_func
455};
456
457/**
458 * intel_gmbus_setup - instantiate all Intel i2c GMBuses
459 * @dev: DRM device
460 */
461int intel_setup_gmbus(struct drm_device *dev)
462{
Chris Wilsonf899fc62010-07-20 15:44:45 -0700463 struct drm_i915_private *dev_priv = dev->dev_private;
464 int ret, i;
465
Daniel Vetter110447fc2012-03-23 23:43:36 +0100466 if (HAS_PCH_SPLIT(dev))
467 dev_priv->gpio_mmio_base = PCH_GPIOA - GPIOA;
468 else
469 dev_priv->gpio_mmio_base = 0;
470
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500471 mutex_init(&dev_priv->gmbus_mutex);
472
Chris Wilsonf899fc62010-07-20 15:44:45 -0700473 for (i = 0; i < GMBUS_NUM_PORTS; i++) {
474 struct intel_gmbus *bus = &dev_priv->gmbus[i];
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800475 u32 port = i + 1; /* +1 to map gmbus index to pin pair */
Chris Wilsonf899fc62010-07-20 15:44:45 -0700476
477 bus->adapter.owner = THIS_MODULE;
478 bus->adapter.class = I2C_CLASS_DDC;
479 snprintf(bus->adapter.name,
Jean Delvare69669452010-11-05 18:51:34 +0100480 sizeof(bus->adapter.name),
481 "i915 gmbus %s",
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800482 gmbus_ports[i].name);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700483
484 bus->adapter.dev.parent = &dev->pdev->dev;
Daniel Vetterc2b91522012-02-14 22:37:19 +0100485 bus->dev_priv = dev_priv;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700486
487 bus->adapter.algo = &gmbus_algorithm;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700488
Chris Wilsone957d772010-09-24 12:52:03 +0100489 /* By default use a conservative clock rate */
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800490 bus->reg0 = port | GMBUS_RATE_100KHZ;
Chris Wilsoncb8ea752010-09-28 13:35:47 +0100491
Daniel Vetter83ee9e62012-05-13 14:44:20 +0200492 /* gmbus seems to be broken on i830 */
493 if (IS_I830(dev))
Chris Wilsonf2ce9fa2012-11-10 15:58:21 +0000494 bus->force_bit = 1;
Daniel Vetter83ee9e62012-05-13 14:44:20 +0200495
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800496 intel_gpio_setup(bus, port);
Jani Nikulacee25162012-08-13 17:33:02 +0300497
498 ret = i2c_add_adapter(&bus->adapter);
499 if (ret)
500 goto err;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700501 }
502
503 intel_i2c_reset(dev_priv->dev);
504
505 return 0;
506
507err:
508 while (--i) {
509 struct intel_gmbus *bus = &dev_priv->gmbus[i];
510 i2c_del_adapter(&bus->adapter);
511 }
Chris Wilsonf899fc62010-07-20 15:44:45 -0700512 return ret;
513}
514
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800515struct i2c_adapter *intel_gmbus_get_adapter(struct drm_i915_private *dev_priv,
516 unsigned port)
517{
518 WARN_ON(!intel_gmbus_is_port_valid(port));
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800519 /* -1 to map pin pair to gmbus index */
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800520 return (intel_gmbus_is_port_valid(port)) ?
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800521 &dev_priv->gmbus[port - 1].adapter : NULL;
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800522}
523
Chris Wilsone957d772010-09-24 12:52:03 +0100524void intel_gmbus_set_speed(struct i2c_adapter *adapter, int speed)
525{
526 struct intel_gmbus *bus = to_intel_gmbus(adapter);
527
Adam Jacksond5090b92011-06-16 16:36:28 -0400528 bus->reg0 = (bus->reg0 & ~(0x3 << 8)) | speed;
Chris Wilsone957d772010-09-24 12:52:03 +0100529}
530
531void intel_gmbus_force_bit(struct i2c_adapter *adapter, bool force_bit)
532{
533 struct intel_gmbus *bus = to_intel_gmbus(adapter);
534
Chris Wilsonf2ce9fa2012-11-10 15:58:21 +0000535 bus->force_bit += force_bit ? 1 : -1;
536 DRM_DEBUG_KMS("%sabling bit-banging on %s. force bit now %d\n",
537 force_bit ? "en" : "dis", adapter->name,
538 bus->force_bit);
Chris Wilsone957d772010-09-24 12:52:03 +0100539}
540
Chris Wilsonf899fc62010-07-20 15:44:45 -0700541void intel_teardown_gmbus(struct drm_device *dev)
542{
543 struct drm_i915_private *dev_priv = dev->dev_private;
544 int i;
545
Chris Wilsonf899fc62010-07-20 15:44:45 -0700546 for (i = 0; i < GMBUS_NUM_PORTS; i++) {
547 struct intel_gmbus *bus = &dev_priv->gmbus[i];
Chris Wilsonf899fc62010-07-20 15:44:45 -0700548 i2c_del_adapter(&bus->adapter);
549 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800550}