blob: 24b8a8be2cd4372391a814900a252305145e24f0 [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"
Alex Deucher40bacf12009-12-23 03:23:21 -050029#include "atom.h"
Jerome Glisse771fe6b2009-06-05 14:42:42 +020030
31/**
32 * radeon_ddc_probe
33 *
34 */
35bool radeon_ddc_probe(struct radeon_connector *radeon_connector)
36{
37 u8 out_buf[] = { 0x0, 0x0};
38 u8 buf[2];
39 int ret;
40 struct i2c_msg msgs[] = {
41 {
42 .addr = 0x50,
43 .flags = 0,
44 .len = 1,
45 .buf = out_buf,
46 },
47 {
48 .addr = 0x50,
49 .flags = I2C_M_RD,
50 .len = 1,
51 .buf = buf,
52 }
53 };
54
Alex Deucher26b5bc92010-08-05 21:21:18 -040055 /* on hw with routers, select right port */
Alex Deucherfb939df2010-11-08 16:08:29 +000056 if (radeon_connector->router.ddc_valid)
57 radeon_router_select_ddc_port(radeon_connector);
Alex Deucher26b5bc92010-08-05 21:21:18 -040058
Jerome Glisse771fe6b2009-06-05 14:42:42 +020059 ret = i2c_transfer(&radeon_connector->ddc_bus->adapter, msgs, 2);
60 if (ret == 2)
61 return true;
62
63 return false;
64}
65
Alex Deucherac1aade2010-03-14 12:22:44 -040066/* bit banging i2c */
Jerome Glisse771fe6b2009-06-05 14:42:42 +020067
Alex Deucher5a6f98f2009-12-22 15:04:48 -050068static void radeon_i2c_do_lock(struct radeon_i2c_chan *i2c, int lock_state)
Jerome Glisse771fe6b2009-06-05 14:42:42 +020069{
Alex Deucherab1e9ea2009-11-05 18:27:30 -050070 struct radeon_device *rdev = i2c->dev->dev_private;
71 struct radeon_i2c_bus_rec *rec = &i2c->rec;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020072 uint32_t temp;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020073
74 /* RV410 appears to have a bug where the hw i2c in reset
75 * holds the i2c port in a bad state - switch hw i2c away before
76 * doing DDC - do this for all r200s/r300s/r400s for safety sake
77 */
Alex Deucher6a93cb22009-11-23 17:39:28 -050078 if (rec->hw_capable) {
79 if ((rdev->family >= CHIP_R200) && !ASIC_IS_AVIVO(rdev)) {
Alex Deucher40bacf12009-12-23 03:23:21 -050080 u32 reg;
81
82 if (rdev->family >= CHIP_RV350)
83 reg = RADEON_GPIO_MONID;
84 else if ((rdev->family == CHIP_R300) ||
85 (rdev->family == CHIP_R350))
86 reg = RADEON_GPIO_DVI_DDC;
87 else
88 reg = RADEON_GPIO_CRT2_DDC;
89
90 mutex_lock(&rdev->dc_hw_i2c_mutex);
91 if (rec->a_clk_reg == reg) {
Alex Deucher6a93cb22009-11-23 17:39:28 -050092 WREG32(RADEON_DVI_I2C_CNTL_0, (RADEON_I2C_SOFT_RST |
93 R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1)));
94 } else {
95 WREG32(RADEON_DVI_I2C_CNTL_0, (RADEON_I2C_SOFT_RST |
96 R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3)));
97 }
Alex Deucher40bacf12009-12-23 03:23:21 -050098 mutex_unlock(&rdev->dc_hw_i2c_mutex);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020099 }
100 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200101
Alex Deucher5786e2c2010-08-19 11:19:31 -0400102 /* switch the pads to ddc mode */
103 if (ASIC_IS_DCE3(rdev) && rec->hw_capable) {
104 temp = RREG32(rec->mask_clk_reg);
105 temp &= ~(1 << 16);
106 WREG32(rec->mask_clk_reg, temp);
107 }
108
Alex Deucher9b9fe722009-11-10 15:59:44 -0500109 /* clear the output pin values */
110 temp = RREG32(rec->a_clk_reg) & ~rec->a_clk_mask;
111 WREG32(rec->a_clk_reg, temp);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200112
Alex Deucher9b9fe722009-11-10 15:59:44 -0500113 temp = RREG32(rec->a_data_reg) & ~rec->a_data_mask;
114 WREG32(rec->a_data_reg, temp);
115
Alex Deucher6a93cb22009-11-23 17:39:28 -0500116 /* set the pins to input */
117 temp = RREG32(rec->en_clk_reg) & ~rec->en_clk_mask;
118 WREG32(rec->en_clk_reg, temp);
119
120 temp = RREG32(rec->en_data_reg) & ~rec->en_data_mask;
121 WREG32(rec->en_data_reg, temp);
Alex Deucher9b9fe722009-11-10 15:59:44 -0500122
123 /* mask the gpio pins for software use */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200124 temp = RREG32(rec->mask_clk_reg);
125 if (lock_state)
126 temp |= rec->mask_clk_mask;
127 else
128 temp &= ~rec->mask_clk_mask;
129 WREG32(rec->mask_clk_reg, temp);
130 temp = RREG32(rec->mask_clk_reg);
131
132 temp = RREG32(rec->mask_data_reg);
133 if (lock_state)
134 temp |= rec->mask_data_mask;
135 else
136 temp &= ~rec->mask_data_mask;
137 WREG32(rec->mask_data_reg, temp);
138 temp = RREG32(rec->mask_data_reg);
139}
140
141static int get_clock(void *i2c_priv)
142{
143 struct radeon_i2c_chan *i2c = i2c_priv;
144 struct radeon_device *rdev = i2c->dev->dev_private;
145 struct radeon_i2c_bus_rec *rec = &i2c->rec;
146 uint32_t val;
147
Alex Deucher9b9fe722009-11-10 15:59:44 -0500148 /* read the value off the pin */
149 val = RREG32(rec->y_clk_reg);
150 val &= rec->y_clk_mask;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200151
152 return (val != 0);
153}
154
155
156static int get_data(void *i2c_priv)
157{
158 struct radeon_i2c_chan *i2c = i2c_priv;
159 struct radeon_device *rdev = i2c->dev->dev_private;
160 struct radeon_i2c_bus_rec *rec = &i2c->rec;
161 uint32_t val;
162
Alex Deucher9b9fe722009-11-10 15:59:44 -0500163 /* read the value off the pin */
164 val = RREG32(rec->y_data_reg);
165 val &= rec->y_data_mask;
166
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200167 return (val != 0);
168}
169
170static void set_clock(void *i2c_priv, int clock)
171{
172 struct radeon_i2c_chan *i2c = i2c_priv;
173 struct radeon_device *rdev = i2c->dev->dev_private;
174 struct radeon_i2c_bus_rec *rec = &i2c->rec;
175 uint32_t val;
176
Alex Deucher9b9fe722009-11-10 15:59:44 -0500177 /* set pin direction */
178 val = RREG32(rec->en_clk_reg) & ~rec->en_clk_mask;
179 val |= clock ? 0 : rec->en_clk_mask;
180 WREG32(rec->en_clk_reg, val);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200181}
182
183static void set_data(void *i2c_priv, int data)
184{
185 struct radeon_i2c_chan *i2c = i2c_priv;
186 struct radeon_device *rdev = i2c->dev->dev_private;
187 struct radeon_i2c_bus_rec *rec = &i2c->rec;
188 uint32_t val;
189
Alex Deucher9b9fe722009-11-10 15:59:44 -0500190 /* set pin direction */
191 val = RREG32(rec->en_data_reg) & ~rec->en_data_mask;
192 val |= data ? 0 : rec->en_data_mask;
193 WREG32(rec->en_data_reg, val);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200194}
195
Alex Deucherac1aade2010-03-14 12:22:44 -0400196static int pre_xfer(struct i2c_adapter *i2c_adap)
197{
198 struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
199
200 radeon_i2c_do_lock(i2c, 1);
201
202 return 0;
203}
204
205static void post_xfer(struct i2c_adapter *i2c_adap)
206{
207 struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
208
209 radeon_i2c_do_lock(i2c, 0);
210}
211
212/* hw i2c */
213
Alex Deucher9dad76e2010-02-08 14:34:43 -0500214static u32 radeon_get_i2c_prescale(struct radeon_device *rdev)
215{
Alex Deucher88072862010-08-10 12:33:20 -0400216 u32 sclk = rdev->pm.current_sclk;
Alex Deucher9dad76e2010-02-08 14:34:43 -0500217 u32 prescale = 0;
Alex Deucher96a4c8d2010-03-12 12:55:34 -0500218 u32 nm;
219 u8 n, m, loop;
Alex Deucher9dad76e2010-02-08 14:34:43 -0500220 int i2c_clock;
221
222 switch (rdev->family) {
223 case CHIP_R100:
224 case CHIP_RV100:
225 case CHIP_RS100:
226 case CHIP_RV200:
227 case CHIP_RS200:
228 case CHIP_R200:
229 case CHIP_RV250:
230 case CHIP_RS300:
231 case CHIP_RV280:
232 case CHIP_R300:
233 case CHIP_R350:
234 case CHIP_RV350:
Alex Deucher96a4c8d2010-03-12 12:55:34 -0500235 i2c_clock = 60;
236 nm = (sclk * 10) / (i2c_clock * 4);
Alex Deucher9dad76e2010-02-08 14:34:43 -0500237 for (loop = 1; loop < 255; loop++) {
Alex Deucher96a4c8d2010-03-12 12:55:34 -0500238 if ((nm / loop) < loop)
Alex Deucher9dad76e2010-02-08 14:34:43 -0500239 break;
240 }
Alex Deucher96a4c8d2010-03-12 12:55:34 -0500241 n = loop - 1;
242 m = loop - 2;
243 prescale = m | (n << 8);
Alex Deucher9dad76e2010-02-08 14:34:43 -0500244 break;
245 case CHIP_RV380:
246 case CHIP_RS400:
247 case CHIP_RS480:
248 case CHIP_R420:
249 case CHIP_R423:
250 case CHIP_RV410:
Alex Deucher9dad76e2010-02-08 14:34:43 -0500251 prescale = (((sclk * 10)/(4 * 128 * 100) + 1) << 8) + 128;
252 break;
253 case CHIP_RS600:
254 case CHIP_RS690:
255 case CHIP_RS740:
256 /* todo */
257 break;
258 case CHIP_RV515:
259 case CHIP_R520:
260 case CHIP_RV530:
261 case CHIP_RV560:
262 case CHIP_RV570:
263 case CHIP_R580:
264 i2c_clock = 50;
Alex Deucher9dad76e2010-02-08 14:34:43 -0500265 if (rdev->family == CHIP_R520)
266 prescale = (127 << 8) + ((sclk * 10) / (4 * 127 * i2c_clock));
267 else
268 prescale = (((sclk * 10)/(4 * 128 * 100) + 1) << 8) + 128;
269 break;
270 case CHIP_R600:
271 case CHIP_RV610:
272 case CHIP_RV630:
273 case CHIP_RV670:
274 /* todo */
275 break;
276 case CHIP_RV620:
277 case CHIP_RV635:
278 case CHIP_RS780:
279 case CHIP_RS880:
280 case CHIP_RV770:
281 case CHIP_RV730:
282 case CHIP_RV710:
283 case CHIP_RV740:
284 /* todo */
285 break;
Alex Deucher4c36b672010-02-09 18:22:00 -0500286 case CHIP_CEDAR:
287 case CHIP_REDWOOD:
288 case CHIP_JUNIPER:
289 case CHIP_CYPRESS:
290 case CHIP_HEMLOCK:
291 /* todo */
292 break;
Alex Deucher9dad76e2010-02-08 14:34:43 -0500293 default:
294 DRM_ERROR("i2c: unhandled radeon chip\n");
295 break;
296 }
297 return prescale;
298}
299
300
Alex Deucher40bacf12009-12-23 03:23:21 -0500301/* hw i2c engine for r1xx-4xx hardware
302 * hw can buffer up to 15 bytes
303 */
304static int r100_hw_i2c_xfer(struct i2c_adapter *i2c_adap,
305 struct i2c_msg *msgs, int num)
306{
307 struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
308 struct radeon_device *rdev = i2c->dev->dev_private;
309 struct radeon_i2c_bus_rec *rec = &i2c->rec;
310 struct i2c_msg *p;
311 int i, j, k, ret = num;
Alex Deucher9dad76e2010-02-08 14:34:43 -0500312 u32 prescale;
Alex Deucher40bacf12009-12-23 03:23:21 -0500313 u32 i2c_cntl_0, i2c_cntl_1, i2c_data;
314 u32 tmp, reg;
315
316 mutex_lock(&rdev->dc_hw_i2c_mutex);
Alex Deucher57fcab62010-02-06 17:06:42 -0500317 /* take the pm lock since we need a constant sclk */
318 mutex_lock(&rdev->pm.mutex);
319
Alex Deucher9dad76e2010-02-08 14:34:43 -0500320 prescale = radeon_get_i2c_prescale(rdev);
Alex Deucher40bacf12009-12-23 03:23:21 -0500321
322 reg = ((prescale << RADEON_I2C_PRESCALE_SHIFT) |
Alex Deucherae088192010-03-11 13:28:14 -0500323 RADEON_I2C_DRIVE_EN |
Alex Deucher40bacf12009-12-23 03:23:21 -0500324 RADEON_I2C_START |
325 RADEON_I2C_STOP |
326 RADEON_I2C_GO);
327
328 if (rdev->is_atom_bios) {
329 tmp = RREG32(RADEON_BIOS_6_SCRATCH);
330 WREG32(RADEON_BIOS_6_SCRATCH, tmp | ATOM_S6_HW_I2C_BUSY_STATE);
331 }
332
333 if (rec->mm_i2c) {
334 i2c_cntl_0 = RADEON_I2C_CNTL_0;
335 i2c_cntl_1 = RADEON_I2C_CNTL_1;
336 i2c_data = RADEON_I2C_DATA;
337 } else {
338 i2c_cntl_0 = RADEON_DVI_I2C_CNTL_0;
339 i2c_cntl_1 = RADEON_DVI_I2C_CNTL_1;
340 i2c_data = RADEON_DVI_I2C_DATA;
341
342 switch (rdev->family) {
343 case CHIP_R100:
344 case CHIP_RV100:
345 case CHIP_RS100:
346 case CHIP_RV200:
347 case CHIP_RS200:
348 case CHIP_RS300:
349 switch (rec->mask_clk_reg) {
350 case RADEON_GPIO_DVI_DDC:
351 /* no gpio select bit */
352 break;
353 default:
354 DRM_ERROR("gpio not supported with hw i2c\n");
355 ret = -EINVAL;
356 goto done;
357 }
358 break;
359 case CHIP_R200:
360 /* only bit 4 on r200 */
361 switch (rec->mask_clk_reg) {
362 case RADEON_GPIO_DVI_DDC:
363 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1);
364 break;
365 case RADEON_GPIO_MONID:
366 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3);
367 break;
368 default:
369 DRM_ERROR("gpio not supported with hw i2c\n");
370 ret = -EINVAL;
371 goto done;
372 }
373 break;
374 case CHIP_RV250:
375 case CHIP_RV280:
376 /* bits 3 and 4 */
377 switch (rec->mask_clk_reg) {
378 case RADEON_GPIO_DVI_DDC:
379 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1);
380 break;
381 case RADEON_GPIO_VGA_DDC:
382 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC2);
383 break;
384 case RADEON_GPIO_CRT2_DDC:
385 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3);
386 break;
387 default:
388 DRM_ERROR("gpio not supported with hw i2c\n");
389 ret = -EINVAL;
390 goto done;
391 }
392 break;
393 case CHIP_R300:
394 case CHIP_R350:
395 /* only bit 4 on r300/r350 */
396 switch (rec->mask_clk_reg) {
397 case RADEON_GPIO_VGA_DDC:
398 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1);
399 break;
400 case RADEON_GPIO_DVI_DDC:
401 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3);
402 break;
403 default:
404 DRM_ERROR("gpio not supported with hw i2c\n");
405 ret = -EINVAL;
406 goto done;
407 }
408 break;
409 case CHIP_RV350:
410 case CHIP_RV380:
411 case CHIP_R420:
412 case CHIP_R423:
413 case CHIP_RV410:
414 case CHIP_RS400:
415 case CHIP_RS480:
416 /* bits 3 and 4 */
417 switch (rec->mask_clk_reg) {
418 case RADEON_GPIO_VGA_DDC:
419 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1);
420 break;
421 case RADEON_GPIO_DVI_DDC:
422 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC2);
423 break;
424 case RADEON_GPIO_MONID:
425 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3);
426 break;
427 default:
428 DRM_ERROR("gpio not supported with hw i2c\n");
429 ret = -EINVAL;
430 goto done;
431 }
432 break;
433 default:
434 DRM_ERROR("unsupported asic\n");
435 ret = -EINVAL;
436 goto done;
437 break;
438 }
439 }
440
441 /* check for bus probe */
442 p = &msgs[0];
443 if ((num == 1) && (p->len == 0)) {
444 WREG32(i2c_cntl_0, (RADEON_I2C_DONE |
445 RADEON_I2C_NACK |
446 RADEON_I2C_HALT |
447 RADEON_I2C_SOFT_RST));
448 WREG32(i2c_data, (p->addr << 1) & 0xff);
449 WREG32(i2c_data, 0);
450 WREG32(i2c_cntl_1, ((1 << RADEON_I2C_DATA_COUNT_SHIFT) |
451 (1 << RADEON_I2C_ADDR_COUNT_SHIFT) |
452 RADEON_I2C_EN |
453 (48 << RADEON_I2C_TIME_LIMIT_SHIFT)));
454 WREG32(i2c_cntl_0, reg);
455 for (k = 0; k < 32; k++) {
456 udelay(10);
457 tmp = RREG32(i2c_cntl_0);
458 if (tmp & RADEON_I2C_GO)
459 continue;
460 tmp = RREG32(i2c_cntl_0);
461 if (tmp & RADEON_I2C_DONE)
462 break;
463 else {
464 DRM_DEBUG("i2c write error 0x%08x\n", tmp);
465 WREG32(i2c_cntl_0, tmp | RADEON_I2C_ABORT);
466 ret = -EIO;
467 goto done;
468 }
469 }
470 goto done;
471 }
472
473 for (i = 0; i < num; i++) {
474 p = &msgs[i];
475 for (j = 0; j < p->len; j++) {
476 if (p->flags & I2C_M_RD) {
477 WREG32(i2c_cntl_0, (RADEON_I2C_DONE |
478 RADEON_I2C_NACK |
479 RADEON_I2C_HALT |
480 RADEON_I2C_SOFT_RST));
481 WREG32(i2c_data, ((p->addr << 1) & 0xff) | 0x1);
482 WREG32(i2c_cntl_1, ((1 << RADEON_I2C_DATA_COUNT_SHIFT) |
483 (1 << RADEON_I2C_ADDR_COUNT_SHIFT) |
484 RADEON_I2C_EN |
485 (48 << RADEON_I2C_TIME_LIMIT_SHIFT)));
486 WREG32(i2c_cntl_0, reg | RADEON_I2C_RECEIVE);
487 for (k = 0; k < 32; k++) {
488 udelay(10);
489 tmp = RREG32(i2c_cntl_0);
490 if (tmp & RADEON_I2C_GO)
491 continue;
492 tmp = RREG32(i2c_cntl_0);
493 if (tmp & RADEON_I2C_DONE)
494 break;
495 else {
496 DRM_DEBUG("i2c read error 0x%08x\n", tmp);
497 WREG32(i2c_cntl_0, tmp | RADEON_I2C_ABORT);
498 ret = -EIO;
499 goto done;
500 }
501 }
502 p->buf[j] = RREG32(i2c_data) & 0xff;
503 } else {
504 WREG32(i2c_cntl_0, (RADEON_I2C_DONE |
505 RADEON_I2C_NACK |
506 RADEON_I2C_HALT |
507 RADEON_I2C_SOFT_RST));
508 WREG32(i2c_data, (p->addr << 1) & 0xff);
509 WREG32(i2c_data, p->buf[j]);
510 WREG32(i2c_cntl_1, ((1 << RADEON_I2C_DATA_COUNT_SHIFT) |
511 (1 << RADEON_I2C_ADDR_COUNT_SHIFT) |
512 RADEON_I2C_EN |
513 (48 << RADEON_I2C_TIME_LIMIT_SHIFT)));
514 WREG32(i2c_cntl_0, reg);
515 for (k = 0; k < 32; k++) {
516 udelay(10);
517 tmp = RREG32(i2c_cntl_0);
518 if (tmp & RADEON_I2C_GO)
519 continue;
520 tmp = RREG32(i2c_cntl_0);
521 if (tmp & RADEON_I2C_DONE)
522 break;
523 else {
524 DRM_DEBUG("i2c write error 0x%08x\n", tmp);
525 WREG32(i2c_cntl_0, tmp | RADEON_I2C_ABORT);
526 ret = -EIO;
527 goto done;
528 }
529 }
530 }
531 }
532 }
533
534done:
535 WREG32(i2c_cntl_0, 0);
536 WREG32(i2c_cntl_1, 0);
537 WREG32(i2c_cntl_0, (RADEON_I2C_DONE |
538 RADEON_I2C_NACK |
539 RADEON_I2C_HALT |
540 RADEON_I2C_SOFT_RST));
541
542 if (rdev->is_atom_bios) {
543 tmp = RREG32(RADEON_BIOS_6_SCRATCH);
544 tmp &= ~ATOM_S6_HW_I2C_BUSY_STATE;
545 WREG32(RADEON_BIOS_6_SCRATCH, tmp);
546 }
547
Alex Deucher57fcab62010-02-06 17:06:42 -0500548 mutex_unlock(&rdev->pm.mutex);
Alex Deucher40bacf12009-12-23 03:23:21 -0500549 mutex_unlock(&rdev->dc_hw_i2c_mutex);
550
551 return ret;
552}
553
554/* hw i2c engine for r5xx hardware
555 * hw can buffer up to 15 bytes
556 */
557static int r500_hw_i2c_xfer(struct i2c_adapter *i2c_adap,
558 struct i2c_msg *msgs, int num)
559{
560 struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
561 struct radeon_device *rdev = i2c->dev->dev_private;
562 struct radeon_i2c_bus_rec *rec = &i2c->rec;
563 struct i2c_msg *p;
Alex Deucher40bacf12009-12-23 03:23:21 -0500564 int i, j, remaining, current_count, buffer_offset, ret = num;
Alex Deucher9dad76e2010-02-08 14:34:43 -0500565 u32 prescale;
Alex Deucher40bacf12009-12-23 03:23:21 -0500566 u32 tmp, reg;
567 u32 saved1, saved2;
568
569 mutex_lock(&rdev->dc_hw_i2c_mutex);
Alex Deucher57fcab62010-02-06 17:06:42 -0500570 /* take the pm lock since we need a constant sclk */
571 mutex_lock(&rdev->pm.mutex);
572
Alex Deucher9dad76e2010-02-08 14:34:43 -0500573 prescale = radeon_get_i2c_prescale(rdev);
Alex Deucher40bacf12009-12-23 03:23:21 -0500574
575 /* clear gpio mask bits */
576 tmp = RREG32(rec->mask_clk_reg);
577 tmp &= ~rec->mask_clk_mask;
578 WREG32(rec->mask_clk_reg, tmp);
579 tmp = RREG32(rec->mask_clk_reg);
580
581 tmp = RREG32(rec->mask_data_reg);
582 tmp &= ~rec->mask_data_mask;
583 WREG32(rec->mask_data_reg, tmp);
584 tmp = RREG32(rec->mask_data_reg);
585
586 /* clear pin values */
587 tmp = RREG32(rec->a_clk_reg);
588 tmp &= ~rec->a_clk_mask;
589 WREG32(rec->a_clk_reg, tmp);
590 tmp = RREG32(rec->a_clk_reg);
591
592 tmp = RREG32(rec->a_data_reg);
593 tmp &= ~rec->a_data_mask;
594 WREG32(rec->a_data_reg, tmp);
595 tmp = RREG32(rec->a_data_reg);
596
597 /* set the pins to input */
598 tmp = RREG32(rec->en_clk_reg);
599 tmp &= ~rec->en_clk_mask;
600 WREG32(rec->en_clk_reg, tmp);
601 tmp = RREG32(rec->en_clk_reg);
602
603 tmp = RREG32(rec->en_data_reg);
604 tmp &= ~rec->en_data_mask;
605 WREG32(rec->en_data_reg, tmp);
606 tmp = RREG32(rec->en_data_reg);
607
608 /* */
609 tmp = RREG32(RADEON_BIOS_6_SCRATCH);
610 WREG32(RADEON_BIOS_6_SCRATCH, tmp | ATOM_S6_HW_I2C_BUSY_STATE);
611 saved1 = RREG32(AVIVO_DC_I2C_CONTROL1);
612 saved2 = RREG32(0x494);
613 WREG32(0x494, saved2 | 0x1);
614
615 WREG32(AVIVO_DC_I2C_ARBITRATION, AVIVO_DC_I2C_SW_WANTS_TO_USE_I2C);
616 for (i = 0; i < 50; i++) {
617 udelay(1);
618 if (RREG32(AVIVO_DC_I2C_ARBITRATION) & AVIVO_DC_I2C_SW_CAN_USE_I2C)
619 break;
620 }
621 if (i == 50) {
622 DRM_ERROR("failed to get i2c bus\n");
623 ret = -EBUSY;
624 goto done;
625 }
626
Alex Deucher40bacf12009-12-23 03:23:21 -0500627 reg = AVIVO_DC_I2C_START | AVIVO_DC_I2C_STOP | AVIVO_DC_I2C_EN;
628 switch (rec->mask_clk_reg) {
629 case AVIVO_DC_GPIO_DDC1_MASK:
630 reg |= AVIVO_DC_I2C_PIN_SELECT(AVIVO_SEL_DDC1);
631 break;
632 case AVIVO_DC_GPIO_DDC2_MASK:
633 reg |= AVIVO_DC_I2C_PIN_SELECT(AVIVO_SEL_DDC2);
634 break;
635 case AVIVO_DC_GPIO_DDC3_MASK:
636 reg |= AVIVO_DC_I2C_PIN_SELECT(AVIVO_SEL_DDC3);
637 break;
638 default:
639 DRM_ERROR("gpio not supported with hw i2c\n");
640 ret = -EINVAL;
641 goto done;
642 }
643
644 /* check for bus probe */
645 p = &msgs[0];
646 if ((num == 1) && (p->len == 0)) {
647 WREG32(AVIVO_DC_I2C_STATUS1, (AVIVO_DC_I2C_DONE |
648 AVIVO_DC_I2C_NACK |
649 AVIVO_DC_I2C_HALT));
650 WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_SOFT_RESET);
651 udelay(1);
652 WREG32(AVIVO_DC_I2C_RESET, 0);
653
654 WREG32(AVIVO_DC_I2C_DATA, (p->addr << 1) & 0xff);
655 WREG32(AVIVO_DC_I2C_DATA, 0);
656
657 WREG32(AVIVO_DC_I2C_CONTROL3, AVIVO_DC_I2C_TIME_LIMIT(48));
658 WREG32(AVIVO_DC_I2C_CONTROL2, (AVIVO_DC_I2C_ADDR_COUNT(1) |
659 AVIVO_DC_I2C_DATA_COUNT(1) |
660 (prescale << 16)));
661 WREG32(AVIVO_DC_I2C_CONTROL1, reg);
662 WREG32(AVIVO_DC_I2C_STATUS1, AVIVO_DC_I2C_GO);
663 for (j = 0; j < 200; j++) {
664 udelay(50);
665 tmp = RREG32(AVIVO_DC_I2C_STATUS1);
666 if (tmp & AVIVO_DC_I2C_GO)
667 continue;
668 tmp = RREG32(AVIVO_DC_I2C_STATUS1);
669 if (tmp & AVIVO_DC_I2C_DONE)
670 break;
671 else {
672 DRM_DEBUG("i2c write error 0x%08x\n", tmp);
673 WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_ABORT);
674 ret = -EIO;
675 goto done;
676 }
677 }
678 goto done;
679 }
680
681 for (i = 0; i < num; i++) {
682 p = &msgs[i];
683 remaining = p->len;
684 buffer_offset = 0;
685 if (p->flags & I2C_M_RD) {
686 while (remaining) {
687 if (remaining > 15)
688 current_count = 15;
689 else
690 current_count = remaining;
691 WREG32(AVIVO_DC_I2C_STATUS1, (AVIVO_DC_I2C_DONE |
692 AVIVO_DC_I2C_NACK |
693 AVIVO_DC_I2C_HALT));
694 WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_SOFT_RESET);
695 udelay(1);
696 WREG32(AVIVO_DC_I2C_RESET, 0);
697
698 WREG32(AVIVO_DC_I2C_DATA, ((p->addr << 1) & 0xff) | 0x1);
699 WREG32(AVIVO_DC_I2C_CONTROL3, AVIVO_DC_I2C_TIME_LIMIT(48));
700 WREG32(AVIVO_DC_I2C_CONTROL2, (AVIVO_DC_I2C_ADDR_COUNT(1) |
701 AVIVO_DC_I2C_DATA_COUNT(current_count) |
702 (prescale << 16)));
703 WREG32(AVIVO_DC_I2C_CONTROL1, reg | AVIVO_DC_I2C_RECEIVE);
704 WREG32(AVIVO_DC_I2C_STATUS1, AVIVO_DC_I2C_GO);
705 for (j = 0; j < 200; j++) {
706 udelay(50);
707 tmp = RREG32(AVIVO_DC_I2C_STATUS1);
708 if (tmp & AVIVO_DC_I2C_GO)
709 continue;
710 tmp = RREG32(AVIVO_DC_I2C_STATUS1);
711 if (tmp & AVIVO_DC_I2C_DONE)
712 break;
713 else {
714 DRM_DEBUG("i2c read error 0x%08x\n", tmp);
715 WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_ABORT);
716 ret = -EIO;
717 goto done;
718 }
719 }
720 for (j = 0; j < current_count; j++)
721 p->buf[buffer_offset + j] = RREG32(AVIVO_DC_I2C_DATA) & 0xff;
722 remaining -= current_count;
723 buffer_offset += current_count;
724 }
725 } else {
726 while (remaining) {
727 if (remaining > 15)
728 current_count = 15;
729 else
730 current_count = remaining;
731 WREG32(AVIVO_DC_I2C_STATUS1, (AVIVO_DC_I2C_DONE |
732 AVIVO_DC_I2C_NACK |
733 AVIVO_DC_I2C_HALT));
734 WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_SOFT_RESET);
735 udelay(1);
736 WREG32(AVIVO_DC_I2C_RESET, 0);
737
738 WREG32(AVIVO_DC_I2C_DATA, (p->addr << 1) & 0xff);
739 for (j = 0; j < current_count; j++)
740 WREG32(AVIVO_DC_I2C_DATA, p->buf[buffer_offset + j]);
741
742 WREG32(AVIVO_DC_I2C_CONTROL3, AVIVO_DC_I2C_TIME_LIMIT(48));
743 WREG32(AVIVO_DC_I2C_CONTROL2, (AVIVO_DC_I2C_ADDR_COUNT(1) |
744 AVIVO_DC_I2C_DATA_COUNT(current_count) |
745 (prescale << 16)));
746 WREG32(AVIVO_DC_I2C_CONTROL1, reg);
747 WREG32(AVIVO_DC_I2C_STATUS1, AVIVO_DC_I2C_GO);
748 for (j = 0; j < 200; j++) {
749 udelay(50);
750 tmp = RREG32(AVIVO_DC_I2C_STATUS1);
751 if (tmp & AVIVO_DC_I2C_GO)
752 continue;
753 tmp = RREG32(AVIVO_DC_I2C_STATUS1);
754 if (tmp & AVIVO_DC_I2C_DONE)
755 break;
756 else {
757 DRM_DEBUG("i2c write error 0x%08x\n", tmp);
758 WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_ABORT);
759 ret = -EIO;
760 goto done;
761 }
762 }
763 remaining -= current_count;
764 buffer_offset += current_count;
765 }
766 }
767 }
768
769done:
770 WREG32(AVIVO_DC_I2C_STATUS1, (AVIVO_DC_I2C_DONE |
771 AVIVO_DC_I2C_NACK |
772 AVIVO_DC_I2C_HALT));
773 WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_SOFT_RESET);
774 udelay(1);
775 WREG32(AVIVO_DC_I2C_RESET, 0);
776
777 WREG32(AVIVO_DC_I2C_ARBITRATION, AVIVO_DC_I2C_SW_DONE_USING_I2C);
778 WREG32(AVIVO_DC_I2C_CONTROL1, saved1);
779 WREG32(0x494, saved2);
780 tmp = RREG32(RADEON_BIOS_6_SCRATCH);
781 tmp &= ~ATOM_S6_HW_I2C_BUSY_STATE;
782 WREG32(RADEON_BIOS_6_SCRATCH, tmp);
783
Alex Deucher57fcab62010-02-06 17:06:42 -0500784 mutex_unlock(&rdev->pm.mutex);
Alex Deucher40bacf12009-12-23 03:23:21 -0500785 mutex_unlock(&rdev->dc_hw_i2c_mutex);
786
787 return ret;
788}
789
Alex Deucherac1aade2010-03-14 12:22:44 -0400790static int radeon_hw_i2c_xfer(struct i2c_adapter *i2c_adap,
Alex Deucher40bacf12009-12-23 03:23:21 -0500791 struct i2c_msg *msgs, int num)
Alex Deucher5a6f98f2009-12-22 15:04:48 -0500792{
793 struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
Alex Deucher40bacf12009-12-23 03:23:21 -0500794 struct radeon_device *rdev = i2c->dev->dev_private;
795 struct radeon_i2c_bus_rec *rec = &i2c->rec;
Alex Deucherac1aade2010-03-14 12:22:44 -0400796 int ret = 0;
Alex Deucher40bacf12009-12-23 03:23:21 -0500797
798 switch (rdev->family) {
799 case CHIP_R100:
800 case CHIP_RV100:
801 case CHIP_RS100:
802 case CHIP_RV200:
803 case CHIP_RS200:
804 case CHIP_R200:
805 case CHIP_RV250:
806 case CHIP_RS300:
807 case CHIP_RV280:
808 case CHIP_R300:
809 case CHIP_R350:
810 case CHIP_RV350:
811 case CHIP_RV380:
812 case CHIP_R420:
813 case CHIP_R423:
814 case CHIP_RV410:
815 case CHIP_RS400:
816 case CHIP_RS480:
Alex Deucherac1aade2010-03-14 12:22:44 -0400817 ret = r100_hw_i2c_xfer(i2c_adap, msgs, num);
Alex Deucher40bacf12009-12-23 03:23:21 -0500818 break;
819 case CHIP_RS600:
820 case CHIP_RS690:
821 case CHIP_RS740:
822 /* XXX fill in hw i2c implementation */
Alex Deucher40bacf12009-12-23 03:23:21 -0500823 break;
824 case CHIP_RV515:
825 case CHIP_R520:
826 case CHIP_RV530:
827 case CHIP_RV560:
828 case CHIP_RV570:
829 case CHIP_R580:
Alex Deucherac1aade2010-03-14 12:22:44 -0400830 if (rec->mm_i2c)
831 ret = r100_hw_i2c_xfer(i2c_adap, msgs, num);
832 else
833 ret = r500_hw_i2c_xfer(i2c_adap, msgs, num);
Alex Deucher40bacf12009-12-23 03:23:21 -0500834 break;
835 case CHIP_R600:
836 case CHIP_RV610:
837 case CHIP_RV630:
838 case CHIP_RV670:
839 /* XXX fill in hw i2c implementation */
Alex Deucher40bacf12009-12-23 03:23:21 -0500840 break;
841 case CHIP_RV620:
842 case CHIP_RV635:
843 case CHIP_RS780:
844 case CHIP_RS880:
845 case CHIP_RV770:
846 case CHIP_RV730:
847 case CHIP_RV710:
848 case CHIP_RV740:
849 /* XXX fill in hw i2c implementation */
Alex Deucher40bacf12009-12-23 03:23:21 -0500850 break;
Alex Deucher4c36b672010-02-09 18:22:00 -0500851 case CHIP_CEDAR:
852 case CHIP_REDWOOD:
853 case CHIP_JUNIPER:
854 case CHIP_CYPRESS:
855 case CHIP_HEMLOCK:
856 /* XXX fill in hw i2c implementation */
Alex Deucher4c36b672010-02-09 18:22:00 -0500857 break;
Alex Deucher40bacf12009-12-23 03:23:21 -0500858 default:
859 DRM_ERROR("i2c: unhandled radeon chip\n");
860 ret = -EIO;
861 break;
862 }
863
864 return ret;
865}
866
Alex Deucherac1aade2010-03-14 12:22:44 -0400867static u32 radeon_hw_i2c_func(struct i2c_adapter *adap)
Alex Deucher5a6f98f2009-12-22 15:04:48 -0500868{
869 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
870}
871
872static const struct i2c_algorithm radeon_i2c_algo = {
Alex Deucherac1aade2010-03-14 12:22:44 -0400873 .master_xfer = radeon_hw_i2c_xfer,
874 .functionality = radeon_hw_i2c_func,
Alex Deucher5a6f98f2009-12-22 15:04:48 -0500875};
876
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200877struct radeon_i2c_chan *radeon_i2c_create(struct drm_device *dev,
Alex Deucherab1e9ea2009-11-05 18:27:30 -0500878 struct radeon_i2c_bus_rec *rec,
879 const char *name)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200880{
Alex Deucherac1aade2010-03-14 12:22:44 -0400881 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200882 struct radeon_i2c_chan *i2c;
883 int ret;
884
Eric Anholt9a298b22009-03-24 12:23:04 -0700885 i2c = kzalloc(sizeof(struct radeon_i2c_chan), GFP_KERNEL);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200886 if (i2c == NULL)
887 return NULL;
888
Alex Deucher40bacf12009-12-23 03:23:21 -0500889 i2c->rec = *rec;
Alex Deucher5a6f98f2009-12-22 15:04:48 -0500890 i2c->adapter.owner = THIS_MODULE;
Alex Deucherac1aade2010-03-14 12:22:44 -0400891 i2c->dev = dev;
Alex Deucher5a6f98f2009-12-22 15:04:48 -0500892 i2c_set_adapdata(&i2c->adapter, i2c);
Alex Deuchere2b0a8e2010-03-17 02:07:37 -0400893 if (rec->mm_i2c ||
894 (rec->hw_capable &&
895 radeon_hw_i2c &&
896 ((rdev->family <= CHIP_RS480) ||
897 ((rdev->family >= CHIP_RV515) && (rdev->family <= CHIP_R580))))) {
Alex Deucherac1aade2010-03-14 12:22:44 -0400898 /* set the radeon hw i2c adapter */
899 sprintf(i2c->adapter.name, "Radeon i2c hw bus %s", name);
900 i2c->adapter.algo = &radeon_i2c_algo;
901 ret = i2c_add_adapter(&i2c->adapter);
902 if (ret) {
903 DRM_ERROR("Failed to register hw i2c %s\n", name);
904 goto out_free;
905 }
906 } else {
907 /* set the radeon bit adapter */
908 sprintf(i2c->adapter.name, "Radeon i2c bit bus %s", name);
909 i2c->adapter.algo_data = &i2c->algo.bit;
910 i2c->algo.bit.pre_xfer = pre_xfer;
911 i2c->algo.bit.post_xfer = post_xfer;
912 i2c->algo.bit.setsda = set_data;
913 i2c->algo.bit.setscl = set_clock;
914 i2c->algo.bit.getsda = get_data;
915 i2c->algo.bit.getscl = get_clock;
916 i2c->algo.bit.udelay = 20;
917 /* vesa says 2.2 ms is enough, 1 jiffy doesn't seem to always
918 * make this, 2 jiffies is a lot more reliable */
919 i2c->algo.bit.timeout = 2;
920 i2c->algo.bit.data = i2c;
921 ret = i2c_bit_add_bus(&i2c->adapter);
922 if (ret) {
923 DRM_ERROR("Failed to register bit i2c %s\n", name);
924 goto out_free;
925 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200926 }
927
928 return i2c;
929out_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700930 kfree(i2c);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200931 return NULL;
932
933}
934
Dave Airlie746c1aa2009-12-08 07:07:28 +1000935struct radeon_i2c_chan *radeon_i2c_create_dp(struct drm_device *dev,
Alex Deucher6a93cb22009-11-23 17:39:28 -0500936 struct radeon_i2c_bus_rec *rec,
937 const char *name)
Dave Airlie746c1aa2009-12-08 07:07:28 +1000938{
939 struct radeon_i2c_chan *i2c;
940 int ret;
941
942 i2c = kzalloc(sizeof(struct radeon_i2c_chan), GFP_KERNEL);
943 if (i2c == NULL)
944 return NULL;
945
Alex Deucher6a93cb22009-11-23 17:39:28 -0500946 i2c->rec = *rec;
Dave Airlie746c1aa2009-12-08 07:07:28 +1000947 i2c->adapter.owner = THIS_MODULE;
948 i2c->dev = dev;
949 i2c_set_adapdata(&i2c->adapter, i2c);
950 i2c->adapter.algo_data = &i2c->algo.dp;
951 i2c->algo.dp.aux_ch = radeon_dp_i2c_aux_ch;
952 i2c->algo.dp.address = 0;
953 ret = i2c_dp_aux_add_bus(&i2c->adapter);
954 if (ret) {
955 DRM_INFO("Failed to register i2c %s\n", name);
956 goto out_free;
957 }
958
959 return i2c;
960out_free:
961 kfree(i2c);
962 return NULL;
963
964}
965
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200966void radeon_i2c_destroy(struct radeon_i2c_chan *i2c)
967{
968 if (!i2c)
969 return;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200970 i2c_del_adapter(&i2c->adapter);
Eric Anholt9a298b22009-03-24 12:23:04 -0700971 kfree(i2c);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200972}
973
Alex Deucherf376b942010-08-05 21:21:16 -0400974/* Add the default buses */
975void radeon_i2c_init(struct radeon_device *rdev)
976{
977 if (rdev->is_atom_bios)
978 radeon_atombios_i2c_init(rdev);
979 else
980 radeon_combios_i2c_init(rdev);
981}
982
983/* remove all the buses */
984void radeon_i2c_fini(struct radeon_device *rdev)
985{
986 int i;
987
988 for (i = 0; i < RADEON_MAX_I2C_BUS; i++) {
989 if (rdev->i2c_bus[i]) {
990 radeon_i2c_destroy(rdev->i2c_bus[i]);
991 rdev->i2c_bus[i] = NULL;
992 }
993 }
994}
995
996/* Add additional buses */
997void radeon_i2c_add(struct radeon_device *rdev,
998 struct radeon_i2c_bus_rec *rec,
999 const char *name)
1000{
1001 struct drm_device *dev = rdev->ddev;
1002 int i;
1003
1004 for (i = 0; i < RADEON_MAX_I2C_BUS; i++) {
1005 if (!rdev->i2c_bus[i]) {
1006 rdev->i2c_bus[i] = radeon_i2c_create(dev, rec, name);
1007 return;
1008 }
1009 }
1010}
1011
1012/* looks up bus based on id */
1013struct radeon_i2c_chan *radeon_i2c_lookup(struct radeon_device *rdev,
1014 struct radeon_i2c_bus_rec *i2c_bus)
1015{
1016 int i;
1017
1018 for (i = 0; i < RADEON_MAX_I2C_BUS; i++) {
1019 if (rdev->i2c_bus[i] &&
1020 (rdev->i2c_bus[i]->rec.i2c_id == i2c_bus->i2c_id)) {
1021 return rdev->i2c_bus[i];
1022 }
1023 }
1024 return NULL;
1025}
1026
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001027struct drm_encoder *radeon_best_encoder(struct drm_connector *connector)
1028{
1029 return NULL;
1030}
Alex Deucherfcec5702009-11-10 21:25:07 -05001031
Alex Deucher5a6f98f2009-12-22 15:04:48 -05001032void radeon_i2c_get_byte(struct radeon_i2c_chan *i2c_bus,
1033 u8 slave_addr,
1034 u8 addr,
1035 u8 *val)
Alex Deucherfcec5702009-11-10 21:25:07 -05001036{
1037 u8 out_buf[2];
1038 u8 in_buf[2];
1039 struct i2c_msg msgs[] = {
1040 {
1041 .addr = slave_addr,
1042 .flags = 0,
1043 .len = 1,
1044 .buf = out_buf,
1045 },
1046 {
1047 .addr = slave_addr,
1048 .flags = I2C_M_RD,
1049 .len = 1,
1050 .buf = in_buf,
1051 }
1052 };
1053
1054 out_buf[0] = addr;
1055 out_buf[1] = 0;
1056
1057 if (i2c_transfer(&i2c_bus->adapter, msgs, 2) == 2) {
1058 *val = in_buf[0];
1059 DRM_DEBUG("val = 0x%02x\n", *val);
1060 } else {
1061 DRM_ERROR("i2c 0x%02x 0x%02x read failed\n",
1062 addr, *val);
1063 }
1064}
1065
Alex Deucher5a6f98f2009-12-22 15:04:48 -05001066void radeon_i2c_put_byte(struct radeon_i2c_chan *i2c_bus,
1067 u8 slave_addr,
1068 u8 addr,
1069 u8 val)
Alex Deucherfcec5702009-11-10 21:25:07 -05001070{
1071 uint8_t out_buf[2];
1072 struct i2c_msg msg = {
1073 .addr = slave_addr,
1074 .flags = 0,
1075 .len = 2,
1076 .buf = out_buf,
1077 };
1078
1079 out_buf[0] = addr;
1080 out_buf[1] = val;
1081
1082 if (i2c_transfer(&i2c_bus->adapter, &msg, 1) != 1)
1083 DRM_ERROR("i2c 0x%02x 0x%02x write failed\n",
1084 addr, val);
1085}
1086
Alex Deucherfb939df2010-11-08 16:08:29 +00001087/* ddc router switching */
1088void radeon_router_select_ddc_port(struct radeon_connector *radeon_connector)
Alex Deucher26b5bc92010-08-05 21:21:18 -04001089{
1090 u8 val;
1091
Alex Deucherfb939df2010-11-08 16:08:29 +00001092 if (!radeon_connector->router.ddc_valid)
Alex Deucher26b5bc92010-08-05 21:21:18 -04001093 return;
1094
1095 radeon_i2c_get_byte(radeon_connector->router_bus,
1096 radeon_connector->router.i2c_addr,
1097 0x3, &val);
Alex Deucherfb939df2010-11-08 16:08:29 +00001098 val &= radeon_connector->router.ddc_mux_control_pin;
Alex Deucher26b5bc92010-08-05 21:21:18 -04001099 radeon_i2c_put_byte(radeon_connector->router_bus,
1100 radeon_connector->router.i2c_addr,
1101 0x3, val);
1102 radeon_i2c_get_byte(radeon_connector->router_bus,
1103 radeon_connector->router.i2c_addr,
1104 0x1, &val);
Alex Deucherfb939df2010-11-08 16:08:29 +00001105 val &= radeon_connector->router.ddc_mux_control_pin;
1106 val |= radeon_connector->router.ddc_mux_state;
1107 radeon_i2c_put_byte(radeon_connector->router_bus,
1108 radeon_connector->router.i2c_addr,
1109 0x1, val);
1110}
1111
1112/* clock/data router switching */
1113void radeon_router_select_cd_port(struct radeon_connector *radeon_connector)
1114{
1115 u8 val;
1116
1117 if (!radeon_connector->router.cd_valid)
1118 return;
1119
1120 radeon_i2c_get_byte(radeon_connector->router_bus,
1121 radeon_connector->router.i2c_addr,
1122 0x3, &val);
1123 val &= radeon_connector->router.cd_mux_control_pin;
1124 radeon_i2c_put_byte(radeon_connector->router_bus,
1125 radeon_connector->router.i2c_addr,
1126 0x3, val);
1127 radeon_i2c_get_byte(radeon_connector->router_bus,
1128 radeon_connector->router.i2c_addr,
1129 0x1, &val);
1130 val &= radeon_connector->router.cd_mux_control_pin;
1131 val |= radeon_connector->router.cd_mux_state;
Alex Deucher26b5bc92010-08-05 21:21:18 -04001132 radeon_i2c_put_byte(radeon_connector->router_bus,
1133 radeon_connector->router.i2c_addr,
1134 0x1, val);
1135}
1136