blob: a16eecdf1ed12bee10723d42349de73ff702f83c [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 Vetter61168c52012-12-01 13:53:43 +0100206gmbus_wait_hw_status(struct drm_i915_private *dev_priv,
207 u32 gmbus2_status)
208{
209 int ret;
210 int reg_offset = dev_priv->gpio_mmio_base;
211 u32 gmbus2;
212
213 ret = wait_for((gmbus2 = I915_READ(GMBUS2 + reg_offset)) &
214 (GMBUS_SATOER | gmbus2_status),
215 50);
216
217 if (gmbus2 & GMBUS_SATOER)
218 return -ENXIO;
219
220 return ret;
221}
222
223static int
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800224gmbus_xfer_read(struct drm_i915_private *dev_priv, struct i2c_msg *msg,
225 u32 gmbus1_index)
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800226{
227 int reg_offset = dev_priv->gpio_mmio_base;
228 u16 len = msg->len;
229 u8 *buf = msg->buf;
230
231 I915_WRITE(GMBUS1 + reg_offset,
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800232 gmbus1_index |
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800233 GMBUS_CYCLE_WAIT |
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800234 (len << GMBUS_BYTE_COUNT_SHIFT) |
235 (msg->addr << GMBUS_SLAVE_ADDR_SHIFT) |
236 GMBUS_SLAVE_READ | GMBUS_SW_RDY);
Daniel Kurtz79985ee2012-04-13 19:47:53 +0800237 while (len) {
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800238 int ret;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800239 u32 val, loop = 0;
240
Daniel Vetter61168c52012-12-01 13:53:43 +0100241 ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_RDY);
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800242 if (ret)
Daniel Vetter61168c52012-12-01 13:53:43 +0100243 return ret;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800244
245 val = I915_READ(GMBUS3 + reg_offset);
246 do {
247 *buf++ = val & 0xff;
248 val >>= 8;
249 } while (--len && ++loop < 4);
Daniel Kurtz79985ee2012-04-13 19:47:53 +0800250 }
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800251
252 return 0;
253}
254
255static int
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800256gmbus_xfer_write(struct drm_i915_private *dev_priv, struct i2c_msg *msg)
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800257{
258 int reg_offset = dev_priv->gpio_mmio_base;
259 u16 len = msg->len;
260 u8 *buf = msg->buf;
261 u32 val, loop;
262
263 val = loop = 0;
Daniel Kurtz26883c32012-03-30 19:46:36 +0800264 while (len && loop < 4) {
265 val |= *buf++ << (8 * loop++);
266 len -= 1;
267 }
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800268
269 I915_WRITE(GMBUS3 + reg_offset, val);
270 I915_WRITE(GMBUS1 + reg_offset,
271 GMBUS_CYCLE_WAIT |
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800272 (msg->len << GMBUS_BYTE_COUNT_SHIFT) |
273 (msg->addr << GMBUS_SLAVE_ADDR_SHIFT) |
274 GMBUS_SLAVE_WRITE | GMBUS_SW_RDY);
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800275 while (len) {
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800276 int ret;
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800277
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800278 val = loop = 0;
279 do {
280 val |= *buf++ << (8 * loop);
281 } while (--len && ++loop < 4);
282
283 I915_WRITE(GMBUS3 + reg_offset, val);
Daniel Kurtz7a39a9d2012-03-30 19:46:37 +0800284
Daniel Vetter61168c52012-12-01 13:53:43 +0100285 ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_RDY);
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800286 if (ret)
Daniel Vetter61168c52012-12-01 13:53:43 +0100287 return ret;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800288 }
289 return 0;
290}
291
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800292/*
293 * The gmbus controller can combine a 1 or 2 byte write with a read that
294 * immediately follows it by using an "INDEX" cycle.
295 */
296static bool
297gmbus_is_index_read(struct i2c_msg *msgs, int i, int num)
298{
299 return (i + 1 < num &&
300 !(msgs[i].flags & I2C_M_RD) && msgs[i].len <= 2 &&
301 (msgs[i + 1].flags & I2C_M_RD));
302}
303
304static int
305gmbus_xfer_index_read(struct drm_i915_private *dev_priv, struct i2c_msg *msgs)
306{
307 int reg_offset = dev_priv->gpio_mmio_base;
308 u32 gmbus1_index = 0;
309 u32 gmbus5 = 0;
310 int ret;
311
312 if (msgs[0].len == 2)
313 gmbus5 = GMBUS_2BYTE_INDEX_EN |
314 msgs[0].buf[1] | (msgs[0].buf[0] << 8);
315 if (msgs[0].len == 1)
316 gmbus1_index = GMBUS_CYCLE_INDEX |
317 (msgs[0].buf[0] << GMBUS_SLAVE_INDEX_SHIFT);
318
319 /* GMBUS5 holds 16-bit index */
320 if (gmbus5)
321 I915_WRITE(GMBUS5 + reg_offset, gmbus5);
322
323 ret = gmbus_xfer_read(dev_priv, &msgs[1], gmbus1_index);
324
325 /* Clear GMBUS5 after each index transfer */
326 if (gmbus5)
327 I915_WRITE(GMBUS5 + reg_offset, 0);
328
329 return ret;
330}
331
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800332static int
Chris Wilsonf899fc62010-07-20 15:44:45 -0700333gmbus_xfer(struct i2c_adapter *adapter,
334 struct i2c_msg *msgs,
335 int num)
336{
337 struct intel_gmbus *bus = container_of(adapter,
338 struct intel_gmbus,
339 adapter);
Daniel Vetterc2b91522012-02-14 22:37:19 +0100340 struct drm_i915_private *dev_priv = bus->dev_priv;
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800341 int i, reg_offset;
342 int ret = 0;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700343
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500344 mutex_lock(&dev_priv->gmbus_mutex);
345
346 if (bus->force_bit) {
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800347 ret = i2c_bit_algo.master_xfer(adapter, msgs, num);
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500348 goto out;
349 }
Chris Wilsonf899fc62010-07-20 15:44:45 -0700350
Daniel Vetter110447fc2012-03-23 23:43:36 +0100351 reg_offset = dev_priv->gpio_mmio_base;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700352
Chris Wilsone957d772010-09-24 12:52:03 +0100353 I915_WRITE(GMBUS0 + reg_offset, bus->reg0);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700354
355 for (i = 0; i < num; i++) {
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800356 if (gmbus_is_index_read(msgs, i, num)) {
357 ret = gmbus_xfer_index_read(dev_priv, &msgs[i]);
358 i += 1; /* set i to the index of the read xfer */
359 } else if (msgs[i].flags & I2C_M_RD) {
360 ret = gmbus_xfer_read(dev_priv, &msgs[i], 0);
361 } else {
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800362 ret = gmbus_xfer_write(dev_priv, &msgs[i]);
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800363 }
Chris Wilsonf899fc62010-07-20 15:44:45 -0700364
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800365 if (ret == -ETIMEDOUT)
366 goto timeout;
367 if (ret == -ENXIO)
368 goto clear_err;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700369
Daniel Vetter61168c52012-12-01 13:53:43 +0100370 ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_WAIT_PHASE);
371 if (ret == -ENXIO)
372 goto clear_err;
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800373 if (ret)
Chris Wilsonf899fc62010-07-20 15:44:45 -0700374 goto timeout;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700375 }
376
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800377 /* Generate a STOP condition on the bus. Note that gmbus can't generata
378 * a STOP on the very first cycle. To simplify the code we
379 * unconditionally generate the STOP condition with an additional gmbus
380 * cycle. */
381 I915_WRITE(GMBUS1 + reg_offset, GMBUS_CYCLE_STOP | GMBUS_SW_RDY);
382
Benson Leungcaae7452012-02-09 12:03:17 -0800383 /* Mark the GMBUS interface as disabled after waiting for idle.
384 * We will re-enable it at the start of the next xfer,
385 * till then let it sleep.
Chris Wilson7f58aab2011-03-30 16:20:43 +0100386 */
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800387 if (wait_for((I915_READ(GMBUS2 + reg_offset) & GMBUS_ACTIVE) == 0,
388 10)) {
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800389 DRM_DEBUG_KMS("GMBUS [%s] timed out waiting for idle\n",
Daniel Kurtze646d572012-03-30 19:46:38 +0800390 adapter->name);
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800391 ret = -ETIMEDOUT;
392 }
Chris Wilson7f58aab2011-03-30 16:20:43 +0100393 I915_WRITE(GMBUS0 + reg_offset, 0);
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800394 ret = ret ?: i;
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500395 goto out;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700396
Daniel Kurtze646d572012-03-30 19:46:38 +0800397clear_err:
398 /*
399 * Wait for bus to IDLE before clearing NAK.
400 * If we clear the NAK while bus is still active, then it will stay
401 * active and the next transaction may fail.
Daniel Vetter65e81862012-05-21 20:19:48 +0200402 *
403 * If no ACK is received during the address phase of a transaction, the
404 * adapter must report -ENXIO. It is not clear what to return if no ACK
405 * is received at other times. But we have to be careful to not return
406 * spurious -ENXIO because that will prevent i2c and drm edid functions
407 * from retrying. So return -ENXIO only when gmbus properly quiescents -
408 * timing out seems to happen when there _is_ a ddc chip present, but
409 * it's slow responding and only answers on the 2nd retry.
Daniel Kurtze646d572012-03-30 19:46:38 +0800410 */
Daniel Vetter65e81862012-05-21 20:19:48 +0200411 ret = -ENXIO;
Daniel Kurtze646d572012-03-30 19:46:38 +0800412 if (wait_for((I915_READ(GMBUS2 + reg_offset) & GMBUS_ACTIVE) == 0,
Daniel Vetter65e81862012-05-21 20:19:48 +0200413 10)) {
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800414 DRM_DEBUG_KMS("GMBUS [%s] timed out after NAK\n",
415 adapter->name);
Daniel Vetter65e81862012-05-21 20:19:48 +0200416 ret = -ETIMEDOUT;
417 }
Daniel Kurtze646d572012-03-30 19:46:38 +0800418
419 /* Toggle the Software Clear Interrupt bit. This has the effect
420 * of resetting the GMBUS controller and so clearing the
421 * BUS_ERROR raised by the slave's NAK.
422 */
423 I915_WRITE(GMBUS1 + reg_offset, GMBUS_SW_CLR_INT);
424 I915_WRITE(GMBUS1 + reg_offset, 0);
425 I915_WRITE(GMBUS0 + reg_offset, 0);
426
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800427 DRM_DEBUG_KMS("GMBUS [%s] NAK for addr: %04x %c(%d)\n",
Daniel Kurtze646d572012-03-30 19:46:38 +0800428 adapter->name, msgs[i].addr,
429 (msgs[i].flags & I2C_M_RD) ? 'r' : 'w', msgs[i].len);
430
Daniel Kurtze646d572012-03-30 19:46:38 +0800431 goto out;
432
Chris Wilsonf899fc62010-07-20 15:44:45 -0700433timeout:
Daniel Kurtz874e3cc2012-03-28 02:36:11 +0800434 DRM_INFO("GMBUS [%s] timed out, falling back to bit banging on pin %d\n",
435 bus->adapter.name, bus->reg0 & 0xff);
Chris Wilson7f58aab2011-03-30 16:20:43 +0100436 I915_WRITE(GMBUS0 + reg_offset, 0);
437
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800438 /* Hardware may not support GMBUS over these pins? Try GPIO bitbanging instead. */
Chris Wilsonf2ce9fa2012-11-10 15:58:21 +0000439 bus->force_bit = 1;
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800440 ret = i2c_bit_algo.master_xfer(adapter, msgs, num);
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800441
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500442out:
443 mutex_unlock(&dev_priv->gmbus_mutex);
444 return ret;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700445}
446
447static u32 gmbus_func(struct i2c_adapter *adapter)
448{
Daniel Vetterf6f808c2012-02-14 18:58:49 +0100449 return i2c_bit_algo.functionality(adapter) &
450 (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
Chris Wilsonf899fc62010-07-20 15:44:45 -0700451 /* I2C_FUNC_10BIT_ADDR | */
452 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
453 I2C_FUNC_SMBUS_BLOCK_PROC_CALL);
454}
455
456static const struct i2c_algorithm gmbus_algorithm = {
457 .master_xfer = gmbus_xfer,
458 .functionality = gmbus_func
459};
460
461/**
462 * intel_gmbus_setup - instantiate all Intel i2c GMBuses
463 * @dev: DRM device
464 */
465int intel_setup_gmbus(struct drm_device *dev)
466{
Chris Wilsonf899fc62010-07-20 15:44:45 -0700467 struct drm_i915_private *dev_priv = dev->dev_private;
468 int ret, i;
469
Daniel Vetter110447fc2012-03-23 23:43:36 +0100470 if (HAS_PCH_SPLIT(dev))
471 dev_priv->gpio_mmio_base = PCH_GPIOA - GPIOA;
472 else
473 dev_priv->gpio_mmio_base = 0;
474
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500475 mutex_init(&dev_priv->gmbus_mutex);
476
Chris Wilsonf899fc62010-07-20 15:44:45 -0700477 for (i = 0; i < GMBUS_NUM_PORTS; i++) {
478 struct intel_gmbus *bus = &dev_priv->gmbus[i];
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800479 u32 port = i + 1; /* +1 to map gmbus index to pin pair */
Chris Wilsonf899fc62010-07-20 15:44:45 -0700480
481 bus->adapter.owner = THIS_MODULE;
482 bus->adapter.class = I2C_CLASS_DDC;
483 snprintf(bus->adapter.name,
Jean Delvare69669452010-11-05 18:51:34 +0100484 sizeof(bus->adapter.name),
485 "i915 gmbus %s",
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800486 gmbus_ports[i].name);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700487
488 bus->adapter.dev.parent = &dev->pdev->dev;
Daniel Vetterc2b91522012-02-14 22:37:19 +0100489 bus->dev_priv = dev_priv;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700490
491 bus->adapter.algo = &gmbus_algorithm;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700492
Chris Wilsone957d772010-09-24 12:52:03 +0100493 /* By default use a conservative clock rate */
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800494 bus->reg0 = port | GMBUS_RATE_100KHZ;
Chris Wilsoncb8ea752010-09-28 13:35:47 +0100495
Daniel Vetter83ee9e62012-05-13 14:44:20 +0200496 /* gmbus seems to be broken on i830 */
497 if (IS_I830(dev))
Chris Wilsonf2ce9fa2012-11-10 15:58:21 +0000498 bus->force_bit = 1;
Daniel Vetter83ee9e62012-05-13 14:44:20 +0200499
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800500 intel_gpio_setup(bus, port);
Jani Nikulacee25162012-08-13 17:33:02 +0300501
502 ret = i2c_add_adapter(&bus->adapter);
503 if (ret)
504 goto err;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700505 }
506
507 intel_i2c_reset(dev_priv->dev);
508
509 return 0;
510
511err:
512 while (--i) {
513 struct intel_gmbus *bus = &dev_priv->gmbus[i];
514 i2c_del_adapter(&bus->adapter);
515 }
Chris Wilsonf899fc62010-07-20 15:44:45 -0700516 return ret;
517}
518
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800519struct i2c_adapter *intel_gmbus_get_adapter(struct drm_i915_private *dev_priv,
520 unsigned port)
521{
522 WARN_ON(!intel_gmbus_is_port_valid(port));
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800523 /* -1 to map pin pair to gmbus index */
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800524 return (intel_gmbus_is_port_valid(port)) ?
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800525 &dev_priv->gmbus[port - 1].adapter : NULL;
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800526}
527
Chris Wilsone957d772010-09-24 12:52:03 +0100528void intel_gmbus_set_speed(struct i2c_adapter *adapter, int speed)
529{
530 struct intel_gmbus *bus = to_intel_gmbus(adapter);
531
Adam Jacksond5090b92011-06-16 16:36:28 -0400532 bus->reg0 = (bus->reg0 & ~(0x3 << 8)) | speed;
Chris Wilsone957d772010-09-24 12:52:03 +0100533}
534
535void intel_gmbus_force_bit(struct i2c_adapter *adapter, bool force_bit)
536{
537 struct intel_gmbus *bus = to_intel_gmbus(adapter);
538
Chris Wilsonf2ce9fa2012-11-10 15:58:21 +0000539 bus->force_bit += force_bit ? 1 : -1;
540 DRM_DEBUG_KMS("%sabling bit-banging on %s. force bit now %d\n",
541 force_bit ? "en" : "dis", adapter->name,
542 bus->force_bit);
Chris Wilsone957d772010-09-24 12:52:03 +0100543}
544
Chris Wilsonf899fc62010-07-20 15:44:45 -0700545void intel_teardown_gmbus(struct drm_device *dev)
546{
547 struct drm_i915_private *dev_priv = dev->dev_private;
548 int i;
549
Chris Wilsonf899fc62010-07-20 15:44:45 -0700550 for (i = 0; i < GMBUS_NUM_PORTS; i++) {
551 struct intel_gmbus *bus = &dev_priv->gmbus[i];
Chris Wilsonf899fc62010-07-20 15:44:45 -0700552 i2c_del_adapter(&bus->adapter);
553 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800554}