blob: fcc753ca5d9446434e026197ba8318cbcfbda9b8 [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright (c) 2006 Dave Airlie <airlied@linux.ie>
3 * Copyright © 2006-2008 Intel Corporation
4 * 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>
27 */
28#include <linux/i2c.h>
29#include <linux/i2c-id.h>
30#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
Shaohua Li0ba0e9e2009-04-07 11:02:28 +080037void intel_i2c_quirk_set(struct drm_device *dev, bool enable)
38{
39 struct drm_i915_private *dev_priv = dev->dev_private;
40
41 /* When using bit bashing for I2C, this bit needs to be set to 1 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -050042 if (!IS_PINEVIEW(dev))
Shaohua Li0ba0e9e2009-04-07 11:02:28 +080043 return;
44 if (enable)
Jesse Barnes652c3932009-08-17 13:31:43 -070045 I915_WRITE(DSPCLK_GATE_D,
46 I915_READ(DSPCLK_GATE_D) | DPCUNIT_CLOCK_GATE_DISABLE);
Shaohua Li0ba0e9e2009-04-07 11:02:28 +080047 else
Jesse Barnes652c3932009-08-17 13:31:43 -070048 I915_WRITE(DSPCLK_GATE_D,
49 I915_READ(DSPCLK_GATE_D) & (~DPCUNIT_CLOCK_GATE_DISABLE));
Shaohua Li0ba0e9e2009-04-07 11:02:28 +080050}
51
Jesse Barnes79e53942008-11-07 14:24:08 -080052/*
53 * Intel GPIO access functions
54 */
55
56#define I2C_RISEFALL_TIME 20
57
58static int get_clock(void *data)
59{
60 struct intel_i2c_chan *chan = data;
61 struct drm_i915_private *dev_priv = chan->drm_dev->dev_private;
62 u32 val;
63
64 val = I915_READ(chan->reg);
65 return ((val & GPIO_CLOCK_VAL_IN) != 0);
66}
67
68static int get_data(void *data)
69{
70 struct intel_i2c_chan *chan = data;
71 struct drm_i915_private *dev_priv = chan->drm_dev->dev_private;
72 u32 val;
73
74 val = I915_READ(chan->reg);
75 return ((val & GPIO_DATA_VAL_IN) != 0);
76}
77
78static void set_clock(void *data, int state_high)
79{
80 struct intel_i2c_chan *chan = data;
81 struct drm_device *dev = chan->drm_dev;
82 struct drm_i915_private *dev_priv = chan->drm_dev->dev_private;
83 u32 reserved = 0, clock_bits;
84
85 /* On most chips, these bits must be preserved in software. */
86 if (!IS_I830(dev) && !IS_845G(dev))
87 reserved = I915_READ(chan->reg) & (GPIO_DATA_PULLUP_DISABLE |
88 GPIO_CLOCK_PULLUP_DISABLE);
89
90 if (state_high)
91 clock_bits = GPIO_CLOCK_DIR_IN | GPIO_CLOCK_DIR_MASK;
92 else
93 clock_bits = GPIO_CLOCK_DIR_OUT | GPIO_CLOCK_DIR_MASK |
94 GPIO_CLOCK_VAL_MASK;
95 I915_WRITE(chan->reg, reserved | clock_bits);
96 udelay(I2C_RISEFALL_TIME); /* wait for the line to change state */
97}
98
99static void set_data(void *data, int state_high)
100{
101 struct intel_i2c_chan *chan = data;
102 struct drm_device *dev = chan->drm_dev;
103 struct drm_i915_private *dev_priv = chan->drm_dev->dev_private;
104 u32 reserved = 0, data_bits;
105
106 /* On most chips, these bits must be preserved in software. */
107 if (!IS_I830(dev) && !IS_845G(dev))
108 reserved = I915_READ(chan->reg) & (GPIO_DATA_PULLUP_DISABLE |
109 GPIO_CLOCK_PULLUP_DISABLE);
110
111 if (state_high)
112 data_bits = GPIO_DATA_DIR_IN | GPIO_DATA_DIR_MASK;
113 else
114 data_bits = GPIO_DATA_DIR_OUT | GPIO_DATA_DIR_MASK |
115 GPIO_DATA_VAL_MASK;
116
117 I915_WRITE(chan->reg, reserved | data_bits);
118 udelay(I2C_RISEFALL_TIME); /* wait for the line to change state */
119}
120
Eric Anholtf0217c42009-12-01 11:56:30 -0800121/* Clears the GMBUS setup. Our driver doesn't make use of the GMBUS I2C
122 * engine, but if the BIOS leaves it enabled, then that can break our use
123 * of the bit-banging I2C interfaces. This is notably the case with the
124 * Mac Mini in EFI mode.
125 */
126void
127intel_i2c_reset_gmbus(struct drm_device *dev)
128{
129 struct drm_i915_private *dev_priv = dev->dev_private;
130
Eric Anholtc619eed2010-01-28 16:45:52 -0800131 if (HAS_PCH_SPLIT(dev)) {
Eric Anholtf0217c42009-12-01 11:56:30 -0800132 I915_WRITE(PCH_GMBUS0, 0);
133 } else {
134 I915_WRITE(GMBUS0, 0);
135 }
136}
137
Jesse Barnes79e53942008-11-07 14:24:08 -0800138/**
139 * intel_i2c_create - instantiate an Intel i2c bus using the specified GPIO reg
140 * @dev: DRM device
141 * @output: driver specific output device
142 * @reg: GPIO reg to use
143 * @name: name for this bus
Keith Packardf9c10a92009-05-30 12:16:25 -0700144 * @slave_addr: slave address (if fixed)
Jesse Barnes79e53942008-11-07 14:24:08 -0800145 *
146 * Creates and registers a new i2c bus with the Linux i2c layer, for use
147 * in output probing and control (e.g. DDC or SDVO control functions).
148 *
149 * Possible values for @reg include:
150 * %GPIOA
151 * %GPIOB
152 * %GPIOC
153 * %GPIOD
154 * %GPIOE
155 * %GPIOF
156 * %GPIOG
157 * %GPIOH
158 * see PRM for details on how these different busses are used.
159 */
Keith Packardf9c10a92009-05-30 12:16:25 -0700160struct i2c_adapter *intel_i2c_create(struct drm_device *dev, const u32 reg,
161 const char *name)
Jesse Barnes79e53942008-11-07 14:24:08 -0800162{
163 struct intel_i2c_chan *chan;
164
165 chan = kzalloc(sizeof(struct intel_i2c_chan), GFP_KERNEL);
166 if (!chan)
167 goto out_free;
168
169 chan->drm_dev = dev;
170 chan->reg = reg;
171 snprintf(chan->adapter.name, I2C_NAME_SIZE, "intel drm %s", name);
172 chan->adapter.owner = THIS_MODULE;
Jesse Barnes79e53942008-11-07 14:24:08 -0800173 chan->adapter.algo_data = &chan->algo;
174 chan->adapter.dev.parent = &dev->pdev->dev;
175 chan->algo.setsda = set_data;
176 chan->algo.setscl = set_clock;
177 chan->algo.getsda = get_data;
178 chan->algo.getscl = get_clock;
179 chan->algo.udelay = 20;
180 chan->algo.timeout = usecs_to_jiffies(2200);
181 chan->algo.data = chan;
182
183 i2c_set_adapdata(&chan->adapter, chan);
184
185 if(i2c_bit_add_bus(&chan->adapter))
186 goto out_free;
187
Eric Anholtf0217c42009-12-01 11:56:30 -0800188 intel_i2c_reset_gmbus(dev);
189
Jesse Barnes79e53942008-11-07 14:24:08 -0800190 /* JJJ: raise SCL and SDA? */
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800191 intel_i2c_quirk_set(dev, true);
Jesse Barnes79e53942008-11-07 14:24:08 -0800192 set_data(chan, 1);
193 set_clock(chan, 1);
Shaohua Li0ba0e9e2009-04-07 11:02:28 +0800194 intel_i2c_quirk_set(dev, false);
Jesse Barnes79e53942008-11-07 14:24:08 -0800195 udelay(20);
196
Keith Packardf9c10a92009-05-30 12:16:25 -0700197 return &chan->adapter;
Jesse Barnes79e53942008-11-07 14:24:08 -0800198
199out_free:
200 kfree(chan);
201 return NULL;
202}
203
204/**
205 * intel_i2c_destroy - unregister and free i2c bus resources
206 * @output: channel to free
207 *
208 * Unregister the adapter from the i2c layer, then free the structure.
209 */
Keith Packardf9c10a92009-05-30 12:16:25 -0700210void intel_i2c_destroy(struct i2c_adapter *adapter)
Jesse Barnes79e53942008-11-07 14:24:08 -0800211{
Keith Packardf9c10a92009-05-30 12:16:25 -0700212 struct intel_i2c_chan *chan;
213
214 if (!adapter)
Jesse Barnes79e53942008-11-07 14:24:08 -0800215 return;
216
Keith Packardf9c10a92009-05-30 12:16:25 -0700217 chan = container_of(adapter,
218 struct intel_i2c_chan,
219 adapter);
Jesse Barnes79e53942008-11-07 14:24:08 -0800220 i2c_del_adapter(&chan->adapter);
221 kfree(chan);
222}