blob: 75b090f60208070dd718c42bc7969af83d5b9598 [file] [log] [blame]
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001/*
2 * Copyright 2007-8 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors: Dave Airlie
24 * Alex Deucher
25 */
26#include "drmP.h"
27#include "radeon_drm.h"
28#include "radeon.h"
29
30/**
31 * radeon_ddc_probe
32 *
33 */
34bool radeon_ddc_probe(struct radeon_connector *radeon_connector)
35{
36 u8 out_buf[] = { 0x0, 0x0};
37 u8 buf[2];
38 int ret;
39 struct i2c_msg msgs[] = {
40 {
41 .addr = 0x50,
42 .flags = 0,
43 .len = 1,
44 .buf = out_buf,
45 },
46 {
47 .addr = 0x50,
48 .flags = I2C_M_RD,
49 .len = 1,
50 .buf = buf,
51 }
52 };
53
54 ret = i2c_transfer(&radeon_connector->ddc_bus->adapter, msgs, 2);
55 if (ret == 2)
56 return true;
57
58 return false;
59}
60
61
Alex Deucher5a6f98f2009-12-22 15:04:48 -050062static void radeon_i2c_do_lock(struct radeon_i2c_chan *i2c, int lock_state)
Jerome Glisse771fe6b2009-06-05 14:42:42 +020063{
Alex Deucherab1e9ea2009-11-05 18:27:30 -050064 struct radeon_device *rdev = i2c->dev->dev_private;
65 struct radeon_i2c_bus_rec *rec = &i2c->rec;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020066 uint32_t temp;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020067
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
71 */
Alex Deucher6a93cb22009-11-23 17:39:28 -050072 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)));
80 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +020081 }
82 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +020083
Alex Deucher9b9fe722009-11-10 15:59:44 -050084 /* clear the output pin values */
85 temp = RREG32(rec->a_clk_reg) & ~rec->a_clk_mask;
86 WREG32(rec->a_clk_reg, temp);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020087
Alex Deucher9b9fe722009-11-10 15:59:44 -050088 temp = RREG32(rec->a_data_reg) & ~rec->a_data_mask;
89 WREG32(rec->a_data_reg, temp);
90
Alex Deucher6a93cb22009-11-23 17:39:28 -050091 /* set the pins to input */
92 temp = RREG32(rec->en_clk_reg) & ~rec->en_clk_mask;
93 WREG32(rec->en_clk_reg, temp);
94
95 temp = RREG32(rec->en_data_reg) & ~rec->en_data_mask;
96 WREG32(rec->en_data_reg, temp);
Alex Deucher9b9fe722009-11-10 15:59:44 -050097
98 /* mask the gpio pins for software use */
Jerome Glisse771fe6b2009-06-05 14:42:42 +020099 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);
106
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);
114}
115
116static int get_clock(void *i2c_priv)
117{
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;
122
Alex Deucher9b9fe722009-11-10 15:59:44 -0500123 /* read the value off the pin */
124 val = RREG32(rec->y_clk_reg);
125 val &= rec->y_clk_mask;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200126
127 return (val != 0);
128}
129
130
131static int get_data(void *i2c_priv)
132{
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;
137
Alex Deucher9b9fe722009-11-10 15:59:44 -0500138 /* read the value off the pin */
139 val = RREG32(rec->y_data_reg);
140 val &= rec->y_data_mask;
141
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200142 return (val != 0);
143}
144
145static void set_clock(void *i2c_priv, int clock)
146{
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;
151
Alex Deucher9b9fe722009-11-10 15:59:44 -0500152 /* 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);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200156}
157
158static void set_data(void *i2c_priv, int data)
159{
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;
164
Alex Deucher9b9fe722009-11-10 15:59:44 -0500165 /* 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);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200169}
170
Alex Deucher5a6f98f2009-12-22 15:04:48 -0500171static int radeon_i2c_xfer(struct i2c_adapter *i2c_adap,
172 struct i2c_msg *msgs, int num)
173{
174 struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
175 int ret;
176
177 radeon_i2c_do_lock(i2c, 1);
178 if (i2c_transfer(&i2c->algo.radeon.bit_adapter, msgs, num) == num)
179 ret = num;
180 else
181 ret = -1;
182 radeon_i2c_do_lock(i2c, 0);
183
184 return ret;
185}
186
187static u32 radeon_i2c_func(struct i2c_adapter *adap)
188{
189 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
190}
191
192static const struct i2c_algorithm radeon_i2c_algo = {
193 .master_xfer = radeon_i2c_xfer,
194 .functionality = radeon_i2c_func,
195};
196
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200197struct radeon_i2c_chan *radeon_i2c_create(struct drm_device *dev,
Alex Deucherab1e9ea2009-11-05 18:27:30 -0500198 struct radeon_i2c_bus_rec *rec,
199 const char *name)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200200{
201 struct radeon_i2c_chan *i2c;
202 int ret;
203
Eric Anholt9a298b22009-03-24 12:23:04 -0700204 i2c = kzalloc(sizeof(struct radeon_i2c_chan), GFP_KERNEL);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200205 if (i2c == NULL)
206 return NULL;
207
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200208 i2c->dev = dev;
Alex Deucher5a6f98f2009-12-22 15:04:48 -0500209 i2c->rec = *rec;
210 /* set the internal bit adapter */
211 i2c->algo.radeon.bit_adapter.owner = THIS_MODULE;
212 i2c_set_adapdata(&i2c->algo.radeon.bit_adapter, i2c);
213 sprintf(i2c->algo.radeon.bit_adapter.name, "Radeon internal i2c bit bus %s", name);
214 i2c->algo.radeon.bit_adapter.algo_data = &i2c->algo.radeon.bit_data;
215 i2c->algo.radeon.bit_data.setsda = set_data;
216 i2c->algo.radeon.bit_data.setscl = set_clock;
217 i2c->algo.radeon.bit_data.getsda = get_data;
218 i2c->algo.radeon.bit_data.getscl = get_clock;
219 i2c->algo.radeon.bit_data.udelay = 20;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200220 /* vesa says 2.2 ms is enough, 1 jiffy doesn't seem to always
221 * make this, 2 jiffies is a lot more reliable */
Alex Deucher5a6f98f2009-12-22 15:04:48 -0500222 i2c->algo.radeon.bit_data.timeout = 2;
223 i2c->algo.radeon.bit_data.data = i2c;
224 ret = i2c_bit_add_bus(&i2c->algo.radeon.bit_adapter);
225 if (ret) {
226 DRM_INFO("Failed to register internal bit i2c %s\n", name);
227 goto out_free;
228 }
229 /* set the radeon i2c adapter */
230 i2c->adapter.owner = THIS_MODULE;
231 i2c_set_adapdata(&i2c->adapter, i2c);
232 sprintf(i2c->adapter.name, "Radeon i2c %s", name);
233 i2c->adapter.algo_data = &i2c->algo.radeon;
234 i2c->adapter.algo = &radeon_i2c_algo;
235 ret = i2c_add_adapter(&i2c->adapter);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200236 if (ret) {
237 DRM_INFO("Failed to register i2c %s\n", name);
238 goto out_free;
239 }
240
241 return i2c;
242out_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700243 kfree(i2c);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200244 return NULL;
245
246}
247
Dave Airlie746c1aa2009-12-08 07:07:28 +1000248struct radeon_i2c_chan *radeon_i2c_create_dp(struct drm_device *dev,
Alex Deucher6a93cb22009-11-23 17:39:28 -0500249 struct radeon_i2c_bus_rec *rec,
250 const char *name)
Dave Airlie746c1aa2009-12-08 07:07:28 +1000251{
252 struct radeon_i2c_chan *i2c;
253 int ret;
254
255 i2c = kzalloc(sizeof(struct radeon_i2c_chan), GFP_KERNEL);
256 if (i2c == NULL)
257 return NULL;
258
Alex Deucher6a93cb22009-11-23 17:39:28 -0500259 i2c->rec = *rec;
Dave Airlie746c1aa2009-12-08 07:07:28 +1000260 i2c->adapter.owner = THIS_MODULE;
261 i2c->dev = dev;
262 i2c_set_adapdata(&i2c->adapter, i2c);
263 i2c->adapter.algo_data = &i2c->algo.dp;
264 i2c->algo.dp.aux_ch = radeon_dp_i2c_aux_ch;
265 i2c->algo.dp.address = 0;
266 ret = i2c_dp_aux_add_bus(&i2c->adapter);
267 if (ret) {
268 DRM_INFO("Failed to register i2c %s\n", name);
269 goto out_free;
270 }
271
272 return i2c;
273out_free:
274 kfree(i2c);
275 return NULL;
276
277}
278
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200279void radeon_i2c_destroy(struct radeon_i2c_chan *i2c)
280{
281 if (!i2c)
282 return;
Alex Deucher5a6f98f2009-12-22 15:04:48 -0500283 i2c_del_adapter(&i2c->algo.radeon.bit_adapter);
284 i2c_del_adapter(&i2c->adapter);
285 kfree(i2c);
286}
287
288void radeon_i2c_destroy_dp(struct radeon_i2c_chan *i2c)
289{
290 if (!i2c)
291 return;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200292
293 i2c_del_adapter(&i2c->adapter);
Eric Anholt9a298b22009-03-24 12:23:04 -0700294 kfree(i2c);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200295}
296
297struct drm_encoder *radeon_best_encoder(struct drm_connector *connector)
298{
299 return NULL;
300}
Alex Deucherfcec5702009-11-10 21:25:07 -0500301
Alex Deucher5a6f98f2009-12-22 15:04:48 -0500302void radeon_i2c_get_byte(struct radeon_i2c_chan *i2c_bus,
303 u8 slave_addr,
304 u8 addr,
305 u8 *val)
Alex Deucherfcec5702009-11-10 21:25:07 -0500306{
307 u8 out_buf[2];
308 u8 in_buf[2];
309 struct i2c_msg msgs[] = {
310 {
311 .addr = slave_addr,
312 .flags = 0,
313 .len = 1,
314 .buf = out_buf,
315 },
316 {
317 .addr = slave_addr,
318 .flags = I2C_M_RD,
319 .len = 1,
320 .buf = in_buf,
321 }
322 };
323
324 out_buf[0] = addr;
325 out_buf[1] = 0;
326
327 if (i2c_transfer(&i2c_bus->adapter, msgs, 2) == 2) {
328 *val = in_buf[0];
329 DRM_DEBUG("val = 0x%02x\n", *val);
330 } else {
331 DRM_ERROR("i2c 0x%02x 0x%02x read failed\n",
332 addr, *val);
333 }
334}
335
Alex Deucher5a6f98f2009-12-22 15:04:48 -0500336void radeon_i2c_put_byte(struct radeon_i2c_chan *i2c_bus,
337 u8 slave_addr,
338 u8 addr,
339 u8 val)
Alex Deucherfcec5702009-11-10 21:25:07 -0500340{
341 uint8_t out_buf[2];
342 struct i2c_msg msg = {
343 .addr = slave_addr,
344 .flags = 0,
345 .len = 2,
346 .buf = out_buf,
347 };
348
349 out_buf[0] = addr;
350 out_buf[1] = val;
351
352 if (i2c_transfer(&i2c_bus->adapter, &msg, 1) != 1)
353 DRM_ERROR("i2c 0x%02x 0x%02x write failed\n",
354 addr, val);
355}
356