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
27 #include <linux/export.h>
28 #include <linux/pci.h>
30 #include <drm/drm_device.h>
31 #include <drm/drm_edid.h>
32 #include <drm/radeon_drm.h>
37 bool radeon_ddc_probe(struct radeon_connector
*radeon_connector
, bool use_aux
)
42 struct i2c_msg msgs
[] = {
57 /* on hw with routers, select right port */
58 if (radeon_connector
->router
.ddc_valid
)
59 radeon_router_select_ddc_port(radeon_connector
);
62 ret
= i2c_transfer(&radeon_connector
->ddc_bus
->aux
.ddc
, msgs
, 2);
64 ret
= i2c_transfer(&radeon_connector
->ddc_bus
->adapter
, msgs
, 2);
68 /* Couldn't find an accessible DDC on this connector */
70 /* Probe also for valid EDID header
71 * EDID header starts with:
72 * 0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00.
73 * Only the first 6 bytes must be valid as
74 * drm_edid_block_valid() can fix the last 2 bytes */
75 if (drm_edid_header_is_valid(buf
) < 6) {
76 /* Couldn't find an accessible EDID on this
85 static int pre_xfer(struct i2c_adapter
*i2c_adap
)
87 struct radeon_i2c_chan
*i2c
= i2c_get_adapdata(i2c_adap
);
88 struct radeon_device
*rdev
= i2c
->dev
->dev_private
;
89 struct radeon_i2c_bus_rec
*rec
= &i2c
->rec
;
92 mutex_lock(&i2c
->mutex
);
94 /* RV410 appears to have a bug where the hw i2c in reset
95 * holds the i2c port in a bad state - switch hw i2c away before
96 * doing DDC - do this for all r200s/r300s/r400s for safety sake
98 if (rec
->hw_capable
) {
99 if ((rdev
->family
>= CHIP_R200
) && !ASIC_IS_AVIVO(rdev
)) {
102 if (rdev
->family
>= CHIP_RV350
)
103 reg
= RADEON_GPIO_MONID
;
104 else if ((rdev
->family
== CHIP_R300
) ||
105 (rdev
->family
== CHIP_R350
))
106 reg
= RADEON_GPIO_DVI_DDC
;
108 reg
= RADEON_GPIO_CRT2_DDC
;
110 mutex_lock(&rdev
->dc_hw_i2c_mutex
);
111 if (rec
->a_clk_reg
== reg
) {
112 WREG32(RADEON_DVI_I2C_CNTL_0
, (RADEON_I2C_SOFT_RST
|
113 R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1
)));
115 WREG32(RADEON_DVI_I2C_CNTL_0
, (RADEON_I2C_SOFT_RST
|
116 R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3
)));
118 mutex_unlock(&rdev
->dc_hw_i2c_mutex
);
122 /* switch the pads to ddc mode */
123 if (ASIC_IS_DCE3(rdev
) && rec
->hw_capable
) {
124 temp
= RREG32(rec
->mask_clk_reg
);
126 WREG32(rec
->mask_clk_reg
, temp
);
129 /* clear the output pin values */
130 temp
= RREG32(rec
->a_clk_reg
) & ~rec
->a_clk_mask
;
131 WREG32(rec
->a_clk_reg
, temp
);
133 temp
= RREG32(rec
->a_data_reg
) & ~rec
->a_data_mask
;
134 WREG32(rec
->a_data_reg
, temp
);
136 /* set the pins to input */
137 temp
= RREG32(rec
->en_clk_reg
) & ~rec
->en_clk_mask
;
138 WREG32(rec
->en_clk_reg
, temp
);
140 temp
= RREG32(rec
->en_data_reg
) & ~rec
->en_data_mask
;
141 WREG32(rec
->en_data_reg
, temp
);
143 /* mask the gpio pins for software use */
144 temp
= RREG32(rec
->mask_clk_reg
) | rec
->mask_clk_mask
;
145 WREG32(rec
->mask_clk_reg
, temp
);
146 temp
= RREG32(rec
->mask_clk_reg
);
148 temp
= RREG32(rec
->mask_data_reg
) | rec
->mask_data_mask
;
149 WREG32(rec
->mask_data_reg
, temp
);
150 temp
= RREG32(rec
->mask_data_reg
);
155 static void post_xfer(struct i2c_adapter
*i2c_adap
)
157 struct radeon_i2c_chan
*i2c
= i2c_get_adapdata(i2c_adap
);
158 struct radeon_device
*rdev
= i2c
->dev
->dev_private
;
159 struct radeon_i2c_bus_rec
*rec
= &i2c
->rec
;
162 /* unmask the gpio pins for software use */
163 temp
= RREG32(rec
->mask_clk_reg
) & ~rec
->mask_clk_mask
;
164 WREG32(rec
->mask_clk_reg
, temp
);
165 temp
= RREG32(rec
->mask_clk_reg
);
167 temp
= RREG32(rec
->mask_data_reg
) & ~rec
->mask_data_mask
;
168 WREG32(rec
->mask_data_reg
, temp
);
169 temp
= RREG32(rec
->mask_data_reg
);
171 mutex_unlock(&i2c
->mutex
);
174 static int get_clock(void *i2c_priv
)
176 struct radeon_i2c_chan
*i2c
= i2c_priv
;
177 struct radeon_device
*rdev
= i2c
->dev
->dev_private
;
178 struct radeon_i2c_bus_rec
*rec
= &i2c
->rec
;
181 /* read the value off the pin */
182 val
= RREG32(rec
->y_clk_reg
);
183 val
&= rec
->y_clk_mask
;
189 static int get_data(void *i2c_priv
)
191 struct radeon_i2c_chan
*i2c
= i2c_priv
;
192 struct radeon_device
*rdev
= i2c
->dev
->dev_private
;
193 struct radeon_i2c_bus_rec
*rec
= &i2c
->rec
;
196 /* read the value off the pin */
197 val
= RREG32(rec
->y_data_reg
);
198 val
&= rec
->y_data_mask
;
203 static void set_clock(void *i2c_priv
, int clock
)
205 struct radeon_i2c_chan
*i2c
= i2c_priv
;
206 struct radeon_device
*rdev
= i2c
->dev
->dev_private
;
207 struct radeon_i2c_bus_rec
*rec
= &i2c
->rec
;
210 /* set pin direction */
211 val
= RREG32(rec
->en_clk_reg
) & ~rec
->en_clk_mask
;
212 val
|= clock
? 0 : rec
->en_clk_mask
;
213 WREG32(rec
->en_clk_reg
, val
);
216 static void set_data(void *i2c_priv
, int data
)
218 struct radeon_i2c_chan
*i2c
= i2c_priv
;
219 struct radeon_device
*rdev
= i2c
->dev
->dev_private
;
220 struct radeon_i2c_bus_rec
*rec
= &i2c
->rec
;
223 /* set pin direction */
224 val
= RREG32(rec
->en_data_reg
) & ~rec
->en_data_mask
;
225 val
|= data
? 0 : rec
->en_data_mask
;
226 WREG32(rec
->en_data_reg
, val
);
231 static u32
radeon_get_i2c_prescale(struct radeon_device
*rdev
)
233 u32 sclk
= rdev
->pm
.current_sclk
;
239 switch (rdev
->family
) {
253 nm
= (sclk
* 10) / (i2c_clock
* 4);
254 for (loop
= 1; loop
< 255; loop
++) {
255 if ((nm
/ loop
) < loop
)
260 prescale
= m
| (n
<< 8);
268 prescale
= (((sclk
* 10)/(4 * 128 * 100) + 1) << 8) + 128;
282 if (rdev
->family
== CHIP_R520
)
283 prescale
= (127 << 8) + ((sclk
* 10) / (4 * 127 * i2c_clock
));
285 prescale
= (((sclk
* 10)/(4 * 128 * 100) + 1) << 8) + 128;
311 DRM_ERROR("i2c: unhandled radeon chip\n");
318 /* hw i2c engine for r1xx-4xx hardware
319 * hw can buffer up to 15 bytes
321 static int r100_hw_i2c_xfer(struct i2c_adapter
*i2c_adap
,
322 struct i2c_msg
*msgs
, int num
)
324 struct radeon_i2c_chan
*i2c
= i2c_get_adapdata(i2c_adap
);
325 struct radeon_device
*rdev
= i2c
->dev
->dev_private
;
326 struct radeon_i2c_bus_rec
*rec
= &i2c
->rec
;
328 int i
, j
, k
, ret
= num
;
330 u32 i2c_cntl_0
, i2c_cntl_1
, i2c_data
;
333 mutex_lock(&rdev
->dc_hw_i2c_mutex
);
334 /* take the pm lock since we need a constant sclk */
335 mutex_lock(&rdev
->pm
.mutex
);
337 prescale
= radeon_get_i2c_prescale(rdev
);
339 reg
= ((prescale
<< RADEON_I2C_PRESCALE_SHIFT
) |
340 RADEON_I2C_DRIVE_EN
|
345 if (rdev
->is_atom_bios
) {
346 tmp
= RREG32(RADEON_BIOS_6_SCRATCH
);
347 WREG32(RADEON_BIOS_6_SCRATCH
, tmp
| ATOM_S6_HW_I2C_BUSY_STATE
);
351 i2c_cntl_0
= RADEON_I2C_CNTL_0
;
352 i2c_cntl_1
= RADEON_I2C_CNTL_1
;
353 i2c_data
= RADEON_I2C_DATA
;
355 i2c_cntl_0
= RADEON_DVI_I2C_CNTL_0
;
356 i2c_cntl_1
= RADEON_DVI_I2C_CNTL_1
;
357 i2c_data
= RADEON_DVI_I2C_DATA
;
359 switch (rdev
->family
) {
366 switch (rec
->mask_clk_reg
) {
367 case RADEON_GPIO_DVI_DDC
:
368 /* no gpio select bit */
371 DRM_ERROR("gpio not supported with hw i2c\n");
377 /* only bit 4 on r200 */
378 switch (rec
->mask_clk_reg
) {
379 case RADEON_GPIO_DVI_DDC
:
380 reg
|= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1
);
382 case RADEON_GPIO_MONID
:
383 reg
|= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3
);
386 DRM_ERROR("gpio not supported with hw i2c\n");
394 switch (rec
->mask_clk_reg
) {
395 case RADEON_GPIO_DVI_DDC
:
396 reg
|= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1
);
398 case RADEON_GPIO_VGA_DDC
:
399 reg
|= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC2
);
401 case RADEON_GPIO_CRT2_DDC
:
402 reg
|= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3
);
405 DRM_ERROR("gpio not supported with hw i2c\n");
412 /* only bit 4 on r300/r350 */
413 switch (rec
->mask_clk_reg
) {
414 case RADEON_GPIO_VGA_DDC
:
415 reg
|= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1
);
417 case RADEON_GPIO_DVI_DDC
:
418 reg
|= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3
);
421 DRM_ERROR("gpio not supported with hw i2c\n");
434 switch (rec
->mask_clk_reg
) {
435 case RADEON_GPIO_VGA_DDC
:
436 reg
|= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1
);
438 case RADEON_GPIO_DVI_DDC
:
439 reg
|= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC2
);
441 case RADEON_GPIO_MONID
:
442 reg
|= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3
);
445 DRM_ERROR("gpio not supported with hw i2c\n");
451 DRM_ERROR("unsupported asic\n");
458 /* check for bus probe */
460 if ((num
== 1) && (p
->len
== 0)) {
461 WREG32(i2c_cntl_0
, (RADEON_I2C_DONE
|
464 RADEON_I2C_SOFT_RST
));
465 WREG32(i2c_data
, (p
->addr
<< 1) & 0xff);
467 WREG32(i2c_cntl_1
, ((1 << RADEON_I2C_DATA_COUNT_SHIFT
) |
468 (1 << RADEON_I2C_ADDR_COUNT_SHIFT
) |
470 (48 << RADEON_I2C_TIME_LIMIT_SHIFT
)));
471 WREG32(i2c_cntl_0
, reg
);
472 for (k
= 0; k
< 32; k
++) {
474 tmp
= RREG32(i2c_cntl_0
);
475 if (tmp
& RADEON_I2C_GO
)
477 tmp
= RREG32(i2c_cntl_0
);
478 if (tmp
& RADEON_I2C_DONE
)
481 DRM_DEBUG("i2c write error 0x%08x\n", tmp
);
482 WREG32(i2c_cntl_0
, tmp
| RADEON_I2C_ABORT
);
490 for (i
= 0; i
< num
; i
++) {
492 for (j
= 0; j
< p
->len
; j
++) {
493 if (p
->flags
& I2C_M_RD
) {
494 WREG32(i2c_cntl_0
, (RADEON_I2C_DONE
|
497 RADEON_I2C_SOFT_RST
));
498 WREG32(i2c_data
, ((p
->addr
<< 1) & 0xff) | 0x1);
499 WREG32(i2c_cntl_1
, ((1 << RADEON_I2C_DATA_COUNT_SHIFT
) |
500 (1 << RADEON_I2C_ADDR_COUNT_SHIFT
) |
502 (48 << RADEON_I2C_TIME_LIMIT_SHIFT
)));
503 WREG32(i2c_cntl_0
, reg
| RADEON_I2C_RECEIVE
);
504 for (k
= 0; k
< 32; k
++) {
506 tmp
= RREG32(i2c_cntl_0
);
507 if (tmp
& RADEON_I2C_GO
)
509 tmp
= RREG32(i2c_cntl_0
);
510 if (tmp
& RADEON_I2C_DONE
)
513 DRM_DEBUG("i2c read error 0x%08x\n", tmp
);
514 WREG32(i2c_cntl_0
, tmp
| RADEON_I2C_ABORT
);
519 p
->buf
[j
] = RREG32(i2c_data
) & 0xff;
521 WREG32(i2c_cntl_0
, (RADEON_I2C_DONE
|
524 RADEON_I2C_SOFT_RST
));
525 WREG32(i2c_data
, (p
->addr
<< 1) & 0xff);
526 WREG32(i2c_data
, p
->buf
[j
]);
527 WREG32(i2c_cntl_1
, ((1 << RADEON_I2C_DATA_COUNT_SHIFT
) |
528 (1 << RADEON_I2C_ADDR_COUNT_SHIFT
) |
530 (48 << RADEON_I2C_TIME_LIMIT_SHIFT
)));
531 WREG32(i2c_cntl_0
, reg
);
532 for (k
= 0; k
< 32; k
++) {
534 tmp
= RREG32(i2c_cntl_0
);
535 if (tmp
& RADEON_I2C_GO
)
537 tmp
= RREG32(i2c_cntl_0
);
538 if (tmp
& RADEON_I2C_DONE
)
541 DRM_DEBUG("i2c write error 0x%08x\n", tmp
);
542 WREG32(i2c_cntl_0
, tmp
| RADEON_I2C_ABORT
);
552 WREG32(i2c_cntl_0
, 0);
553 WREG32(i2c_cntl_1
, 0);
554 WREG32(i2c_cntl_0
, (RADEON_I2C_DONE
|
557 RADEON_I2C_SOFT_RST
));
559 if (rdev
->is_atom_bios
) {
560 tmp
= RREG32(RADEON_BIOS_6_SCRATCH
);
561 tmp
&= ~ATOM_S6_HW_I2C_BUSY_STATE
;
562 WREG32(RADEON_BIOS_6_SCRATCH
, tmp
);
565 mutex_unlock(&rdev
->pm
.mutex
);
566 mutex_unlock(&rdev
->dc_hw_i2c_mutex
);
571 /* hw i2c engine for r5xx hardware
572 * hw can buffer up to 15 bytes
574 static int r500_hw_i2c_xfer(struct i2c_adapter
*i2c_adap
,
575 struct i2c_msg
*msgs
, int num
)
577 struct radeon_i2c_chan
*i2c
= i2c_get_adapdata(i2c_adap
);
578 struct radeon_device
*rdev
= i2c
->dev
->dev_private
;
579 struct radeon_i2c_bus_rec
*rec
= &i2c
->rec
;
581 int i
, j
, remaining
, current_count
, buffer_offset
, ret
= num
;
586 mutex_lock(&rdev
->dc_hw_i2c_mutex
);
587 /* take the pm lock since we need a constant sclk */
588 mutex_lock(&rdev
->pm
.mutex
);
590 prescale
= radeon_get_i2c_prescale(rdev
);
592 /* clear gpio mask bits */
593 tmp
= RREG32(rec
->mask_clk_reg
);
594 tmp
&= ~rec
->mask_clk_mask
;
595 WREG32(rec
->mask_clk_reg
, tmp
);
596 tmp
= RREG32(rec
->mask_clk_reg
);
598 tmp
= RREG32(rec
->mask_data_reg
);
599 tmp
&= ~rec
->mask_data_mask
;
600 WREG32(rec
->mask_data_reg
, tmp
);
601 tmp
= RREG32(rec
->mask_data_reg
);
603 /* clear pin values */
604 tmp
= RREG32(rec
->a_clk_reg
);
605 tmp
&= ~rec
->a_clk_mask
;
606 WREG32(rec
->a_clk_reg
, tmp
);
607 tmp
= RREG32(rec
->a_clk_reg
);
609 tmp
= RREG32(rec
->a_data_reg
);
610 tmp
&= ~rec
->a_data_mask
;
611 WREG32(rec
->a_data_reg
, tmp
);
612 tmp
= RREG32(rec
->a_data_reg
);
614 /* set the pins to input */
615 tmp
= RREG32(rec
->en_clk_reg
);
616 tmp
&= ~rec
->en_clk_mask
;
617 WREG32(rec
->en_clk_reg
, tmp
);
618 tmp
= RREG32(rec
->en_clk_reg
);
620 tmp
= RREG32(rec
->en_data_reg
);
621 tmp
&= ~rec
->en_data_mask
;
622 WREG32(rec
->en_data_reg
, tmp
);
623 tmp
= RREG32(rec
->en_data_reg
);
626 tmp
= RREG32(RADEON_BIOS_6_SCRATCH
);
627 WREG32(RADEON_BIOS_6_SCRATCH
, tmp
| ATOM_S6_HW_I2C_BUSY_STATE
);
628 saved1
= RREG32(AVIVO_DC_I2C_CONTROL1
);
629 saved2
= RREG32(0x494);
630 WREG32(0x494, saved2
| 0x1);
632 WREG32(AVIVO_DC_I2C_ARBITRATION
, AVIVO_DC_I2C_SW_WANTS_TO_USE_I2C
);
633 for (i
= 0; i
< 50; i
++) {
635 if (RREG32(AVIVO_DC_I2C_ARBITRATION
) & AVIVO_DC_I2C_SW_CAN_USE_I2C
)
639 DRM_ERROR("failed to get i2c bus\n");
644 reg
= AVIVO_DC_I2C_START
| AVIVO_DC_I2C_STOP
| AVIVO_DC_I2C_EN
;
645 switch (rec
->mask_clk_reg
) {
646 case AVIVO_DC_GPIO_DDC1_MASK
:
647 reg
|= AVIVO_DC_I2C_PIN_SELECT(AVIVO_SEL_DDC1
);
649 case AVIVO_DC_GPIO_DDC2_MASK
:
650 reg
|= AVIVO_DC_I2C_PIN_SELECT(AVIVO_SEL_DDC2
);
652 case AVIVO_DC_GPIO_DDC3_MASK
:
653 reg
|= AVIVO_DC_I2C_PIN_SELECT(AVIVO_SEL_DDC3
);
656 DRM_ERROR("gpio not supported with hw i2c\n");
661 /* check for bus probe */
663 if ((num
== 1) && (p
->len
== 0)) {
664 WREG32(AVIVO_DC_I2C_STATUS1
, (AVIVO_DC_I2C_DONE
|
667 WREG32(AVIVO_DC_I2C_RESET
, AVIVO_DC_I2C_SOFT_RESET
);
669 WREG32(AVIVO_DC_I2C_RESET
, 0);
671 WREG32(AVIVO_DC_I2C_DATA
, (p
->addr
<< 1) & 0xff);
672 WREG32(AVIVO_DC_I2C_DATA
, 0);
674 WREG32(AVIVO_DC_I2C_CONTROL3
, AVIVO_DC_I2C_TIME_LIMIT(48));
675 WREG32(AVIVO_DC_I2C_CONTROL2
, (AVIVO_DC_I2C_ADDR_COUNT(1) |
676 AVIVO_DC_I2C_DATA_COUNT(1) |
678 WREG32(AVIVO_DC_I2C_CONTROL1
, reg
);
679 WREG32(AVIVO_DC_I2C_STATUS1
, AVIVO_DC_I2C_GO
);
680 for (j
= 0; j
< 200; j
++) {
682 tmp
= RREG32(AVIVO_DC_I2C_STATUS1
);
683 if (tmp
& AVIVO_DC_I2C_GO
)
685 tmp
= RREG32(AVIVO_DC_I2C_STATUS1
);
686 if (tmp
& AVIVO_DC_I2C_DONE
)
689 DRM_DEBUG("i2c write error 0x%08x\n", tmp
);
690 WREG32(AVIVO_DC_I2C_RESET
, AVIVO_DC_I2C_ABORT
);
698 for (i
= 0; i
< num
; i
++) {
702 if (p
->flags
& I2C_M_RD
) {
707 current_count
= remaining
;
708 WREG32(AVIVO_DC_I2C_STATUS1
, (AVIVO_DC_I2C_DONE
|
711 WREG32(AVIVO_DC_I2C_RESET
, AVIVO_DC_I2C_SOFT_RESET
);
713 WREG32(AVIVO_DC_I2C_RESET
, 0);
715 WREG32(AVIVO_DC_I2C_DATA
, ((p
->addr
<< 1) & 0xff) | 0x1);
716 WREG32(AVIVO_DC_I2C_CONTROL3
, AVIVO_DC_I2C_TIME_LIMIT(48));
717 WREG32(AVIVO_DC_I2C_CONTROL2
, (AVIVO_DC_I2C_ADDR_COUNT(1) |
718 AVIVO_DC_I2C_DATA_COUNT(current_count
) |
720 WREG32(AVIVO_DC_I2C_CONTROL1
, reg
| AVIVO_DC_I2C_RECEIVE
);
721 WREG32(AVIVO_DC_I2C_STATUS1
, AVIVO_DC_I2C_GO
);
722 for (j
= 0; j
< 200; j
++) {
724 tmp
= RREG32(AVIVO_DC_I2C_STATUS1
);
725 if (tmp
& AVIVO_DC_I2C_GO
)
727 tmp
= RREG32(AVIVO_DC_I2C_STATUS1
);
728 if (tmp
& AVIVO_DC_I2C_DONE
)
731 DRM_DEBUG("i2c read error 0x%08x\n", tmp
);
732 WREG32(AVIVO_DC_I2C_RESET
, AVIVO_DC_I2C_ABORT
);
737 for (j
= 0; j
< current_count
; j
++)
738 p
->buf
[buffer_offset
+ j
] = RREG32(AVIVO_DC_I2C_DATA
) & 0xff;
739 remaining
-= current_count
;
740 buffer_offset
+= current_count
;
747 current_count
= remaining
;
748 WREG32(AVIVO_DC_I2C_STATUS1
, (AVIVO_DC_I2C_DONE
|
751 WREG32(AVIVO_DC_I2C_RESET
, AVIVO_DC_I2C_SOFT_RESET
);
753 WREG32(AVIVO_DC_I2C_RESET
, 0);
755 WREG32(AVIVO_DC_I2C_DATA
, (p
->addr
<< 1) & 0xff);
756 for (j
= 0; j
< current_count
; j
++)
757 WREG32(AVIVO_DC_I2C_DATA
, p
->buf
[buffer_offset
+ j
]);
759 WREG32(AVIVO_DC_I2C_CONTROL3
, AVIVO_DC_I2C_TIME_LIMIT(48));
760 WREG32(AVIVO_DC_I2C_CONTROL2
, (AVIVO_DC_I2C_ADDR_COUNT(1) |
761 AVIVO_DC_I2C_DATA_COUNT(current_count
) |
763 WREG32(AVIVO_DC_I2C_CONTROL1
, reg
);
764 WREG32(AVIVO_DC_I2C_STATUS1
, AVIVO_DC_I2C_GO
);
765 for (j
= 0; j
< 200; j
++) {
767 tmp
= RREG32(AVIVO_DC_I2C_STATUS1
);
768 if (tmp
& AVIVO_DC_I2C_GO
)
770 tmp
= RREG32(AVIVO_DC_I2C_STATUS1
);
771 if (tmp
& AVIVO_DC_I2C_DONE
)
774 DRM_DEBUG("i2c write error 0x%08x\n", tmp
);
775 WREG32(AVIVO_DC_I2C_RESET
, AVIVO_DC_I2C_ABORT
);
780 remaining
-= current_count
;
781 buffer_offset
+= current_count
;
787 WREG32(AVIVO_DC_I2C_STATUS1
, (AVIVO_DC_I2C_DONE
|
790 WREG32(AVIVO_DC_I2C_RESET
, AVIVO_DC_I2C_SOFT_RESET
);
792 WREG32(AVIVO_DC_I2C_RESET
, 0);
794 WREG32(AVIVO_DC_I2C_ARBITRATION
, AVIVO_DC_I2C_SW_DONE_USING_I2C
);
795 WREG32(AVIVO_DC_I2C_CONTROL1
, saved1
);
796 WREG32(0x494, saved2
);
797 tmp
= RREG32(RADEON_BIOS_6_SCRATCH
);
798 tmp
&= ~ATOM_S6_HW_I2C_BUSY_STATE
;
799 WREG32(RADEON_BIOS_6_SCRATCH
, tmp
);
801 mutex_unlock(&rdev
->pm
.mutex
);
802 mutex_unlock(&rdev
->dc_hw_i2c_mutex
);
807 static int radeon_hw_i2c_xfer(struct i2c_adapter
*i2c_adap
,
808 struct i2c_msg
*msgs
, int num
)
810 struct radeon_i2c_chan
*i2c
= i2c_get_adapdata(i2c_adap
);
811 struct radeon_device
*rdev
= i2c
->dev
->dev_private
;
812 struct radeon_i2c_bus_rec
*rec
= &i2c
->rec
;
815 mutex_lock(&i2c
->mutex
);
817 switch (rdev
->family
) {
836 ret
= r100_hw_i2c_xfer(i2c_adap
, msgs
, num
);
841 /* XXX fill in hw i2c implementation */
850 ret
= r100_hw_i2c_xfer(i2c_adap
, msgs
, num
);
852 ret
= r500_hw_i2c_xfer(i2c_adap
, msgs
, num
);
858 /* XXX fill in hw i2c implementation */
868 /* XXX fill in hw i2c implementation */
875 /* XXX fill in hw i2c implementation */
878 DRM_ERROR("i2c: unhandled radeon chip\n");
883 mutex_unlock(&i2c
->mutex
);
888 static u32
radeon_hw_i2c_func(struct i2c_adapter
*adap
)
890 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
893 static const struct i2c_algorithm radeon_i2c_algo
= {
894 .master_xfer
= radeon_hw_i2c_xfer
,
895 .functionality
= radeon_hw_i2c_func
,
898 static const struct i2c_algorithm radeon_atom_i2c_algo
= {
899 .master_xfer
= radeon_atom_hw_i2c_xfer
,
900 .functionality
= radeon_atom_hw_i2c_func
,
903 struct radeon_i2c_chan
*radeon_i2c_create(struct drm_device
*dev
,
904 struct radeon_i2c_bus_rec
*rec
,
907 struct radeon_device
*rdev
= dev
->dev_private
;
908 struct radeon_i2c_chan
*i2c
;
911 /* don't add the mm_i2c bus unless hw_i2c is enabled */
912 if (rec
->mm_i2c
&& (radeon_hw_i2c
== 0))
915 i2c
= kzalloc(sizeof(struct radeon_i2c_chan
), GFP_KERNEL
);
920 i2c
->adapter
.owner
= THIS_MODULE
;
921 i2c
->adapter
.class = I2C_CLASS_DDC
;
922 i2c
->adapter
.dev
.parent
= &dev
->pdev
->dev
;
924 i2c_set_adapdata(&i2c
->adapter
, i2c
);
925 mutex_init(&i2c
->mutex
);
929 ((rdev
->family
<= CHIP_RS480
) ||
930 ((rdev
->family
>= CHIP_RV515
) && (rdev
->family
<= CHIP_R580
))))) {
931 /* set the radeon hw i2c adapter */
932 snprintf(i2c
->adapter
.name
, sizeof(i2c
->adapter
.name
),
933 "Radeon i2c hw bus %s", name
);
934 i2c
->adapter
.algo
= &radeon_i2c_algo
;
935 ret
= i2c_add_adapter(&i2c
->adapter
);
938 } else if (rec
->hw_capable
&&
940 ASIC_IS_DCE3(rdev
)) {
941 /* hw i2c using atom */
942 snprintf(i2c
->adapter
.name
, sizeof(i2c
->adapter
.name
),
943 "Radeon i2c hw bus %s", name
);
944 i2c
->adapter
.algo
= &radeon_atom_i2c_algo
;
945 ret
= i2c_add_adapter(&i2c
->adapter
);
949 /* set the radeon bit adapter */
950 snprintf(i2c
->adapter
.name
, sizeof(i2c
->adapter
.name
),
951 "Radeon i2c bit bus %s", name
);
952 i2c
->adapter
.algo_data
= &i2c
->bit
;
953 i2c
->bit
.pre_xfer
= pre_xfer
;
954 i2c
->bit
.post_xfer
= post_xfer
;
955 i2c
->bit
.setsda
= set_data
;
956 i2c
->bit
.setscl
= set_clock
;
957 i2c
->bit
.getsda
= get_data
;
958 i2c
->bit
.getscl
= get_clock
;
959 i2c
->bit
.udelay
= 10;
960 i2c
->bit
.timeout
= usecs_to_jiffies(2200); /* from VESA */
962 ret
= i2c_bit_add_bus(&i2c
->adapter
);
964 DRM_ERROR("Failed to register bit i2c %s\n", name
);
976 void radeon_i2c_destroy(struct radeon_i2c_chan
*i2c
)
980 WARN_ON(i2c
->has_aux
);
981 i2c_del_adapter(&i2c
->adapter
);
985 /* Add the default buses */
986 void radeon_i2c_init(struct radeon_device
*rdev
)
989 DRM_INFO("hw_i2c forced on, you may experience display detection problems!\n");
991 if (rdev
->is_atom_bios
)
992 radeon_atombios_i2c_init(rdev
);
994 radeon_combios_i2c_init(rdev
);
997 /* remove all the buses */
998 void radeon_i2c_fini(struct radeon_device
*rdev
)
1002 for (i
= 0; i
< RADEON_MAX_I2C_BUS
; i
++) {
1003 if (rdev
->i2c_bus
[i
]) {
1004 radeon_i2c_destroy(rdev
->i2c_bus
[i
]);
1005 rdev
->i2c_bus
[i
] = NULL
;
1010 /* Add additional buses */
1011 void radeon_i2c_add(struct radeon_device
*rdev
,
1012 struct radeon_i2c_bus_rec
*rec
,
1015 struct drm_device
*dev
= rdev
->ddev
;
1018 for (i
= 0; i
< RADEON_MAX_I2C_BUS
; i
++) {
1019 if (!rdev
->i2c_bus
[i
]) {
1020 rdev
->i2c_bus
[i
] = radeon_i2c_create(dev
, rec
, name
);
1026 /* looks up bus based on id */
1027 struct radeon_i2c_chan
*radeon_i2c_lookup(struct radeon_device
*rdev
,
1028 struct radeon_i2c_bus_rec
*i2c_bus
)
1032 for (i
= 0; i
< RADEON_MAX_I2C_BUS
; i
++) {
1033 if (rdev
->i2c_bus
[i
] &&
1034 (rdev
->i2c_bus
[i
]->rec
.i2c_id
== i2c_bus
->i2c_id
)) {
1035 return rdev
->i2c_bus
[i
];
1041 void radeon_i2c_get_byte(struct radeon_i2c_chan
*i2c_bus
,
1048 struct i2c_msg msgs
[] = {
1066 if (i2c_transfer(&i2c_bus
->adapter
, msgs
, 2) == 2) {
1068 DRM_DEBUG("val = 0x%02x\n", *val
);
1070 DRM_DEBUG("i2c 0x%02x 0x%02x read failed\n",
1075 void radeon_i2c_put_byte(struct radeon_i2c_chan
*i2c_bus
,
1081 struct i2c_msg msg
= {
1091 if (i2c_transfer(&i2c_bus
->adapter
, &msg
, 1) != 1)
1092 DRM_DEBUG("i2c 0x%02x 0x%02x write failed\n",
1096 /* ddc router switching */
1097 void radeon_router_select_ddc_port(struct radeon_connector
*radeon_connector
)
1101 if (!radeon_connector
->router
.ddc_valid
)
1104 if (!radeon_connector
->router_bus
)
1107 radeon_i2c_get_byte(radeon_connector
->router_bus
,
1108 radeon_connector
->router
.i2c_addr
,
1110 val
&= ~radeon_connector
->router
.ddc_mux_control_pin
;
1111 radeon_i2c_put_byte(radeon_connector
->router_bus
,
1112 radeon_connector
->router
.i2c_addr
,
1114 radeon_i2c_get_byte(radeon_connector
->router_bus
,
1115 radeon_connector
->router
.i2c_addr
,
1117 val
&= ~radeon_connector
->router
.ddc_mux_control_pin
;
1118 val
|= radeon_connector
->router
.ddc_mux_state
;
1119 radeon_i2c_put_byte(radeon_connector
->router_bus
,
1120 radeon_connector
->router
.i2c_addr
,
1124 /* clock/data router switching */
1125 void radeon_router_select_cd_port(struct radeon_connector
*radeon_connector
)
1129 if (!radeon_connector
->router
.cd_valid
)
1132 if (!radeon_connector
->router_bus
)
1135 radeon_i2c_get_byte(radeon_connector
->router_bus
,
1136 radeon_connector
->router
.i2c_addr
,
1138 val
&= ~radeon_connector
->router
.cd_mux_control_pin
;
1139 radeon_i2c_put_byte(radeon_connector
->router_bus
,
1140 radeon_connector
->router
.i2c_addr
,
1142 radeon_i2c_get_byte(radeon_connector
->router_bus
,
1143 radeon_connector
->router
.i2c_addr
,
1145 val
&= ~radeon_connector
->router
.cd_mux_control_pin
;
1146 val
|= radeon_connector
->router
.cd_mux_state
;
1147 radeon_i2c_put_byte(radeon_connector
->router_bus
,
1148 radeon_connector
->router
.i2c_addr
,