Linux Audio

Check our new training course

Loading...
v3.1
   1/*
   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#include "atom.h"
  30
 
 
 
 
  31/**
  32 * radeon_ddc_probe
  33 *
  34 */
  35bool radeon_ddc_probe(struct radeon_connector *radeon_connector, bool requires_extended_probe)
  36{
  37	u8 out = 0x0;
  38	u8 buf[8];
  39	int ret;
  40	struct i2c_msg msgs[] = {
  41		{
  42			.addr = 0x50,
  43			.flags = 0,
  44			.len = 1,
  45			.buf = &out,
  46		},
  47		{
  48			.addr = 0x50,
  49			.flags = I2C_M_RD,
  50			.len = 1,
  51			.buf = buf,
  52		}
  53	};
  54
  55	/* Read 8 bytes from i2c for extended probe of EDID header */
  56	if (requires_extended_probe)
  57		msgs[1].len = 8;
  58
  59	/* on hw with routers, select right port */
  60	if (radeon_connector->router.ddc_valid)
  61		radeon_router_select_ddc_port(radeon_connector);
  62
  63	ret = i2c_transfer(&radeon_connector->ddc_bus->adapter, msgs, 2);
 
 
 
 
 
  64	if (ret != 2)
  65		/* Couldn't find an accessible DDC on this connector */
  66		return false;
  67	if (requires_extended_probe) {
  68		/* Probe also for valid EDID header
  69		 * EDID header starts with:
  70		 * 0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00.
  71		 * Only the first 6 bytes must be valid as
  72		 * drm_edid_block_valid() can fix the last 2 bytes */
  73		if (drm_edid_header_is_valid(buf) < 6) {
  74			/* Couldn't find an accessible EDID on this
  75			 * connector */
  76			return false;
  77		}
  78	}
  79	return true;
  80}
  81
  82/* bit banging i2c */
  83
  84static void radeon_i2c_do_lock(struct radeon_i2c_chan *i2c, int lock_state)
  85{
 
  86	struct radeon_device *rdev = i2c->dev->dev_private;
  87	struct radeon_i2c_bus_rec *rec = &i2c->rec;
  88	uint32_t temp;
  89
 
 
  90	/* RV410 appears to have a bug where the hw i2c in reset
  91	 * holds the i2c port in a bad state - switch hw i2c away before
  92	 * doing DDC - do this for all r200s/r300s/r400s for safety sake
  93	 */
  94	if (rec->hw_capable) {
  95		if ((rdev->family >= CHIP_R200) && !ASIC_IS_AVIVO(rdev)) {
  96			u32 reg;
  97
  98			if (rdev->family >= CHIP_RV350)
  99				reg = RADEON_GPIO_MONID;
 100			else if ((rdev->family == CHIP_R300) ||
 101				 (rdev->family == CHIP_R350))
 102				reg = RADEON_GPIO_DVI_DDC;
 103			else
 104				reg = RADEON_GPIO_CRT2_DDC;
 105
 106			mutex_lock(&rdev->dc_hw_i2c_mutex);
 107			if (rec->a_clk_reg == reg) {
 108				WREG32(RADEON_DVI_I2C_CNTL_0, (RADEON_I2C_SOFT_RST |
 109							       R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1)));
 110			} else {
 111				WREG32(RADEON_DVI_I2C_CNTL_0, (RADEON_I2C_SOFT_RST |
 112							       R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3)));
 113			}
 114			mutex_unlock(&rdev->dc_hw_i2c_mutex);
 115		}
 116	}
 117
 118	/* switch the pads to ddc mode */
 119	if (ASIC_IS_DCE3(rdev) && rec->hw_capable) {
 120		temp = RREG32(rec->mask_clk_reg);
 121		temp &= ~(1 << 16);
 122		WREG32(rec->mask_clk_reg, temp);
 123	}
 124
 125	/* clear the output pin values */
 126	temp = RREG32(rec->a_clk_reg) & ~rec->a_clk_mask;
 127	WREG32(rec->a_clk_reg, temp);
 128
 129	temp = RREG32(rec->a_data_reg) & ~rec->a_data_mask;
 130	WREG32(rec->a_data_reg, temp);
 131
 132	/* set the pins to input */
 133	temp = RREG32(rec->en_clk_reg) & ~rec->en_clk_mask;
 134	WREG32(rec->en_clk_reg, temp);
 135
 136	temp = RREG32(rec->en_data_reg) & ~rec->en_data_mask;
 137	WREG32(rec->en_data_reg, temp);
 138
 139	/* mask the gpio pins for software use */
 140	temp = RREG32(rec->mask_clk_reg);
 141	if (lock_state)
 142		temp |= rec->mask_clk_mask;
 143	else
 144		temp &= ~rec->mask_clk_mask;
 145	WREG32(rec->mask_clk_reg, temp);
 146	temp = RREG32(rec->mask_clk_reg);
 147
 
 
 148	temp = RREG32(rec->mask_data_reg);
 149	if (lock_state)
 150		temp |= rec->mask_data_mask;
 151	else
 152		temp &= ~rec->mask_data_mask;
 
 
 
 
 
 
 
 
 
 
 
 
 
 153	WREG32(rec->mask_data_reg, temp);
 154	temp = RREG32(rec->mask_data_reg);
 
 
 155}
 156
 157static int get_clock(void *i2c_priv)
 158{
 159	struct radeon_i2c_chan *i2c = i2c_priv;
 160	struct radeon_device *rdev = i2c->dev->dev_private;
 161	struct radeon_i2c_bus_rec *rec = &i2c->rec;
 162	uint32_t val;
 163
 164	/* read the value off the pin */
 165	val = RREG32(rec->y_clk_reg);
 166	val &= rec->y_clk_mask;
 167
 168	return (val != 0);
 169}
 170
 171
 172static int get_data(void *i2c_priv)
 173{
 174	struct radeon_i2c_chan *i2c = i2c_priv;
 175	struct radeon_device *rdev = i2c->dev->dev_private;
 176	struct radeon_i2c_bus_rec *rec = &i2c->rec;
 177	uint32_t val;
 178
 179	/* read the value off the pin */
 180	val = RREG32(rec->y_data_reg);
 181	val &= rec->y_data_mask;
 182
 183	return (val != 0);
 184}
 185
 186static void set_clock(void *i2c_priv, int clock)
 187{
 188	struct radeon_i2c_chan *i2c = i2c_priv;
 189	struct radeon_device *rdev = i2c->dev->dev_private;
 190	struct radeon_i2c_bus_rec *rec = &i2c->rec;
 191	uint32_t val;
 192
 193	/* set pin direction */
 194	val = RREG32(rec->en_clk_reg) & ~rec->en_clk_mask;
 195	val |= clock ? 0 : rec->en_clk_mask;
 196	WREG32(rec->en_clk_reg, val);
 197}
 198
 199static void set_data(void *i2c_priv, int data)
 200{
 201	struct radeon_i2c_chan *i2c = i2c_priv;
 202	struct radeon_device *rdev = i2c->dev->dev_private;
 203	struct radeon_i2c_bus_rec *rec = &i2c->rec;
 204	uint32_t val;
 205
 206	/* set pin direction */
 207	val = RREG32(rec->en_data_reg) & ~rec->en_data_mask;
 208	val |= data ? 0 : rec->en_data_mask;
 209	WREG32(rec->en_data_reg, val);
 210}
 211
 212static int pre_xfer(struct i2c_adapter *i2c_adap)
 213{
 214	struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
 215
 216	radeon_i2c_do_lock(i2c, 1);
 217
 218	return 0;
 219}
 220
 221static void post_xfer(struct i2c_adapter *i2c_adap)
 222{
 223	struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
 224
 225	radeon_i2c_do_lock(i2c, 0);
 226}
 227
 228/* hw i2c */
 229
 230static u32 radeon_get_i2c_prescale(struct radeon_device *rdev)
 231{
 232	u32 sclk = rdev->pm.current_sclk;
 233	u32 prescale = 0;
 234	u32 nm;
 235	u8 n, m, loop;
 236	int i2c_clock;
 237
 238	switch (rdev->family) {
 239	case CHIP_R100:
 240	case CHIP_RV100:
 241	case CHIP_RS100:
 242	case CHIP_RV200:
 243	case CHIP_RS200:
 244	case CHIP_R200:
 245	case CHIP_RV250:
 246	case CHIP_RS300:
 247	case CHIP_RV280:
 248	case CHIP_R300:
 249	case CHIP_R350:
 250	case CHIP_RV350:
 251		i2c_clock = 60;
 252		nm = (sclk * 10) / (i2c_clock * 4);
 253		for (loop = 1; loop < 255; loop++) {
 254			if ((nm / loop) < loop)
 255				break;
 256		}
 257		n = loop - 1;
 258		m = loop - 2;
 259		prescale = m | (n << 8);
 260		break;
 261	case CHIP_RV380:
 262	case CHIP_RS400:
 263	case CHIP_RS480:
 264	case CHIP_R420:
 265	case CHIP_R423:
 266	case CHIP_RV410:
 267		prescale = (((sclk * 10)/(4 * 128 * 100) + 1) << 8) + 128;
 268		break;
 269	case CHIP_RS600:
 270	case CHIP_RS690:
 271	case CHIP_RS740:
 272		/* todo */
 273		break;
 274	case CHIP_RV515:
 275	case CHIP_R520:
 276	case CHIP_RV530:
 277	case CHIP_RV560:
 278	case CHIP_RV570:
 279	case CHIP_R580:
 280		i2c_clock = 50;
 281		if (rdev->family == CHIP_R520)
 282			prescale = (127 << 8) + ((sclk * 10) / (4 * 127 * i2c_clock));
 283		else
 284			prescale = (((sclk * 10)/(4 * 128 * 100) + 1) << 8) + 128;
 285		break;
 286	case CHIP_R600:
 287	case CHIP_RV610:
 288	case CHIP_RV630:
 289	case CHIP_RV670:
 290		/* todo */
 291		break;
 292	case CHIP_RV620:
 293	case CHIP_RV635:
 294	case CHIP_RS780:
 295	case CHIP_RS880:
 296	case CHIP_RV770:
 297	case CHIP_RV730:
 298	case CHIP_RV710:
 299	case CHIP_RV740:
 300		/* todo */
 301		break;
 302	case CHIP_CEDAR:
 303	case CHIP_REDWOOD:
 304	case CHIP_JUNIPER:
 305	case CHIP_CYPRESS:
 306	case CHIP_HEMLOCK:
 307		/* todo */
 308		break;
 309	default:
 310		DRM_ERROR("i2c: unhandled radeon chip\n");
 311		break;
 312	}
 313	return prescale;
 314}
 315
 316
 317/* hw i2c engine for r1xx-4xx hardware
 318 * hw can buffer up to 15 bytes
 319 */
 320static int r100_hw_i2c_xfer(struct i2c_adapter *i2c_adap,
 321			    struct i2c_msg *msgs, int num)
 322{
 323	struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
 324	struct radeon_device *rdev = i2c->dev->dev_private;
 325	struct radeon_i2c_bus_rec *rec = &i2c->rec;
 326	struct i2c_msg *p;
 327	int i, j, k, ret = num;
 328	u32 prescale;
 329	u32 i2c_cntl_0, i2c_cntl_1, i2c_data;
 330	u32 tmp, reg;
 331
 332	mutex_lock(&rdev->dc_hw_i2c_mutex);
 333	/* take the pm lock since we need a constant sclk */
 334	mutex_lock(&rdev->pm.mutex);
 335
 336	prescale = radeon_get_i2c_prescale(rdev);
 337
 338	reg = ((prescale << RADEON_I2C_PRESCALE_SHIFT) |
 339	       RADEON_I2C_DRIVE_EN |
 340	       RADEON_I2C_START |
 341	       RADEON_I2C_STOP |
 342	       RADEON_I2C_GO);
 343
 344	if (rdev->is_atom_bios) {
 345		tmp = RREG32(RADEON_BIOS_6_SCRATCH);
 346		WREG32(RADEON_BIOS_6_SCRATCH, tmp | ATOM_S6_HW_I2C_BUSY_STATE);
 347	}
 348
 349	if (rec->mm_i2c) {
 350		i2c_cntl_0 = RADEON_I2C_CNTL_0;
 351		i2c_cntl_1 = RADEON_I2C_CNTL_1;
 352		i2c_data = RADEON_I2C_DATA;
 353	} else {
 354		i2c_cntl_0 = RADEON_DVI_I2C_CNTL_0;
 355		i2c_cntl_1 = RADEON_DVI_I2C_CNTL_1;
 356		i2c_data = RADEON_DVI_I2C_DATA;
 357
 358		switch (rdev->family) {
 359		case CHIP_R100:
 360		case CHIP_RV100:
 361		case CHIP_RS100:
 362		case CHIP_RV200:
 363		case CHIP_RS200:
 364		case CHIP_RS300:
 365			switch (rec->mask_clk_reg) {
 366			case RADEON_GPIO_DVI_DDC:
 367				/* no gpio select bit */
 368				break;
 369			default:
 370				DRM_ERROR("gpio not supported with hw i2c\n");
 371				ret = -EINVAL;
 372				goto done;
 373			}
 374			break;
 375		case CHIP_R200:
 376			/* only bit 4 on r200 */
 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_MONID:
 382				reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3);
 383				break;
 384			default:
 385				DRM_ERROR("gpio not supported with hw i2c\n");
 386				ret = -EINVAL;
 387				goto done;
 388			}
 389			break;
 390		case CHIP_RV250:
 391		case CHIP_RV280:
 392			/* bits 3 and 4 */
 393			switch (rec->mask_clk_reg) {
 394			case RADEON_GPIO_DVI_DDC:
 395				reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1);
 396				break;
 397			case RADEON_GPIO_VGA_DDC:
 398				reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC2);
 399				break;
 400			case RADEON_GPIO_CRT2_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_R300:
 410		case CHIP_R350:
 411			/* only bit 4 on r300/r350 */
 412			switch (rec->mask_clk_reg) {
 413			case RADEON_GPIO_VGA_DDC:
 414				reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1);
 415				break;
 416			case RADEON_GPIO_DVI_DDC:
 417				reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3);
 418				break;
 419			default:
 420				DRM_ERROR("gpio not supported with hw i2c\n");
 421				ret = -EINVAL;
 422				goto done;
 423			}
 424			break;
 425		case CHIP_RV350:
 426		case CHIP_RV380:
 427		case CHIP_R420:
 428		case CHIP_R423:
 429		case CHIP_RV410:
 430		case CHIP_RS400:
 431		case CHIP_RS480:
 432			/* bits 3 and 4 */
 433			switch (rec->mask_clk_reg) {
 434			case RADEON_GPIO_VGA_DDC:
 435				reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1);
 436				break;
 437			case RADEON_GPIO_DVI_DDC:
 438				reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC2);
 439				break;
 440			case RADEON_GPIO_MONID:
 441				reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3);
 442				break;
 443			default:
 444				DRM_ERROR("gpio not supported with hw i2c\n");
 445				ret = -EINVAL;
 446				goto done;
 447			}
 448			break;
 449		default:
 450			DRM_ERROR("unsupported asic\n");
 451			ret = -EINVAL;
 452			goto done;
 453			break;
 454		}
 455	}
 456
 457	/* check for bus probe */
 458	p = &msgs[0];
 459	if ((num == 1) && (p->len == 0)) {
 460		WREG32(i2c_cntl_0, (RADEON_I2C_DONE |
 461				    RADEON_I2C_NACK |
 462				    RADEON_I2C_HALT |
 463				    RADEON_I2C_SOFT_RST));
 464		WREG32(i2c_data, (p->addr << 1) & 0xff);
 465		WREG32(i2c_data, 0);
 466		WREG32(i2c_cntl_1, ((1 << RADEON_I2C_DATA_COUNT_SHIFT) |
 467				    (1 << RADEON_I2C_ADDR_COUNT_SHIFT) |
 468				    RADEON_I2C_EN |
 469				    (48 << RADEON_I2C_TIME_LIMIT_SHIFT)));
 470		WREG32(i2c_cntl_0, reg);
 471		for (k = 0; k < 32; k++) {
 472			udelay(10);
 473			tmp = RREG32(i2c_cntl_0);
 474			if (tmp & RADEON_I2C_GO)
 475				continue;
 476			tmp = RREG32(i2c_cntl_0);
 477			if (tmp & RADEON_I2C_DONE)
 478				break;
 479			else {
 480				DRM_DEBUG("i2c write error 0x%08x\n", tmp);
 481				WREG32(i2c_cntl_0, tmp | RADEON_I2C_ABORT);
 482				ret = -EIO;
 483				goto done;
 484			}
 485		}
 486		goto done;
 487	}
 488
 489	for (i = 0; i < num; i++) {
 490		p = &msgs[i];
 491		for (j = 0; j < p->len; j++) {
 492			if (p->flags & I2C_M_RD) {
 493				WREG32(i2c_cntl_0, (RADEON_I2C_DONE |
 494						    RADEON_I2C_NACK |
 495						    RADEON_I2C_HALT |
 496						    RADEON_I2C_SOFT_RST));
 497				WREG32(i2c_data, ((p->addr << 1) & 0xff) | 0x1);
 498				WREG32(i2c_cntl_1, ((1 << RADEON_I2C_DATA_COUNT_SHIFT) |
 499						    (1 << RADEON_I2C_ADDR_COUNT_SHIFT) |
 500						    RADEON_I2C_EN |
 501						    (48 << RADEON_I2C_TIME_LIMIT_SHIFT)));
 502				WREG32(i2c_cntl_0, reg | RADEON_I2C_RECEIVE);
 503				for (k = 0; k < 32; k++) {
 504					udelay(10);
 505					tmp = RREG32(i2c_cntl_0);
 506					if (tmp & RADEON_I2C_GO)
 507						continue;
 508					tmp = RREG32(i2c_cntl_0);
 509					if (tmp & RADEON_I2C_DONE)
 510						break;
 511					else {
 512						DRM_DEBUG("i2c read error 0x%08x\n", tmp);
 513						WREG32(i2c_cntl_0, tmp | RADEON_I2C_ABORT);
 514						ret = -EIO;
 515						goto done;
 516					}
 517				}
 518				p->buf[j] = RREG32(i2c_data) & 0xff;
 519			} else {
 520				WREG32(i2c_cntl_0, (RADEON_I2C_DONE |
 521						    RADEON_I2C_NACK |
 522						    RADEON_I2C_HALT |
 523						    RADEON_I2C_SOFT_RST));
 524				WREG32(i2c_data, (p->addr << 1) & 0xff);
 525				WREG32(i2c_data, p->buf[j]);
 526				WREG32(i2c_cntl_1, ((1 << RADEON_I2C_DATA_COUNT_SHIFT) |
 527						    (1 << RADEON_I2C_ADDR_COUNT_SHIFT) |
 528						    RADEON_I2C_EN |
 529						    (48 << RADEON_I2C_TIME_LIMIT_SHIFT)));
 530				WREG32(i2c_cntl_0, reg);
 531				for (k = 0; k < 32; k++) {
 532					udelay(10);
 533					tmp = RREG32(i2c_cntl_0);
 534					if (tmp & RADEON_I2C_GO)
 535						continue;
 536					tmp = RREG32(i2c_cntl_0);
 537					if (tmp & RADEON_I2C_DONE)
 538						break;
 539					else {
 540						DRM_DEBUG("i2c write error 0x%08x\n", tmp);
 541						WREG32(i2c_cntl_0, tmp | RADEON_I2C_ABORT);
 542						ret = -EIO;
 543						goto done;
 544					}
 545				}
 546			}
 547		}
 548	}
 549
 550done:
 551	WREG32(i2c_cntl_0, 0);
 552	WREG32(i2c_cntl_1, 0);
 553	WREG32(i2c_cntl_0, (RADEON_I2C_DONE |
 554			    RADEON_I2C_NACK |
 555			    RADEON_I2C_HALT |
 556			    RADEON_I2C_SOFT_RST));
 557
 558	if (rdev->is_atom_bios) {
 559		tmp = RREG32(RADEON_BIOS_6_SCRATCH);
 560		tmp &= ~ATOM_S6_HW_I2C_BUSY_STATE;
 561		WREG32(RADEON_BIOS_6_SCRATCH, tmp);
 562	}
 563
 564	mutex_unlock(&rdev->pm.mutex);
 565	mutex_unlock(&rdev->dc_hw_i2c_mutex);
 566
 567	return ret;
 568}
 569
 570/* hw i2c engine for r5xx hardware
 571 * hw can buffer up to 15 bytes
 572 */
 573static int r500_hw_i2c_xfer(struct i2c_adapter *i2c_adap,
 574			    struct i2c_msg *msgs, int num)
 575{
 576	struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
 577	struct radeon_device *rdev = i2c->dev->dev_private;
 578	struct radeon_i2c_bus_rec *rec = &i2c->rec;
 579	struct i2c_msg *p;
 580	int i, j, remaining, current_count, buffer_offset, ret = num;
 581	u32 prescale;
 582	u32 tmp, reg;
 583	u32 saved1, saved2;
 584
 585	mutex_lock(&rdev->dc_hw_i2c_mutex);
 586	/* take the pm lock since we need a constant sclk */
 587	mutex_lock(&rdev->pm.mutex);
 588
 589	prescale = radeon_get_i2c_prescale(rdev);
 590
 591	/* clear gpio mask bits */
 592	tmp = RREG32(rec->mask_clk_reg);
 593	tmp &= ~rec->mask_clk_mask;
 594	WREG32(rec->mask_clk_reg, tmp);
 595	tmp = RREG32(rec->mask_clk_reg);
 596
 597	tmp = RREG32(rec->mask_data_reg);
 598	tmp &= ~rec->mask_data_mask;
 599	WREG32(rec->mask_data_reg, tmp);
 600	tmp = RREG32(rec->mask_data_reg);
 601
 602	/* clear pin values */
 603	tmp = RREG32(rec->a_clk_reg);
 604	tmp &= ~rec->a_clk_mask;
 605	WREG32(rec->a_clk_reg, tmp);
 606	tmp = RREG32(rec->a_clk_reg);
 607
 608	tmp = RREG32(rec->a_data_reg);
 609	tmp &= ~rec->a_data_mask;
 610	WREG32(rec->a_data_reg, tmp);
 611	tmp = RREG32(rec->a_data_reg);
 612
 613	/* set the pins to input */
 614	tmp = RREG32(rec->en_clk_reg);
 615	tmp &= ~rec->en_clk_mask;
 616	WREG32(rec->en_clk_reg, tmp);
 617	tmp = RREG32(rec->en_clk_reg);
 618
 619	tmp = RREG32(rec->en_data_reg);
 620	tmp &= ~rec->en_data_mask;
 621	WREG32(rec->en_data_reg, tmp);
 622	tmp = RREG32(rec->en_data_reg);
 623
 624	/* */
 625	tmp = RREG32(RADEON_BIOS_6_SCRATCH);
 626	WREG32(RADEON_BIOS_6_SCRATCH, tmp | ATOM_S6_HW_I2C_BUSY_STATE);
 627	saved1 = RREG32(AVIVO_DC_I2C_CONTROL1);
 628	saved2 = RREG32(0x494);
 629	WREG32(0x494, saved2 | 0x1);
 630
 631	WREG32(AVIVO_DC_I2C_ARBITRATION, AVIVO_DC_I2C_SW_WANTS_TO_USE_I2C);
 632	for (i = 0; i < 50; i++) {
 633		udelay(1);
 634		if (RREG32(AVIVO_DC_I2C_ARBITRATION) & AVIVO_DC_I2C_SW_CAN_USE_I2C)
 635			break;
 636	}
 637	if (i == 50) {
 638		DRM_ERROR("failed to get i2c bus\n");
 639		ret = -EBUSY;
 640		goto done;
 641	}
 642
 643	reg = AVIVO_DC_I2C_START | AVIVO_DC_I2C_STOP | AVIVO_DC_I2C_EN;
 644	switch (rec->mask_clk_reg) {
 645	case AVIVO_DC_GPIO_DDC1_MASK:
 646		reg |= AVIVO_DC_I2C_PIN_SELECT(AVIVO_SEL_DDC1);
 647		break;
 648	case AVIVO_DC_GPIO_DDC2_MASK:
 649		reg |= AVIVO_DC_I2C_PIN_SELECT(AVIVO_SEL_DDC2);
 650		break;
 651	case AVIVO_DC_GPIO_DDC3_MASK:
 652		reg |= AVIVO_DC_I2C_PIN_SELECT(AVIVO_SEL_DDC3);
 653		break;
 654	default:
 655		DRM_ERROR("gpio not supported with hw i2c\n");
 656		ret = -EINVAL;
 657		goto done;
 658	}
 659
 660	/* check for bus probe */
 661	p = &msgs[0];
 662	if ((num == 1) && (p->len == 0)) {
 663		WREG32(AVIVO_DC_I2C_STATUS1, (AVIVO_DC_I2C_DONE |
 664					      AVIVO_DC_I2C_NACK |
 665					      AVIVO_DC_I2C_HALT));
 666		WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_SOFT_RESET);
 667		udelay(1);
 668		WREG32(AVIVO_DC_I2C_RESET, 0);
 669
 670		WREG32(AVIVO_DC_I2C_DATA, (p->addr << 1) & 0xff);
 671		WREG32(AVIVO_DC_I2C_DATA, 0);
 672
 673		WREG32(AVIVO_DC_I2C_CONTROL3, AVIVO_DC_I2C_TIME_LIMIT(48));
 674		WREG32(AVIVO_DC_I2C_CONTROL2, (AVIVO_DC_I2C_ADDR_COUNT(1) |
 675					       AVIVO_DC_I2C_DATA_COUNT(1) |
 676					       (prescale << 16)));
 677		WREG32(AVIVO_DC_I2C_CONTROL1, reg);
 678		WREG32(AVIVO_DC_I2C_STATUS1, AVIVO_DC_I2C_GO);
 679		for (j = 0; j < 200; j++) {
 680			udelay(50);
 681			tmp = RREG32(AVIVO_DC_I2C_STATUS1);
 682			if (tmp & AVIVO_DC_I2C_GO)
 683				continue;
 684			tmp = RREG32(AVIVO_DC_I2C_STATUS1);
 685			if (tmp & AVIVO_DC_I2C_DONE)
 686				break;
 687			else {
 688				DRM_DEBUG("i2c write error 0x%08x\n", tmp);
 689				WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_ABORT);
 690				ret = -EIO;
 691				goto done;
 692			}
 693		}
 694		goto done;
 695	}
 696
 697	for (i = 0; i < num; i++) {
 698		p = &msgs[i];
 699		remaining = p->len;
 700		buffer_offset = 0;
 701		if (p->flags & I2C_M_RD) {
 702			while (remaining) {
 703				if (remaining > 15)
 704					current_count = 15;
 705				else
 706					current_count = remaining;
 707				WREG32(AVIVO_DC_I2C_STATUS1, (AVIVO_DC_I2C_DONE |
 708							      AVIVO_DC_I2C_NACK |
 709							      AVIVO_DC_I2C_HALT));
 710				WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_SOFT_RESET);
 711				udelay(1);
 712				WREG32(AVIVO_DC_I2C_RESET, 0);
 713
 714				WREG32(AVIVO_DC_I2C_DATA, ((p->addr << 1) & 0xff) | 0x1);
 715				WREG32(AVIVO_DC_I2C_CONTROL3, AVIVO_DC_I2C_TIME_LIMIT(48));
 716				WREG32(AVIVO_DC_I2C_CONTROL2, (AVIVO_DC_I2C_ADDR_COUNT(1) |
 717							       AVIVO_DC_I2C_DATA_COUNT(current_count) |
 718							       (prescale << 16)));
 719				WREG32(AVIVO_DC_I2C_CONTROL1, reg | AVIVO_DC_I2C_RECEIVE);
 720				WREG32(AVIVO_DC_I2C_STATUS1, AVIVO_DC_I2C_GO);
 721				for (j = 0; j < 200; j++) {
 722					udelay(50);
 723					tmp = RREG32(AVIVO_DC_I2C_STATUS1);
 724					if (tmp & AVIVO_DC_I2C_GO)
 725						continue;
 726					tmp = RREG32(AVIVO_DC_I2C_STATUS1);
 727					if (tmp & AVIVO_DC_I2C_DONE)
 728						break;
 729					else {
 730						DRM_DEBUG("i2c read error 0x%08x\n", tmp);
 731						WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_ABORT);
 732						ret = -EIO;
 733						goto done;
 734					}
 735				}
 736				for (j = 0; j < current_count; j++)
 737					p->buf[buffer_offset + j] = RREG32(AVIVO_DC_I2C_DATA) & 0xff;
 738				remaining -= current_count;
 739				buffer_offset += current_count;
 740			}
 741		} else {
 742			while (remaining) {
 743				if (remaining > 15)
 744					current_count = 15;
 745				else
 746					current_count = remaining;
 747				WREG32(AVIVO_DC_I2C_STATUS1, (AVIVO_DC_I2C_DONE |
 748							      AVIVO_DC_I2C_NACK |
 749							      AVIVO_DC_I2C_HALT));
 750				WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_SOFT_RESET);
 751				udelay(1);
 752				WREG32(AVIVO_DC_I2C_RESET, 0);
 753
 754				WREG32(AVIVO_DC_I2C_DATA, (p->addr << 1) & 0xff);
 755				for (j = 0; j < current_count; j++)
 756					WREG32(AVIVO_DC_I2C_DATA, p->buf[buffer_offset + j]);
 757
 758				WREG32(AVIVO_DC_I2C_CONTROL3, AVIVO_DC_I2C_TIME_LIMIT(48));
 759				WREG32(AVIVO_DC_I2C_CONTROL2, (AVIVO_DC_I2C_ADDR_COUNT(1) |
 760							       AVIVO_DC_I2C_DATA_COUNT(current_count) |
 761							       (prescale << 16)));
 762				WREG32(AVIVO_DC_I2C_CONTROL1, reg);
 763				WREG32(AVIVO_DC_I2C_STATUS1, AVIVO_DC_I2C_GO);
 764				for (j = 0; j < 200; j++) {
 765					udelay(50);
 766					tmp = RREG32(AVIVO_DC_I2C_STATUS1);
 767					if (tmp & AVIVO_DC_I2C_GO)
 768						continue;
 769					tmp = RREG32(AVIVO_DC_I2C_STATUS1);
 770					if (tmp & AVIVO_DC_I2C_DONE)
 771						break;
 772					else {
 773						DRM_DEBUG("i2c write error 0x%08x\n", tmp);
 774						WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_ABORT);
 775						ret = -EIO;
 776						goto done;
 777					}
 778				}
 779				remaining -= current_count;
 780				buffer_offset += current_count;
 781			}
 782		}
 783	}
 784
 785done:
 786	WREG32(AVIVO_DC_I2C_STATUS1, (AVIVO_DC_I2C_DONE |
 787				      AVIVO_DC_I2C_NACK |
 788				      AVIVO_DC_I2C_HALT));
 789	WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_SOFT_RESET);
 790	udelay(1);
 791	WREG32(AVIVO_DC_I2C_RESET, 0);
 792
 793	WREG32(AVIVO_DC_I2C_ARBITRATION, AVIVO_DC_I2C_SW_DONE_USING_I2C);
 794	WREG32(AVIVO_DC_I2C_CONTROL1, saved1);
 795	WREG32(0x494, saved2);
 796	tmp = RREG32(RADEON_BIOS_6_SCRATCH);
 797	tmp &= ~ATOM_S6_HW_I2C_BUSY_STATE;
 798	WREG32(RADEON_BIOS_6_SCRATCH, tmp);
 799
 800	mutex_unlock(&rdev->pm.mutex);
 801	mutex_unlock(&rdev->dc_hw_i2c_mutex);
 802
 803	return ret;
 804}
 805
 806static int radeon_hw_i2c_xfer(struct i2c_adapter *i2c_adap,
 807			      struct i2c_msg *msgs, int num)
 808{
 809	struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
 810	struct radeon_device *rdev = i2c->dev->dev_private;
 811	struct radeon_i2c_bus_rec *rec = &i2c->rec;
 812	int ret = 0;
 813
 
 
 814	switch (rdev->family) {
 815	case CHIP_R100:
 816	case CHIP_RV100:
 817	case CHIP_RS100:
 818	case CHIP_RV200:
 819	case CHIP_RS200:
 820	case CHIP_R200:
 821	case CHIP_RV250:
 822	case CHIP_RS300:
 823	case CHIP_RV280:
 824	case CHIP_R300:
 825	case CHIP_R350:
 826	case CHIP_RV350:
 827	case CHIP_RV380:
 828	case CHIP_R420:
 829	case CHIP_R423:
 830	case CHIP_RV410:
 831	case CHIP_RS400:
 832	case CHIP_RS480:
 833		ret = r100_hw_i2c_xfer(i2c_adap, msgs, num);
 834		break;
 835	case CHIP_RS600:
 836	case CHIP_RS690:
 837	case CHIP_RS740:
 838		/* XXX fill in hw i2c implementation */
 839		break;
 840	case CHIP_RV515:
 841	case CHIP_R520:
 842	case CHIP_RV530:
 843	case CHIP_RV560:
 844	case CHIP_RV570:
 845	case CHIP_R580:
 846		if (rec->mm_i2c)
 847			ret = r100_hw_i2c_xfer(i2c_adap, msgs, num);
 848		else
 849			ret = r500_hw_i2c_xfer(i2c_adap, msgs, num);
 850		break;
 851	case CHIP_R600:
 852	case CHIP_RV610:
 853	case CHIP_RV630:
 854	case CHIP_RV670:
 855		/* XXX fill in hw i2c implementation */
 856		break;
 857	case CHIP_RV620:
 858	case CHIP_RV635:
 859	case CHIP_RS780:
 860	case CHIP_RS880:
 861	case CHIP_RV770:
 862	case CHIP_RV730:
 863	case CHIP_RV710:
 864	case CHIP_RV740:
 865		/* XXX fill in hw i2c implementation */
 866		break;
 867	case CHIP_CEDAR:
 868	case CHIP_REDWOOD:
 869	case CHIP_JUNIPER:
 870	case CHIP_CYPRESS:
 871	case CHIP_HEMLOCK:
 872		/* XXX fill in hw i2c implementation */
 873		break;
 874	default:
 875		DRM_ERROR("i2c: unhandled radeon chip\n");
 876		ret = -EIO;
 877		break;
 878	}
 879
 
 
 880	return ret;
 881}
 882
 883static u32 radeon_hw_i2c_func(struct i2c_adapter *adap)
 884{
 885	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
 886}
 887
 888static const struct i2c_algorithm radeon_i2c_algo = {
 889	.master_xfer = radeon_hw_i2c_xfer,
 890	.functionality = radeon_hw_i2c_func,
 891};
 892
 
 
 
 
 
 893struct radeon_i2c_chan *radeon_i2c_create(struct drm_device *dev,
 894					  struct radeon_i2c_bus_rec *rec,
 895					  const char *name)
 896{
 897	struct radeon_device *rdev = dev->dev_private;
 898	struct radeon_i2c_chan *i2c;
 899	int ret;
 900
 
 
 
 
 901	i2c = kzalloc(sizeof(struct radeon_i2c_chan), GFP_KERNEL);
 902	if (i2c == NULL)
 903		return NULL;
 904
 905	i2c->rec = *rec;
 906	i2c->adapter.owner = THIS_MODULE;
 907	i2c->adapter.class = I2C_CLASS_DDC;
 
 908	i2c->dev = dev;
 909	i2c_set_adapdata(&i2c->adapter, i2c);
 
 910	if (rec->mm_i2c ||
 911	    (rec->hw_capable &&
 912	     radeon_hw_i2c &&
 913	     ((rdev->family <= CHIP_RS480) ||
 914	      ((rdev->family >= CHIP_RV515) && (rdev->family <= CHIP_R580))))) {
 915		/* set the radeon hw i2c adapter */
 916		snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
 917			 "Radeon i2c hw bus %s", name);
 918		i2c->adapter.algo = &radeon_i2c_algo;
 919		ret = i2c_add_adapter(&i2c->adapter);
 920		if (ret) {
 921			DRM_ERROR("Failed to register hw i2c %s\n", name);
 
 
 
 
 
 
 
 
 
 922			goto out_free;
 923		}
 924	} else {
 925		/* set the radeon bit adapter */
 926		snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
 927			 "Radeon i2c bit bus %s", name);
 928		i2c->adapter.algo_data = &i2c->algo.bit;
 929		i2c->algo.bit.pre_xfer = pre_xfer;
 930		i2c->algo.bit.post_xfer = post_xfer;
 931		i2c->algo.bit.setsda = set_data;
 932		i2c->algo.bit.setscl = set_clock;
 933		i2c->algo.bit.getsda = get_data;
 934		i2c->algo.bit.getscl = get_clock;
 935		i2c->algo.bit.udelay = 20;
 936		/* vesa says 2.2 ms is enough, 1 jiffy doesn't seem to always
 937		 * make this, 2 jiffies is a lot more reliable */
 938		i2c->algo.bit.timeout = 2;
 939		i2c->algo.bit.data = i2c;
 940		ret = i2c_bit_add_bus(&i2c->adapter);
 941		if (ret) {
 942			DRM_ERROR("Failed to register bit i2c %s\n", name);
 943			goto out_free;
 944		}
 945	}
 946
 947	return i2c;
 948out_free:
 949	kfree(i2c);
 950	return NULL;
 951
 952}
 953
 954struct radeon_i2c_chan *radeon_i2c_create_dp(struct drm_device *dev,
 955					     struct radeon_i2c_bus_rec *rec,
 956					     const char *name)
 957{
 958	struct radeon_i2c_chan *i2c;
 959	int ret;
 960
 961	i2c = kzalloc(sizeof(struct radeon_i2c_chan), GFP_KERNEL);
 962	if (i2c == NULL)
 963		return NULL;
 964
 965	i2c->rec = *rec;
 966	i2c->adapter.owner = THIS_MODULE;
 967	i2c->adapter.class = I2C_CLASS_DDC;
 968	i2c->dev = dev;
 969	snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
 970		 "Radeon aux bus %s", name);
 971	i2c_set_adapdata(&i2c->adapter, i2c);
 972	i2c->adapter.algo_data = &i2c->algo.dp;
 973	i2c->algo.dp.aux_ch = radeon_dp_i2c_aux_ch;
 974	i2c->algo.dp.address = 0;
 975	ret = i2c_dp_aux_add_bus(&i2c->adapter);
 976	if (ret) {
 977		DRM_INFO("Failed to register i2c %s\n", name);
 978		goto out_free;
 979	}
 980
 981	return i2c;
 982out_free:
 983	kfree(i2c);
 984	return NULL;
 985
 986}
 987
 988void radeon_i2c_destroy(struct radeon_i2c_chan *i2c)
 989{
 990	if (!i2c)
 991		return;
 
 992	i2c_del_adapter(&i2c->adapter);
 993	kfree(i2c);
 994}
 995
 996/* Add the default buses */
 997void radeon_i2c_init(struct radeon_device *rdev)
 998{
 
 
 
 999	if (rdev->is_atom_bios)
1000		radeon_atombios_i2c_init(rdev);
1001	else
1002		radeon_combios_i2c_init(rdev);
1003}
1004
1005/* remove all the buses */
1006void radeon_i2c_fini(struct radeon_device *rdev)
1007{
1008	int i;
1009
1010	for (i = 0; i < RADEON_MAX_I2C_BUS; i++) {
1011		if (rdev->i2c_bus[i]) {
1012			radeon_i2c_destroy(rdev->i2c_bus[i]);
1013			rdev->i2c_bus[i] = NULL;
1014		}
1015	}
1016}
1017
1018/* Add additional buses */
1019void radeon_i2c_add(struct radeon_device *rdev,
1020		    struct radeon_i2c_bus_rec *rec,
1021		    const char *name)
1022{
1023	struct drm_device *dev = rdev->ddev;
1024	int i;
1025
1026	for (i = 0; i < RADEON_MAX_I2C_BUS; i++) {
1027		if (!rdev->i2c_bus[i]) {
1028			rdev->i2c_bus[i] = radeon_i2c_create(dev, rec, name);
1029			return;
1030		}
1031	}
1032}
1033
1034/* looks up bus based on id */
1035struct radeon_i2c_chan *radeon_i2c_lookup(struct radeon_device *rdev,
1036					  struct radeon_i2c_bus_rec *i2c_bus)
1037{
1038	int i;
1039
1040	for (i = 0; i < RADEON_MAX_I2C_BUS; i++) {
1041		if (rdev->i2c_bus[i] &&
1042		    (rdev->i2c_bus[i]->rec.i2c_id == i2c_bus->i2c_id)) {
1043			return rdev->i2c_bus[i];
1044		}
1045	}
1046	return NULL;
1047}
1048
1049struct drm_encoder *radeon_best_encoder(struct drm_connector *connector)
1050{
1051	return NULL;
1052}
1053
1054void radeon_i2c_get_byte(struct radeon_i2c_chan *i2c_bus,
1055			 u8 slave_addr,
1056			 u8 addr,
1057			 u8 *val)
1058{
1059	u8 out_buf[2];
1060	u8 in_buf[2];
1061	struct i2c_msg msgs[] = {
1062		{
1063			.addr = slave_addr,
1064			.flags = 0,
1065			.len = 1,
1066			.buf = out_buf,
1067		},
1068		{
1069			.addr = slave_addr,
1070			.flags = I2C_M_RD,
1071			.len = 1,
1072			.buf = in_buf,
1073		}
1074	};
1075
1076	out_buf[0] = addr;
1077	out_buf[1] = 0;
1078
1079	if (i2c_transfer(&i2c_bus->adapter, msgs, 2) == 2) {
1080		*val = in_buf[0];
1081		DRM_DEBUG("val = 0x%02x\n", *val);
1082	} else {
1083		DRM_DEBUG("i2c 0x%02x 0x%02x read failed\n",
1084			  addr, *val);
1085	}
1086}
1087
1088void radeon_i2c_put_byte(struct radeon_i2c_chan *i2c_bus,
1089			 u8 slave_addr,
1090			 u8 addr,
1091			 u8 val)
1092{
1093	uint8_t out_buf[2];
1094	struct i2c_msg msg = {
1095		.addr = slave_addr,
1096		.flags = 0,
1097		.len = 2,
1098		.buf = out_buf,
1099	};
1100
1101	out_buf[0] = addr;
1102	out_buf[1] = val;
1103
1104	if (i2c_transfer(&i2c_bus->adapter, &msg, 1) != 1)
1105		DRM_DEBUG("i2c 0x%02x 0x%02x write failed\n",
1106			  addr, val);
1107}
1108
1109/* ddc router switching */
1110void radeon_router_select_ddc_port(struct radeon_connector *radeon_connector)
1111{
1112	u8 val;
1113
1114	if (!radeon_connector->router.ddc_valid)
1115		return;
1116
1117	if (!radeon_connector->router_bus)
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.ddc_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.ddc_mux_control_pin;
1131	val |= radeon_connector->router.ddc_mux_state;
1132	radeon_i2c_put_byte(radeon_connector->router_bus,
1133			    radeon_connector->router.i2c_addr,
1134			    0x1, val);
1135}
1136
1137/* clock/data router switching */
1138void radeon_router_select_cd_port(struct radeon_connector *radeon_connector)
1139{
1140	u8 val;
1141
1142	if (!radeon_connector->router.cd_valid)
1143		return;
1144
1145	if (!radeon_connector->router_bus)
1146		return;
1147
1148	radeon_i2c_get_byte(radeon_connector->router_bus,
1149			    radeon_connector->router.i2c_addr,
1150			    0x3, &val);
1151	val &= ~radeon_connector->router.cd_mux_control_pin;
1152	radeon_i2c_put_byte(radeon_connector->router_bus,
1153			    radeon_connector->router.i2c_addr,
1154			    0x3, val);
1155	radeon_i2c_get_byte(radeon_connector->router_bus,
1156			    radeon_connector->router.i2c_addr,
1157			    0x1, &val);
1158	val &= ~radeon_connector->router.cd_mux_control_pin;
1159	val |= radeon_connector->router.cd_mux_state;
1160	radeon_i2c_put_byte(radeon_connector->router_bus,
1161			    radeon_connector->router.i2c_addr,
1162			    0x1, val);
1163}
1164
v5.4
   1/*
   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
  27#include <linux/export.h>
  28
  29#include <drm/drm_device.h>
  30#include <drm/drm_edid.h>
  31#include <drm/drm_pci.h>
  32#include <drm/radeon_drm.h>
  33
  34#include "radeon.h"
  35#include "atom.h"
  36
  37extern int radeon_atom_hw_i2c_xfer(struct i2c_adapter *i2c_adap,
  38				   struct i2c_msg *msgs, int num);
  39extern u32 radeon_atom_hw_i2c_func(struct i2c_adapter *adap);
  40
  41/**
  42 * radeon_ddc_probe
  43 *
  44 */
  45bool radeon_ddc_probe(struct radeon_connector *radeon_connector, bool use_aux)
  46{
  47	u8 out = 0x0;
  48	u8 buf[8];
  49	int ret;
  50	struct i2c_msg msgs[] = {
  51		{
  52			.addr = DDC_ADDR,
  53			.flags = 0,
  54			.len = 1,
  55			.buf = &out,
  56		},
  57		{
  58			.addr = DDC_ADDR,
  59			.flags = I2C_M_RD,
  60			.len = 8,
  61			.buf = buf,
  62		}
  63	};
  64
 
 
 
 
  65	/* on hw with routers, select right port */
  66	if (radeon_connector->router.ddc_valid)
  67		radeon_router_select_ddc_port(radeon_connector);
  68
  69	if (use_aux) {
  70		ret = i2c_transfer(&radeon_connector->ddc_bus->aux.ddc, msgs, 2);
  71	} else {
  72		ret = i2c_transfer(&radeon_connector->ddc_bus->adapter, msgs, 2);
  73	}
  74
  75	if (ret != 2)
  76		/* Couldn't find an accessible DDC on this connector */
  77		return false;
  78	/* Probe also for valid EDID header
  79	 * EDID header starts with:
  80	 * 0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00.
  81	 * Only the first 6 bytes must be valid as
  82	 * drm_edid_block_valid() can fix the last 2 bytes */
  83	if (drm_edid_header_is_valid(buf) < 6) {
  84		/* Couldn't find an accessible EDID on this
  85		 * connector */
  86		return false;
 
 
  87	}
  88	return true;
  89}
  90
  91/* bit banging i2c */
  92
  93static int pre_xfer(struct i2c_adapter *i2c_adap)
  94{
  95	struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
  96	struct radeon_device *rdev = i2c->dev->dev_private;
  97	struct radeon_i2c_bus_rec *rec = &i2c->rec;
  98	uint32_t temp;
  99
 100	mutex_lock(&i2c->mutex);
 101
 102	/* RV410 appears to have a bug where the hw i2c in reset
 103	 * holds the i2c port in a bad state - switch hw i2c away before
 104	 * doing DDC - do this for all r200s/r300s/r400s for safety sake
 105	 */
 106	if (rec->hw_capable) {
 107		if ((rdev->family >= CHIP_R200) && !ASIC_IS_AVIVO(rdev)) {
 108			u32 reg;
 109
 110			if (rdev->family >= CHIP_RV350)
 111				reg = RADEON_GPIO_MONID;
 112			else if ((rdev->family == CHIP_R300) ||
 113				 (rdev->family == CHIP_R350))
 114				reg = RADEON_GPIO_DVI_DDC;
 115			else
 116				reg = RADEON_GPIO_CRT2_DDC;
 117
 118			mutex_lock(&rdev->dc_hw_i2c_mutex);
 119			if (rec->a_clk_reg == reg) {
 120				WREG32(RADEON_DVI_I2C_CNTL_0, (RADEON_I2C_SOFT_RST |
 121							       R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1)));
 122			} else {
 123				WREG32(RADEON_DVI_I2C_CNTL_0, (RADEON_I2C_SOFT_RST |
 124							       R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3)));
 125			}
 126			mutex_unlock(&rdev->dc_hw_i2c_mutex);
 127		}
 128	}
 129
 130	/* switch the pads to ddc mode */
 131	if (ASIC_IS_DCE3(rdev) && rec->hw_capable) {
 132		temp = RREG32(rec->mask_clk_reg);
 133		temp &= ~(1 << 16);
 134		WREG32(rec->mask_clk_reg, temp);
 135	}
 136
 137	/* clear the output pin values */
 138	temp = RREG32(rec->a_clk_reg) & ~rec->a_clk_mask;
 139	WREG32(rec->a_clk_reg, temp);
 140
 141	temp = RREG32(rec->a_data_reg) & ~rec->a_data_mask;
 142	WREG32(rec->a_data_reg, temp);
 143
 144	/* set the pins to input */
 145	temp = RREG32(rec->en_clk_reg) & ~rec->en_clk_mask;
 146	WREG32(rec->en_clk_reg, temp);
 147
 148	temp = RREG32(rec->en_data_reg) & ~rec->en_data_mask;
 149	WREG32(rec->en_data_reg, temp);
 150
 151	/* mask the gpio pins for software use */
 152	temp = RREG32(rec->mask_clk_reg) | rec->mask_clk_mask;
 
 
 
 
 153	WREG32(rec->mask_clk_reg, temp);
 154	temp = RREG32(rec->mask_clk_reg);
 155
 156	temp = RREG32(rec->mask_data_reg) | rec->mask_data_mask;
 157	WREG32(rec->mask_data_reg, temp);
 158	temp = RREG32(rec->mask_data_reg);
 159
 160	return 0;
 161}
 162
 163static void post_xfer(struct i2c_adapter *i2c_adap)
 164{
 165	struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
 166	struct radeon_device *rdev = i2c->dev->dev_private;
 167	struct radeon_i2c_bus_rec *rec = &i2c->rec;
 168	uint32_t temp;
 169
 170	/* unmask the gpio pins for software use */
 171	temp = RREG32(rec->mask_clk_reg) & ~rec->mask_clk_mask;
 172	WREG32(rec->mask_clk_reg, temp);
 173	temp = RREG32(rec->mask_clk_reg);
 174
 175	temp = RREG32(rec->mask_data_reg) & ~rec->mask_data_mask;
 176	WREG32(rec->mask_data_reg, temp);
 177	temp = RREG32(rec->mask_data_reg);
 178
 179	mutex_unlock(&i2c->mutex);
 180}
 181
 182static int get_clock(void *i2c_priv)
 183{
 184	struct radeon_i2c_chan *i2c = i2c_priv;
 185	struct radeon_device *rdev = i2c->dev->dev_private;
 186	struct radeon_i2c_bus_rec *rec = &i2c->rec;
 187	uint32_t val;
 188
 189	/* read the value off the pin */
 190	val = RREG32(rec->y_clk_reg);
 191	val &= rec->y_clk_mask;
 192
 193	return (val != 0);
 194}
 195
 196
 197static int get_data(void *i2c_priv)
 198{
 199	struct radeon_i2c_chan *i2c = i2c_priv;
 200	struct radeon_device *rdev = i2c->dev->dev_private;
 201	struct radeon_i2c_bus_rec *rec = &i2c->rec;
 202	uint32_t val;
 203
 204	/* read the value off the pin */
 205	val = RREG32(rec->y_data_reg);
 206	val &= rec->y_data_mask;
 207
 208	return (val != 0);
 209}
 210
 211static void set_clock(void *i2c_priv, int clock)
 212{
 213	struct radeon_i2c_chan *i2c = i2c_priv;
 214	struct radeon_device *rdev = i2c->dev->dev_private;
 215	struct radeon_i2c_bus_rec *rec = &i2c->rec;
 216	uint32_t val;
 217
 218	/* set pin direction */
 219	val = RREG32(rec->en_clk_reg) & ~rec->en_clk_mask;
 220	val |= clock ? 0 : rec->en_clk_mask;
 221	WREG32(rec->en_clk_reg, val);
 222}
 223
 224static void set_data(void *i2c_priv, int data)
 225{
 226	struct radeon_i2c_chan *i2c = i2c_priv;
 227	struct radeon_device *rdev = i2c->dev->dev_private;
 228	struct radeon_i2c_bus_rec *rec = &i2c->rec;
 229	uint32_t val;
 230
 231	/* set pin direction */
 232	val = RREG32(rec->en_data_reg) & ~rec->en_data_mask;
 233	val |= data ? 0 : rec->en_data_mask;
 234	WREG32(rec->en_data_reg, val);
 235}
 236
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 237/* hw i2c */
 238
 239static u32 radeon_get_i2c_prescale(struct radeon_device *rdev)
 240{
 241	u32 sclk = rdev->pm.current_sclk;
 242	u32 prescale = 0;
 243	u32 nm;
 244	u8 n, m, loop;
 245	int i2c_clock;
 246
 247	switch (rdev->family) {
 248	case CHIP_R100:
 249	case CHIP_RV100:
 250	case CHIP_RS100:
 251	case CHIP_RV200:
 252	case CHIP_RS200:
 253	case CHIP_R200:
 254	case CHIP_RV250:
 255	case CHIP_RS300:
 256	case CHIP_RV280:
 257	case CHIP_R300:
 258	case CHIP_R350:
 259	case CHIP_RV350:
 260		i2c_clock = 60;
 261		nm = (sclk * 10) / (i2c_clock * 4);
 262		for (loop = 1; loop < 255; loop++) {
 263			if ((nm / loop) < loop)
 264				break;
 265		}
 266		n = loop - 1;
 267		m = loop - 2;
 268		prescale = m | (n << 8);
 269		break;
 270	case CHIP_RV380:
 271	case CHIP_RS400:
 272	case CHIP_RS480:
 273	case CHIP_R420:
 274	case CHIP_R423:
 275	case CHIP_RV410:
 276		prescale = (((sclk * 10)/(4 * 128 * 100) + 1) << 8) + 128;
 277		break;
 278	case CHIP_RS600:
 279	case CHIP_RS690:
 280	case CHIP_RS740:
 281		/* todo */
 282		break;
 283	case CHIP_RV515:
 284	case CHIP_R520:
 285	case CHIP_RV530:
 286	case CHIP_RV560:
 287	case CHIP_RV570:
 288	case CHIP_R580:
 289		i2c_clock = 50;
 290		if (rdev->family == CHIP_R520)
 291			prescale = (127 << 8) + ((sclk * 10) / (4 * 127 * i2c_clock));
 292		else
 293			prescale = (((sclk * 10)/(4 * 128 * 100) + 1) << 8) + 128;
 294		break;
 295	case CHIP_R600:
 296	case CHIP_RV610:
 297	case CHIP_RV630:
 298	case CHIP_RV670:
 299		/* todo */
 300		break;
 301	case CHIP_RV620:
 302	case CHIP_RV635:
 303	case CHIP_RS780:
 304	case CHIP_RS880:
 305	case CHIP_RV770:
 306	case CHIP_RV730:
 307	case CHIP_RV710:
 308	case CHIP_RV740:
 309		/* todo */
 310		break;
 311	case CHIP_CEDAR:
 312	case CHIP_REDWOOD:
 313	case CHIP_JUNIPER:
 314	case CHIP_CYPRESS:
 315	case CHIP_HEMLOCK:
 316		/* todo */
 317		break;
 318	default:
 319		DRM_ERROR("i2c: unhandled radeon chip\n");
 320		break;
 321	}
 322	return prescale;
 323}
 324
 325
 326/* hw i2c engine for r1xx-4xx hardware
 327 * hw can buffer up to 15 bytes
 328 */
 329static int r100_hw_i2c_xfer(struct i2c_adapter *i2c_adap,
 330			    struct i2c_msg *msgs, int num)
 331{
 332	struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
 333	struct radeon_device *rdev = i2c->dev->dev_private;
 334	struct radeon_i2c_bus_rec *rec = &i2c->rec;
 335	struct i2c_msg *p;
 336	int i, j, k, ret = num;
 337	u32 prescale;
 338	u32 i2c_cntl_0, i2c_cntl_1, i2c_data;
 339	u32 tmp, reg;
 340
 341	mutex_lock(&rdev->dc_hw_i2c_mutex);
 342	/* take the pm lock since we need a constant sclk */
 343	mutex_lock(&rdev->pm.mutex);
 344
 345	prescale = radeon_get_i2c_prescale(rdev);
 346
 347	reg = ((prescale << RADEON_I2C_PRESCALE_SHIFT) |
 348	       RADEON_I2C_DRIVE_EN |
 349	       RADEON_I2C_START |
 350	       RADEON_I2C_STOP |
 351	       RADEON_I2C_GO);
 352
 353	if (rdev->is_atom_bios) {
 354		tmp = RREG32(RADEON_BIOS_6_SCRATCH);
 355		WREG32(RADEON_BIOS_6_SCRATCH, tmp | ATOM_S6_HW_I2C_BUSY_STATE);
 356	}
 357
 358	if (rec->mm_i2c) {
 359		i2c_cntl_0 = RADEON_I2C_CNTL_0;
 360		i2c_cntl_1 = RADEON_I2C_CNTL_1;
 361		i2c_data = RADEON_I2C_DATA;
 362	} else {
 363		i2c_cntl_0 = RADEON_DVI_I2C_CNTL_0;
 364		i2c_cntl_1 = RADEON_DVI_I2C_CNTL_1;
 365		i2c_data = RADEON_DVI_I2C_DATA;
 366
 367		switch (rdev->family) {
 368		case CHIP_R100:
 369		case CHIP_RV100:
 370		case CHIP_RS100:
 371		case CHIP_RV200:
 372		case CHIP_RS200:
 373		case CHIP_RS300:
 374			switch (rec->mask_clk_reg) {
 375			case RADEON_GPIO_DVI_DDC:
 376				/* no gpio select bit */
 377				break;
 378			default:
 379				DRM_ERROR("gpio not supported with hw i2c\n");
 380				ret = -EINVAL;
 381				goto done;
 382			}
 383			break;
 384		case CHIP_R200:
 385			/* only bit 4 on r200 */
 386			switch (rec->mask_clk_reg) {
 387			case RADEON_GPIO_DVI_DDC:
 388				reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1);
 389				break;
 390			case RADEON_GPIO_MONID:
 391				reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3);
 392				break;
 393			default:
 394				DRM_ERROR("gpio not supported with hw i2c\n");
 395				ret = -EINVAL;
 396				goto done;
 397			}
 398			break;
 399		case CHIP_RV250:
 400		case CHIP_RV280:
 401			/* bits 3 and 4 */
 402			switch (rec->mask_clk_reg) {
 403			case RADEON_GPIO_DVI_DDC:
 404				reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1);
 405				break;
 406			case RADEON_GPIO_VGA_DDC:
 407				reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC2);
 408				break;
 409			case RADEON_GPIO_CRT2_DDC:
 410				reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3);
 411				break;
 412			default:
 413				DRM_ERROR("gpio not supported with hw i2c\n");
 414				ret = -EINVAL;
 415				goto done;
 416			}
 417			break;
 418		case CHIP_R300:
 419		case CHIP_R350:
 420			/* only bit 4 on r300/r350 */
 421			switch (rec->mask_clk_reg) {
 422			case RADEON_GPIO_VGA_DDC:
 423				reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1);
 424				break;
 425			case RADEON_GPIO_DVI_DDC:
 426				reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3);
 427				break;
 428			default:
 429				DRM_ERROR("gpio not supported with hw i2c\n");
 430				ret = -EINVAL;
 431				goto done;
 432			}
 433			break;
 434		case CHIP_RV350:
 435		case CHIP_RV380:
 436		case CHIP_R420:
 437		case CHIP_R423:
 438		case CHIP_RV410:
 439		case CHIP_RS400:
 440		case CHIP_RS480:
 441			/* bits 3 and 4 */
 442			switch (rec->mask_clk_reg) {
 443			case RADEON_GPIO_VGA_DDC:
 444				reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1);
 445				break;
 446			case RADEON_GPIO_DVI_DDC:
 447				reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC2);
 448				break;
 449			case RADEON_GPIO_MONID:
 450				reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3);
 451				break;
 452			default:
 453				DRM_ERROR("gpio not supported with hw i2c\n");
 454				ret = -EINVAL;
 455				goto done;
 456			}
 457			break;
 458		default:
 459			DRM_ERROR("unsupported asic\n");
 460			ret = -EINVAL;
 461			goto done;
 462			break;
 463		}
 464	}
 465
 466	/* check for bus probe */
 467	p = &msgs[0];
 468	if ((num == 1) && (p->len == 0)) {
 469		WREG32(i2c_cntl_0, (RADEON_I2C_DONE |
 470				    RADEON_I2C_NACK |
 471				    RADEON_I2C_HALT |
 472				    RADEON_I2C_SOFT_RST));
 473		WREG32(i2c_data, (p->addr << 1) & 0xff);
 474		WREG32(i2c_data, 0);
 475		WREG32(i2c_cntl_1, ((1 << RADEON_I2C_DATA_COUNT_SHIFT) |
 476				    (1 << RADEON_I2C_ADDR_COUNT_SHIFT) |
 477				    RADEON_I2C_EN |
 478				    (48 << RADEON_I2C_TIME_LIMIT_SHIFT)));
 479		WREG32(i2c_cntl_0, reg);
 480		for (k = 0; k < 32; k++) {
 481			udelay(10);
 482			tmp = RREG32(i2c_cntl_0);
 483			if (tmp & RADEON_I2C_GO)
 484				continue;
 485			tmp = RREG32(i2c_cntl_0);
 486			if (tmp & RADEON_I2C_DONE)
 487				break;
 488			else {
 489				DRM_DEBUG("i2c write error 0x%08x\n", tmp);
 490				WREG32(i2c_cntl_0, tmp | RADEON_I2C_ABORT);
 491				ret = -EIO;
 492				goto done;
 493			}
 494		}
 495		goto done;
 496	}
 497
 498	for (i = 0; i < num; i++) {
 499		p = &msgs[i];
 500		for (j = 0; j < p->len; j++) {
 501			if (p->flags & I2C_M_RD) {
 502				WREG32(i2c_cntl_0, (RADEON_I2C_DONE |
 503						    RADEON_I2C_NACK |
 504						    RADEON_I2C_HALT |
 505						    RADEON_I2C_SOFT_RST));
 506				WREG32(i2c_data, ((p->addr << 1) & 0xff) | 0x1);
 507				WREG32(i2c_cntl_1, ((1 << RADEON_I2C_DATA_COUNT_SHIFT) |
 508						    (1 << RADEON_I2C_ADDR_COUNT_SHIFT) |
 509						    RADEON_I2C_EN |
 510						    (48 << RADEON_I2C_TIME_LIMIT_SHIFT)));
 511				WREG32(i2c_cntl_0, reg | RADEON_I2C_RECEIVE);
 512				for (k = 0; k < 32; k++) {
 513					udelay(10);
 514					tmp = RREG32(i2c_cntl_0);
 515					if (tmp & RADEON_I2C_GO)
 516						continue;
 517					tmp = RREG32(i2c_cntl_0);
 518					if (tmp & RADEON_I2C_DONE)
 519						break;
 520					else {
 521						DRM_DEBUG("i2c read error 0x%08x\n", tmp);
 522						WREG32(i2c_cntl_0, tmp | RADEON_I2C_ABORT);
 523						ret = -EIO;
 524						goto done;
 525					}
 526				}
 527				p->buf[j] = RREG32(i2c_data) & 0xff;
 528			} else {
 529				WREG32(i2c_cntl_0, (RADEON_I2C_DONE |
 530						    RADEON_I2C_NACK |
 531						    RADEON_I2C_HALT |
 532						    RADEON_I2C_SOFT_RST));
 533				WREG32(i2c_data, (p->addr << 1) & 0xff);
 534				WREG32(i2c_data, p->buf[j]);
 535				WREG32(i2c_cntl_1, ((1 << RADEON_I2C_DATA_COUNT_SHIFT) |
 536						    (1 << RADEON_I2C_ADDR_COUNT_SHIFT) |
 537						    RADEON_I2C_EN |
 538						    (48 << RADEON_I2C_TIME_LIMIT_SHIFT)));
 539				WREG32(i2c_cntl_0, reg);
 540				for (k = 0; k < 32; k++) {
 541					udelay(10);
 542					tmp = RREG32(i2c_cntl_0);
 543					if (tmp & RADEON_I2C_GO)
 544						continue;
 545					tmp = RREG32(i2c_cntl_0);
 546					if (tmp & RADEON_I2C_DONE)
 547						break;
 548					else {
 549						DRM_DEBUG("i2c write error 0x%08x\n", tmp);
 550						WREG32(i2c_cntl_0, tmp | RADEON_I2C_ABORT);
 551						ret = -EIO;
 552						goto done;
 553					}
 554				}
 555			}
 556		}
 557	}
 558
 559done:
 560	WREG32(i2c_cntl_0, 0);
 561	WREG32(i2c_cntl_1, 0);
 562	WREG32(i2c_cntl_0, (RADEON_I2C_DONE |
 563			    RADEON_I2C_NACK |
 564			    RADEON_I2C_HALT |
 565			    RADEON_I2C_SOFT_RST));
 566
 567	if (rdev->is_atom_bios) {
 568		tmp = RREG32(RADEON_BIOS_6_SCRATCH);
 569		tmp &= ~ATOM_S6_HW_I2C_BUSY_STATE;
 570		WREG32(RADEON_BIOS_6_SCRATCH, tmp);
 571	}
 572
 573	mutex_unlock(&rdev->pm.mutex);
 574	mutex_unlock(&rdev->dc_hw_i2c_mutex);
 575
 576	return ret;
 577}
 578
 579/* hw i2c engine for r5xx hardware
 580 * hw can buffer up to 15 bytes
 581 */
 582static int r500_hw_i2c_xfer(struct i2c_adapter *i2c_adap,
 583			    struct i2c_msg *msgs, int num)
 584{
 585	struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
 586	struct radeon_device *rdev = i2c->dev->dev_private;
 587	struct radeon_i2c_bus_rec *rec = &i2c->rec;
 588	struct i2c_msg *p;
 589	int i, j, remaining, current_count, buffer_offset, ret = num;
 590	u32 prescale;
 591	u32 tmp, reg;
 592	u32 saved1, saved2;
 593
 594	mutex_lock(&rdev->dc_hw_i2c_mutex);
 595	/* take the pm lock since we need a constant sclk */
 596	mutex_lock(&rdev->pm.mutex);
 597
 598	prescale = radeon_get_i2c_prescale(rdev);
 599
 600	/* clear gpio mask bits */
 601	tmp = RREG32(rec->mask_clk_reg);
 602	tmp &= ~rec->mask_clk_mask;
 603	WREG32(rec->mask_clk_reg, tmp);
 604	tmp = RREG32(rec->mask_clk_reg);
 605
 606	tmp = RREG32(rec->mask_data_reg);
 607	tmp &= ~rec->mask_data_mask;
 608	WREG32(rec->mask_data_reg, tmp);
 609	tmp = RREG32(rec->mask_data_reg);
 610
 611	/* clear pin values */
 612	tmp = RREG32(rec->a_clk_reg);
 613	tmp &= ~rec->a_clk_mask;
 614	WREG32(rec->a_clk_reg, tmp);
 615	tmp = RREG32(rec->a_clk_reg);
 616
 617	tmp = RREG32(rec->a_data_reg);
 618	tmp &= ~rec->a_data_mask;
 619	WREG32(rec->a_data_reg, tmp);
 620	tmp = RREG32(rec->a_data_reg);
 621
 622	/* set the pins to input */
 623	tmp = RREG32(rec->en_clk_reg);
 624	tmp &= ~rec->en_clk_mask;
 625	WREG32(rec->en_clk_reg, tmp);
 626	tmp = RREG32(rec->en_clk_reg);
 627
 628	tmp = RREG32(rec->en_data_reg);
 629	tmp &= ~rec->en_data_mask;
 630	WREG32(rec->en_data_reg, tmp);
 631	tmp = RREG32(rec->en_data_reg);
 632
 633	/* */
 634	tmp = RREG32(RADEON_BIOS_6_SCRATCH);
 635	WREG32(RADEON_BIOS_6_SCRATCH, tmp | ATOM_S6_HW_I2C_BUSY_STATE);
 636	saved1 = RREG32(AVIVO_DC_I2C_CONTROL1);
 637	saved2 = RREG32(0x494);
 638	WREG32(0x494, saved2 | 0x1);
 639
 640	WREG32(AVIVO_DC_I2C_ARBITRATION, AVIVO_DC_I2C_SW_WANTS_TO_USE_I2C);
 641	for (i = 0; i < 50; i++) {
 642		udelay(1);
 643		if (RREG32(AVIVO_DC_I2C_ARBITRATION) & AVIVO_DC_I2C_SW_CAN_USE_I2C)
 644			break;
 645	}
 646	if (i == 50) {
 647		DRM_ERROR("failed to get i2c bus\n");
 648		ret = -EBUSY;
 649		goto done;
 650	}
 651
 652	reg = AVIVO_DC_I2C_START | AVIVO_DC_I2C_STOP | AVIVO_DC_I2C_EN;
 653	switch (rec->mask_clk_reg) {
 654	case AVIVO_DC_GPIO_DDC1_MASK:
 655		reg |= AVIVO_DC_I2C_PIN_SELECT(AVIVO_SEL_DDC1);
 656		break;
 657	case AVIVO_DC_GPIO_DDC2_MASK:
 658		reg |= AVIVO_DC_I2C_PIN_SELECT(AVIVO_SEL_DDC2);
 659		break;
 660	case AVIVO_DC_GPIO_DDC3_MASK:
 661		reg |= AVIVO_DC_I2C_PIN_SELECT(AVIVO_SEL_DDC3);
 662		break;
 663	default:
 664		DRM_ERROR("gpio not supported with hw i2c\n");
 665		ret = -EINVAL;
 666		goto done;
 667	}
 668
 669	/* check for bus probe */
 670	p = &msgs[0];
 671	if ((num == 1) && (p->len == 0)) {
 672		WREG32(AVIVO_DC_I2C_STATUS1, (AVIVO_DC_I2C_DONE |
 673					      AVIVO_DC_I2C_NACK |
 674					      AVIVO_DC_I2C_HALT));
 675		WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_SOFT_RESET);
 676		udelay(1);
 677		WREG32(AVIVO_DC_I2C_RESET, 0);
 678
 679		WREG32(AVIVO_DC_I2C_DATA, (p->addr << 1) & 0xff);
 680		WREG32(AVIVO_DC_I2C_DATA, 0);
 681
 682		WREG32(AVIVO_DC_I2C_CONTROL3, AVIVO_DC_I2C_TIME_LIMIT(48));
 683		WREG32(AVIVO_DC_I2C_CONTROL2, (AVIVO_DC_I2C_ADDR_COUNT(1) |
 684					       AVIVO_DC_I2C_DATA_COUNT(1) |
 685					       (prescale << 16)));
 686		WREG32(AVIVO_DC_I2C_CONTROL1, reg);
 687		WREG32(AVIVO_DC_I2C_STATUS1, AVIVO_DC_I2C_GO);
 688		for (j = 0; j < 200; j++) {
 689			udelay(50);
 690			tmp = RREG32(AVIVO_DC_I2C_STATUS1);
 691			if (tmp & AVIVO_DC_I2C_GO)
 692				continue;
 693			tmp = RREG32(AVIVO_DC_I2C_STATUS1);
 694			if (tmp & AVIVO_DC_I2C_DONE)
 695				break;
 696			else {
 697				DRM_DEBUG("i2c write error 0x%08x\n", tmp);
 698				WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_ABORT);
 699				ret = -EIO;
 700				goto done;
 701			}
 702		}
 703		goto done;
 704	}
 705
 706	for (i = 0; i < num; i++) {
 707		p = &msgs[i];
 708		remaining = p->len;
 709		buffer_offset = 0;
 710		if (p->flags & I2C_M_RD) {
 711			while (remaining) {
 712				if (remaining > 15)
 713					current_count = 15;
 714				else
 715					current_count = remaining;
 716				WREG32(AVIVO_DC_I2C_STATUS1, (AVIVO_DC_I2C_DONE |
 717							      AVIVO_DC_I2C_NACK |
 718							      AVIVO_DC_I2C_HALT));
 719				WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_SOFT_RESET);
 720				udelay(1);
 721				WREG32(AVIVO_DC_I2C_RESET, 0);
 722
 723				WREG32(AVIVO_DC_I2C_DATA, ((p->addr << 1) & 0xff) | 0x1);
 724				WREG32(AVIVO_DC_I2C_CONTROL3, AVIVO_DC_I2C_TIME_LIMIT(48));
 725				WREG32(AVIVO_DC_I2C_CONTROL2, (AVIVO_DC_I2C_ADDR_COUNT(1) |
 726							       AVIVO_DC_I2C_DATA_COUNT(current_count) |
 727							       (prescale << 16)));
 728				WREG32(AVIVO_DC_I2C_CONTROL1, reg | AVIVO_DC_I2C_RECEIVE);
 729				WREG32(AVIVO_DC_I2C_STATUS1, AVIVO_DC_I2C_GO);
 730				for (j = 0; j < 200; j++) {
 731					udelay(50);
 732					tmp = RREG32(AVIVO_DC_I2C_STATUS1);
 733					if (tmp & AVIVO_DC_I2C_GO)
 734						continue;
 735					tmp = RREG32(AVIVO_DC_I2C_STATUS1);
 736					if (tmp & AVIVO_DC_I2C_DONE)
 737						break;
 738					else {
 739						DRM_DEBUG("i2c read error 0x%08x\n", tmp);
 740						WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_ABORT);
 741						ret = -EIO;
 742						goto done;
 743					}
 744				}
 745				for (j = 0; j < current_count; j++)
 746					p->buf[buffer_offset + j] = RREG32(AVIVO_DC_I2C_DATA) & 0xff;
 747				remaining -= current_count;
 748				buffer_offset += current_count;
 749			}
 750		} else {
 751			while (remaining) {
 752				if (remaining > 15)
 753					current_count = 15;
 754				else
 755					current_count = remaining;
 756				WREG32(AVIVO_DC_I2C_STATUS1, (AVIVO_DC_I2C_DONE |
 757							      AVIVO_DC_I2C_NACK |
 758							      AVIVO_DC_I2C_HALT));
 759				WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_SOFT_RESET);
 760				udelay(1);
 761				WREG32(AVIVO_DC_I2C_RESET, 0);
 762
 763				WREG32(AVIVO_DC_I2C_DATA, (p->addr << 1) & 0xff);
 764				for (j = 0; j < current_count; j++)
 765					WREG32(AVIVO_DC_I2C_DATA, p->buf[buffer_offset + j]);
 766
 767				WREG32(AVIVO_DC_I2C_CONTROL3, AVIVO_DC_I2C_TIME_LIMIT(48));
 768				WREG32(AVIVO_DC_I2C_CONTROL2, (AVIVO_DC_I2C_ADDR_COUNT(1) |
 769							       AVIVO_DC_I2C_DATA_COUNT(current_count) |
 770							       (prescale << 16)));
 771				WREG32(AVIVO_DC_I2C_CONTROL1, reg);
 772				WREG32(AVIVO_DC_I2C_STATUS1, AVIVO_DC_I2C_GO);
 773				for (j = 0; j < 200; j++) {
 774					udelay(50);
 775					tmp = RREG32(AVIVO_DC_I2C_STATUS1);
 776					if (tmp & AVIVO_DC_I2C_GO)
 777						continue;
 778					tmp = RREG32(AVIVO_DC_I2C_STATUS1);
 779					if (tmp & AVIVO_DC_I2C_DONE)
 780						break;
 781					else {
 782						DRM_DEBUG("i2c write error 0x%08x\n", tmp);
 783						WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_ABORT);
 784						ret = -EIO;
 785						goto done;
 786					}
 787				}
 788				remaining -= current_count;
 789				buffer_offset += current_count;
 790			}
 791		}
 792	}
 793
 794done:
 795	WREG32(AVIVO_DC_I2C_STATUS1, (AVIVO_DC_I2C_DONE |
 796				      AVIVO_DC_I2C_NACK |
 797				      AVIVO_DC_I2C_HALT));
 798	WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_SOFT_RESET);
 799	udelay(1);
 800	WREG32(AVIVO_DC_I2C_RESET, 0);
 801
 802	WREG32(AVIVO_DC_I2C_ARBITRATION, AVIVO_DC_I2C_SW_DONE_USING_I2C);
 803	WREG32(AVIVO_DC_I2C_CONTROL1, saved1);
 804	WREG32(0x494, saved2);
 805	tmp = RREG32(RADEON_BIOS_6_SCRATCH);
 806	tmp &= ~ATOM_S6_HW_I2C_BUSY_STATE;
 807	WREG32(RADEON_BIOS_6_SCRATCH, tmp);
 808
 809	mutex_unlock(&rdev->pm.mutex);
 810	mutex_unlock(&rdev->dc_hw_i2c_mutex);
 811
 812	return ret;
 813}
 814
 815static int radeon_hw_i2c_xfer(struct i2c_adapter *i2c_adap,
 816			      struct i2c_msg *msgs, int num)
 817{
 818	struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
 819	struct radeon_device *rdev = i2c->dev->dev_private;
 820	struct radeon_i2c_bus_rec *rec = &i2c->rec;
 821	int ret = 0;
 822
 823	mutex_lock(&i2c->mutex);
 824
 825	switch (rdev->family) {
 826	case CHIP_R100:
 827	case CHIP_RV100:
 828	case CHIP_RS100:
 829	case CHIP_RV200:
 830	case CHIP_RS200:
 831	case CHIP_R200:
 832	case CHIP_RV250:
 833	case CHIP_RS300:
 834	case CHIP_RV280:
 835	case CHIP_R300:
 836	case CHIP_R350:
 837	case CHIP_RV350:
 838	case CHIP_RV380:
 839	case CHIP_R420:
 840	case CHIP_R423:
 841	case CHIP_RV410:
 842	case CHIP_RS400:
 843	case CHIP_RS480:
 844		ret = r100_hw_i2c_xfer(i2c_adap, msgs, num);
 845		break;
 846	case CHIP_RS600:
 847	case CHIP_RS690:
 848	case CHIP_RS740:
 849		/* XXX fill in hw i2c implementation */
 850		break;
 851	case CHIP_RV515:
 852	case CHIP_R520:
 853	case CHIP_RV530:
 854	case CHIP_RV560:
 855	case CHIP_RV570:
 856	case CHIP_R580:
 857		if (rec->mm_i2c)
 858			ret = r100_hw_i2c_xfer(i2c_adap, msgs, num);
 859		else
 860			ret = r500_hw_i2c_xfer(i2c_adap, msgs, num);
 861		break;
 862	case CHIP_R600:
 863	case CHIP_RV610:
 864	case CHIP_RV630:
 865	case CHIP_RV670:
 866		/* XXX fill in hw i2c implementation */
 867		break;
 868	case CHIP_RV620:
 869	case CHIP_RV635:
 870	case CHIP_RS780:
 871	case CHIP_RS880:
 872	case CHIP_RV770:
 873	case CHIP_RV730:
 874	case CHIP_RV710:
 875	case CHIP_RV740:
 876		/* XXX fill in hw i2c implementation */
 877		break;
 878	case CHIP_CEDAR:
 879	case CHIP_REDWOOD:
 880	case CHIP_JUNIPER:
 881	case CHIP_CYPRESS:
 882	case CHIP_HEMLOCK:
 883		/* XXX fill in hw i2c implementation */
 884		break;
 885	default:
 886		DRM_ERROR("i2c: unhandled radeon chip\n");
 887		ret = -EIO;
 888		break;
 889	}
 890
 891	mutex_unlock(&i2c->mutex);
 892
 893	return ret;
 894}
 895
 896static u32 radeon_hw_i2c_func(struct i2c_adapter *adap)
 897{
 898	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
 899}
 900
 901static const struct i2c_algorithm radeon_i2c_algo = {
 902	.master_xfer = radeon_hw_i2c_xfer,
 903	.functionality = radeon_hw_i2c_func,
 904};
 905
 906static const struct i2c_algorithm radeon_atom_i2c_algo = {
 907	.master_xfer = radeon_atom_hw_i2c_xfer,
 908	.functionality = radeon_atom_hw_i2c_func,
 909};
 910
 911struct radeon_i2c_chan *radeon_i2c_create(struct drm_device *dev,
 912					  struct radeon_i2c_bus_rec *rec,
 913					  const char *name)
 914{
 915	struct radeon_device *rdev = dev->dev_private;
 916	struct radeon_i2c_chan *i2c;
 917	int ret;
 918
 919	/* don't add the mm_i2c bus unless hw_i2c is enabled */
 920	if (rec->mm_i2c && (radeon_hw_i2c == 0))
 921		return NULL;
 922
 923	i2c = kzalloc(sizeof(struct radeon_i2c_chan), GFP_KERNEL);
 924	if (i2c == NULL)
 925		return NULL;
 926
 927	i2c->rec = *rec;
 928	i2c->adapter.owner = THIS_MODULE;
 929	i2c->adapter.class = I2C_CLASS_DDC;
 930	i2c->adapter.dev.parent = &dev->pdev->dev;
 931	i2c->dev = dev;
 932	i2c_set_adapdata(&i2c->adapter, i2c);
 933	mutex_init(&i2c->mutex);
 934	if (rec->mm_i2c ||
 935	    (rec->hw_capable &&
 936	     radeon_hw_i2c &&
 937	     ((rdev->family <= CHIP_RS480) ||
 938	      ((rdev->family >= CHIP_RV515) && (rdev->family <= CHIP_R580))))) {
 939		/* set the radeon hw i2c adapter */
 940		snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
 941			 "Radeon i2c hw bus %s", name);
 942		i2c->adapter.algo = &radeon_i2c_algo;
 943		ret = i2c_add_adapter(&i2c->adapter);
 944		if (ret)
 945			goto out_free;
 946	} else if (rec->hw_capable &&
 947		   radeon_hw_i2c &&
 948		   ASIC_IS_DCE3(rdev)) {
 949		/* hw i2c using atom */
 950		snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
 951			 "Radeon i2c hw bus %s", name);
 952		i2c->adapter.algo = &radeon_atom_i2c_algo;
 953		ret = i2c_add_adapter(&i2c->adapter);
 954		if (ret)
 955			goto out_free;
 
 956	} else {
 957		/* set the radeon bit adapter */
 958		snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
 959			 "Radeon i2c bit bus %s", name);
 960		i2c->adapter.algo_data = &i2c->bit;
 961		i2c->bit.pre_xfer = pre_xfer;
 962		i2c->bit.post_xfer = post_xfer;
 963		i2c->bit.setsda = set_data;
 964		i2c->bit.setscl = set_clock;
 965		i2c->bit.getsda = get_data;
 966		i2c->bit.getscl = get_clock;
 967		i2c->bit.udelay = 10;
 968		i2c->bit.timeout = usecs_to_jiffies(2200);	/* from VESA */
 969		i2c->bit.data = i2c;
 
 
 970		ret = i2c_bit_add_bus(&i2c->adapter);
 971		if (ret) {
 972			DRM_ERROR("Failed to register bit i2c %s\n", name);
 973			goto out_free;
 974		}
 975	}
 976
 977	return i2c;
 978out_free:
 979	kfree(i2c);
 980	return NULL;
 981
 982}
 983
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 984void radeon_i2c_destroy(struct radeon_i2c_chan *i2c)
 985{
 986	if (!i2c)
 987		return;
 988	WARN_ON(i2c->has_aux);
 989	i2c_del_adapter(&i2c->adapter);
 990	kfree(i2c);
 991}
 992
 993/* Add the default buses */
 994void radeon_i2c_init(struct radeon_device *rdev)
 995{
 996	if (radeon_hw_i2c)
 997		DRM_INFO("hw_i2c forced on, you may experience display detection problems!\n");
 998
 999	if (rdev->is_atom_bios)
1000		radeon_atombios_i2c_init(rdev);
1001	else
1002		radeon_combios_i2c_init(rdev);
1003}
1004
1005/* remove all the buses */
1006void radeon_i2c_fini(struct radeon_device *rdev)
1007{
1008	int i;
1009
1010	for (i = 0; i < RADEON_MAX_I2C_BUS; i++) {
1011		if (rdev->i2c_bus[i]) {
1012			radeon_i2c_destroy(rdev->i2c_bus[i]);
1013			rdev->i2c_bus[i] = NULL;
1014		}
1015	}
1016}
1017
1018/* Add additional buses */
1019void radeon_i2c_add(struct radeon_device *rdev,
1020		    struct radeon_i2c_bus_rec *rec,
1021		    const char *name)
1022{
1023	struct drm_device *dev = rdev->ddev;
1024	int i;
1025
1026	for (i = 0; i < RADEON_MAX_I2C_BUS; i++) {
1027		if (!rdev->i2c_bus[i]) {
1028			rdev->i2c_bus[i] = radeon_i2c_create(dev, rec, name);
1029			return;
1030		}
1031	}
1032}
1033
1034/* looks up bus based on id */
1035struct radeon_i2c_chan *radeon_i2c_lookup(struct radeon_device *rdev,
1036					  struct radeon_i2c_bus_rec *i2c_bus)
1037{
1038	int i;
1039
1040	for (i = 0; i < RADEON_MAX_I2C_BUS; i++) {
1041		if (rdev->i2c_bus[i] &&
1042		    (rdev->i2c_bus[i]->rec.i2c_id == i2c_bus->i2c_id)) {
1043			return rdev->i2c_bus[i];
1044		}
1045	}
 
 
 
 
 
1046	return NULL;
1047}
1048
1049void radeon_i2c_get_byte(struct radeon_i2c_chan *i2c_bus,
1050			 u8 slave_addr,
1051			 u8 addr,
1052			 u8 *val)
1053{
1054	u8 out_buf[2];
1055	u8 in_buf[2];
1056	struct i2c_msg msgs[] = {
1057		{
1058			.addr = slave_addr,
1059			.flags = 0,
1060			.len = 1,
1061			.buf = out_buf,
1062		},
1063		{
1064			.addr = slave_addr,
1065			.flags = I2C_M_RD,
1066			.len = 1,
1067			.buf = in_buf,
1068		}
1069	};
1070
1071	out_buf[0] = addr;
1072	out_buf[1] = 0;
1073
1074	if (i2c_transfer(&i2c_bus->adapter, msgs, 2) == 2) {
1075		*val = in_buf[0];
1076		DRM_DEBUG("val = 0x%02x\n", *val);
1077	} else {
1078		DRM_DEBUG("i2c 0x%02x 0x%02x read failed\n",
1079			  addr, *val);
1080	}
1081}
1082
1083void radeon_i2c_put_byte(struct radeon_i2c_chan *i2c_bus,
1084			 u8 slave_addr,
1085			 u8 addr,
1086			 u8 val)
1087{
1088	uint8_t out_buf[2];
1089	struct i2c_msg msg = {
1090		.addr = slave_addr,
1091		.flags = 0,
1092		.len = 2,
1093		.buf = out_buf,
1094	};
1095
1096	out_buf[0] = addr;
1097	out_buf[1] = val;
1098
1099	if (i2c_transfer(&i2c_bus->adapter, &msg, 1) != 1)
1100		DRM_DEBUG("i2c 0x%02x 0x%02x write failed\n",
1101			  addr, val);
1102}
1103
1104/* ddc router switching */
1105void radeon_router_select_ddc_port(struct radeon_connector *radeon_connector)
1106{
1107	u8 val;
1108
1109	if (!radeon_connector->router.ddc_valid)
1110		return;
1111
1112	if (!radeon_connector->router_bus)
1113		return;
1114
1115	radeon_i2c_get_byte(radeon_connector->router_bus,
1116			    radeon_connector->router.i2c_addr,
1117			    0x3, &val);
1118	val &= ~radeon_connector->router.ddc_mux_control_pin;
1119	radeon_i2c_put_byte(radeon_connector->router_bus,
1120			    radeon_connector->router.i2c_addr,
1121			    0x3, val);
1122	radeon_i2c_get_byte(radeon_connector->router_bus,
1123			    radeon_connector->router.i2c_addr,
1124			    0x1, &val);
1125	val &= ~radeon_connector->router.ddc_mux_control_pin;
1126	val |= radeon_connector->router.ddc_mux_state;
1127	radeon_i2c_put_byte(radeon_connector->router_bus,
1128			    radeon_connector->router.i2c_addr,
1129			    0x1, val);
1130}
1131
1132/* clock/data router switching */
1133void radeon_router_select_cd_port(struct radeon_connector *radeon_connector)
1134{
1135	u8 val;
1136
1137	if (!radeon_connector->router.cd_valid)
1138		return;
1139
1140	if (!radeon_connector->router_bus)
1141		return;
1142
1143	radeon_i2c_get_byte(radeon_connector->router_bus,
1144			    radeon_connector->router.i2c_addr,
1145			    0x3, &val);
1146	val &= ~radeon_connector->router.cd_mux_control_pin;
1147	radeon_i2c_put_byte(radeon_connector->router_bus,
1148			    radeon_connector->router.i2c_addr,
1149			    0x3, val);
1150	radeon_i2c_get_byte(radeon_connector->router_bus,
1151			    radeon_connector->router.i2c_addr,
1152			    0x1, &val);
1153	val &= ~radeon_connector->router.cd_mux_control_pin;
1154	val |= radeon_connector->router.cd_mux_state;
1155	radeon_i2c_put_byte(radeon_connector->router_bus,
1156			    radeon_connector->router.i2c_addr,
1157			    0x1, val);
1158}
1159