blob: 2ca17b14b6c1c91710601dfcb34c661b45707347 [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
Chon Ming Lee24eb2d52013-09-27 15:31:00 +080037enum disp_clk {
38 CDCLK,
39 CZCLK
40};
41
Daniel Kurtz2ed06c92012-03-28 02:36:15 +080042struct gmbus_port {
43 const char *name;
44 int reg;
45};
46
47static const struct gmbus_port gmbus_ports[] = {
48 { "ssc", GPIOB },
49 { "vga", GPIOA },
50 { "panel", GPIOC },
51 { "dpc", GPIOD },
52 { "dpb", GPIOE },
53 { "dpd", GPIOF },
54};
55
Chris Wilsonf899fc62010-07-20 15:44:45 -070056/* Intel GPIO access functions */
57
Jean Delvare1849ecb2012-01-28 11:07:09 +010058#define I2C_RISEFALL_TIME 10
Chris Wilsonf899fc62010-07-20 15:44:45 -070059
Chris Wilsone957d772010-09-24 12:52:03 +010060static inline struct intel_gmbus *
61to_intel_gmbus(struct i2c_adapter *i2c)
62{
63 return container_of(i2c, struct intel_gmbus, adapter);
64}
65
Chon Ming Lee24eb2d52013-09-27 15:31:00 +080066static int get_disp_clk_div(struct drm_i915_private *dev_priv,
67 enum disp_clk clk)
68{
69 u32 reg_val;
70 int clk_ratio;
71
72 reg_val = I915_READ(CZCLK_CDCLK_FREQ_RATIO);
73
74 if (clk == CDCLK)
75 clk_ratio =
76 ((reg_val & CDCLK_FREQ_MASK) >> CDCLK_FREQ_SHIFT) + 1;
77 else
78 clk_ratio = (reg_val & CZCLK_FREQ_MASK) + 1;
79
80 return clk_ratio;
81}
82
83static void gmbus_set_freq(struct drm_i915_private *dev_priv)
84{
85 int vco_freq[] = { 800, 1600, 2000, 2400 };
86 int gmbus_freq = 0, cdclk_div, hpll_freq;
87
88 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
89
90 /* Skip setting the gmbus freq if BIOS has already programmed it */
91 if (I915_READ(GMBUSFREQ_VLV) != 0xA0)
92 return;
93
94 /* Obtain SKU information */
95 mutex_lock(&dev_priv->dpio_lock);
96 hpll_freq =
97 vlv_cck_read(dev_priv, CCK_FUSE_REG) & CCK_FUSE_HPLL_FREQ_MASK;
98 mutex_unlock(&dev_priv->dpio_lock);
99
100 /* Get the CDCLK divide ratio */
101 cdclk_div = get_disp_clk_div(dev_priv, CDCLK);
102
103 /*
104 * Program the gmbus_freq based on the cdclk frequency.
105 * BSpec erroneously claims we should aim for 4MHz, but
106 * in fact 1MHz is the correct frequency.
107 */
108 if (cdclk_div)
109 gmbus_freq = (vco_freq[hpll_freq] << 1) / cdclk_div;
110
111 if (WARN_ON(gmbus_freq == 0))
112 return;
113
114 I915_WRITE(GMBUSFREQ_VLV, gmbus_freq);
115}
116
Chris Wilsonf899fc62010-07-20 15:44:45 -0700117void
118intel_i2c_reset(struct drm_device *dev)
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800119{
120 struct drm_i915_private *dev_priv = dev->dev_private;
Chon Ming Lee24eb2d52013-09-27 15:31:00 +0800121
122 /*
123 * In BIOS-less system, program the correct gmbus frequency
124 * before reading edid.
125 */
126 if (IS_VALLEYVIEW(dev))
127 gmbus_set_freq(dev_priv);
128
Daniel Vetter110447fc2012-03-23 23:43:36 +0100129 I915_WRITE(dev_priv->gpio_mmio_base + GMBUS0, 0);
Daniel Vetter28c70f12012-12-01 13:53:45 +0100130 I915_WRITE(dev_priv->gpio_mmio_base + GMBUS4, 0);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700131}
132
133static void intel_i2c_quirk_set(struct drm_i915_private *dev_priv, bool enable)
134{
Chris Wilsonb222f262010-09-11 21:48:25 +0100135 u32 val;
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800136
137 /* When using bit bashing for I2C, this bit needs to be set to 1 */
Chris Wilsonf899fc62010-07-20 15:44:45 -0700138 if (!IS_PINEVIEW(dev_priv->dev))
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800139 return;
Chris Wilsonb222f262010-09-11 21:48:25 +0100140
141 val = I915_READ(DSPCLK_GATE_D);
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800142 if (enable)
Chris Wilsonb222f262010-09-11 21:48:25 +0100143 val |= DPCUNIT_CLOCK_GATE_DISABLE;
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800144 else
Chris Wilsonb222f262010-09-11 21:48:25 +0100145 val &= ~DPCUNIT_CLOCK_GATE_DISABLE;
146 I915_WRITE(DSPCLK_GATE_D, val);
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800147}
148
Daniel Vetter36c785f2012-02-14 22:37:22 +0100149static u32 get_reserved(struct intel_gmbus *bus)
Chris Wilsone957d772010-09-24 12:52:03 +0100150{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100151 struct drm_i915_private *dev_priv = bus->dev_priv;
Chris Wilsone957d772010-09-24 12:52:03 +0100152 struct drm_device *dev = dev_priv->dev;
153 u32 reserved = 0;
154
155 /* On most chips, these bits must be preserved in software. */
156 if (!IS_I830(dev) && !IS_845G(dev))
Daniel Vetter36c785f2012-02-14 22:37:22 +0100157 reserved = I915_READ_NOTRACE(bus->gpio_reg) &
Yuanhan Liudb5e4172010-11-08 09:58:16 +0000158 (GPIO_DATA_PULLUP_DISABLE |
159 GPIO_CLOCK_PULLUP_DISABLE);
Chris Wilsone957d772010-09-24 12:52:03 +0100160
161 return reserved;
162}
163
Jesse Barnes79e53942008-11-07 14:24:08 -0800164static int get_clock(void *data)
165{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100166 struct intel_gmbus *bus = data;
167 struct drm_i915_private *dev_priv = bus->dev_priv;
168 u32 reserved = get_reserved(bus);
169 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_CLOCK_DIR_MASK);
170 I915_WRITE_NOTRACE(bus->gpio_reg, reserved);
171 return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_CLOCK_VAL_IN) != 0;
Jesse Barnes79e53942008-11-07 14:24:08 -0800172}
173
174static int get_data(void *data)
175{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100176 struct intel_gmbus *bus = data;
177 struct drm_i915_private *dev_priv = bus->dev_priv;
178 u32 reserved = get_reserved(bus);
179 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_DATA_DIR_MASK);
180 I915_WRITE_NOTRACE(bus->gpio_reg, reserved);
181 return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_DATA_VAL_IN) != 0;
Jesse Barnes79e53942008-11-07 14:24:08 -0800182}
183
184static void set_clock(void *data, int state_high)
185{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100186 struct intel_gmbus *bus = data;
187 struct drm_i915_private *dev_priv = bus->dev_priv;
188 u32 reserved = get_reserved(bus);
Chris Wilsone957d772010-09-24 12:52:03 +0100189 u32 clock_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -0800190
191 if (state_high)
192 clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
193 else
194 clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
195 GPIO_CLOCK_VAL_MASK;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700196
Daniel Vetter36c785f2012-02-14 22:37:22 +0100197 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | clock_bits);
198 POSTING_READ(bus->gpio_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -0800199}
200
201static void set_data(void *data, int state_high)
202{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100203 struct intel_gmbus *bus = data;
204 struct drm_i915_private *dev_priv = bus->dev_priv;
205 u32 reserved = get_reserved(bus);
Chris Wilsone957d772010-09-24 12:52:03 +0100206 u32 data_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -0800207
208 if (state_high)
209 data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
210 else
211 data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
212 GPIO_DATA_VAL_MASK;
213
Daniel Vetter36c785f2012-02-14 22:37:22 +0100214 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | data_bits);
215 POSTING_READ(bus->gpio_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -0800216}
217
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800218static int
219intel_gpio_pre_xfer(struct i2c_adapter *adapter)
220{
221 struct intel_gmbus *bus = container_of(adapter,
222 struct intel_gmbus,
223 adapter);
224 struct drm_i915_private *dev_priv = bus->dev_priv;
225
226 intel_i2c_reset(dev_priv->dev);
227 intel_i2c_quirk_set(dev_priv, true);
228 set_data(bus, 1);
229 set_clock(bus, 1);
230 udelay(I2C_RISEFALL_TIME);
231 return 0;
232}
233
234static void
235intel_gpio_post_xfer(struct i2c_adapter *adapter)
236{
237 struct intel_gmbus *bus = container_of(adapter,
238 struct intel_gmbus,
239 adapter);
240 struct drm_i915_private *dev_priv = bus->dev_priv;
241
242 set_data(bus, 1);
243 set_clock(bus, 1);
244 intel_i2c_quirk_set(dev_priv, false);
245}
246
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800247static void
Daniel Vetterf6f808c2012-02-14 18:58:49 +0100248intel_gpio_setup(struct intel_gmbus *bus, u32 pin)
Eric Anholtf0217c42009-12-01 11:56:30 -0800249{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100250 struct drm_i915_private *dev_priv = bus->dev_priv;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100251 struct i2c_algo_bit_data *algo;
Eric Anholtf0217c42009-12-01 11:56:30 -0800252
Daniel Vetterc167a6f2012-02-28 00:43:09 +0100253 algo = &bus->bit_algo;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100254
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800255 /* -1 to map pin pair to gmbus index */
256 bus->gpio_reg = dev_priv->gpio_mmio_base + gmbus_ports[pin - 1].reg;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700257
Daniel Vetterc167a6f2012-02-28 00:43:09 +0100258 bus->adapter.algo_data = algo;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100259 algo->setsda = set_data;
260 algo->setscl = set_clock;
261 algo->getsda = get_data;
262 algo->getscl = get_clock;
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800263 algo->pre_xfer = intel_gpio_pre_xfer;
264 algo->post_xfer = intel_gpio_post_xfer;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100265 algo->udelay = I2C_RISEFALL_TIME;
266 algo->timeout = usecs_to_jiffies(2200);
267 algo->data = bus;
Jesse Barnes79e53942008-11-07 14:24:08 -0800268}
269
Jiri Kosinac12aba52013-03-19 09:56:57 +0100270/*
271 * gmbus on gen4 seems to be able to generate legacy interrupts even when in MSI
272 * mode. This results in spurious interrupt warnings if the legacy irq no. is
273 * shared with another device. The kernel then disables that interrupt source
274 * and so prevents the other device from working properly.
275 */
276#define HAS_GMBUS_IRQ(dev) (INTEL_INFO(dev)->gen >= 5)
Chris Wilsonf899fc62010-07-20 15:44:45 -0700277static int
Daniel Vetter61168c52012-12-01 13:53:43 +0100278gmbus_wait_hw_status(struct drm_i915_private *dev_priv,
Daniel Vetter28c70f12012-12-01 13:53:45 +0100279 u32 gmbus2_status,
280 u32 gmbus4_irq_en)
Daniel Vetter61168c52012-12-01 13:53:43 +0100281{
Daniel Vetter28c70f12012-12-01 13:53:45 +0100282 int i;
Daniel Vetter61168c52012-12-01 13:53:43 +0100283 int reg_offset = dev_priv->gpio_mmio_base;
Daniel Vetter28c70f12012-12-01 13:53:45 +0100284 u32 gmbus2 = 0;
285 DEFINE_WAIT(wait);
Daniel Vetter61168c52012-12-01 13:53:43 +0100286
Jiri Kosinac12aba52013-03-19 09:56:57 +0100287 if (!HAS_GMBUS_IRQ(dev_priv->dev))
288 gmbus4_irq_en = 0;
289
Daniel Vetter28c70f12012-12-01 13:53:45 +0100290 /* Important: The hw handles only the first bit, so set only one! Since
291 * we also need to check for NAKs besides the hw ready/idle signal, we
292 * need to wake up periodically and check that ourselves. */
293 I915_WRITE(GMBUS4 + reg_offset, gmbus4_irq_en);
294
Imre Deak2554fc12013-05-21 20:03:18 +0300295 for (i = 0; i < msecs_to_jiffies_timeout(50); i++) {
Daniel Vetter28c70f12012-12-01 13:53:45 +0100296 prepare_to_wait(&dev_priv->gmbus_wait_queue, &wait,
297 TASK_UNINTERRUPTIBLE);
298
Daniel Vetteref04f002012-12-01 21:03:59 +0100299 gmbus2 = I915_READ_NOTRACE(GMBUS2 + reg_offset);
Daniel Vetter28c70f12012-12-01 13:53:45 +0100300 if (gmbus2 & (GMBUS_SATOER | gmbus2_status))
301 break;
302
303 schedule_timeout(1);
304 }
305 finish_wait(&dev_priv->gmbus_wait_queue, &wait);
306
307 I915_WRITE(GMBUS4 + reg_offset, 0);
Daniel Vetter61168c52012-12-01 13:53:43 +0100308
309 if (gmbus2 & GMBUS_SATOER)
310 return -ENXIO;
Daniel Vetter28c70f12012-12-01 13:53:45 +0100311 if (gmbus2 & gmbus2_status)
312 return 0;
313 return -ETIMEDOUT;
Daniel Vetter61168c52012-12-01 13:53:43 +0100314}
315
316static int
Daniel Vetter2c438c02012-12-01 13:53:46 +0100317gmbus_wait_idle(struct drm_i915_private *dev_priv)
318{
319 int ret;
320 int reg_offset = dev_priv->gpio_mmio_base;
321
Daniel Vetteref04f002012-12-01 21:03:59 +0100322#define C ((I915_READ_NOTRACE(GMBUS2 + reg_offset) & GMBUS_ACTIVE) == 0)
Daniel Vetter2c438c02012-12-01 13:53:46 +0100323
324 if (!HAS_GMBUS_IRQ(dev_priv->dev))
325 return wait_for(C, 10);
326
327 /* Important: The hw handles only the first bit, so set only one! */
328 I915_WRITE(GMBUS4 + reg_offset, GMBUS_IDLE_EN);
329
Imre Deak35987062013-05-21 20:03:20 +0300330 ret = wait_event_timeout(dev_priv->gmbus_wait_queue, C,
331 msecs_to_jiffies_timeout(10));
Daniel Vetter2c438c02012-12-01 13:53:46 +0100332
333 I915_WRITE(GMBUS4 + reg_offset, 0);
334
335 if (ret)
336 return 0;
337 else
338 return -ETIMEDOUT;
339#undef C
340}
341
342static int
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800343gmbus_xfer_read(struct drm_i915_private *dev_priv, struct i2c_msg *msg,
344 u32 gmbus1_index)
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800345{
346 int reg_offset = dev_priv->gpio_mmio_base;
347 u16 len = msg->len;
348 u8 *buf = msg->buf;
349
350 I915_WRITE(GMBUS1 + reg_offset,
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800351 gmbus1_index |
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800352 GMBUS_CYCLE_WAIT |
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800353 (len << GMBUS_BYTE_COUNT_SHIFT) |
354 (msg->addr << GMBUS_SLAVE_ADDR_SHIFT) |
355 GMBUS_SLAVE_READ | GMBUS_SW_RDY);
Daniel Kurtz79985ee2012-04-13 19:47:53 +0800356 while (len) {
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800357 int ret;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800358 u32 val, loop = 0;
359
Daniel Vetter28c70f12012-12-01 13:53:45 +0100360 ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_RDY,
361 GMBUS_HW_RDY_EN);
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800362 if (ret)
Daniel Vetter61168c52012-12-01 13:53:43 +0100363 return ret;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800364
365 val = I915_READ(GMBUS3 + reg_offset);
366 do {
367 *buf++ = val & 0xff;
368 val >>= 8;
369 } while (--len && ++loop < 4);
Daniel Kurtz79985ee2012-04-13 19:47:53 +0800370 }
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800371
372 return 0;
373}
374
375static int
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800376gmbus_xfer_write(struct drm_i915_private *dev_priv, struct i2c_msg *msg)
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800377{
378 int reg_offset = dev_priv->gpio_mmio_base;
379 u16 len = msg->len;
380 u8 *buf = msg->buf;
381 u32 val, loop;
382
383 val = loop = 0;
Daniel Kurtz26883c32012-03-30 19:46:36 +0800384 while (len && loop < 4) {
385 val |= *buf++ << (8 * loop++);
386 len -= 1;
387 }
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800388
389 I915_WRITE(GMBUS3 + reg_offset, val);
390 I915_WRITE(GMBUS1 + reg_offset,
391 GMBUS_CYCLE_WAIT |
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800392 (msg->len << GMBUS_BYTE_COUNT_SHIFT) |
393 (msg->addr << GMBUS_SLAVE_ADDR_SHIFT) |
394 GMBUS_SLAVE_WRITE | GMBUS_SW_RDY);
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800395 while (len) {
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800396 int ret;
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800397
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800398 val = loop = 0;
399 do {
400 val |= *buf++ << (8 * loop);
401 } while (--len && ++loop < 4);
402
403 I915_WRITE(GMBUS3 + reg_offset, val);
Daniel Kurtz7a39a9d2012-03-30 19:46:37 +0800404
Daniel Vetter28c70f12012-12-01 13:53:45 +0100405 ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_RDY,
406 GMBUS_HW_RDY_EN);
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800407 if (ret)
Daniel Vetter61168c52012-12-01 13:53:43 +0100408 return ret;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800409 }
410 return 0;
411}
412
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800413/*
414 * The gmbus controller can combine a 1 or 2 byte write with a read that
415 * immediately follows it by using an "INDEX" cycle.
416 */
417static bool
418gmbus_is_index_read(struct i2c_msg *msgs, int i, int num)
419{
420 return (i + 1 < num &&
421 !(msgs[i].flags & I2C_M_RD) && msgs[i].len <= 2 &&
422 (msgs[i + 1].flags & I2C_M_RD));
423}
424
425static int
426gmbus_xfer_index_read(struct drm_i915_private *dev_priv, struct i2c_msg *msgs)
427{
428 int reg_offset = dev_priv->gpio_mmio_base;
429 u32 gmbus1_index = 0;
430 u32 gmbus5 = 0;
431 int ret;
432
433 if (msgs[0].len == 2)
434 gmbus5 = GMBUS_2BYTE_INDEX_EN |
435 msgs[0].buf[1] | (msgs[0].buf[0] << 8);
436 if (msgs[0].len == 1)
437 gmbus1_index = GMBUS_CYCLE_INDEX |
438 (msgs[0].buf[0] << GMBUS_SLAVE_INDEX_SHIFT);
439
440 /* GMBUS5 holds 16-bit index */
441 if (gmbus5)
442 I915_WRITE(GMBUS5 + reg_offset, gmbus5);
443
444 ret = gmbus_xfer_read(dev_priv, &msgs[1], gmbus1_index);
445
446 /* Clear GMBUS5 after each index transfer */
447 if (gmbus5)
448 I915_WRITE(GMBUS5 + reg_offset, 0);
449
450 return ret;
451}
452
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800453static int
Chris Wilsonf899fc62010-07-20 15:44:45 -0700454gmbus_xfer(struct i2c_adapter *adapter,
455 struct i2c_msg *msgs,
456 int num)
457{
458 struct intel_gmbus *bus = container_of(adapter,
459 struct intel_gmbus,
460 adapter);
Daniel Vetterc2b91522012-02-14 22:37:19 +0100461 struct drm_i915_private *dev_priv = bus->dev_priv;
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800462 int i, reg_offset;
463 int ret = 0;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700464
Paulo Zanonic67a4702013-08-19 13:18:09 -0300465 intel_aux_display_runtime_get(dev_priv);
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500466 mutex_lock(&dev_priv->gmbus_mutex);
467
468 if (bus->force_bit) {
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800469 ret = i2c_bit_algo.master_xfer(adapter, msgs, num);
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500470 goto out;
471 }
Chris Wilsonf899fc62010-07-20 15:44:45 -0700472
Daniel Vetter110447fc2012-03-23 23:43:36 +0100473 reg_offset = dev_priv->gpio_mmio_base;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700474
Chris Wilsone957d772010-09-24 12:52:03 +0100475 I915_WRITE(GMBUS0 + reg_offset, bus->reg0);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700476
477 for (i = 0; i < num; i++) {
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800478 if (gmbus_is_index_read(msgs, i, num)) {
479 ret = gmbus_xfer_index_read(dev_priv, &msgs[i]);
480 i += 1; /* set i to the index of the read xfer */
481 } else if (msgs[i].flags & I2C_M_RD) {
482 ret = gmbus_xfer_read(dev_priv, &msgs[i], 0);
483 } else {
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800484 ret = gmbus_xfer_write(dev_priv, &msgs[i]);
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800485 }
Chris Wilsonf899fc62010-07-20 15:44:45 -0700486
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800487 if (ret == -ETIMEDOUT)
488 goto timeout;
489 if (ret == -ENXIO)
490 goto clear_err;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700491
Daniel Vetter28c70f12012-12-01 13:53:45 +0100492 ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_WAIT_PHASE,
493 GMBUS_HW_WAIT_EN);
Daniel Vetter61168c52012-12-01 13:53:43 +0100494 if (ret == -ENXIO)
495 goto clear_err;
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800496 if (ret)
Chris Wilsonf899fc62010-07-20 15:44:45 -0700497 goto timeout;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700498 }
499
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800500 /* Generate a STOP condition on the bus. Note that gmbus can't generata
501 * a STOP on the very first cycle. To simplify the code we
502 * unconditionally generate the STOP condition with an additional gmbus
503 * cycle. */
504 I915_WRITE(GMBUS1 + reg_offset, GMBUS_CYCLE_STOP | GMBUS_SW_RDY);
505
Benson Leungcaae7452012-02-09 12:03:17 -0800506 /* Mark the GMBUS interface as disabled after waiting for idle.
507 * We will re-enable it at the start of the next xfer,
508 * till then let it sleep.
Chris Wilson7f58aab2011-03-30 16:20:43 +0100509 */
Daniel Vetter2c438c02012-12-01 13:53:46 +0100510 if (gmbus_wait_idle(dev_priv)) {
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800511 DRM_DEBUG_KMS("GMBUS [%s] timed out waiting for idle\n",
Daniel Kurtze646d572012-03-30 19:46:38 +0800512 adapter->name);
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800513 ret = -ETIMEDOUT;
514 }
Chris Wilson7f58aab2011-03-30 16:20:43 +0100515 I915_WRITE(GMBUS0 + reg_offset, 0);
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800516 ret = ret ?: i;
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500517 goto out;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700518
Daniel Kurtze646d572012-03-30 19:46:38 +0800519clear_err:
520 /*
521 * Wait for bus to IDLE before clearing NAK.
522 * If we clear the NAK while bus is still active, then it will stay
523 * active and the next transaction may fail.
Daniel Vetter65e81862012-05-21 20:19:48 +0200524 *
525 * If no ACK is received during the address phase of a transaction, the
526 * adapter must report -ENXIO. It is not clear what to return if no ACK
527 * is received at other times. But we have to be careful to not return
528 * spurious -ENXIO because that will prevent i2c and drm edid functions
529 * from retrying. So return -ENXIO only when gmbus properly quiescents -
530 * timing out seems to happen when there _is_ a ddc chip present, but
531 * it's slow responding and only answers on the 2nd retry.
Daniel Kurtze646d572012-03-30 19:46:38 +0800532 */
Daniel Vetter65e81862012-05-21 20:19:48 +0200533 ret = -ENXIO;
Daniel Vetter2c438c02012-12-01 13:53:46 +0100534 if (gmbus_wait_idle(dev_priv)) {
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800535 DRM_DEBUG_KMS("GMBUS [%s] timed out after NAK\n",
536 adapter->name);
Daniel Vetter65e81862012-05-21 20:19:48 +0200537 ret = -ETIMEDOUT;
538 }
Daniel Kurtze646d572012-03-30 19:46:38 +0800539
540 /* Toggle the Software Clear Interrupt bit. This has the effect
541 * of resetting the GMBUS controller and so clearing the
542 * BUS_ERROR raised by the slave's NAK.
543 */
544 I915_WRITE(GMBUS1 + reg_offset, GMBUS_SW_CLR_INT);
545 I915_WRITE(GMBUS1 + reg_offset, 0);
546 I915_WRITE(GMBUS0 + reg_offset, 0);
547
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800548 DRM_DEBUG_KMS("GMBUS [%s] NAK for addr: %04x %c(%d)\n",
Daniel Kurtze646d572012-03-30 19:46:38 +0800549 adapter->name, msgs[i].addr,
550 (msgs[i].flags & I2C_M_RD) ? 'r' : 'w', msgs[i].len);
551
Daniel Kurtze646d572012-03-30 19:46:38 +0800552 goto out;
553
Chris Wilsonf899fc62010-07-20 15:44:45 -0700554timeout:
Daniel Kurtz874e3cc2012-03-28 02:36:11 +0800555 DRM_INFO("GMBUS [%s] timed out, falling back to bit banging on pin %d\n",
556 bus->adapter.name, bus->reg0 & 0xff);
Chris Wilson7f58aab2011-03-30 16:20:43 +0100557 I915_WRITE(GMBUS0 + reg_offset, 0);
558
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800559 /* Hardware may not support GMBUS over these pins? Try GPIO bitbanging instead. */
Chris Wilsonf2ce9fa2012-11-10 15:58:21 +0000560 bus->force_bit = 1;
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800561 ret = i2c_bit_algo.master_xfer(adapter, msgs, num);
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800562
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500563out:
564 mutex_unlock(&dev_priv->gmbus_mutex);
Paulo Zanonic67a4702013-08-19 13:18:09 -0300565 intel_aux_display_runtime_put(dev_priv);
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500566 return ret;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700567}
568
569static u32 gmbus_func(struct i2c_adapter *adapter)
570{
Daniel Vetterf6f808c2012-02-14 18:58:49 +0100571 return i2c_bit_algo.functionality(adapter) &
572 (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
Chris Wilsonf899fc62010-07-20 15:44:45 -0700573 /* I2C_FUNC_10BIT_ADDR | */
574 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
575 I2C_FUNC_SMBUS_BLOCK_PROC_CALL);
576}
577
578static const struct i2c_algorithm gmbus_algorithm = {
579 .master_xfer = gmbus_xfer,
580 .functionality = gmbus_func
581};
582
583/**
584 * intel_gmbus_setup - instantiate all Intel i2c GMBuses
585 * @dev: DRM device
586 */
587int intel_setup_gmbus(struct drm_device *dev)
588{
Chris Wilsonf899fc62010-07-20 15:44:45 -0700589 struct drm_i915_private *dev_priv = dev->dev_private;
590 int ret, i;
591
Ben Widawskyab5c6082013-04-05 13:12:41 -0700592 if (HAS_PCH_NOP(dev))
593 return 0;
594 else if (HAS_PCH_SPLIT(dev))
Daniel Vetter110447fc2012-03-23 23:43:36 +0100595 dev_priv->gpio_mmio_base = PCH_GPIOA - GPIOA;
Ville Syrjäläd8112152013-01-24 15:29:55 +0200596 else if (IS_VALLEYVIEW(dev))
597 dev_priv->gpio_mmio_base = VLV_DISPLAY_BASE;
Daniel Vetter110447fc2012-03-23 23:43:36 +0100598 else
599 dev_priv->gpio_mmio_base = 0;
600
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500601 mutex_init(&dev_priv->gmbus_mutex);
Daniel Vetter28c70f12012-12-01 13:53:45 +0100602 init_waitqueue_head(&dev_priv->gmbus_wait_queue);
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500603
Chris Wilsonf899fc62010-07-20 15:44:45 -0700604 for (i = 0; i < GMBUS_NUM_PORTS; i++) {
605 struct intel_gmbus *bus = &dev_priv->gmbus[i];
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800606 u32 port = i + 1; /* +1 to map gmbus index to pin pair */
Chris Wilsonf899fc62010-07-20 15:44:45 -0700607
608 bus->adapter.owner = THIS_MODULE;
609 bus->adapter.class = I2C_CLASS_DDC;
610 snprintf(bus->adapter.name,
Jean Delvare69669452010-11-05 18:51:34 +0100611 sizeof(bus->adapter.name),
612 "i915 gmbus %s",
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800613 gmbus_ports[i].name);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700614
615 bus->adapter.dev.parent = &dev->pdev->dev;
Daniel Vetterc2b91522012-02-14 22:37:19 +0100616 bus->dev_priv = dev_priv;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700617
618 bus->adapter.algo = &gmbus_algorithm;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700619
Chris Wilsone957d772010-09-24 12:52:03 +0100620 /* By default use a conservative clock rate */
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800621 bus->reg0 = port | GMBUS_RATE_100KHZ;
Chris Wilsoncb8ea752010-09-28 13:35:47 +0100622
Daniel Vetter83ee9e62012-05-13 14:44:20 +0200623 /* gmbus seems to be broken on i830 */
624 if (IS_I830(dev))
Chris Wilsonf2ce9fa2012-11-10 15:58:21 +0000625 bus->force_bit = 1;
Daniel Vetter83ee9e62012-05-13 14:44:20 +0200626
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800627 intel_gpio_setup(bus, port);
Jani Nikulacee25162012-08-13 17:33:02 +0300628
629 ret = i2c_add_adapter(&bus->adapter);
630 if (ret)
631 goto err;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700632 }
633
634 intel_i2c_reset(dev_priv->dev);
635
636 return 0;
637
638err:
639 while (--i) {
640 struct intel_gmbus *bus = &dev_priv->gmbus[i];
641 i2c_del_adapter(&bus->adapter);
642 }
Chris Wilsonf899fc62010-07-20 15:44:45 -0700643 return ret;
644}
645
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800646struct i2c_adapter *intel_gmbus_get_adapter(struct drm_i915_private *dev_priv,
647 unsigned port)
648{
649 WARN_ON(!intel_gmbus_is_port_valid(port));
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800650 /* -1 to map pin pair to gmbus index */
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800651 return (intel_gmbus_is_port_valid(port)) ?
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800652 &dev_priv->gmbus[port - 1].adapter : NULL;
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800653}
654
Chris Wilsone957d772010-09-24 12:52:03 +0100655void intel_gmbus_set_speed(struct i2c_adapter *adapter, int speed)
656{
657 struct intel_gmbus *bus = to_intel_gmbus(adapter);
658
Adam Jacksond5090b92011-06-16 16:36:28 -0400659 bus->reg0 = (bus->reg0 & ~(0x3 << 8)) | speed;
Chris Wilsone957d772010-09-24 12:52:03 +0100660}
661
662void intel_gmbus_force_bit(struct i2c_adapter *adapter, bool force_bit)
663{
664 struct intel_gmbus *bus = to_intel_gmbus(adapter);
665
Chris Wilsonf2ce9fa2012-11-10 15:58:21 +0000666 bus->force_bit += force_bit ? 1 : -1;
667 DRM_DEBUG_KMS("%sabling bit-banging on %s. force bit now %d\n",
668 force_bit ? "en" : "dis", adapter->name,
669 bus->force_bit);
Chris Wilsone957d772010-09-24 12:52:03 +0100670}
671
Chris Wilsonf899fc62010-07-20 15:44:45 -0700672void intel_teardown_gmbus(struct drm_device *dev)
673{
674 struct drm_i915_private *dev_priv = dev->dev_private;
675 int i;
676
Chris Wilsonf899fc62010-07-20 15:44:45 -0700677 for (i = 0; i < GMBUS_NUM_PORTS; i++) {
678 struct intel_gmbus *bus = &dev_priv->gmbus[i];
Chris Wilsonf899fc62010-07-20 15:44:45 -0700679 i2c_del_adapter(&bus->adapter);
680 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800681}