blob: 76070c4e76b3c0dcae835555bb02cece3f4195cc [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
Jani Nikula5ea6e5e2015-04-01 10:55:04 +030037struct gmbus_pin {
Daniel Kurtz2ed06c92012-03-28 02:36:15 +080038 const char *name;
39 int reg;
40};
41
Jani Nikula5ea6e5e2015-04-01 10:55:04 +030042/* Map gmbus pin pairs to names and registers. */
43static const struct gmbus_pin gmbus_pins[] = {
44 [GMBUS_PIN_SSC] = { "ssc", GPIOB },
45 [GMBUS_PIN_VGADDC] = { "vga", GPIOA },
46 [GMBUS_PIN_PANEL] = { "panel", GPIOC },
47 [GMBUS_PIN_DPC] = { "dpc", GPIOD },
48 [GMBUS_PIN_DPB] = { "dpb", GPIOE },
49 [GMBUS_PIN_DPD] = { "dpd", GPIOF },
Daniel Kurtz2ed06c92012-03-28 02:36:15 +080050};
51
Jani Nikulac1bad5b2015-05-06 15:33:43 +030052static const struct gmbus_pin gmbus_pins_bdw[] = {
53 [GMBUS_PIN_VGADDC] = { "vga", GPIOA },
54 [GMBUS_PIN_DPC] = { "dpc", GPIOD },
55 [GMBUS_PIN_DPB] = { "dpb", GPIOE },
56 [GMBUS_PIN_DPD] = { "dpd", GPIOF },
57};
58
Jani Nikula4c272832015-04-01 10:58:05 +030059static const struct gmbus_pin gmbus_pins_bxt[] = {
60 [GMBUS_PIN_1_BXT] = { "dpb", PCH_GPIOB },
61 [GMBUS_PIN_2_BXT] = { "dpc", PCH_GPIOC },
62 [GMBUS_PIN_3_BXT] = { "misc", PCH_GPIOD },
63};
64
65/* pin is expected to be valid */
66static const struct gmbus_pin *get_gmbus_pin(struct drm_i915_private *dev_priv,
67 unsigned int pin)
68{
69 if (IS_BROXTON(dev_priv))
70 return &gmbus_pins_bxt[pin];
Jani Nikulac1bad5b2015-05-06 15:33:43 +030071 else if (IS_BROADWELL(dev_priv))
72 return &gmbus_pins_bdw[pin];
Jani Nikula4c272832015-04-01 10:58:05 +030073 else
74 return &gmbus_pins[pin];
75}
76
Jani Nikula88ac7932015-03-27 00:20:22 +020077bool intel_gmbus_is_valid_pin(struct drm_i915_private *dev_priv,
78 unsigned int pin)
79{
Jani Nikula4c272832015-04-01 10:58:05 +030080 unsigned int size;
81
82 if (IS_BROXTON(dev_priv))
83 size = ARRAY_SIZE(gmbus_pins_bxt);
Jani Nikulac1bad5b2015-05-06 15:33:43 +030084 else if (IS_BROADWELL(dev_priv))
85 size = ARRAY_SIZE(gmbus_pins_bdw);
Jani Nikula4c272832015-04-01 10:58:05 +030086 else
87 size = ARRAY_SIZE(gmbus_pins);
88
89 return pin < size && get_gmbus_pin(dev_priv, pin)->reg;
Jani Nikula88ac7932015-03-27 00:20:22 +020090}
91
Chris Wilsonf899fc62010-07-20 15:44:45 -070092/* Intel GPIO access functions */
93
Jean Delvare1849ecb2012-01-28 11:07:09 +010094#define I2C_RISEFALL_TIME 10
Chris Wilsonf899fc62010-07-20 15:44:45 -070095
Chris Wilsone957d772010-09-24 12:52:03 +010096static inline struct intel_gmbus *
97to_intel_gmbus(struct i2c_adapter *i2c)
98{
99 return container_of(i2c, struct intel_gmbus, adapter);
100}
101
Chris Wilsonf899fc62010-07-20 15:44:45 -0700102void
103intel_i2c_reset(struct drm_device *dev)
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800104{
105 struct drm_i915_private *dev_priv = dev->dev_private;
Chon Ming Lee24eb2d52013-09-27 15:31:00 +0800106
Daniel Vetter110447fc2012-03-23 23:43:36 +0100107 I915_WRITE(dev_priv->gpio_mmio_base + GMBUS0, 0);
Daniel Vetter28c70f12012-12-01 13:53:45 +0100108 I915_WRITE(dev_priv->gpio_mmio_base + GMBUS4, 0);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700109}
110
111static void intel_i2c_quirk_set(struct drm_i915_private *dev_priv, bool enable)
112{
Chris Wilsonb222f262010-09-11 21:48:25 +0100113 u32 val;
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800114
115 /* When using bit bashing for I2C, this bit needs to be set to 1 */
Chris Wilsonf899fc62010-07-20 15:44:45 -0700116 if (!IS_PINEVIEW(dev_priv->dev))
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800117 return;
Chris Wilsonb222f262010-09-11 21:48:25 +0100118
119 val = I915_READ(DSPCLK_GATE_D);
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800120 if (enable)
Chris Wilsonb222f262010-09-11 21:48:25 +0100121 val |= DPCUNIT_CLOCK_GATE_DISABLE;
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800122 else
Chris Wilsonb222f262010-09-11 21:48:25 +0100123 val &= ~DPCUNIT_CLOCK_GATE_DISABLE;
124 I915_WRITE(DSPCLK_GATE_D, val);
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800125}
126
Daniel Vetter36c785f2012-02-14 22:37:22 +0100127static u32 get_reserved(struct intel_gmbus *bus)
Chris Wilsone957d772010-09-24 12:52:03 +0100128{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100129 struct drm_i915_private *dev_priv = bus->dev_priv;
Chris Wilsone957d772010-09-24 12:52:03 +0100130 struct drm_device *dev = dev_priv->dev;
131 u32 reserved = 0;
132
133 /* On most chips, these bits must be preserved in software. */
134 if (!IS_I830(dev) && !IS_845G(dev))
Daniel Vetter36c785f2012-02-14 22:37:22 +0100135 reserved = I915_READ_NOTRACE(bus->gpio_reg) &
Yuanhan Liudb5e4172010-11-08 09:58:16 +0000136 (GPIO_DATA_PULLUP_DISABLE |
137 GPIO_CLOCK_PULLUP_DISABLE);
Chris Wilsone957d772010-09-24 12:52:03 +0100138
139 return reserved;
140}
141
Jesse Barnes79e53942008-11-07 14:24:08 -0800142static int get_clock(void *data)
143{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100144 struct intel_gmbus *bus = data;
145 struct drm_i915_private *dev_priv = bus->dev_priv;
146 u32 reserved = get_reserved(bus);
147 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_CLOCK_DIR_MASK);
148 I915_WRITE_NOTRACE(bus->gpio_reg, reserved);
149 return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_CLOCK_VAL_IN) != 0;
Jesse Barnes79e53942008-11-07 14:24:08 -0800150}
151
152static int get_data(void *data)
153{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100154 struct intel_gmbus *bus = data;
155 struct drm_i915_private *dev_priv = bus->dev_priv;
156 u32 reserved = get_reserved(bus);
157 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | GPIO_DATA_DIR_MASK);
158 I915_WRITE_NOTRACE(bus->gpio_reg, reserved);
159 return (I915_READ_NOTRACE(bus->gpio_reg) & GPIO_DATA_VAL_IN) != 0;
Jesse Barnes79e53942008-11-07 14:24:08 -0800160}
161
162static void set_clock(void *data, int state_high)
163{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100164 struct intel_gmbus *bus = data;
165 struct drm_i915_private *dev_priv = bus->dev_priv;
166 u32 reserved = get_reserved(bus);
Chris Wilsone957d772010-09-24 12:52:03 +0100167 u32 clock_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -0800168
169 if (state_high)
170 clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
171 else
172 clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
173 GPIO_CLOCK_VAL_MASK;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700174
Daniel Vetter36c785f2012-02-14 22:37:22 +0100175 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | clock_bits);
176 POSTING_READ(bus->gpio_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -0800177}
178
179static void set_data(void *data, int state_high)
180{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100181 struct intel_gmbus *bus = data;
182 struct drm_i915_private *dev_priv = bus->dev_priv;
183 u32 reserved = get_reserved(bus);
Chris Wilsone957d772010-09-24 12:52:03 +0100184 u32 data_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -0800185
186 if (state_high)
187 data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
188 else
189 data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
190 GPIO_DATA_VAL_MASK;
191
Daniel Vetter36c785f2012-02-14 22:37:22 +0100192 I915_WRITE_NOTRACE(bus->gpio_reg, reserved | data_bits);
193 POSTING_READ(bus->gpio_reg);
Jesse Barnes79e53942008-11-07 14:24:08 -0800194}
195
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800196static int
197intel_gpio_pre_xfer(struct i2c_adapter *adapter)
198{
199 struct intel_gmbus *bus = container_of(adapter,
200 struct intel_gmbus,
201 adapter);
202 struct drm_i915_private *dev_priv = bus->dev_priv;
203
204 intel_i2c_reset(dev_priv->dev);
205 intel_i2c_quirk_set(dev_priv, true);
206 set_data(bus, 1);
207 set_clock(bus, 1);
208 udelay(I2C_RISEFALL_TIME);
209 return 0;
210}
211
212static void
213intel_gpio_post_xfer(struct i2c_adapter *adapter)
214{
215 struct intel_gmbus *bus = container_of(adapter,
216 struct intel_gmbus,
217 adapter);
218 struct drm_i915_private *dev_priv = bus->dev_priv;
219
220 set_data(bus, 1);
221 set_clock(bus, 1);
222 intel_i2c_quirk_set(dev_priv, false);
223}
224
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800225static void
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300226intel_gpio_setup(struct intel_gmbus *bus, unsigned int pin)
Eric Anholtf0217c42009-12-01 11:56:30 -0800227{
Daniel Vetter36c785f2012-02-14 22:37:22 +0100228 struct drm_i915_private *dev_priv = bus->dev_priv;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100229 struct i2c_algo_bit_data *algo;
Eric Anholtf0217c42009-12-01 11:56:30 -0800230
Daniel Vetterc167a6f2012-02-28 00:43:09 +0100231 algo = &bus->bit_algo;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100232
Jani Nikula4c272832015-04-01 10:58:05 +0300233 bus->gpio_reg = dev_priv->gpio_mmio_base +
234 get_gmbus_pin(dev_priv, pin)->reg;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700235
Daniel Vetterc167a6f2012-02-28 00:43:09 +0100236 bus->adapter.algo_data = algo;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100237 algo->setsda = set_data;
238 algo->setscl = set_clock;
239 algo->getsda = get_data;
240 algo->getscl = get_clock;
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800241 algo->pre_xfer = intel_gpio_pre_xfer;
242 algo->post_xfer = intel_gpio_post_xfer;
Daniel Vetter36c785f2012-02-14 22:37:22 +0100243 algo->udelay = I2C_RISEFALL_TIME;
244 algo->timeout = usecs_to_jiffies(2200);
245 algo->data = bus;
Jesse Barnes79e53942008-11-07 14:24:08 -0800246}
247
Chris Wilsonf899fc62010-07-20 15:44:45 -0700248static int
Daniel Vetter61168c52012-12-01 13:53:43 +0100249gmbus_wait_hw_status(struct drm_i915_private *dev_priv,
Daniel Vetter28c70f12012-12-01 13:53:45 +0100250 u32 gmbus2_status,
251 u32 gmbus4_irq_en)
Daniel Vetter61168c52012-12-01 13:53:43 +0100252{
Daniel Vetter28c70f12012-12-01 13:53:45 +0100253 int i;
Daniel Vetter61168c52012-12-01 13:53:43 +0100254 int reg_offset = dev_priv->gpio_mmio_base;
Daniel Vetter28c70f12012-12-01 13:53:45 +0100255 u32 gmbus2 = 0;
256 DEFINE_WAIT(wait);
Daniel Vetter61168c52012-12-01 13:53:43 +0100257
Jiri Kosinac12aba52013-03-19 09:56:57 +0100258 if (!HAS_GMBUS_IRQ(dev_priv->dev))
259 gmbus4_irq_en = 0;
260
Daniel Vetter28c70f12012-12-01 13:53:45 +0100261 /* Important: The hw handles only the first bit, so set only one! Since
262 * we also need to check for NAKs besides the hw ready/idle signal, we
263 * need to wake up periodically and check that ourselves. */
264 I915_WRITE(GMBUS4 + reg_offset, gmbus4_irq_en);
265
Imre Deak2554fc12013-05-21 20:03:18 +0300266 for (i = 0; i < msecs_to_jiffies_timeout(50); i++) {
Daniel Vetter28c70f12012-12-01 13:53:45 +0100267 prepare_to_wait(&dev_priv->gmbus_wait_queue, &wait,
268 TASK_UNINTERRUPTIBLE);
269
Daniel Vetteref04f002012-12-01 21:03:59 +0100270 gmbus2 = I915_READ_NOTRACE(GMBUS2 + reg_offset);
Daniel Vetter28c70f12012-12-01 13:53:45 +0100271 if (gmbus2 & (GMBUS_SATOER | gmbus2_status))
272 break;
273
274 schedule_timeout(1);
275 }
276 finish_wait(&dev_priv->gmbus_wait_queue, &wait);
277
278 I915_WRITE(GMBUS4 + reg_offset, 0);
Daniel Vetter61168c52012-12-01 13:53:43 +0100279
280 if (gmbus2 & GMBUS_SATOER)
281 return -ENXIO;
Daniel Vetter28c70f12012-12-01 13:53:45 +0100282 if (gmbus2 & gmbus2_status)
283 return 0;
284 return -ETIMEDOUT;
Daniel Vetter61168c52012-12-01 13:53:43 +0100285}
286
287static int
Daniel Vetter2c438c02012-12-01 13:53:46 +0100288gmbus_wait_idle(struct drm_i915_private *dev_priv)
289{
290 int ret;
291 int reg_offset = dev_priv->gpio_mmio_base;
292
Daniel Vetteref04f002012-12-01 21:03:59 +0100293#define C ((I915_READ_NOTRACE(GMBUS2 + reg_offset) & GMBUS_ACTIVE) == 0)
Daniel Vetter2c438c02012-12-01 13:53:46 +0100294
295 if (!HAS_GMBUS_IRQ(dev_priv->dev))
296 return wait_for(C, 10);
297
298 /* Important: The hw handles only the first bit, so set only one! */
299 I915_WRITE(GMBUS4 + reg_offset, GMBUS_IDLE_EN);
300
Imre Deak35987062013-05-21 20:03:20 +0300301 ret = wait_event_timeout(dev_priv->gmbus_wait_queue, C,
302 msecs_to_jiffies_timeout(10));
Daniel Vetter2c438c02012-12-01 13:53:46 +0100303
304 I915_WRITE(GMBUS4 + reg_offset, 0);
305
306 if (ret)
307 return 0;
308 else
309 return -ETIMEDOUT;
310#undef C
311}
312
313static int
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700314gmbus_xfer_read_chunk(struct drm_i915_private *dev_priv,
315 unsigned short addr, u8 *buf, unsigned int len,
316 u32 gmbus1_index)
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800317{
318 int reg_offset = dev_priv->gpio_mmio_base;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800319
320 I915_WRITE(GMBUS1 + reg_offset,
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800321 gmbus1_index |
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800322 GMBUS_CYCLE_WAIT |
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800323 (len << GMBUS_BYTE_COUNT_SHIFT) |
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700324 (addr << GMBUS_SLAVE_ADDR_SHIFT) |
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800325 GMBUS_SLAVE_READ | GMBUS_SW_RDY);
Daniel Kurtz79985ee2012-04-13 19:47:53 +0800326 while (len) {
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800327 int ret;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800328 u32 val, loop = 0;
329
Daniel Vetter28c70f12012-12-01 13:53:45 +0100330 ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_RDY,
331 GMBUS_HW_RDY_EN);
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800332 if (ret)
Daniel Vetter61168c52012-12-01 13:53:43 +0100333 return ret;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800334
335 val = I915_READ(GMBUS3 + reg_offset);
336 do {
337 *buf++ = val & 0xff;
338 val >>= 8;
339 } while (--len && ++loop < 4);
Daniel Kurtz79985ee2012-04-13 19:47:53 +0800340 }
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800341
342 return 0;
343}
344
345static int
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700346gmbus_xfer_read(struct drm_i915_private *dev_priv, struct i2c_msg *msg,
347 u32 gmbus1_index)
348{
349 u8 *buf = msg->buf;
350 unsigned int rx_size = msg->len;
351 unsigned int len;
352 int ret;
353
354 do {
355 len = min(rx_size, GMBUS_BYTE_COUNT_MAX);
356
357 ret = gmbus_xfer_read_chunk(dev_priv, msg->addr,
358 buf, len, gmbus1_index);
359 if (ret)
360 return ret;
361
362 rx_size -= len;
363 buf += len;
364 } while (rx_size != 0);
365
366 return 0;
367}
368
369static int
370gmbus_xfer_write_chunk(struct drm_i915_private *dev_priv,
371 unsigned short addr, u8 *buf, unsigned int len)
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800372{
373 int reg_offset = dev_priv->gpio_mmio_base;
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700374 unsigned int chunk_size = len;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800375 u32 val, loop;
376
377 val = loop = 0;
Daniel Kurtz26883c32012-03-30 19:46:36 +0800378 while (len && loop < 4) {
379 val |= *buf++ << (8 * loop++);
380 len -= 1;
381 }
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800382
383 I915_WRITE(GMBUS3 + reg_offset, val);
384 I915_WRITE(GMBUS1 + reg_offset,
385 GMBUS_CYCLE_WAIT |
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700386 (chunk_size << GMBUS_BYTE_COUNT_SHIFT) |
387 (addr << GMBUS_SLAVE_ADDR_SHIFT) |
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800388 GMBUS_SLAVE_WRITE | GMBUS_SW_RDY);
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800389 while (len) {
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800390 int ret;
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800391
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800392 val = loop = 0;
393 do {
394 val |= *buf++ << (8 * loop);
395 } while (--len && ++loop < 4);
396
397 I915_WRITE(GMBUS3 + reg_offset, val);
Daniel Kurtz7a39a9d2012-03-30 19:46:37 +0800398
Daniel Vetter28c70f12012-12-01 13:53:45 +0100399 ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_RDY,
400 GMBUS_HW_RDY_EN);
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800401 if (ret)
Daniel Vetter61168c52012-12-01 13:53:43 +0100402 return ret;
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800403 }
Dmitry Torokhov9535c472015-04-21 09:49:11 -0700404
405 return 0;
406}
407
408static int
409gmbus_xfer_write(struct drm_i915_private *dev_priv, struct i2c_msg *msg)
410{
411 u8 *buf = msg->buf;
412 unsigned int tx_size = msg->len;
413 unsigned int len;
414 int ret;
415
416 do {
417 len = min(tx_size, GMBUS_BYTE_COUNT_MAX);
418
419 ret = gmbus_xfer_write_chunk(dev_priv, msg->addr, buf, len);
420 if (ret)
421 return ret;
422
423 buf += len;
424 tx_size -= len;
425 } while (tx_size != 0);
426
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800427 return 0;
428}
429
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800430/*
431 * The gmbus controller can combine a 1 or 2 byte write with a read that
432 * immediately follows it by using an "INDEX" cycle.
433 */
434static bool
435gmbus_is_index_read(struct i2c_msg *msgs, int i, int num)
436{
437 return (i + 1 < num &&
438 !(msgs[i].flags & I2C_M_RD) && msgs[i].len <= 2 &&
439 (msgs[i + 1].flags & I2C_M_RD));
440}
441
442static int
443gmbus_xfer_index_read(struct drm_i915_private *dev_priv, struct i2c_msg *msgs)
444{
445 int reg_offset = dev_priv->gpio_mmio_base;
446 u32 gmbus1_index = 0;
447 u32 gmbus5 = 0;
448 int ret;
449
450 if (msgs[0].len == 2)
451 gmbus5 = GMBUS_2BYTE_INDEX_EN |
452 msgs[0].buf[1] | (msgs[0].buf[0] << 8);
453 if (msgs[0].len == 1)
454 gmbus1_index = GMBUS_CYCLE_INDEX |
455 (msgs[0].buf[0] << GMBUS_SLAVE_INDEX_SHIFT);
456
457 /* GMBUS5 holds 16-bit index */
458 if (gmbus5)
459 I915_WRITE(GMBUS5 + reg_offset, gmbus5);
460
461 ret = gmbus_xfer_read(dev_priv, &msgs[1], gmbus1_index);
462
463 /* Clear GMBUS5 after each index transfer */
464 if (gmbus5)
465 I915_WRITE(GMBUS5 + reg_offset, 0);
466
467 return ret;
468}
469
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800470static int
Chris Wilsonf899fc62010-07-20 15:44:45 -0700471gmbus_xfer(struct i2c_adapter *adapter,
472 struct i2c_msg *msgs,
473 int num)
474{
475 struct intel_gmbus *bus = container_of(adapter,
476 struct intel_gmbus,
477 adapter);
Daniel Vetterc2b91522012-02-14 22:37:19 +0100478 struct drm_i915_private *dev_priv = bus->dev_priv;
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800479 int i, reg_offset;
480 int ret = 0;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700481
Paulo Zanonic67a4702013-08-19 13:18:09 -0300482 intel_aux_display_runtime_get(dev_priv);
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500483 mutex_lock(&dev_priv->gmbus_mutex);
484
485 if (bus->force_bit) {
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800486 ret = i2c_bit_algo.master_xfer(adapter, msgs, num);
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500487 goto out;
488 }
Chris Wilsonf899fc62010-07-20 15:44:45 -0700489
Daniel Vetter110447fc2012-03-23 23:43:36 +0100490 reg_offset = dev_priv->gpio_mmio_base;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700491
Chris Wilsone957d772010-09-24 12:52:03 +0100492 I915_WRITE(GMBUS0 + reg_offset, bus->reg0);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700493
494 for (i = 0; i < num; i++) {
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800495 if (gmbus_is_index_read(msgs, i, num)) {
496 ret = gmbus_xfer_index_read(dev_priv, &msgs[i]);
497 i += 1; /* set i to the index of the read xfer */
498 } else if (msgs[i].flags & I2C_M_RD) {
499 ret = gmbus_xfer_read(dev_priv, &msgs[i], 0);
500 } else {
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800501 ret = gmbus_xfer_write(dev_priv, &msgs[i]);
Daniel Kurtz56f9eac2012-03-30 19:46:40 +0800502 }
Chris Wilsonf899fc62010-07-20 15:44:45 -0700503
Daniel Kurtz924a93e2012-03-28 02:36:10 +0800504 if (ret == -ETIMEDOUT)
505 goto timeout;
506 if (ret == -ENXIO)
507 goto clear_err;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700508
Daniel Vetter28c70f12012-12-01 13:53:45 +0100509 ret = gmbus_wait_hw_status(dev_priv, GMBUS_HW_WAIT_PHASE,
510 GMBUS_HW_WAIT_EN);
Daniel Vetter61168c52012-12-01 13:53:43 +0100511 if (ret == -ENXIO)
512 goto clear_err;
Daniel Kurtz90e6b262012-03-30 19:46:41 +0800513 if (ret)
Chris Wilsonf899fc62010-07-20 15:44:45 -0700514 goto timeout;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700515 }
516
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800517 /* Generate a STOP condition on the bus. Note that gmbus can't generata
518 * a STOP on the very first cycle. To simplify the code we
519 * unconditionally generate the STOP condition with an additional gmbus
520 * cycle. */
521 I915_WRITE(GMBUS1 + reg_offset, GMBUS_CYCLE_STOP | GMBUS_SW_RDY);
522
Benson Leungcaae7452012-02-09 12:03:17 -0800523 /* Mark the GMBUS interface as disabled after waiting for idle.
524 * We will re-enable it at the start of the next xfer,
525 * till then let it sleep.
Chris Wilson7f58aab2011-03-30 16:20:43 +0100526 */
Daniel Vetter2c438c02012-12-01 13:53:46 +0100527 if (gmbus_wait_idle(dev_priv)) {
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800528 DRM_DEBUG_KMS("GMBUS [%s] timed out waiting for idle\n",
Daniel Kurtze646d572012-03-30 19:46:38 +0800529 adapter->name);
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800530 ret = -ETIMEDOUT;
531 }
Chris Wilson7f58aab2011-03-30 16:20:43 +0100532 I915_WRITE(GMBUS0 + reg_offset, 0);
Daniel Kurtz72d66af2012-03-30 19:46:39 +0800533 ret = ret ?: i;
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500534 goto out;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700535
Daniel Kurtze646d572012-03-30 19:46:38 +0800536clear_err:
537 /*
538 * Wait for bus to IDLE before clearing NAK.
539 * If we clear the NAK while bus is still active, then it will stay
540 * active and the next transaction may fail.
Daniel Vetter65e81862012-05-21 20:19:48 +0200541 *
542 * If no ACK is received during the address phase of a transaction, the
543 * adapter must report -ENXIO. It is not clear what to return if no ACK
544 * is received at other times. But we have to be careful to not return
545 * spurious -ENXIO because that will prevent i2c and drm edid functions
546 * from retrying. So return -ENXIO only when gmbus properly quiescents -
547 * timing out seems to happen when there _is_ a ddc chip present, but
548 * it's slow responding and only answers on the 2nd retry.
Daniel Kurtze646d572012-03-30 19:46:38 +0800549 */
Daniel Vetter65e81862012-05-21 20:19:48 +0200550 ret = -ENXIO;
Daniel Vetter2c438c02012-12-01 13:53:46 +0100551 if (gmbus_wait_idle(dev_priv)) {
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800552 DRM_DEBUG_KMS("GMBUS [%s] timed out after NAK\n",
553 adapter->name);
Daniel Vetter65e81862012-05-21 20:19:48 +0200554 ret = -ETIMEDOUT;
555 }
Daniel Kurtze646d572012-03-30 19:46:38 +0800556
557 /* Toggle the Software Clear Interrupt bit. This has the effect
558 * of resetting the GMBUS controller and so clearing the
559 * BUS_ERROR raised by the slave's NAK.
560 */
561 I915_WRITE(GMBUS1 + reg_offset, GMBUS_SW_CLR_INT);
562 I915_WRITE(GMBUS1 + reg_offset, 0);
563 I915_WRITE(GMBUS0 + reg_offset, 0);
564
Daniel Kurtz56fa6d62012-04-13 19:47:54 +0800565 DRM_DEBUG_KMS("GMBUS [%s] NAK for addr: %04x %c(%d)\n",
Daniel Kurtze646d572012-03-30 19:46:38 +0800566 adapter->name, msgs[i].addr,
567 (msgs[i].flags & I2C_M_RD) ? 'r' : 'w', msgs[i].len);
568
Daniel Kurtze646d572012-03-30 19:46:38 +0800569 goto out;
570
Chris Wilsonf899fc62010-07-20 15:44:45 -0700571timeout:
Daniel Kurtz874e3cc2012-03-28 02:36:11 +0800572 DRM_INFO("GMBUS [%s] timed out, falling back to bit banging on pin %d\n",
573 bus->adapter.name, bus->reg0 & 0xff);
Chris Wilson7f58aab2011-03-30 16:20:43 +0100574 I915_WRITE(GMBUS0 + reg_offset, 0);
575
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800576 /* Hardware may not support GMBUS over these pins? Try GPIO bitbanging instead. */
Chris Wilsonf2ce9fa2012-11-10 15:58:21 +0000577 bus->force_bit = 1;
Daniel Kurtz2ed06c92012-03-28 02:36:15 +0800578 ret = i2c_bit_algo.master_xfer(adapter, msgs, num);
Daniel Kurtz489fbc12012-03-28 02:36:13 +0800579
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500580out:
581 mutex_unlock(&dev_priv->gmbus_mutex);
Paulo Zanonic67a4702013-08-19 13:18:09 -0300582 intel_aux_display_runtime_put(dev_priv);
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500583 return ret;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700584}
585
586static u32 gmbus_func(struct i2c_adapter *adapter)
587{
Daniel Vetterf6f808c2012-02-14 18:58:49 +0100588 return i2c_bit_algo.functionality(adapter) &
589 (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
Chris Wilsonf899fc62010-07-20 15:44:45 -0700590 /* I2C_FUNC_10BIT_ADDR | */
591 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
592 I2C_FUNC_SMBUS_BLOCK_PROC_CALL);
593}
594
595static const struct i2c_algorithm gmbus_algorithm = {
596 .master_xfer = gmbus_xfer,
597 .functionality = gmbus_func
598};
599
600/**
601 * intel_gmbus_setup - instantiate all Intel i2c GMBuses
602 * @dev: DRM device
603 */
604int intel_setup_gmbus(struct drm_device *dev)
605{
Chris Wilsonf899fc62010-07-20 15:44:45 -0700606 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300607 struct intel_gmbus *bus;
608 unsigned int pin;
609 int ret;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700610
Ben Widawskyab5c6082013-04-05 13:12:41 -0700611 if (HAS_PCH_NOP(dev))
612 return 0;
613 else if (HAS_PCH_SPLIT(dev))
Daniel Vetter110447fc2012-03-23 23:43:36 +0100614 dev_priv->gpio_mmio_base = PCH_GPIOA - GPIOA;
Ville Syrjäläd8112152013-01-24 15:29:55 +0200615 else if (IS_VALLEYVIEW(dev))
616 dev_priv->gpio_mmio_base = VLV_DISPLAY_BASE;
Daniel Vetter110447fc2012-03-23 23:43:36 +0100617 else
618 dev_priv->gpio_mmio_base = 0;
619
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500620 mutex_init(&dev_priv->gmbus_mutex);
Daniel Vetter28c70f12012-12-01 13:53:45 +0100621 init_waitqueue_head(&dev_priv->gmbus_wait_queue);
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500622
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300623 for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) {
Jani Nikula88ac7932015-03-27 00:20:22 +0200624 if (!intel_gmbus_is_valid_pin(dev_priv, pin))
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300625 continue;
626
627 bus = &dev_priv->gmbus[pin];
Chris Wilsonf899fc62010-07-20 15:44:45 -0700628
629 bus->adapter.owner = THIS_MODULE;
630 bus->adapter.class = I2C_CLASS_DDC;
631 snprintf(bus->adapter.name,
Jean Delvare69669452010-11-05 18:51:34 +0100632 sizeof(bus->adapter.name),
633 "i915 gmbus %s",
Jani Nikula4c272832015-04-01 10:58:05 +0300634 get_gmbus_pin(dev_priv, pin)->name);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700635
636 bus->adapter.dev.parent = &dev->pdev->dev;
Daniel Vetterc2b91522012-02-14 22:37:19 +0100637 bus->dev_priv = dev_priv;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700638
639 bus->adapter.algo = &gmbus_algorithm;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700640
Chris Wilsone957d772010-09-24 12:52:03 +0100641 /* By default use a conservative clock rate */
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300642 bus->reg0 = pin | GMBUS_RATE_100KHZ;
Chris Wilsoncb8ea752010-09-28 13:35:47 +0100643
Daniel Vetter83ee9e62012-05-13 14:44:20 +0200644 /* gmbus seems to be broken on i830 */
645 if (IS_I830(dev))
Chris Wilsonf2ce9fa2012-11-10 15:58:21 +0000646 bus->force_bit = 1;
Daniel Vetter83ee9e62012-05-13 14:44:20 +0200647
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300648 intel_gpio_setup(bus, pin);
Jani Nikulacee25162012-08-13 17:33:02 +0300649
650 ret = i2c_add_adapter(&bus->adapter);
651 if (ret)
652 goto err;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700653 }
654
655 intel_i2c_reset(dev_priv->dev);
656
657 return 0;
658
659err:
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300660 while (--pin) {
Jani Nikula88ac7932015-03-27 00:20:22 +0200661 if (!intel_gmbus_is_valid_pin(dev_priv, pin))
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300662 continue;
663
664 bus = &dev_priv->gmbus[pin];
Chris Wilsonf899fc62010-07-20 15:44:45 -0700665 i2c_del_adapter(&bus->adapter);
666 }
Chris Wilsonf899fc62010-07-20 15:44:45 -0700667 return ret;
668}
669
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800670struct i2c_adapter *intel_gmbus_get_adapter(struct drm_i915_private *dev_priv,
Jani Nikula0184df42015-03-27 00:20:20 +0200671 unsigned int pin)
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800672{
Jani Nikula88ac7932015-03-27 00:20:22 +0200673 if (WARN_ON(!intel_gmbus_is_valid_pin(dev_priv, pin)))
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300674 return NULL;
675
676 return &dev_priv->gmbus[pin].adapter;
Daniel Kurtz3bd7d902012-03-28 02:36:14 +0800677}
678
Chris Wilsone957d772010-09-24 12:52:03 +0100679void intel_gmbus_set_speed(struct i2c_adapter *adapter, int speed)
680{
681 struct intel_gmbus *bus = to_intel_gmbus(adapter);
682
Adam Jacksond5090b92011-06-16 16:36:28 -0400683 bus->reg0 = (bus->reg0 & ~(0x3 << 8)) | speed;
Chris Wilsone957d772010-09-24 12:52:03 +0100684}
685
686void intel_gmbus_force_bit(struct i2c_adapter *adapter, bool force_bit)
687{
688 struct intel_gmbus *bus = to_intel_gmbus(adapter);
689
Chris Wilsonf2ce9fa2012-11-10 15:58:21 +0000690 bus->force_bit += force_bit ? 1 : -1;
691 DRM_DEBUG_KMS("%sabling bit-banging on %s. force bit now %d\n",
692 force_bit ? "en" : "dis", adapter->name,
693 bus->force_bit);
Chris Wilsone957d772010-09-24 12:52:03 +0100694}
695
Chris Wilsonf899fc62010-07-20 15:44:45 -0700696void intel_teardown_gmbus(struct drm_device *dev)
697{
698 struct drm_i915_private *dev_priv = dev->dev_private;
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300699 struct intel_gmbus *bus;
700 unsigned int pin;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700701
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300702 for (pin = 0; pin < ARRAY_SIZE(dev_priv->gmbus); pin++) {
Jani Nikula88ac7932015-03-27 00:20:22 +0200703 if (!intel_gmbus_is_valid_pin(dev_priv, pin))
Jani Nikula5ea6e5e2015-04-01 10:55:04 +0300704 continue;
705
706 bus = &dev_priv->gmbus[pin];
Chris Wilsonf899fc62010-07-20 15:44:45 -0700707 i2c_del_adapter(&bus->adapter);
708 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800709}