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
26 #include <linux/export.h>
29 #include <drm/drm_edid.h>
30 #include <drm/radeon_drm.h>
34 extern int radeon_atom_hw_i2c_xfer(struct i2c_adapter *i2c_adap,
35 struct i2c_msg *msgs, int num);
36 extern u32 radeon_atom_hw_i2c_func(struct i2c_adapter *adap);
42 bool radeon_ddc_probe(struct radeon_connector *radeon_connector, bool use_aux)
47 struct i2c_msg msgs[] = {
62 /* on hw with routers, select right port */
63 if (radeon_connector->router.ddc_valid)
64 radeon_router_select_ddc_port(radeon_connector);
67 ret = i2c_transfer(&radeon_connector->ddc_bus->aux.ddc, msgs, 2);
69 ret = i2c_transfer(&radeon_connector->ddc_bus->adapter, msgs, 2);
73 /* Couldn't find an accessible DDC on this connector */
75 /* Probe also for valid EDID header
76 * EDID header starts with:
77 * 0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00.
78 * Only the first 6 bytes must be valid as
79 * drm_edid_block_valid() can fix the last 2 bytes */
80 if (drm_edid_header_is_valid(buf) < 6) {
81 /* Couldn't find an accessible EDID on this
90 static int pre_xfer(struct i2c_adapter *i2c_adap)
92 struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
93 struct radeon_device *rdev = i2c->dev->dev_private;
94 struct radeon_i2c_bus_rec *rec = &i2c->rec;
97 mutex_lock(&i2c->mutex);
99 /* RV410 appears to have a bug where the hw i2c in reset
100 * holds the i2c port in a bad state - switch hw i2c away before
101 * doing DDC - do this for all r200s/r300s/r400s for safety sake
103 if (rec->hw_capable) {
104 if ((rdev->family >= CHIP_R200) && !ASIC_IS_AVIVO(rdev)) {
107 if (rdev->family >= CHIP_RV350)
108 reg = RADEON_GPIO_MONID;
109 else if ((rdev->family == CHIP_R300) ||
110 (rdev->family == CHIP_R350))
111 reg = RADEON_GPIO_DVI_DDC;
113 reg = RADEON_GPIO_CRT2_DDC;
115 mutex_lock(&rdev->dc_hw_i2c_mutex);
116 if (rec->a_clk_reg == reg) {
117 WREG32(RADEON_DVI_I2C_CNTL_0, (RADEON_I2C_SOFT_RST |
118 R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1)));
120 WREG32(RADEON_DVI_I2C_CNTL_0, (RADEON_I2C_SOFT_RST |
121 R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3)));
123 mutex_unlock(&rdev->dc_hw_i2c_mutex);
127 /* switch the pads to ddc mode */
128 if (ASIC_IS_DCE3(rdev) && rec->hw_capable) {
129 temp = RREG32(rec->mask_clk_reg);
131 WREG32(rec->mask_clk_reg, temp);
134 /* clear the output pin values */
135 temp = RREG32(rec->a_clk_reg) & ~rec->a_clk_mask;
136 WREG32(rec->a_clk_reg, temp);
138 temp = RREG32(rec->a_data_reg) & ~rec->a_data_mask;
139 WREG32(rec->a_data_reg, temp);
141 /* set the pins to input */
142 temp = RREG32(rec->en_clk_reg) & ~rec->en_clk_mask;
143 WREG32(rec->en_clk_reg, temp);
145 temp = RREG32(rec->en_data_reg) & ~rec->en_data_mask;
146 WREG32(rec->en_data_reg, temp);
148 /* mask the gpio pins for software use */
149 temp = RREG32(rec->mask_clk_reg) | rec->mask_clk_mask;
150 WREG32(rec->mask_clk_reg, temp);
151 temp = RREG32(rec->mask_clk_reg);
153 temp = RREG32(rec->mask_data_reg) | rec->mask_data_mask;
154 WREG32(rec->mask_data_reg, temp);
155 temp = RREG32(rec->mask_data_reg);
160 static void post_xfer(struct i2c_adapter *i2c_adap)
162 struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
163 struct radeon_device *rdev = i2c->dev->dev_private;
164 struct radeon_i2c_bus_rec *rec = &i2c->rec;
167 /* unmask the gpio pins for software use */
168 temp = RREG32(rec->mask_clk_reg) & ~rec->mask_clk_mask;
169 WREG32(rec->mask_clk_reg, temp);
170 temp = RREG32(rec->mask_clk_reg);
172 temp = RREG32(rec->mask_data_reg) & ~rec->mask_data_mask;
173 WREG32(rec->mask_data_reg, temp);
174 temp = RREG32(rec->mask_data_reg);
176 mutex_unlock(&i2c->mutex);
179 static int get_clock(void *i2c_priv)
181 struct radeon_i2c_chan *i2c = i2c_priv;
182 struct radeon_device *rdev = i2c->dev->dev_private;
183 struct radeon_i2c_bus_rec *rec = &i2c->rec;
186 /* read the value off the pin */
187 val = RREG32(rec->y_clk_reg);
188 val &= rec->y_clk_mask;
194 static int get_data(void *i2c_priv)
196 struct radeon_i2c_chan *i2c = i2c_priv;
197 struct radeon_device *rdev = i2c->dev->dev_private;
198 struct radeon_i2c_bus_rec *rec = &i2c->rec;
201 /* read the value off the pin */
202 val = RREG32(rec->y_data_reg);
203 val &= rec->y_data_mask;
208 static void set_clock(void *i2c_priv, int clock)
210 struct radeon_i2c_chan *i2c = i2c_priv;
211 struct radeon_device *rdev = i2c->dev->dev_private;
212 struct radeon_i2c_bus_rec *rec = &i2c->rec;
215 /* set pin direction */
216 val = RREG32(rec->en_clk_reg) & ~rec->en_clk_mask;
217 val |= clock ? 0 : rec->en_clk_mask;
218 WREG32(rec->en_clk_reg, val);
221 static void set_data(void *i2c_priv, int data)
223 struct radeon_i2c_chan *i2c = i2c_priv;
224 struct radeon_device *rdev = i2c->dev->dev_private;
225 struct radeon_i2c_bus_rec *rec = &i2c->rec;
228 /* set pin direction */
229 val = RREG32(rec->en_data_reg) & ~rec->en_data_mask;
230 val |= data ? 0 : rec->en_data_mask;
231 WREG32(rec->en_data_reg, val);
236 static u32 radeon_get_i2c_prescale(struct radeon_device *rdev)
238 u32 sclk = rdev->pm.current_sclk;
244 switch (rdev->family) {
258 nm = (sclk * 10) / (i2c_clock * 4);
259 for (loop = 1; loop < 255; loop++) {
260 if ((nm / loop) < loop)
265 prescale = m | (n << 8);
273 prescale = (((sclk * 10)/(4 * 128 * 100) + 1) << 8) + 128;
287 if (rdev->family == CHIP_R520)
288 prescale = (127 << 8) + ((sclk * 10) / (4 * 127 * i2c_clock));
290 prescale = (((sclk * 10)/(4 * 128 * 100) + 1) << 8) + 128;
316 DRM_ERROR("i2c: unhandled radeon chip\n");
323 /* hw i2c engine for r1xx-4xx hardware
324 * hw can buffer up to 15 bytes
326 static int r100_hw_i2c_xfer(struct i2c_adapter *i2c_adap,
327 struct i2c_msg *msgs, int num)
329 struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
330 struct radeon_device *rdev = i2c->dev->dev_private;
331 struct radeon_i2c_bus_rec *rec = &i2c->rec;
333 int i, j, k, ret = num;
335 u32 i2c_cntl_0, i2c_cntl_1, i2c_data;
338 mutex_lock(&rdev->dc_hw_i2c_mutex);
339 /* take the pm lock since we need a constant sclk */
340 mutex_lock(&rdev->pm.mutex);
342 prescale = radeon_get_i2c_prescale(rdev);
344 reg = ((prescale << RADEON_I2C_PRESCALE_SHIFT) |
345 RADEON_I2C_DRIVE_EN |
350 if (rdev->is_atom_bios) {
351 tmp = RREG32(RADEON_BIOS_6_SCRATCH);
352 WREG32(RADEON_BIOS_6_SCRATCH, tmp | ATOM_S6_HW_I2C_BUSY_STATE);
356 i2c_cntl_0 = RADEON_I2C_CNTL_0;
357 i2c_cntl_1 = RADEON_I2C_CNTL_1;
358 i2c_data = RADEON_I2C_DATA;
360 i2c_cntl_0 = RADEON_DVI_I2C_CNTL_0;
361 i2c_cntl_1 = RADEON_DVI_I2C_CNTL_1;
362 i2c_data = RADEON_DVI_I2C_DATA;
364 switch (rdev->family) {
371 switch (rec->mask_clk_reg) {
372 case RADEON_GPIO_DVI_DDC:
373 /* no gpio select bit */
376 DRM_ERROR("gpio not supported with hw i2c\n");
382 /* only bit 4 on r200 */
383 switch (rec->mask_clk_reg) {
384 case RADEON_GPIO_DVI_DDC:
385 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1);
387 case RADEON_GPIO_MONID:
388 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3);
391 DRM_ERROR("gpio not supported with hw i2c\n");
399 switch (rec->mask_clk_reg) {
400 case RADEON_GPIO_DVI_DDC:
401 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1);
403 case RADEON_GPIO_VGA_DDC:
404 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC2);
406 case RADEON_GPIO_CRT2_DDC:
407 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3);
410 DRM_ERROR("gpio not supported with hw i2c\n");
417 /* only bit 4 on r300/r350 */
418 switch (rec->mask_clk_reg) {
419 case RADEON_GPIO_VGA_DDC:
420 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1);
422 case RADEON_GPIO_DVI_DDC:
423 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3);
426 DRM_ERROR("gpio not supported with hw i2c\n");
439 switch (rec->mask_clk_reg) {
440 case RADEON_GPIO_VGA_DDC:
441 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1);
443 case RADEON_GPIO_DVI_DDC:
444 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC2);
446 case RADEON_GPIO_MONID:
447 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3);
450 DRM_ERROR("gpio not supported with hw i2c\n");
456 DRM_ERROR("unsupported asic\n");
463 /* check for bus probe */
465 if ((num == 1) && (p->len == 0)) {
466 WREG32(i2c_cntl_0, (RADEON_I2C_DONE |
469 RADEON_I2C_SOFT_RST));
470 WREG32(i2c_data, (p->addr << 1) & 0xff);
472 WREG32(i2c_cntl_1, ((1 << RADEON_I2C_DATA_COUNT_SHIFT) |
473 (1 << RADEON_I2C_ADDR_COUNT_SHIFT) |
475 (48 << RADEON_I2C_TIME_LIMIT_SHIFT)));
476 WREG32(i2c_cntl_0, reg);
477 for (k = 0; k < 32; k++) {
479 tmp = RREG32(i2c_cntl_0);
480 if (tmp & RADEON_I2C_GO)
482 tmp = RREG32(i2c_cntl_0);
483 if (tmp & RADEON_I2C_DONE)
486 DRM_DEBUG("i2c write error 0x%08x\n", tmp);
487 WREG32(i2c_cntl_0, tmp | RADEON_I2C_ABORT);
495 for (i = 0; i < num; i++) {
497 for (j = 0; j < p->len; j++) {
498 if (p->flags & I2C_M_RD) {
499 WREG32(i2c_cntl_0, (RADEON_I2C_DONE |
502 RADEON_I2C_SOFT_RST));
503 WREG32(i2c_data, ((p->addr << 1) & 0xff) | 0x1);
504 WREG32(i2c_cntl_1, ((1 << RADEON_I2C_DATA_COUNT_SHIFT) |
505 (1 << RADEON_I2C_ADDR_COUNT_SHIFT) |
507 (48 << RADEON_I2C_TIME_LIMIT_SHIFT)));
508 WREG32(i2c_cntl_0, reg | RADEON_I2C_RECEIVE);
509 for (k = 0; k < 32; k++) {
511 tmp = RREG32(i2c_cntl_0);
512 if (tmp & RADEON_I2C_GO)
514 tmp = RREG32(i2c_cntl_0);
515 if (tmp & RADEON_I2C_DONE)
518 DRM_DEBUG("i2c read error 0x%08x\n", tmp);
519 WREG32(i2c_cntl_0, tmp | RADEON_I2C_ABORT);
524 p->buf[j] = RREG32(i2c_data) & 0xff;
526 WREG32(i2c_cntl_0, (RADEON_I2C_DONE |
529 RADEON_I2C_SOFT_RST));
530 WREG32(i2c_data, (p->addr << 1) & 0xff);
531 WREG32(i2c_data, p->buf[j]);
532 WREG32(i2c_cntl_1, ((1 << RADEON_I2C_DATA_COUNT_SHIFT) |
533 (1 << RADEON_I2C_ADDR_COUNT_SHIFT) |
535 (48 << RADEON_I2C_TIME_LIMIT_SHIFT)));
536 WREG32(i2c_cntl_0, reg);
537 for (k = 0; k < 32; k++) {
539 tmp = RREG32(i2c_cntl_0);
540 if (tmp & RADEON_I2C_GO)
542 tmp = RREG32(i2c_cntl_0);
543 if (tmp & RADEON_I2C_DONE)
546 DRM_DEBUG("i2c write error 0x%08x\n", tmp);
547 WREG32(i2c_cntl_0, tmp | RADEON_I2C_ABORT);
557 WREG32(i2c_cntl_0, 0);
558 WREG32(i2c_cntl_1, 0);
559 WREG32(i2c_cntl_0, (RADEON_I2C_DONE |
562 RADEON_I2C_SOFT_RST));
564 if (rdev->is_atom_bios) {
565 tmp = RREG32(RADEON_BIOS_6_SCRATCH);
566 tmp &= ~ATOM_S6_HW_I2C_BUSY_STATE;
567 WREG32(RADEON_BIOS_6_SCRATCH, tmp);
570 mutex_unlock(&rdev->pm.mutex);
571 mutex_unlock(&rdev->dc_hw_i2c_mutex);
576 /* hw i2c engine for r5xx hardware
577 * hw can buffer up to 15 bytes
579 static int r500_hw_i2c_xfer(struct i2c_adapter *i2c_adap,
580 struct i2c_msg *msgs, int num)
582 struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
583 struct radeon_device *rdev = i2c->dev->dev_private;
584 struct radeon_i2c_bus_rec *rec = &i2c->rec;
586 int i, j, remaining, current_count, buffer_offset, ret = num;
591 mutex_lock(&rdev->dc_hw_i2c_mutex);
592 /* take the pm lock since we need a constant sclk */
593 mutex_lock(&rdev->pm.mutex);
595 prescale = radeon_get_i2c_prescale(rdev);
597 /* clear gpio mask bits */
598 tmp = RREG32(rec->mask_clk_reg);
599 tmp &= ~rec->mask_clk_mask;
600 WREG32(rec->mask_clk_reg, tmp);
601 tmp = RREG32(rec->mask_clk_reg);
603 tmp = RREG32(rec->mask_data_reg);
604 tmp &= ~rec->mask_data_mask;
605 WREG32(rec->mask_data_reg, tmp);
606 tmp = RREG32(rec->mask_data_reg);
608 /* clear pin values */
609 tmp = RREG32(rec->a_clk_reg);
610 tmp &= ~rec->a_clk_mask;
611 WREG32(rec->a_clk_reg, tmp);
612 tmp = RREG32(rec->a_clk_reg);
614 tmp = RREG32(rec->a_data_reg);
615 tmp &= ~rec->a_data_mask;
616 WREG32(rec->a_data_reg, tmp);
617 tmp = RREG32(rec->a_data_reg);
619 /* set the pins to input */
620 tmp = RREG32(rec->en_clk_reg);
621 tmp &= ~rec->en_clk_mask;
622 WREG32(rec->en_clk_reg, tmp);
623 tmp = RREG32(rec->en_clk_reg);
625 tmp = RREG32(rec->en_data_reg);
626 tmp &= ~rec->en_data_mask;
627 WREG32(rec->en_data_reg, tmp);
628 tmp = RREG32(rec->en_data_reg);
631 tmp = RREG32(RADEON_BIOS_6_SCRATCH);
632 WREG32(RADEON_BIOS_6_SCRATCH, tmp | ATOM_S6_HW_I2C_BUSY_STATE);
633 saved1 = RREG32(AVIVO_DC_I2C_CONTROL1);
634 saved2 = RREG32(0x494);
635 WREG32(0x494, saved2 | 0x1);
637 WREG32(AVIVO_DC_I2C_ARBITRATION, AVIVO_DC_I2C_SW_WANTS_TO_USE_I2C);
638 for (i = 0; i < 50; i++) {
640 if (RREG32(AVIVO_DC_I2C_ARBITRATION) & AVIVO_DC_I2C_SW_CAN_USE_I2C)
644 DRM_ERROR("failed to get i2c bus\n");
649 reg = AVIVO_DC_I2C_START | AVIVO_DC_I2C_STOP | AVIVO_DC_I2C_EN;
650 switch (rec->mask_clk_reg) {
651 case AVIVO_DC_GPIO_DDC1_MASK:
652 reg |= AVIVO_DC_I2C_PIN_SELECT(AVIVO_SEL_DDC1);
654 case AVIVO_DC_GPIO_DDC2_MASK:
655 reg |= AVIVO_DC_I2C_PIN_SELECT(AVIVO_SEL_DDC2);
657 case AVIVO_DC_GPIO_DDC3_MASK:
658 reg |= AVIVO_DC_I2C_PIN_SELECT(AVIVO_SEL_DDC3);
661 DRM_ERROR("gpio not supported with hw i2c\n");
666 /* check for bus probe */
668 if ((num == 1) && (p->len == 0)) {
669 WREG32(AVIVO_DC_I2C_STATUS1, (AVIVO_DC_I2C_DONE |
672 WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_SOFT_RESET);
674 WREG32(AVIVO_DC_I2C_RESET, 0);
676 WREG32(AVIVO_DC_I2C_DATA, (p->addr << 1) & 0xff);
677 WREG32(AVIVO_DC_I2C_DATA, 0);
679 WREG32(AVIVO_DC_I2C_CONTROL3, AVIVO_DC_I2C_TIME_LIMIT(48));
680 WREG32(AVIVO_DC_I2C_CONTROL2, (AVIVO_DC_I2C_ADDR_COUNT(1) |
681 AVIVO_DC_I2C_DATA_COUNT(1) |
683 WREG32(AVIVO_DC_I2C_CONTROL1, reg);
684 WREG32(AVIVO_DC_I2C_STATUS1, AVIVO_DC_I2C_GO);
685 for (j = 0; j < 200; j++) {
687 tmp = RREG32(AVIVO_DC_I2C_STATUS1);
688 if (tmp & AVIVO_DC_I2C_GO)
690 tmp = RREG32(AVIVO_DC_I2C_STATUS1);
691 if (tmp & AVIVO_DC_I2C_DONE)
694 DRM_DEBUG("i2c write error 0x%08x\n", tmp);
695 WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_ABORT);
703 for (i = 0; i < num; i++) {
707 if (p->flags & I2C_M_RD) {
712 current_count = remaining;
713 WREG32(AVIVO_DC_I2C_STATUS1, (AVIVO_DC_I2C_DONE |
716 WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_SOFT_RESET);
718 WREG32(AVIVO_DC_I2C_RESET, 0);
720 WREG32(AVIVO_DC_I2C_DATA, ((p->addr << 1) & 0xff) | 0x1);
721 WREG32(AVIVO_DC_I2C_CONTROL3, AVIVO_DC_I2C_TIME_LIMIT(48));
722 WREG32(AVIVO_DC_I2C_CONTROL2, (AVIVO_DC_I2C_ADDR_COUNT(1) |
723 AVIVO_DC_I2C_DATA_COUNT(current_count) |
725 WREG32(AVIVO_DC_I2C_CONTROL1, reg | AVIVO_DC_I2C_RECEIVE);
726 WREG32(AVIVO_DC_I2C_STATUS1, AVIVO_DC_I2C_GO);
727 for (j = 0; j < 200; j++) {
729 tmp = RREG32(AVIVO_DC_I2C_STATUS1);
730 if (tmp & AVIVO_DC_I2C_GO)
732 tmp = RREG32(AVIVO_DC_I2C_STATUS1);
733 if (tmp & AVIVO_DC_I2C_DONE)
736 DRM_DEBUG("i2c read error 0x%08x\n", tmp);
737 WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_ABORT);
742 for (j = 0; j < current_count; j++)
743 p->buf[buffer_offset + j] = RREG32(AVIVO_DC_I2C_DATA) & 0xff;
744 remaining -= current_count;
745 buffer_offset += current_count;
752 current_count = remaining;
753 WREG32(AVIVO_DC_I2C_STATUS1, (AVIVO_DC_I2C_DONE |
756 WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_SOFT_RESET);
758 WREG32(AVIVO_DC_I2C_RESET, 0);
760 WREG32(AVIVO_DC_I2C_DATA, (p->addr << 1) & 0xff);
761 for (j = 0; j < current_count; j++)
762 WREG32(AVIVO_DC_I2C_DATA, p->buf[buffer_offset + j]);
764 WREG32(AVIVO_DC_I2C_CONTROL3, AVIVO_DC_I2C_TIME_LIMIT(48));
765 WREG32(AVIVO_DC_I2C_CONTROL2, (AVIVO_DC_I2C_ADDR_COUNT(1) |
766 AVIVO_DC_I2C_DATA_COUNT(current_count) |
768 WREG32(AVIVO_DC_I2C_CONTROL1, reg);
769 WREG32(AVIVO_DC_I2C_STATUS1, AVIVO_DC_I2C_GO);
770 for (j = 0; j < 200; j++) {
772 tmp = RREG32(AVIVO_DC_I2C_STATUS1);
773 if (tmp & AVIVO_DC_I2C_GO)
775 tmp = RREG32(AVIVO_DC_I2C_STATUS1);
776 if (tmp & AVIVO_DC_I2C_DONE)
779 DRM_DEBUG("i2c write error 0x%08x\n", tmp);
780 WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_ABORT);
785 remaining -= current_count;
786 buffer_offset += current_count;
792 WREG32(AVIVO_DC_I2C_STATUS1, (AVIVO_DC_I2C_DONE |
795 WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_SOFT_RESET);
797 WREG32(AVIVO_DC_I2C_RESET, 0);
799 WREG32(AVIVO_DC_I2C_ARBITRATION, AVIVO_DC_I2C_SW_DONE_USING_I2C);
800 WREG32(AVIVO_DC_I2C_CONTROL1, saved1);
801 WREG32(0x494, saved2);
802 tmp = RREG32(RADEON_BIOS_6_SCRATCH);
803 tmp &= ~ATOM_S6_HW_I2C_BUSY_STATE;
804 WREG32(RADEON_BIOS_6_SCRATCH, tmp);
806 mutex_unlock(&rdev->pm.mutex);
807 mutex_unlock(&rdev->dc_hw_i2c_mutex);
812 static int radeon_hw_i2c_xfer(struct i2c_adapter *i2c_adap,
813 struct i2c_msg *msgs, int num)
815 struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
816 struct radeon_device *rdev = i2c->dev->dev_private;
817 struct radeon_i2c_bus_rec *rec = &i2c->rec;
820 mutex_lock(&i2c->mutex);
822 switch (rdev->family) {
841 ret = r100_hw_i2c_xfer(i2c_adap, msgs, num);
846 /* XXX fill in hw i2c implementation */
855 ret = r100_hw_i2c_xfer(i2c_adap, msgs, num);
857 ret = r500_hw_i2c_xfer(i2c_adap, msgs, num);
863 /* XXX fill in hw i2c implementation */
873 /* XXX fill in hw i2c implementation */
880 /* XXX fill in hw i2c implementation */
883 DRM_ERROR("i2c: unhandled radeon chip\n");
888 mutex_unlock(&i2c->mutex);
893 static u32 radeon_hw_i2c_func(struct i2c_adapter *adap)
895 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
898 static const struct i2c_algorithm radeon_i2c_algo = {
899 .master_xfer = radeon_hw_i2c_xfer,
900 .functionality = radeon_hw_i2c_func,
903 static const struct i2c_algorithm radeon_atom_i2c_algo = {
904 .master_xfer = radeon_atom_hw_i2c_xfer,
905 .functionality = radeon_atom_hw_i2c_func,
908 struct radeon_i2c_chan *radeon_i2c_create(struct drm_device *dev,
909 struct radeon_i2c_bus_rec *rec,
912 struct radeon_device *rdev = dev->dev_private;
913 struct radeon_i2c_chan *i2c;
916 /* don't add the mm_i2c bus unless hw_i2c is enabled */
917 if (rec->mm_i2c && (radeon_hw_i2c == 0))
920 i2c = kzalloc(sizeof(struct radeon_i2c_chan), GFP_KERNEL);
925 i2c->adapter.owner = THIS_MODULE;
926 i2c->adapter.class = I2C_CLASS_DDC;
927 i2c->adapter.dev.parent = &dev->pdev->dev;
929 i2c_set_adapdata(&i2c->adapter, i2c);
930 mutex_init(&i2c->mutex);
934 ((rdev->family <= CHIP_RS480) ||
935 ((rdev->family >= CHIP_RV515) && (rdev->family <= CHIP_R580))))) {
936 /* set the radeon hw i2c adapter */
937 snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
938 "Radeon i2c hw bus %s", name);
939 i2c->adapter.algo = &radeon_i2c_algo;
940 ret = i2c_add_adapter(&i2c->adapter);
942 DRM_ERROR("Failed to register hw i2c %s\n", name);
945 } else if (rec->hw_capable &&
947 ASIC_IS_DCE3(rdev)) {
948 /* hw i2c using atom */
949 snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
950 "Radeon i2c hw bus %s", name);
951 i2c->adapter.algo = &radeon_atom_i2c_algo;
952 ret = i2c_add_adapter(&i2c->adapter);
954 DRM_ERROR("Failed to register hw i2c %s\n", name);
958 /* set the radeon bit adapter */
959 snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
960 "Radeon i2c bit bus %s", name);
961 i2c->adapter.algo_data = &i2c->bit;
962 i2c->bit.pre_xfer = pre_xfer;
963 i2c->bit.post_xfer = post_xfer;
964 i2c->bit.setsda = set_data;
965 i2c->bit.setscl = set_clock;
966 i2c->bit.getsda = get_data;
967 i2c->bit.getscl = get_clock;
968 i2c->bit.udelay = 10;
969 i2c->bit.timeout = usecs_to_jiffies(2200); /* from VESA */
971 ret = i2c_bit_add_bus(&i2c->adapter);
973 DRM_ERROR("Failed to register bit i2c %s\n", name);
985 void radeon_i2c_destroy(struct radeon_i2c_chan *i2c)
989 i2c_del_adapter(&i2c->adapter);
991 drm_dp_aux_unregister(&i2c->aux);
995 /* Add the default buses */
996 void radeon_i2c_init(struct radeon_device *rdev)
999 DRM_INFO("hw_i2c forced on, you may experience display detection problems!\n");
1001 if (rdev->is_atom_bios)
1002 radeon_atombios_i2c_init(rdev);
1004 radeon_combios_i2c_init(rdev);
1007 /* remove all the buses */
1008 void radeon_i2c_fini(struct radeon_device *rdev)
1012 for (i = 0; i < RADEON_MAX_I2C_BUS; i++) {
1013 if (rdev->i2c_bus[i]) {
1014 radeon_i2c_destroy(rdev->i2c_bus[i]);
1015 rdev->i2c_bus[i] = NULL;
1020 /* Add additional buses */
1021 void radeon_i2c_add(struct radeon_device *rdev,
1022 struct radeon_i2c_bus_rec *rec,
1025 struct drm_device *dev = rdev->ddev;
1028 for (i = 0; i < RADEON_MAX_I2C_BUS; i++) {
1029 if (!rdev->i2c_bus[i]) {
1030 rdev->i2c_bus[i] = radeon_i2c_create(dev, rec, name);
1036 /* looks up bus based on id */
1037 struct radeon_i2c_chan *radeon_i2c_lookup(struct radeon_device *rdev,
1038 struct radeon_i2c_bus_rec *i2c_bus)
1042 for (i = 0; i < RADEON_MAX_I2C_BUS; i++) {
1043 if (rdev->i2c_bus[i] &&
1044 (rdev->i2c_bus[i]->rec.i2c_id == i2c_bus->i2c_id)) {
1045 return rdev->i2c_bus[i];
1051 void radeon_i2c_get_byte(struct radeon_i2c_chan *i2c_bus,
1058 struct i2c_msg msgs[] = {
1076 if (i2c_transfer(&i2c_bus->adapter, msgs, 2) == 2) {
1078 DRM_DEBUG("val = 0x%02x\n", *val);
1080 DRM_DEBUG("i2c 0x%02x 0x%02x read failed\n",
1085 void radeon_i2c_put_byte(struct radeon_i2c_chan *i2c_bus,
1091 struct i2c_msg msg = {
1101 if (i2c_transfer(&i2c_bus->adapter, &msg, 1) != 1)
1102 DRM_DEBUG("i2c 0x%02x 0x%02x write failed\n",
1106 /* ddc router switching */
1107 void radeon_router_select_ddc_port(struct radeon_connector *radeon_connector)
1111 if (!radeon_connector->router.ddc_valid)
1114 if (!radeon_connector->router_bus)
1117 radeon_i2c_get_byte(radeon_connector->router_bus,
1118 radeon_connector->router.i2c_addr,
1120 val &= ~radeon_connector->router.ddc_mux_control_pin;
1121 radeon_i2c_put_byte(radeon_connector->router_bus,
1122 radeon_connector->router.i2c_addr,
1124 radeon_i2c_get_byte(radeon_connector->router_bus,
1125 radeon_connector->router.i2c_addr,
1127 val &= ~radeon_connector->router.ddc_mux_control_pin;
1128 val |= radeon_connector->router.ddc_mux_state;
1129 radeon_i2c_put_byte(radeon_connector->router_bus,
1130 radeon_connector->router.i2c_addr,
1134 /* clock/data router switching */
1135 void radeon_router_select_cd_port(struct radeon_connector *radeon_connector)
1139 if (!radeon_connector->router.cd_valid)
1142 if (!radeon_connector->router_bus)
1145 radeon_i2c_get_byte(radeon_connector->router_bus,
1146 radeon_connector->router.i2c_addr,
1148 val &= ~radeon_connector->router.cd_mux_control_pin;
1149 radeon_i2c_put_byte(radeon_connector->router_bus,
1150 radeon_connector->router.i2c_addr,
1152 radeon_i2c_get_byte(radeon_connector->router_bus,
1153 radeon_connector->router.i2c_addr,
1155 val &= ~radeon_connector->router.cd_mux_control_pin;
1156 val |= radeon_connector->router.cd_mux_state;
1157 radeon_i2c_put_byte(radeon_connector->router_bus,
1158 radeon_connector->router.i2c_addr,