blob: 89346cc0bc4b0febb5630d5e83f0774001add785 [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>
Jesse Barnes79e53942008-11-07 14:24:08 -080032#include "drmP.h"
33#include "drm.h"
34#include "intel_drv.h"
35#include "i915_drm.h"
36#include "i915_drv.h"
37
Chris Wilsonf899fc62010-07-20 15:44:45 -070038/* Intel GPIO access functions */
39
Jean Delvare1849ecb2012-01-28 11:07:09 +010040#define I2C_RISEFALL_TIME 10
Chris Wilsonf899fc62010-07-20 15:44:45 -070041
Chris Wilsone957d772010-09-24 12:52:03 +010042static inline struct intel_gmbus *
43to_intel_gmbus(struct i2c_adapter *i2c)
44{
45 return container_of(i2c, struct intel_gmbus, adapter);
46}
47
Chris Wilsonf899fc62010-07-20 15:44:45 -070048struct intel_gpio {
49 struct i2c_adapter adapter;
50 struct i2c_algo_bit_data algo;
51 struct drm_i915_private *dev_priv;
52 u32 reg;
53};
54
55void
56intel_i2c_reset(struct drm_device *dev)
Shaohua Li0ba0e9e2009-04-07 11:02:28 +080057{
58 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonf899fc62010-07-20 15:44:45 -070059 if (HAS_PCH_SPLIT(dev))
60 I915_WRITE(PCH_GMBUS0, 0);
61 else
62 I915_WRITE(GMBUS0, 0);
63}
64
65static void intel_i2c_quirk_set(struct drm_i915_private *dev_priv, bool enable)
66{
Chris Wilsonb222f262010-09-11 21:48:25 +010067 u32 val;
Shaohua Li0ba0e9e2009-04-07 11:02:28 +080068
69 /* When using bit bashing for I2C, this bit needs to be set to 1 */
Chris Wilsonf899fc62010-07-20 15:44:45 -070070 if (!IS_PINEVIEW(dev_priv->dev))
Shaohua Li0ba0e9e2009-04-07 11:02:28 +080071 return;
Chris Wilsonb222f262010-09-11 21:48:25 +010072
73 val = I915_READ(DSPCLK_GATE_D);
Shaohua Li0ba0e9e2009-04-07 11:02:28 +080074 if (enable)
Chris Wilsonb222f262010-09-11 21:48:25 +010075 val |= DPCUNIT_CLOCK_GATE_DISABLE;
Shaohua Li0ba0e9e2009-04-07 11:02:28 +080076 else
Chris Wilsonb222f262010-09-11 21:48:25 +010077 val &= ~DPCUNIT_CLOCK_GATE_DISABLE;
78 I915_WRITE(DSPCLK_GATE_D, val);
Shaohua Li0ba0e9e2009-04-07 11:02:28 +080079}
80
Chris Wilsone957d772010-09-24 12:52:03 +010081static u32 get_reserved(struct intel_gpio *gpio)
82{
83 struct drm_i915_private *dev_priv = gpio->dev_priv;
84 struct drm_device *dev = dev_priv->dev;
85 u32 reserved = 0;
86
87 /* On most chips, these bits must be preserved in software. */
88 if (!IS_I830(dev) && !IS_845G(dev))
Yuanhan Liudb5e4172010-11-08 09:58:16 +000089 reserved = I915_READ_NOTRACE(gpio->reg) &
90 (GPIO_DATA_PULLUP_DISABLE |
91 GPIO_CLOCK_PULLUP_DISABLE);
Chris Wilsone957d772010-09-24 12:52:03 +010092
93 return reserved;
94}
95
Jesse Barnes79e53942008-11-07 14:24:08 -080096static int get_clock(void *data)
97{
Chris Wilsonf899fc62010-07-20 15:44:45 -070098 struct intel_gpio *gpio = data;
99 struct drm_i915_private *dev_priv = gpio->dev_priv;
Chris Wilsone957d772010-09-24 12:52:03 +0100100 u32 reserved = get_reserved(gpio);
Yuanhan Liudb5e4172010-11-08 09:58:16 +0000101 I915_WRITE_NOTRACE(gpio->reg, reserved | GPIO_CLOCK_DIR_MASK);
102 I915_WRITE_NOTRACE(gpio->reg, reserved);
103 return (I915_READ_NOTRACE(gpio->reg) & GPIO_CLOCK_VAL_IN) != 0;
Jesse Barnes79e53942008-11-07 14:24:08 -0800104}
105
106static int get_data(void *data)
107{
Chris Wilsonf899fc62010-07-20 15:44:45 -0700108 struct intel_gpio *gpio = data;
109 struct drm_i915_private *dev_priv = gpio->dev_priv;
Chris Wilsone957d772010-09-24 12:52:03 +0100110 u32 reserved = get_reserved(gpio);
Yuanhan Liudb5e4172010-11-08 09:58:16 +0000111 I915_WRITE_NOTRACE(gpio->reg, reserved | GPIO_DATA_DIR_MASK);
112 I915_WRITE_NOTRACE(gpio->reg, reserved);
113 return (I915_READ_NOTRACE(gpio->reg) & GPIO_DATA_VAL_IN) != 0;
Jesse Barnes79e53942008-11-07 14:24:08 -0800114}
115
116static void set_clock(void *data, int state_high)
117{
Chris Wilsonf899fc62010-07-20 15:44:45 -0700118 struct intel_gpio *gpio = data;
119 struct drm_i915_private *dev_priv = gpio->dev_priv;
Chris Wilsone957d772010-09-24 12:52:03 +0100120 u32 reserved = get_reserved(gpio);
121 u32 clock_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -0800122
123 if (state_high)
124 clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
125 else
126 clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
127 GPIO_CLOCK_VAL_MASK;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700128
Yuanhan Liudb5e4172010-11-08 09:58:16 +0000129 I915_WRITE_NOTRACE(gpio->reg, reserved | clock_bits);
Chris Wilson374c4792010-11-08 21:07:24 +0000130 POSTING_READ(gpio->reg);
Jesse Barnes79e53942008-11-07 14:24:08 -0800131}
132
133static void set_data(void *data, int state_high)
134{
Chris Wilsonf899fc62010-07-20 15:44:45 -0700135 struct intel_gpio *gpio = data;
136 struct drm_i915_private *dev_priv = gpio->dev_priv;
Chris Wilsone957d772010-09-24 12:52:03 +0100137 u32 reserved = get_reserved(gpio);
138 u32 data_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -0800139
140 if (state_high)
141 data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
142 else
143 data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
144 GPIO_DATA_VAL_MASK;
145
Yuanhan Liudb5e4172010-11-08 09:58:16 +0000146 I915_WRITE_NOTRACE(gpio->reg, reserved | data_bits);
Chris Wilson374c4792010-11-08 21:07:24 +0000147 POSTING_READ(gpio->reg);
Jesse Barnes79e53942008-11-07 14:24:08 -0800148}
149
Chris Wilsonf899fc62010-07-20 15:44:45 -0700150static struct i2c_adapter *
151intel_gpio_create(struct drm_i915_private *dev_priv, u32 pin)
Eric Anholtf0217c42009-12-01 11:56:30 -0800152{
Chris Wilsonf899fc62010-07-20 15:44:45 -0700153 static const int map_pin_to_reg[] = {
154 0,
155 GPIOB,
156 GPIOA,
157 GPIOC,
158 GPIOD,
159 GPIOE,
Zhenyu Wang7b5337d2010-10-13 16:40:12 +0800160 0,
Chris Wilsonf899fc62010-07-20 15:44:45 -0700161 GPIOF,
162 };
163 struct intel_gpio *gpio;
Eric Anholtf0217c42009-12-01 11:56:30 -0800164
Jean Delvare69669452010-11-05 18:51:34 +0100165 if (pin >= ARRAY_SIZE(map_pin_to_reg) || !map_pin_to_reg[pin])
Chris Wilsonf899fc62010-07-20 15:44:45 -0700166 return NULL;
Eric Anholtf0217c42009-12-01 11:56:30 -0800167
Chris Wilsonf899fc62010-07-20 15:44:45 -0700168 gpio = kzalloc(sizeof(struct intel_gpio), GFP_KERNEL);
169 if (gpio == NULL)
170 return NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -0800171
Chris Wilsonf899fc62010-07-20 15:44:45 -0700172 gpio->reg = map_pin_to_reg[pin];
173 if (HAS_PCH_SPLIT(dev_priv->dev))
174 gpio->reg += PCH_GPIOA - GPIOA;
175 gpio->dev_priv = dev_priv;
176
Jean Delvare69669452010-11-05 18:51:34 +0100177 snprintf(gpio->adapter.name, sizeof(gpio->adapter.name),
178 "i915 GPIO%c", "?BACDE?F"[pin]);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700179 gpio->adapter.owner = THIS_MODULE;
180 gpio->adapter.algo_data = &gpio->algo;
181 gpio->adapter.dev.parent = &dev_priv->dev->pdev->dev;
182 gpio->algo.setsda = set_data;
183 gpio->algo.setscl = set_clock;
184 gpio->algo.getsda = get_data;
185 gpio->algo.getscl = get_clock;
186 gpio->algo.udelay = I2C_RISEFALL_TIME;
187 gpio->algo.timeout = usecs_to_jiffies(2200);
188 gpio->algo.data = gpio;
189
190 if (i2c_bit_add_bus(&gpio->adapter))
Jesse Barnes79e53942008-11-07 14:24:08 -0800191 goto out_free;
192
Chris Wilsonf899fc62010-07-20 15:44:45 -0700193 return &gpio->adapter;
Jesse Barnes79e53942008-11-07 14:24:08 -0800194
195out_free:
Chris Wilsonf899fc62010-07-20 15:44:45 -0700196 kfree(gpio);
Jesse Barnes79e53942008-11-07 14:24:08 -0800197 return NULL;
198}
199
Chris Wilsonf899fc62010-07-20 15:44:45 -0700200static int
Chris Wilsone957d772010-09-24 12:52:03 +0100201intel_i2c_quirk_xfer(struct drm_i915_private *dev_priv,
202 struct i2c_adapter *adapter,
203 struct i2c_msg *msgs,
204 int num)
Jesse Barnes79e53942008-11-07 14:24:08 -0800205{
Chris Wilsone957d772010-09-24 12:52:03 +0100206 struct intel_gpio *gpio = container_of(adapter,
207 struct intel_gpio,
208 adapter);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700209 int ret;
Keith Packardf9c10a92009-05-30 12:16:25 -0700210
Chris Wilsonf899fc62010-07-20 15:44:45 -0700211 intel_i2c_reset(dev_priv->dev);
212
213 intel_i2c_quirk_set(dev_priv, true);
Chris Wilsone957d772010-09-24 12:52:03 +0100214 set_data(gpio, 1);
215 set_clock(gpio, 1);
216 udelay(I2C_RISEFALL_TIME);
217
218 ret = adapter->algo->master_xfer(adapter, msgs, num);
219
220 set_data(gpio, 1);
221 set_clock(gpio, 1);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700222 intel_i2c_quirk_set(dev_priv, false);
223
224 return ret;
225}
226
227static int
228gmbus_xfer(struct i2c_adapter *adapter,
229 struct i2c_msg *msgs,
230 int num)
231{
232 struct intel_gmbus *bus = container_of(adapter,
233 struct intel_gmbus,
234 adapter);
Daniel Vetterc2b91522012-02-14 22:37:19 +0100235 struct drm_i915_private *dev_priv = bus->dev_priv;
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500236 int i, reg_offset, ret;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700237
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500238 mutex_lock(&dev_priv->gmbus_mutex);
239
240 if (bus->force_bit) {
241 ret = intel_i2c_quirk_xfer(dev_priv,
Chris Wilsone957d772010-09-24 12:52:03 +0100242 bus->force_bit, msgs, num);
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500243 goto out;
244 }
Chris Wilsonf899fc62010-07-20 15:44:45 -0700245
246 reg_offset = HAS_PCH_SPLIT(dev_priv->dev) ? PCH_GMBUS0 - GMBUS0 : 0;
247
Chris Wilsone957d772010-09-24 12:52:03 +0100248 I915_WRITE(GMBUS0 + reg_offset, bus->reg0);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700249
250 for (i = 0; i < num; i++) {
251 u16 len = msgs[i].len;
252 u8 *buf = msgs[i].buf;
253
254 if (msgs[i].flags & I2C_M_RD) {
255 I915_WRITE(GMBUS1 + reg_offset,
Benson Leungcaae7452012-02-09 12:03:17 -0800256 GMBUS_CYCLE_WAIT |
257 (i + 1 == num ? GMBUS_CYCLE_STOP : 0) |
Chris Wilsonf899fc62010-07-20 15:44:45 -0700258 (len << GMBUS_BYTE_COUNT_SHIFT) |
259 (msgs[i].addr << GMBUS_SLAVE_ADDR_SHIFT) |
260 GMBUS_SLAVE_READ | GMBUS_SW_RDY);
Chris Wilsone957d772010-09-24 12:52:03 +0100261 POSTING_READ(GMBUS2+reg_offset);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700262 do {
263 u32 val, loop = 0;
264
265 if (wait_for(I915_READ(GMBUS2 + reg_offset) & (GMBUS_SATOER | GMBUS_HW_RDY), 50))
266 goto timeout;
267 if (I915_READ(GMBUS2 + reg_offset) & GMBUS_SATOER)
Chris Wilson7f58aab2011-03-30 16:20:43 +0100268 goto clear_err;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700269
270 val = I915_READ(GMBUS3 + reg_offset);
271 do {
272 *buf++ = val & 0xff;
273 val >>= 8;
274 } while (--len && ++loop < 4);
275 } while (len);
276 } else {
Chris Wilsone957d772010-09-24 12:52:03 +0100277 u32 val, loop;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700278
Chris Wilsone957d772010-09-24 12:52:03 +0100279 val = loop = 0;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700280 do {
Chris Wilsone957d772010-09-24 12:52:03 +0100281 val |= *buf++ << (8 * loop);
282 } while (--len && ++loop < 4);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700283
284 I915_WRITE(GMBUS3 + reg_offset, val);
285 I915_WRITE(GMBUS1 + reg_offset,
Benson Leungcaae7452012-02-09 12:03:17 -0800286 GMBUS_CYCLE_WAIT |
287 (i + 1 == num ? GMBUS_CYCLE_STOP : 0) |
Chris Wilsonf899fc62010-07-20 15:44:45 -0700288 (msgs[i].len << GMBUS_BYTE_COUNT_SHIFT) |
289 (msgs[i].addr << GMBUS_SLAVE_ADDR_SHIFT) |
290 GMBUS_SLAVE_WRITE | GMBUS_SW_RDY);
Chris Wilsone957d772010-09-24 12:52:03 +0100291 POSTING_READ(GMBUS2+reg_offset);
292
293 while (len) {
294 if (wait_for(I915_READ(GMBUS2 + reg_offset) & (GMBUS_SATOER | GMBUS_HW_RDY), 50))
295 goto timeout;
296 if (I915_READ(GMBUS2 + reg_offset) & GMBUS_SATOER)
Chris Wilson7f58aab2011-03-30 16:20:43 +0100297 goto clear_err;
Chris Wilsone957d772010-09-24 12:52:03 +0100298
299 val = loop = 0;
300 do {
301 val |= *buf++ << (8 * loop);
302 } while (--len && ++loop < 4);
303
304 I915_WRITE(GMBUS3 + reg_offset, val);
305 POSTING_READ(GMBUS2+reg_offset);
306 }
Chris Wilsonf899fc62010-07-20 15:44:45 -0700307 }
308
309 if (i + 1 < num && wait_for(I915_READ(GMBUS2 + reg_offset) & (GMBUS_SATOER | GMBUS_HW_WAIT_PHASE), 50))
310 goto timeout;
311 if (I915_READ(GMBUS2 + reg_offset) & GMBUS_SATOER)
Chris Wilson7f58aab2011-03-30 16:20:43 +0100312 goto clear_err;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700313 }
314
Chris Wilson7f58aab2011-03-30 16:20:43 +0100315 goto done;
316
317clear_err:
318 /* Toggle the Software Clear Interrupt bit. This has the effect
319 * of resetting the GMBUS controller and so clearing the
320 * BUS_ERROR raised by the slave's NAK.
321 */
322 I915_WRITE(GMBUS1 + reg_offset, GMBUS_SW_CLR_INT);
323 I915_WRITE(GMBUS1 + reg_offset, 0);
324
325done:
Benson Leungcaae7452012-02-09 12:03:17 -0800326 /* Mark the GMBUS interface as disabled after waiting for idle.
327 * We will re-enable it at the start of the next xfer,
328 * till then let it sleep.
Chris Wilson7f58aab2011-03-30 16:20:43 +0100329 */
Benson Leungcaae7452012-02-09 12:03:17 -0800330 if (wait_for((I915_READ(GMBUS2 + reg_offset) & GMBUS_ACTIVE) == 0, 10))
331 DRM_INFO("GMBUS timed out waiting for idle\n");
Chris Wilson7f58aab2011-03-30 16:20:43 +0100332 I915_WRITE(GMBUS0 + reg_offset, 0);
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500333 ret = i;
334 goto out;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700335
336timeout:
Chris Wilsone957d772010-09-24 12:52:03 +0100337 DRM_INFO("GMBUS timed out, falling back to bit banging on pin %d [%s]\n",
338 bus->reg0 & 0xff, bus->adapter.name);
Chris Wilson7f58aab2011-03-30 16:20:43 +0100339 I915_WRITE(GMBUS0 + reg_offset, 0);
340
Chris Wilsonf899fc62010-07-20 15:44:45 -0700341 /* Hardware may not support GMBUS over these pins? Try GPIO bitbanging instead. */
Chris Wilsone957d772010-09-24 12:52:03 +0100342 bus->force_bit = intel_gpio_create(dev_priv, bus->reg0 & 0xff);
343 if (!bus->force_bit)
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500344 ret = -ENOMEM;
345 else
346 ret = intel_i2c_quirk_xfer(dev_priv, bus->force_bit, msgs, num);
347out:
348 mutex_unlock(&dev_priv->gmbus_mutex);
349 return ret;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700350}
351
352static u32 gmbus_func(struct i2c_adapter *adapter)
353{
Chris Wilsone957d772010-09-24 12:52:03 +0100354 struct intel_gmbus *bus = container_of(adapter,
355 struct intel_gmbus,
356 adapter);
357
358 if (bus->force_bit)
359 bus->force_bit->algo->functionality(bus->force_bit);
360
Chris Wilsonf899fc62010-07-20 15:44:45 -0700361 return (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
362 /* I2C_FUNC_10BIT_ADDR | */
363 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
364 I2C_FUNC_SMBUS_BLOCK_PROC_CALL);
365}
366
367static const struct i2c_algorithm gmbus_algorithm = {
368 .master_xfer = gmbus_xfer,
369 .functionality = gmbus_func
370};
371
372/**
373 * intel_gmbus_setup - instantiate all Intel i2c GMBuses
374 * @dev: DRM device
375 */
376int intel_setup_gmbus(struct drm_device *dev)
377{
Chris Wilsone957d772010-09-24 12:52:03 +0100378 static const char *names[GMBUS_NUM_PORTS] = {
Chris Wilsonf899fc62010-07-20 15:44:45 -0700379 "disabled",
380 "ssc",
381 "vga",
382 "panel",
383 "dpc",
384 "dpb",
Jean Delvare69669452010-11-05 18:51:34 +0100385 "reserved",
Chris Wilsone957d772010-09-24 12:52:03 +0100386 "dpd",
Chris Wilsonf899fc62010-07-20 15:44:45 -0700387 };
388 struct drm_i915_private *dev_priv = dev->dev_private;
389 int ret, i;
390
Axel Lin51a59ac2012-02-10 20:04:52 +0800391 dev_priv->gmbus = kcalloc(GMBUS_NUM_PORTS, sizeof(struct intel_gmbus),
Chris Wilsonf899fc62010-07-20 15:44:45 -0700392 GFP_KERNEL);
393 if (dev_priv->gmbus == NULL)
394 return -ENOMEM;
395
Yufeng Shen8a8ed1f2012-02-13 17:36:54 -0500396 mutex_init(&dev_priv->gmbus_mutex);
397
Chris Wilsonf899fc62010-07-20 15:44:45 -0700398 for (i = 0; i < GMBUS_NUM_PORTS; i++) {
399 struct intel_gmbus *bus = &dev_priv->gmbus[i];
400
401 bus->adapter.owner = THIS_MODULE;
402 bus->adapter.class = I2C_CLASS_DDC;
403 snprintf(bus->adapter.name,
Jean Delvare69669452010-11-05 18:51:34 +0100404 sizeof(bus->adapter.name),
405 "i915 gmbus %s",
Chris Wilsonf899fc62010-07-20 15:44:45 -0700406 names[i]);
407
408 bus->adapter.dev.parent = &dev->pdev->dev;
Daniel Vetterc2b91522012-02-14 22:37:19 +0100409 bus->dev_priv = dev_priv;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700410
411 bus->adapter.algo = &gmbus_algorithm;
412 ret = i2c_add_adapter(&bus->adapter);
413 if (ret)
414 goto err;
415
Chris Wilsone957d772010-09-24 12:52:03 +0100416 /* By default use a conservative clock rate */
417 bus->reg0 = i | GMBUS_RATE_100KHZ;
Chris Wilsoncb8ea752010-09-28 13:35:47 +0100418
419 /* XXX force bit banging until GMBUS is fully debugged */
Jean Delvare826c7e42011-06-04 19:34:56 +0000420 bus->force_bit = intel_gpio_create(dev_priv, i);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700421 }
422
423 intel_i2c_reset(dev_priv->dev);
424
425 return 0;
426
427err:
428 while (--i) {
429 struct intel_gmbus *bus = &dev_priv->gmbus[i];
430 i2c_del_adapter(&bus->adapter);
431 }
432 kfree(dev_priv->gmbus);
433 dev_priv->gmbus = NULL;
434 return ret;
435}
436
Chris Wilsone957d772010-09-24 12:52:03 +0100437void intel_gmbus_set_speed(struct i2c_adapter *adapter, int speed)
438{
439 struct intel_gmbus *bus = to_intel_gmbus(adapter);
440
Adam Jacksond5090b92011-06-16 16:36:28 -0400441 bus->reg0 = (bus->reg0 & ~(0x3 << 8)) | speed;
Chris Wilsone957d772010-09-24 12:52:03 +0100442}
443
444void intel_gmbus_force_bit(struct i2c_adapter *adapter, bool force_bit)
445{
446 struct intel_gmbus *bus = to_intel_gmbus(adapter);
447
448 if (force_bit) {
449 if (bus->force_bit == NULL) {
Daniel Vetterc2b91522012-02-14 22:37:19 +0100450 struct drm_i915_private *dev_priv = bus->dev_priv;
Chris Wilsone957d772010-09-24 12:52:03 +0100451 bus->force_bit = intel_gpio_create(dev_priv,
452 bus->reg0 & 0xff);
453 }
454 } else {
455 if (bus->force_bit) {
456 i2c_del_adapter(bus->force_bit);
457 kfree(bus->force_bit);
458 bus->force_bit = NULL;
459 }
460 }
461}
462
Chris Wilsonf899fc62010-07-20 15:44:45 -0700463void intel_teardown_gmbus(struct drm_device *dev)
464{
465 struct drm_i915_private *dev_priv = dev->dev_private;
466 int i;
467
468 if (dev_priv->gmbus == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -0800469 return;
470
Chris Wilsonf899fc62010-07-20 15:44:45 -0700471 for (i = 0; i < GMBUS_NUM_PORTS; i++) {
472 struct intel_gmbus *bus = &dev_priv->gmbus[i];
Chris Wilsone957d772010-09-24 12:52:03 +0100473 if (bus->force_bit) {
474 i2c_del_adapter(bus->force_bit);
475 kfree(bus->force_bit);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700476 }
477 i2c_del_adapter(&bus->adapter);
478 }
479
480 kfree(dev_priv->gmbus);
481 dev_priv->gmbus = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -0800482}