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