Linux 2.6.33-rc6
[cris-mirror.git] / drivers / gpu / drm / radeon / radeon_i2c.c
blobda3da1e89d002eff60cbd5d7df5ea84e94648a94
1 /*
2 * Copyright 2007-8 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
23 * Authors: Dave Airlie
24 * Alex Deucher
26 #include "drmP.h"
27 #include "radeon_drm.h"
28 #include "radeon.h"
30 /**
31 * radeon_ddc_probe
34 bool radeon_ddc_probe(struct radeon_connector *radeon_connector)
36 u8 out_buf[] = { 0x0, 0x0};
37 u8 buf[2];
38 int ret;
39 struct i2c_msg msgs[] = {
41 .addr = 0x50,
42 .flags = 0,
43 .len = 1,
44 .buf = out_buf,
47 .addr = 0x50,
48 .flags = I2C_M_RD,
49 .len = 1,
50 .buf = buf,
54 ret = i2c_transfer(&radeon_connector->ddc_bus->adapter, msgs, 2);
55 if (ret == 2)
56 return true;
58 return false;
62 void radeon_i2c_do_lock(struct radeon_i2c_chan *i2c, int lock_state)
64 struct radeon_device *rdev = i2c->dev->dev_private;
65 struct radeon_i2c_bus_rec *rec = &i2c->rec;
66 uint32_t temp;
68 /* RV410 appears to have a bug where the hw i2c in reset
69 * holds the i2c port in a bad state - switch hw i2c away before
70 * doing DDC - do this for all r200s/r300s/r400s for safety sake
72 if (rec->hw_capable) {
73 if ((rdev->family >= CHIP_R200) && !ASIC_IS_AVIVO(rdev)) {
74 if (rec->a_clk_reg == RADEON_GPIO_MONID) {
75 WREG32(RADEON_DVI_I2C_CNTL_0, (RADEON_I2C_SOFT_RST |
76 R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1)));
77 } else {
78 WREG32(RADEON_DVI_I2C_CNTL_0, (RADEON_I2C_SOFT_RST |
79 R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3)));
84 /* clear the output pin values */
85 temp = RREG32(rec->a_clk_reg) & ~rec->a_clk_mask;
86 WREG32(rec->a_clk_reg, temp);
88 temp = RREG32(rec->a_data_reg) & ~rec->a_data_mask;
89 WREG32(rec->a_data_reg, temp);
91 /* set the pins to input */
92 temp = RREG32(rec->en_clk_reg) & ~rec->en_clk_mask;
93 WREG32(rec->en_clk_reg, temp);
95 temp = RREG32(rec->en_data_reg) & ~rec->en_data_mask;
96 WREG32(rec->en_data_reg, temp);
98 /* mask the gpio pins for software use */
99 temp = RREG32(rec->mask_clk_reg);
100 if (lock_state)
101 temp |= rec->mask_clk_mask;
102 else
103 temp &= ~rec->mask_clk_mask;
104 WREG32(rec->mask_clk_reg, temp);
105 temp = RREG32(rec->mask_clk_reg);
107 temp = RREG32(rec->mask_data_reg);
108 if (lock_state)
109 temp |= rec->mask_data_mask;
110 else
111 temp &= ~rec->mask_data_mask;
112 WREG32(rec->mask_data_reg, temp);
113 temp = RREG32(rec->mask_data_reg);
116 static int get_clock(void *i2c_priv)
118 struct radeon_i2c_chan *i2c = i2c_priv;
119 struct radeon_device *rdev = i2c->dev->dev_private;
120 struct radeon_i2c_bus_rec *rec = &i2c->rec;
121 uint32_t val;
123 /* read the value off the pin */
124 val = RREG32(rec->y_clk_reg);
125 val &= rec->y_clk_mask;
127 return (val != 0);
131 static int get_data(void *i2c_priv)
133 struct radeon_i2c_chan *i2c = i2c_priv;
134 struct radeon_device *rdev = i2c->dev->dev_private;
135 struct radeon_i2c_bus_rec *rec = &i2c->rec;
136 uint32_t val;
138 /* read the value off the pin */
139 val = RREG32(rec->y_data_reg);
140 val &= rec->y_data_mask;
142 return (val != 0);
145 static void set_clock(void *i2c_priv, int clock)
147 struct radeon_i2c_chan *i2c = i2c_priv;
148 struct radeon_device *rdev = i2c->dev->dev_private;
149 struct radeon_i2c_bus_rec *rec = &i2c->rec;
150 uint32_t val;
152 /* set pin direction */
153 val = RREG32(rec->en_clk_reg) & ~rec->en_clk_mask;
154 val |= clock ? 0 : rec->en_clk_mask;
155 WREG32(rec->en_clk_reg, val);
158 static void set_data(void *i2c_priv, int data)
160 struct radeon_i2c_chan *i2c = i2c_priv;
161 struct radeon_device *rdev = i2c->dev->dev_private;
162 struct radeon_i2c_bus_rec *rec = &i2c->rec;
163 uint32_t val;
165 /* set pin direction */
166 val = RREG32(rec->en_data_reg) & ~rec->en_data_mask;
167 val |= data ? 0 : rec->en_data_mask;
168 WREG32(rec->en_data_reg, val);
171 struct radeon_i2c_chan *radeon_i2c_create(struct drm_device *dev,
172 struct radeon_i2c_bus_rec *rec,
173 const char *name)
175 struct radeon_i2c_chan *i2c;
176 int ret;
178 i2c = kzalloc(sizeof(struct radeon_i2c_chan), GFP_KERNEL);
179 if (i2c == NULL)
180 return NULL;
182 i2c->adapter.owner = THIS_MODULE;
183 i2c->dev = dev;
184 i2c_set_adapdata(&i2c->adapter, i2c);
185 i2c->adapter.algo_data = &i2c->algo.bit;
186 i2c->algo.bit.setsda = set_data;
187 i2c->algo.bit.setscl = set_clock;
188 i2c->algo.bit.getsda = get_data;
189 i2c->algo.bit.getscl = get_clock;
190 i2c->algo.bit.udelay = 20;
191 /* vesa says 2.2 ms is enough, 1 jiffy doesn't seem to always
192 * make this, 2 jiffies is a lot more reliable */
193 i2c->algo.bit.timeout = 2;
194 i2c->algo.bit.data = i2c;
195 i2c->rec = *rec;
196 ret = i2c_bit_add_bus(&i2c->adapter);
197 if (ret) {
198 DRM_INFO("Failed to register i2c %s\n", name);
199 goto out_free;
202 return i2c;
203 out_free:
204 kfree(i2c);
205 return NULL;
209 struct radeon_i2c_chan *radeon_i2c_create_dp(struct drm_device *dev,
210 struct radeon_i2c_bus_rec *rec,
211 const char *name)
213 struct radeon_i2c_chan *i2c;
214 int ret;
216 i2c = kzalloc(sizeof(struct radeon_i2c_chan), GFP_KERNEL);
217 if (i2c == NULL)
218 return NULL;
220 i2c->rec = *rec;
221 i2c->adapter.owner = THIS_MODULE;
222 i2c->dev = dev;
223 i2c_set_adapdata(&i2c->adapter, i2c);
224 i2c->adapter.algo_data = &i2c->algo.dp;
225 i2c->algo.dp.aux_ch = radeon_dp_i2c_aux_ch;
226 i2c->algo.dp.address = 0;
227 ret = i2c_dp_aux_add_bus(&i2c->adapter);
228 if (ret) {
229 DRM_INFO("Failed to register i2c %s\n", name);
230 goto out_free;
233 return i2c;
234 out_free:
235 kfree(i2c);
236 return NULL;
241 void radeon_i2c_destroy(struct radeon_i2c_chan *i2c)
243 if (!i2c)
244 return;
246 i2c_del_adapter(&i2c->adapter);
247 kfree(i2c);
250 struct drm_encoder *radeon_best_encoder(struct drm_connector *connector)
252 return NULL;
255 void radeon_i2c_sw_get_byte(struct radeon_i2c_chan *i2c_bus,
256 u8 slave_addr,
257 u8 addr,
258 u8 *val)
260 u8 out_buf[2];
261 u8 in_buf[2];
262 struct i2c_msg msgs[] = {
264 .addr = slave_addr,
265 .flags = 0,
266 .len = 1,
267 .buf = out_buf,
270 .addr = slave_addr,
271 .flags = I2C_M_RD,
272 .len = 1,
273 .buf = in_buf,
277 out_buf[0] = addr;
278 out_buf[1] = 0;
280 if (i2c_transfer(&i2c_bus->adapter, msgs, 2) == 2) {
281 *val = in_buf[0];
282 DRM_DEBUG("val = 0x%02x\n", *val);
283 } else {
284 DRM_ERROR("i2c 0x%02x 0x%02x read failed\n",
285 addr, *val);
289 void radeon_i2c_sw_put_byte(struct radeon_i2c_chan *i2c_bus,
290 u8 slave_addr,
291 u8 addr,
292 u8 val)
294 uint8_t out_buf[2];
295 struct i2c_msg msg = {
296 .addr = slave_addr,
297 .flags = 0,
298 .len = 2,
299 .buf = out_buf,
302 out_buf[0] = addr;
303 out_buf[1] = val;
305 if (i2c_transfer(&i2c_bus->adapter, &msg, 1) != 1)
306 DRM_ERROR("i2c 0x%02x 0x%02x write failed\n",
307 addr, val);