blob: 91920247d4ff19325785c963adfd2de2ce5e226b [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>
31#include "drmP.h"
32#include "drm.h"
33#include "intel_drv.h"
34#include "i915_drm.h"
35#include "i915_drv.h"
36
Chris Wilsonf899fc62010-07-20 15:44:45 -070037/* Intel GPIO access functions */
38
39#define I2C_RISEFALL_TIME 20
40
Chris Wilsone957d772010-09-24 12:52:03 +010041static inline struct intel_gmbus *
42to_intel_gmbus(struct i2c_adapter *i2c)
43{
44 return container_of(i2c, struct intel_gmbus, adapter);
45}
46
Chris Wilsonf899fc62010-07-20 15:44:45 -070047struct intel_gpio {
48 struct i2c_adapter adapter;
49 struct i2c_algo_bit_data algo;
50 struct drm_i915_private *dev_priv;
51 u32 reg;
52};
53
54void
55intel_i2c_reset(struct drm_device *dev)
Shaohua Li0ba0e9e2009-04-07 11:02:28 +080056{
57 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonf899fc62010-07-20 15:44:45 -070058 if (HAS_PCH_SPLIT(dev))
59 I915_WRITE(PCH_GMBUS0, 0);
60 else
61 I915_WRITE(GMBUS0, 0);
62}
63
64static void intel_i2c_quirk_set(struct drm_i915_private *dev_priv, bool enable)
65{
Chris Wilsonb222f262010-09-11 21:48:25 +010066 u32 val;
Shaohua Li0ba0e9e2009-04-07 11:02:28 +080067
68 /* When using bit bashing for I2C, this bit needs to be set to 1 */
Chris Wilsonf899fc62010-07-20 15:44:45 -070069 if (!IS_PINEVIEW(dev_priv->dev))
Shaohua Li0ba0e9e2009-04-07 11:02:28 +080070 return;
Chris Wilsonb222f262010-09-11 21:48:25 +010071
72 val = I915_READ(DSPCLK_GATE_D);
Shaohua Li0ba0e9e2009-04-07 11:02:28 +080073 if (enable)
Chris Wilsonb222f262010-09-11 21:48:25 +010074 val |= DPCUNIT_CLOCK_GATE_DISABLE;
Shaohua Li0ba0e9e2009-04-07 11:02:28 +080075 else
Chris Wilsonb222f262010-09-11 21:48:25 +010076 val &= ~DPCUNIT_CLOCK_GATE_DISABLE;
77 I915_WRITE(DSPCLK_GATE_D, val);
Shaohua Li0ba0e9e2009-04-07 11:02:28 +080078}
79
Chris Wilsone957d772010-09-24 12:52:03 +010080static u32 get_reserved(struct intel_gpio *gpio)
81{
82 struct drm_i915_private *dev_priv = gpio->dev_priv;
83 struct drm_device *dev = dev_priv->dev;
84 u32 reserved = 0;
85
86 /* On most chips, these bits must be preserved in software. */
87 if (!IS_I830(dev) && !IS_845G(dev))
88 reserved = I915_READ(gpio->reg) & (GPIO_DATA_PULLUP_DISABLE |
89 GPIO_CLOCK_PULLUP_DISABLE);
90
91 return reserved;
92}
93
Jesse Barnes79e53942008-11-07 14:24:08 -080094static int get_clock(void *data)
95{
Chris Wilsonf899fc62010-07-20 15:44:45 -070096 struct intel_gpio *gpio = data;
97 struct drm_i915_private *dev_priv = gpio->dev_priv;
Chris Wilsone957d772010-09-24 12:52:03 +010098 u32 reserved = get_reserved(gpio);
99 I915_WRITE(gpio->reg, reserved | GPIO_CLOCK_DIR_MASK);
100 I915_WRITE(gpio->reg, reserved);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700101 return (I915_READ(gpio->reg) & GPIO_CLOCK_VAL_IN) != 0;
Jesse Barnes79e53942008-11-07 14:24:08 -0800102}
103
104static int get_data(void *data)
105{
Chris Wilsonf899fc62010-07-20 15:44:45 -0700106 struct intel_gpio *gpio = data;
107 struct drm_i915_private *dev_priv = gpio->dev_priv;
Chris Wilsone957d772010-09-24 12:52:03 +0100108 u32 reserved = get_reserved(gpio);
109 I915_WRITE(gpio->reg, reserved | GPIO_DATA_DIR_MASK);
110 I915_WRITE(gpio->reg, reserved);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700111 return (I915_READ(gpio->reg) & GPIO_DATA_VAL_IN) != 0;
Jesse Barnes79e53942008-11-07 14:24:08 -0800112}
113
114static void set_clock(void *data, int state_high)
115{
Chris Wilsonf899fc62010-07-20 15:44:45 -0700116 struct intel_gpio *gpio = data;
117 struct drm_i915_private *dev_priv = gpio->dev_priv;
Chris Wilsone957d772010-09-24 12:52:03 +0100118 u32 reserved = get_reserved(gpio);
119 u32 clock_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -0800120
121 if (state_high)
122 clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
123 else
124 clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
125 GPIO_CLOCK_VAL_MASK;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700126
127 I915_WRITE(gpio->reg, reserved | clock_bits);
128 POSTING_READ(gpio->reg);
Jesse Barnes79e53942008-11-07 14:24:08 -0800129}
130
131static void set_data(void *data, int state_high)
132{
Chris Wilsonf899fc62010-07-20 15:44:45 -0700133 struct intel_gpio *gpio = data;
134 struct drm_i915_private *dev_priv = gpio->dev_priv;
Chris Wilsone957d772010-09-24 12:52:03 +0100135 u32 reserved = get_reserved(gpio);
136 u32 data_bits;
Jesse Barnes79e53942008-11-07 14:24:08 -0800137
138 if (state_high)
139 data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
140 else
141 data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
142 GPIO_DATA_VAL_MASK;
143
Chris Wilsonf899fc62010-07-20 15:44:45 -0700144 I915_WRITE(gpio->reg, reserved | data_bits);
145 POSTING_READ(gpio->reg);
Jesse Barnes79e53942008-11-07 14:24:08 -0800146}
147
Chris Wilsonf899fc62010-07-20 15:44:45 -0700148static struct i2c_adapter *
149intel_gpio_create(struct drm_i915_private *dev_priv, u32 pin)
Eric Anholtf0217c42009-12-01 11:56:30 -0800150{
Chris Wilsonf899fc62010-07-20 15:44:45 -0700151 static const int map_pin_to_reg[] = {
152 0,
153 GPIOB,
154 GPIOA,
155 GPIOC,
156 GPIOD,
157 GPIOE,
158 GPIOF,
159 };
160 struct intel_gpio *gpio;
Eric Anholtf0217c42009-12-01 11:56:30 -0800161
Chris Wilsonf899fc62010-07-20 15:44:45 -0700162 if (pin < 1 || pin > 7)
163 return NULL;
Eric Anholtf0217c42009-12-01 11:56:30 -0800164
Chris Wilsonf899fc62010-07-20 15:44:45 -0700165 gpio = kzalloc(sizeof(struct intel_gpio), GFP_KERNEL);
166 if (gpio == NULL)
167 return NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -0800168
Chris Wilsonf899fc62010-07-20 15:44:45 -0700169 gpio->reg = map_pin_to_reg[pin];
170 if (HAS_PCH_SPLIT(dev_priv->dev))
171 gpio->reg += PCH_GPIOA - GPIOA;
172 gpio->dev_priv = dev_priv;
173
Chris Wilsone957d772010-09-24 12:52:03 +0100174 snprintf(gpio->adapter.name, I2C_NAME_SIZE, "GPIO%c", "?BACDEF?"[pin]);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700175 gpio->adapter.owner = THIS_MODULE;
176 gpio->adapter.algo_data = &gpio->algo;
177 gpio->adapter.dev.parent = &dev_priv->dev->pdev->dev;
178 gpio->algo.setsda = set_data;
179 gpio->algo.setscl = set_clock;
180 gpio->algo.getsda = get_data;
181 gpio->algo.getscl = get_clock;
182 gpio->algo.udelay = I2C_RISEFALL_TIME;
183 gpio->algo.timeout = usecs_to_jiffies(2200);
184 gpio->algo.data = gpio;
185
186 if (i2c_bit_add_bus(&gpio->adapter))
Jesse Barnes79e53942008-11-07 14:24:08 -0800187 goto out_free;
188
Chris Wilsonf899fc62010-07-20 15:44:45 -0700189 return &gpio->adapter;
Jesse Barnes79e53942008-11-07 14:24:08 -0800190
191out_free:
Chris Wilsonf899fc62010-07-20 15:44:45 -0700192 kfree(gpio);
Jesse Barnes79e53942008-11-07 14:24:08 -0800193 return NULL;
194}
195
Chris Wilsonf899fc62010-07-20 15:44:45 -0700196static int
Chris Wilsone957d772010-09-24 12:52:03 +0100197intel_i2c_quirk_xfer(struct drm_i915_private *dev_priv,
198 struct i2c_adapter *adapter,
199 struct i2c_msg *msgs,
200 int num)
Jesse Barnes79e53942008-11-07 14:24:08 -0800201{
Chris Wilsone957d772010-09-24 12:52:03 +0100202 struct intel_gpio *gpio = container_of(adapter,
203 struct intel_gpio,
204 adapter);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700205 int ret;
Keith Packardf9c10a92009-05-30 12:16:25 -0700206
Chris Wilsonf899fc62010-07-20 15:44:45 -0700207 intel_i2c_reset(dev_priv->dev);
208
209 intel_i2c_quirk_set(dev_priv, true);
Chris Wilsone957d772010-09-24 12:52:03 +0100210 set_data(gpio, 1);
211 set_clock(gpio, 1);
212 udelay(I2C_RISEFALL_TIME);
213
214 ret = adapter->algo->master_xfer(adapter, msgs, num);
215
216 set_data(gpio, 1);
217 set_clock(gpio, 1);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700218 intel_i2c_quirk_set(dev_priv, false);
219
220 return ret;
221}
222
223static int
224gmbus_xfer(struct i2c_adapter *adapter,
225 struct i2c_msg *msgs,
226 int num)
227{
228 struct intel_gmbus *bus = container_of(adapter,
229 struct intel_gmbus,
230 adapter);
231 struct drm_i915_private *dev_priv = adapter->algo_data;
Chris Wilsone957d772010-09-24 12:52:03 +0100232 int i, reg_offset;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700233
Chris Wilsone957d772010-09-24 12:52:03 +0100234 if (bus->force_bit)
235 return intel_i2c_quirk_xfer(dev_priv,
236 bus->force_bit, msgs, num);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700237
238 reg_offset = HAS_PCH_SPLIT(dev_priv->dev) ? PCH_GMBUS0 - GMBUS0 : 0;
239
Chris Wilsone957d772010-09-24 12:52:03 +0100240 I915_WRITE(GMBUS0 + reg_offset, bus->reg0);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700241
242 for (i = 0; i < num; i++) {
243 u16 len = msgs[i].len;
244 u8 *buf = msgs[i].buf;
245
246 if (msgs[i].flags & I2C_M_RD) {
247 I915_WRITE(GMBUS1 + reg_offset,
248 GMBUS_CYCLE_WAIT | (i + 1 == num ? GMBUS_CYCLE_STOP : 0) |
249 (len << GMBUS_BYTE_COUNT_SHIFT) |
250 (msgs[i].addr << GMBUS_SLAVE_ADDR_SHIFT) |
251 GMBUS_SLAVE_READ | GMBUS_SW_RDY);
Chris Wilsone957d772010-09-24 12:52:03 +0100252 POSTING_READ(GMBUS2+reg_offset);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700253 do {
254 u32 val, loop = 0;
255
256 if (wait_for(I915_READ(GMBUS2 + reg_offset) & (GMBUS_SATOER | GMBUS_HW_RDY), 50))
257 goto timeout;
258 if (I915_READ(GMBUS2 + reg_offset) & GMBUS_SATOER)
259 return 0;
260
261 val = I915_READ(GMBUS3 + reg_offset);
262 do {
263 *buf++ = val & 0xff;
264 val >>= 8;
265 } while (--len && ++loop < 4);
266 } while (len);
267 } else {
Chris Wilsone957d772010-09-24 12:52:03 +0100268 u32 val, loop;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700269
Chris Wilsone957d772010-09-24 12:52:03 +0100270 val = loop = 0;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700271 do {
Chris Wilsone957d772010-09-24 12:52:03 +0100272 val |= *buf++ << (8 * loop);
273 } while (--len && ++loop < 4);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700274
275 I915_WRITE(GMBUS3 + reg_offset, val);
276 I915_WRITE(GMBUS1 + reg_offset,
Chris Wilsone957d772010-09-24 12:52:03 +0100277 (i + 1 == num ? GMBUS_CYCLE_STOP : GMBUS_CYCLE_WAIT) |
Chris Wilsonf899fc62010-07-20 15:44:45 -0700278 (msgs[i].len << GMBUS_BYTE_COUNT_SHIFT) |
279 (msgs[i].addr << GMBUS_SLAVE_ADDR_SHIFT) |
280 GMBUS_SLAVE_WRITE | GMBUS_SW_RDY);
Chris Wilsone957d772010-09-24 12:52:03 +0100281 POSTING_READ(GMBUS2+reg_offset);
282
283 while (len) {
284 if (wait_for(I915_READ(GMBUS2 + reg_offset) & (GMBUS_SATOER | GMBUS_HW_RDY), 50))
285 goto timeout;
286 if (I915_READ(GMBUS2 + reg_offset) & GMBUS_SATOER)
287 return 0;
288
289 val = loop = 0;
290 do {
291 val |= *buf++ << (8 * loop);
292 } while (--len && ++loop < 4);
293
294 I915_WRITE(GMBUS3 + reg_offset, val);
295 POSTING_READ(GMBUS2+reg_offset);
296 }
Chris Wilsonf899fc62010-07-20 15:44:45 -0700297 }
298
299 if (i + 1 < num && wait_for(I915_READ(GMBUS2 + reg_offset) & (GMBUS_SATOER | GMBUS_HW_WAIT_PHASE), 50))
300 goto timeout;
301 if (I915_READ(GMBUS2 + reg_offset) & GMBUS_SATOER)
302 return 0;
303 }
304
305 return num;
306
307timeout:
Chris Wilsone957d772010-09-24 12:52:03 +0100308 DRM_INFO("GMBUS timed out, falling back to bit banging on pin %d [%s]\n",
309 bus->reg0 & 0xff, bus->adapter.name);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700310 /* Hardware may not support GMBUS over these pins? Try GPIO bitbanging instead. */
Chris Wilsone957d772010-09-24 12:52:03 +0100311 bus->force_bit = intel_gpio_create(dev_priv, bus->reg0 & 0xff);
312 if (!bus->force_bit)
Chris Wilsonf899fc62010-07-20 15:44:45 -0700313 return -ENOMEM;
314
Chris Wilsone957d772010-09-24 12:52:03 +0100315 return intel_i2c_quirk_xfer(dev_priv, bus->force_bit, msgs, num);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700316}
317
318static u32 gmbus_func(struct i2c_adapter *adapter)
319{
Chris Wilsone957d772010-09-24 12:52:03 +0100320 struct intel_gmbus *bus = container_of(adapter,
321 struct intel_gmbus,
322 adapter);
323
324 if (bus->force_bit)
325 bus->force_bit->algo->functionality(bus->force_bit);
326
Chris Wilsonf899fc62010-07-20 15:44:45 -0700327 return (I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL |
328 /* I2C_FUNC_10BIT_ADDR | */
329 I2C_FUNC_SMBUS_READ_BLOCK_DATA |
330 I2C_FUNC_SMBUS_BLOCK_PROC_CALL);
331}
332
333static const struct i2c_algorithm gmbus_algorithm = {
334 .master_xfer = gmbus_xfer,
335 .functionality = gmbus_func
336};
337
338/**
339 * intel_gmbus_setup - instantiate all Intel i2c GMBuses
340 * @dev: DRM device
341 */
342int intel_setup_gmbus(struct drm_device *dev)
343{
Chris Wilsone957d772010-09-24 12:52:03 +0100344 static const char *names[GMBUS_NUM_PORTS] = {
Chris Wilsonf899fc62010-07-20 15:44:45 -0700345 "disabled",
346 "ssc",
347 "vga",
348 "panel",
349 "dpc",
350 "dpb",
Chris Wilsonf899fc62010-07-20 15:44:45 -0700351 "reserved"
Chris Wilsone957d772010-09-24 12:52:03 +0100352 "dpd",
Chris Wilsonf899fc62010-07-20 15:44:45 -0700353 };
354 struct drm_i915_private *dev_priv = dev->dev_private;
355 int ret, i;
356
357 dev_priv->gmbus = kcalloc(sizeof(struct intel_gmbus), GMBUS_NUM_PORTS,
358 GFP_KERNEL);
359 if (dev_priv->gmbus == NULL)
360 return -ENOMEM;
361
362 for (i = 0; i < GMBUS_NUM_PORTS; i++) {
363 struct intel_gmbus *bus = &dev_priv->gmbus[i];
364
365 bus->adapter.owner = THIS_MODULE;
366 bus->adapter.class = I2C_CLASS_DDC;
367 snprintf(bus->adapter.name,
368 I2C_NAME_SIZE,
369 "gmbus %s",
370 names[i]);
371
372 bus->adapter.dev.parent = &dev->pdev->dev;
373 bus->adapter.algo_data = dev_priv;
374
375 bus->adapter.algo = &gmbus_algorithm;
376 ret = i2c_add_adapter(&bus->adapter);
377 if (ret)
378 goto err;
379
Chris Wilsone957d772010-09-24 12:52:03 +0100380 /* By default use a conservative clock rate */
381 bus->reg0 = i | GMBUS_RATE_100KHZ;
Chris Wilsonf899fc62010-07-20 15:44:45 -0700382 }
383
384 intel_i2c_reset(dev_priv->dev);
385
386 return 0;
387
388err:
389 while (--i) {
390 struct intel_gmbus *bus = &dev_priv->gmbus[i];
391 i2c_del_adapter(&bus->adapter);
392 }
393 kfree(dev_priv->gmbus);
394 dev_priv->gmbus = NULL;
395 return ret;
396}
397
Chris Wilsone957d772010-09-24 12:52:03 +0100398void intel_gmbus_set_speed(struct i2c_adapter *adapter, int speed)
399{
400 struct intel_gmbus *bus = to_intel_gmbus(adapter);
401
402 /* speed:
403 * 0x0 = 100 KHz
404 * 0x1 = 50 KHz
405 * 0x2 = 400 KHz
406 * 0x3 = 1000 Khz
407 */
408 bus->reg0 = (bus->reg0 & ~(0x3 << 8)) | (speed << 8);
409}
410
411void intel_gmbus_force_bit(struct i2c_adapter *adapter, bool force_bit)
412{
413 struct intel_gmbus *bus = to_intel_gmbus(adapter);
414
415 if (force_bit) {
416 if (bus->force_bit == NULL) {
417 struct drm_i915_private *dev_priv = adapter->algo_data;
418 bus->force_bit = intel_gpio_create(dev_priv,
419 bus->reg0 & 0xff);
420 }
421 } else {
422 if (bus->force_bit) {
423 i2c_del_adapter(bus->force_bit);
424 kfree(bus->force_bit);
425 bus->force_bit = NULL;
426 }
427 }
428}
429
Chris Wilsonf899fc62010-07-20 15:44:45 -0700430void intel_teardown_gmbus(struct drm_device *dev)
431{
432 struct drm_i915_private *dev_priv = dev->dev_private;
433 int i;
434
435 if (dev_priv->gmbus == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -0800436 return;
437
Chris Wilsonf899fc62010-07-20 15:44:45 -0700438 for (i = 0; i < GMBUS_NUM_PORTS; i++) {
439 struct intel_gmbus *bus = &dev_priv->gmbus[i];
Chris Wilsone957d772010-09-24 12:52:03 +0100440 if (bus->force_bit) {
441 i2c_del_adapter(bus->force_bit);
442 kfree(bus->force_bit);
Chris Wilsonf899fc62010-07-20 15:44:45 -0700443 }
444 i2c_del_adapter(&bus->adapter);
445 }
446
447 kfree(dev_priv->gmbus);
448 dev_priv->gmbus = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -0800449}