blob: 98a8ad680109efc5fdb7b246d4a79a68839f383d [file] [log] [blame]
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001/*
2 * Copyright 2007-8 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors: Dave Airlie
24 * Alex Deucher
25 */
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040026#include <linux/export.h>
27
Jerome Glisse771fe6b2009-06-05 14:42:42 +020028#include "drmP.h"
29#include "radeon_drm.h"
30#include "radeon.h"
Alex Deucher40bacf12009-12-23 03:23:21 -050031#include "atom.h"
Jerome Glisse771fe6b2009-06-05 14:42:42 +020032
33/**
34 * radeon_ddc_probe
35 *
36 */
Alex Deucherbc1c4dc2011-10-30 16:54:27 -040037bool radeon_ddc_probe(struct radeon_connector *radeon_connector)
Jerome Glisse771fe6b2009-06-05 14:42:42 +020038{
Thomas Reime384fab2011-07-29 14:28:58 +000039 u8 out = 0x0;
40 u8 buf[8];
Jerome Glisse771fe6b2009-06-05 14:42:42 +020041 int ret;
42 struct i2c_msg msgs[] = {
43 {
44 .addr = 0x50,
45 .flags = 0,
46 .len = 1,
Thomas Reime384fab2011-07-29 14:28:58 +000047 .buf = &out,
Jerome Glisse771fe6b2009-06-05 14:42:42 +020048 },
49 {
50 .addr = 0x50,
51 .flags = I2C_M_RD,
Alex Deucherbc1c4dc2011-10-30 16:54:27 -040052 .len = 8,
Jerome Glisse771fe6b2009-06-05 14:42:42 +020053 .buf = buf,
54 }
55 };
56
Alex Deucher26b5bc92010-08-05 21:21:18 -040057 /* on hw with routers, select right port */
Alex Deucherfb939df2010-11-08 16:08:29 +000058 if (radeon_connector->router.ddc_valid)
59 radeon_router_select_ddc_port(radeon_connector);
Alex Deucher26b5bc92010-08-05 21:21:18 -040060
Jerome Glisse771fe6b2009-06-05 14:42:42 +020061 ret = i2c_transfer(&radeon_connector->ddc_bus->adapter, msgs, 2);
Thomas Reime384fab2011-07-29 14:28:58 +000062 if (ret != 2)
63 /* Couldn't find an accessible DDC on this connector */
64 return false;
Alex Deucherbc1c4dc2011-10-30 16:54:27 -040065 /* Probe also for valid EDID header
66 * EDID header starts with:
67 * 0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x00.
68 * Only the first 6 bytes must be valid as
69 * drm_edid_block_valid() can fix the last 2 bytes */
70 if (drm_edid_header_is_valid(buf) < 6) {
71 /* Couldn't find an accessible EDID on this
72 * connector */
73 return false;
Thomas Reime384fab2011-07-29 14:28:58 +000074 }
75 return true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020076}
77
Alex Deucherac1aade2010-03-14 12:22:44 -040078/* bit banging i2c */
Jerome Glisse771fe6b2009-06-05 14:42:42 +020079
Jean Delvare43e5f612011-10-13 10:39:22 +020080static int pre_xfer(struct i2c_adapter *i2c_adap)
Jerome Glisse771fe6b2009-06-05 14:42:42 +020081{
Jean Delvare43e5f612011-10-13 10:39:22 +020082 struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
Alex Deucherab1e9ea2009-11-05 18:27:30 -050083 struct radeon_device *rdev = i2c->dev->dev_private;
84 struct radeon_i2c_bus_rec *rec = &i2c->rec;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020085 uint32_t temp;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020086
87 /* RV410 appears to have a bug where the hw i2c in reset
88 * holds the i2c port in a bad state - switch hw i2c away before
89 * doing DDC - do this for all r200s/r300s/r400s for safety sake
90 */
Alex Deucher6a93cb22009-11-23 17:39:28 -050091 if (rec->hw_capable) {
92 if ((rdev->family >= CHIP_R200) && !ASIC_IS_AVIVO(rdev)) {
Alex Deucher40bacf12009-12-23 03:23:21 -050093 u32 reg;
94
95 if (rdev->family >= CHIP_RV350)
96 reg = RADEON_GPIO_MONID;
97 else if ((rdev->family == CHIP_R300) ||
98 (rdev->family == CHIP_R350))
99 reg = RADEON_GPIO_DVI_DDC;
100 else
101 reg = RADEON_GPIO_CRT2_DDC;
102
103 mutex_lock(&rdev->dc_hw_i2c_mutex);
104 if (rec->a_clk_reg == reg) {
Alex Deucher6a93cb22009-11-23 17:39:28 -0500105 WREG32(RADEON_DVI_I2C_CNTL_0, (RADEON_I2C_SOFT_RST |
106 R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1)));
107 } else {
108 WREG32(RADEON_DVI_I2C_CNTL_0, (RADEON_I2C_SOFT_RST |
109 R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3)));
110 }
Alex Deucher40bacf12009-12-23 03:23:21 -0500111 mutex_unlock(&rdev->dc_hw_i2c_mutex);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200112 }
113 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200114
Alex Deucher5786e2c2010-08-19 11:19:31 -0400115 /* switch the pads to ddc mode */
116 if (ASIC_IS_DCE3(rdev) && rec->hw_capable) {
117 temp = RREG32(rec->mask_clk_reg);
118 temp &= ~(1 << 16);
119 WREG32(rec->mask_clk_reg, temp);
120 }
121
Alex Deucher9b9fe722009-11-10 15:59:44 -0500122 /* clear the output pin values */
123 temp = RREG32(rec->a_clk_reg) & ~rec->a_clk_mask;
124 WREG32(rec->a_clk_reg, temp);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200125
Alex Deucher9b9fe722009-11-10 15:59:44 -0500126 temp = RREG32(rec->a_data_reg) & ~rec->a_data_mask;
127 WREG32(rec->a_data_reg, temp);
128
Alex Deucher6a93cb22009-11-23 17:39:28 -0500129 /* set the pins to input */
130 temp = RREG32(rec->en_clk_reg) & ~rec->en_clk_mask;
131 WREG32(rec->en_clk_reg, temp);
132
133 temp = RREG32(rec->en_data_reg) & ~rec->en_data_mask;
134 WREG32(rec->en_data_reg, temp);
Alex Deucher9b9fe722009-11-10 15:59:44 -0500135
136 /* mask the gpio pins for software use */
Jean Delvare43e5f612011-10-13 10:39:22 +0200137 temp = RREG32(rec->mask_clk_reg) | rec->mask_clk_mask;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200138 WREG32(rec->mask_clk_reg, temp);
139 temp = RREG32(rec->mask_clk_reg);
140
Jean Delvare43e5f612011-10-13 10:39:22 +0200141 temp = RREG32(rec->mask_data_reg) | rec->mask_data_mask;
142 WREG32(rec->mask_data_reg, temp);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200143 temp = RREG32(rec->mask_data_reg);
Jean Delvare43e5f612011-10-13 10:39:22 +0200144
145 return 0;
146}
147
148static void post_xfer(struct i2c_adapter *i2c_adap)
149{
150 struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
151 struct radeon_device *rdev = i2c->dev->dev_private;
152 struct radeon_i2c_bus_rec *rec = &i2c->rec;
153 uint32_t temp;
154
155 /* unmask the gpio pins for software use */
156 temp = RREG32(rec->mask_clk_reg) & ~rec->mask_clk_mask;
157 WREG32(rec->mask_clk_reg, temp);
158 temp = RREG32(rec->mask_clk_reg);
159
160 temp = RREG32(rec->mask_data_reg) & ~rec->mask_data_mask;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200161 WREG32(rec->mask_data_reg, temp);
162 temp = RREG32(rec->mask_data_reg);
163}
164
165static int get_clock(void *i2c_priv)
166{
167 struct radeon_i2c_chan *i2c = i2c_priv;
168 struct radeon_device *rdev = i2c->dev->dev_private;
169 struct radeon_i2c_bus_rec *rec = &i2c->rec;
170 uint32_t val;
171
Alex Deucher9b9fe722009-11-10 15:59:44 -0500172 /* read the value off the pin */
173 val = RREG32(rec->y_clk_reg);
174 val &= rec->y_clk_mask;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200175
176 return (val != 0);
177}
178
179
180static int get_data(void *i2c_priv)
181{
182 struct radeon_i2c_chan *i2c = i2c_priv;
183 struct radeon_device *rdev = i2c->dev->dev_private;
184 struct radeon_i2c_bus_rec *rec = &i2c->rec;
185 uint32_t val;
186
Alex Deucher9b9fe722009-11-10 15:59:44 -0500187 /* read the value off the pin */
188 val = RREG32(rec->y_data_reg);
189 val &= rec->y_data_mask;
190
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200191 return (val != 0);
192}
193
194static void set_clock(void *i2c_priv, int clock)
195{
196 struct radeon_i2c_chan *i2c = i2c_priv;
197 struct radeon_device *rdev = i2c->dev->dev_private;
198 struct radeon_i2c_bus_rec *rec = &i2c->rec;
199 uint32_t val;
200
Alex Deucher9b9fe722009-11-10 15:59:44 -0500201 /* set pin direction */
202 val = RREG32(rec->en_clk_reg) & ~rec->en_clk_mask;
203 val |= clock ? 0 : rec->en_clk_mask;
204 WREG32(rec->en_clk_reg, val);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200205}
206
207static void set_data(void *i2c_priv, int data)
208{
209 struct radeon_i2c_chan *i2c = i2c_priv;
210 struct radeon_device *rdev = i2c->dev->dev_private;
211 struct radeon_i2c_bus_rec *rec = &i2c->rec;
212 uint32_t val;
213
Alex Deucher9b9fe722009-11-10 15:59:44 -0500214 /* set pin direction */
215 val = RREG32(rec->en_data_reg) & ~rec->en_data_mask;
216 val |= data ? 0 : rec->en_data_mask;
217 WREG32(rec->en_data_reg, val);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200218}
219
Alex Deucherac1aade2010-03-14 12:22:44 -0400220/* hw i2c */
221
Alex Deucher9dad76e2010-02-08 14:34:43 -0500222static u32 radeon_get_i2c_prescale(struct radeon_device *rdev)
223{
Alex Deucher88072862010-08-10 12:33:20 -0400224 u32 sclk = rdev->pm.current_sclk;
Alex Deucher9dad76e2010-02-08 14:34:43 -0500225 u32 prescale = 0;
Alex Deucher96a4c8d2010-03-12 12:55:34 -0500226 u32 nm;
227 u8 n, m, loop;
Alex Deucher9dad76e2010-02-08 14:34:43 -0500228 int i2c_clock;
229
230 switch (rdev->family) {
231 case CHIP_R100:
232 case CHIP_RV100:
233 case CHIP_RS100:
234 case CHIP_RV200:
235 case CHIP_RS200:
236 case CHIP_R200:
237 case CHIP_RV250:
238 case CHIP_RS300:
239 case CHIP_RV280:
240 case CHIP_R300:
241 case CHIP_R350:
242 case CHIP_RV350:
Alex Deucher96a4c8d2010-03-12 12:55:34 -0500243 i2c_clock = 60;
244 nm = (sclk * 10) / (i2c_clock * 4);
Alex Deucher9dad76e2010-02-08 14:34:43 -0500245 for (loop = 1; loop < 255; loop++) {
Alex Deucher96a4c8d2010-03-12 12:55:34 -0500246 if ((nm / loop) < loop)
Alex Deucher9dad76e2010-02-08 14:34:43 -0500247 break;
248 }
Alex Deucher96a4c8d2010-03-12 12:55:34 -0500249 n = loop - 1;
250 m = loop - 2;
251 prescale = m | (n << 8);
Alex Deucher9dad76e2010-02-08 14:34:43 -0500252 break;
253 case CHIP_RV380:
254 case CHIP_RS400:
255 case CHIP_RS480:
256 case CHIP_R420:
257 case CHIP_R423:
258 case CHIP_RV410:
Alex Deucher9dad76e2010-02-08 14:34:43 -0500259 prescale = (((sclk * 10)/(4 * 128 * 100) + 1) << 8) + 128;
260 break;
261 case CHIP_RS600:
262 case CHIP_RS690:
263 case CHIP_RS740:
264 /* todo */
265 break;
266 case CHIP_RV515:
267 case CHIP_R520:
268 case CHIP_RV530:
269 case CHIP_RV560:
270 case CHIP_RV570:
271 case CHIP_R580:
272 i2c_clock = 50;
Alex Deucher9dad76e2010-02-08 14:34:43 -0500273 if (rdev->family == CHIP_R520)
274 prescale = (127 << 8) + ((sclk * 10) / (4 * 127 * i2c_clock));
275 else
276 prescale = (((sclk * 10)/(4 * 128 * 100) + 1) << 8) + 128;
277 break;
278 case CHIP_R600:
279 case CHIP_RV610:
280 case CHIP_RV630:
281 case CHIP_RV670:
282 /* todo */
283 break;
284 case CHIP_RV620:
285 case CHIP_RV635:
286 case CHIP_RS780:
287 case CHIP_RS880:
288 case CHIP_RV770:
289 case CHIP_RV730:
290 case CHIP_RV710:
291 case CHIP_RV740:
292 /* todo */
293 break;
Alex Deucher4c36b672010-02-09 18:22:00 -0500294 case CHIP_CEDAR:
295 case CHIP_REDWOOD:
296 case CHIP_JUNIPER:
297 case CHIP_CYPRESS:
298 case CHIP_HEMLOCK:
299 /* todo */
300 break;
Alex Deucher9dad76e2010-02-08 14:34:43 -0500301 default:
302 DRM_ERROR("i2c: unhandled radeon chip\n");
303 break;
304 }
305 return prescale;
306}
307
308
Alex Deucher40bacf12009-12-23 03:23:21 -0500309/* hw i2c engine for r1xx-4xx hardware
310 * hw can buffer up to 15 bytes
311 */
312static int r100_hw_i2c_xfer(struct i2c_adapter *i2c_adap,
313 struct i2c_msg *msgs, int num)
314{
315 struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
316 struct radeon_device *rdev = i2c->dev->dev_private;
317 struct radeon_i2c_bus_rec *rec = &i2c->rec;
318 struct i2c_msg *p;
319 int i, j, k, ret = num;
Alex Deucher9dad76e2010-02-08 14:34:43 -0500320 u32 prescale;
Alex Deucher40bacf12009-12-23 03:23:21 -0500321 u32 i2c_cntl_0, i2c_cntl_1, i2c_data;
322 u32 tmp, reg;
323
324 mutex_lock(&rdev->dc_hw_i2c_mutex);
Alex Deucher57fcab62010-02-06 17:06:42 -0500325 /* take the pm lock since we need a constant sclk */
326 mutex_lock(&rdev->pm.mutex);
327
Alex Deucher9dad76e2010-02-08 14:34:43 -0500328 prescale = radeon_get_i2c_prescale(rdev);
Alex Deucher40bacf12009-12-23 03:23:21 -0500329
330 reg = ((prescale << RADEON_I2C_PRESCALE_SHIFT) |
Alex Deucherae088192010-03-11 13:28:14 -0500331 RADEON_I2C_DRIVE_EN |
Alex Deucher40bacf12009-12-23 03:23:21 -0500332 RADEON_I2C_START |
333 RADEON_I2C_STOP |
334 RADEON_I2C_GO);
335
336 if (rdev->is_atom_bios) {
337 tmp = RREG32(RADEON_BIOS_6_SCRATCH);
338 WREG32(RADEON_BIOS_6_SCRATCH, tmp | ATOM_S6_HW_I2C_BUSY_STATE);
339 }
340
341 if (rec->mm_i2c) {
342 i2c_cntl_0 = RADEON_I2C_CNTL_0;
343 i2c_cntl_1 = RADEON_I2C_CNTL_1;
344 i2c_data = RADEON_I2C_DATA;
345 } else {
346 i2c_cntl_0 = RADEON_DVI_I2C_CNTL_0;
347 i2c_cntl_1 = RADEON_DVI_I2C_CNTL_1;
348 i2c_data = RADEON_DVI_I2C_DATA;
349
350 switch (rdev->family) {
351 case CHIP_R100:
352 case CHIP_RV100:
353 case CHIP_RS100:
354 case CHIP_RV200:
355 case CHIP_RS200:
356 case CHIP_RS300:
357 switch (rec->mask_clk_reg) {
358 case RADEON_GPIO_DVI_DDC:
359 /* no gpio select bit */
360 break;
361 default:
362 DRM_ERROR("gpio not supported with hw i2c\n");
363 ret = -EINVAL;
364 goto done;
365 }
366 break;
367 case CHIP_R200:
368 /* only bit 4 on r200 */
369 switch (rec->mask_clk_reg) {
370 case RADEON_GPIO_DVI_DDC:
371 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1);
372 break;
373 case RADEON_GPIO_MONID:
374 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3);
375 break;
376 default:
377 DRM_ERROR("gpio not supported with hw i2c\n");
378 ret = -EINVAL;
379 goto done;
380 }
381 break;
382 case CHIP_RV250:
383 case CHIP_RV280:
384 /* bits 3 and 4 */
385 switch (rec->mask_clk_reg) {
386 case RADEON_GPIO_DVI_DDC:
387 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1);
388 break;
389 case RADEON_GPIO_VGA_DDC:
390 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC2);
391 break;
392 case RADEON_GPIO_CRT2_DDC:
393 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3);
394 break;
395 default:
396 DRM_ERROR("gpio not supported with hw i2c\n");
397 ret = -EINVAL;
398 goto done;
399 }
400 break;
401 case CHIP_R300:
402 case CHIP_R350:
403 /* only bit 4 on r300/r350 */
404 switch (rec->mask_clk_reg) {
405 case RADEON_GPIO_VGA_DDC:
406 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1);
407 break;
408 case RADEON_GPIO_DVI_DDC:
409 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3);
410 break;
411 default:
412 DRM_ERROR("gpio not supported with hw i2c\n");
413 ret = -EINVAL;
414 goto done;
415 }
416 break;
417 case CHIP_RV350:
418 case CHIP_RV380:
419 case CHIP_R420:
420 case CHIP_R423:
421 case CHIP_RV410:
422 case CHIP_RS400:
423 case CHIP_RS480:
424 /* bits 3 and 4 */
425 switch (rec->mask_clk_reg) {
426 case RADEON_GPIO_VGA_DDC:
427 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1);
428 break;
429 case RADEON_GPIO_DVI_DDC:
430 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC2);
431 break;
432 case RADEON_GPIO_MONID:
433 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3);
434 break;
435 default:
436 DRM_ERROR("gpio not supported with hw i2c\n");
437 ret = -EINVAL;
438 goto done;
439 }
440 break;
441 default:
442 DRM_ERROR("unsupported asic\n");
443 ret = -EINVAL;
444 goto done;
445 break;
446 }
447 }
448
449 /* check for bus probe */
450 p = &msgs[0];
451 if ((num == 1) && (p->len == 0)) {
452 WREG32(i2c_cntl_0, (RADEON_I2C_DONE |
453 RADEON_I2C_NACK |
454 RADEON_I2C_HALT |
455 RADEON_I2C_SOFT_RST));
456 WREG32(i2c_data, (p->addr << 1) & 0xff);
457 WREG32(i2c_data, 0);
458 WREG32(i2c_cntl_1, ((1 << RADEON_I2C_DATA_COUNT_SHIFT) |
459 (1 << RADEON_I2C_ADDR_COUNT_SHIFT) |
460 RADEON_I2C_EN |
461 (48 << RADEON_I2C_TIME_LIMIT_SHIFT)));
462 WREG32(i2c_cntl_0, reg);
463 for (k = 0; k < 32; k++) {
464 udelay(10);
465 tmp = RREG32(i2c_cntl_0);
466 if (tmp & RADEON_I2C_GO)
467 continue;
468 tmp = RREG32(i2c_cntl_0);
469 if (tmp & RADEON_I2C_DONE)
470 break;
471 else {
472 DRM_DEBUG("i2c write error 0x%08x\n", tmp);
473 WREG32(i2c_cntl_0, tmp | RADEON_I2C_ABORT);
474 ret = -EIO;
475 goto done;
476 }
477 }
478 goto done;
479 }
480
481 for (i = 0; i < num; i++) {
482 p = &msgs[i];
483 for (j = 0; j < p->len; j++) {
484 if (p->flags & I2C_M_RD) {
485 WREG32(i2c_cntl_0, (RADEON_I2C_DONE |
486 RADEON_I2C_NACK |
487 RADEON_I2C_HALT |
488 RADEON_I2C_SOFT_RST));
489 WREG32(i2c_data, ((p->addr << 1) & 0xff) | 0x1);
490 WREG32(i2c_cntl_1, ((1 << RADEON_I2C_DATA_COUNT_SHIFT) |
491 (1 << RADEON_I2C_ADDR_COUNT_SHIFT) |
492 RADEON_I2C_EN |
493 (48 << RADEON_I2C_TIME_LIMIT_SHIFT)));
494 WREG32(i2c_cntl_0, reg | RADEON_I2C_RECEIVE);
495 for (k = 0; k < 32; k++) {
496 udelay(10);
497 tmp = RREG32(i2c_cntl_0);
498 if (tmp & RADEON_I2C_GO)
499 continue;
500 tmp = RREG32(i2c_cntl_0);
501 if (tmp & RADEON_I2C_DONE)
502 break;
503 else {
504 DRM_DEBUG("i2c read error 0x%08x\n", tmp);
505 WREG32(i2c_cntl_0, tmp | RADEON_I2C_ABORT);
506 ret = -EIO;
507 goto done;
508 }
509 }
510 p->buf[j] = RREG32(i2c_data) & 0xff;
511 } else {
512 WREG32(i2c_cntl_0, (RADEON_I2C_DONE |
513 RADEON_I2C_NACK |
514 RADEON_I2C_HALT |
515 RADEON_I2C_SOFT_RST));
516 WREG32(i2c_data, (p->addr << 1) & 0xff);
517 WREG32(i2c_data, p->buf[j]);
518 WREG32(i2c_cntl_1, ((1 << RADEON_I2C_DATA_COUNT_SHIFT) |
519 (1 << RADEON_I2C_ADDR_COUNT_SHIFT) |
520 RADEON_I2C_EN |
521 (48 << RADEON_I2C_TIME_LIMIT_SHIFT)));
522 WREG32(i2c_cntl_0, reg);
523 for (k = 0; k < 32; k++) {
524 udelay(10);
525 tmp = RREG32(i2c_cntl_0);
526 if (tmp & RADEON_I2C_GO)
527 continue;
528 tmp = RREG32(i2c_cntl_0);
529 if (tmp & RADEON_I2C_DONE)
530 break;
531 else {
532 DRM_DEBUG("i2c write error 0x%08x\n", tmp);
533 WREG32(i2c_cntl_0, tmp | RADEON_I2C_ABORT);
534 ret = -EIO;
535 goto done;
536 }
537 }
538 }
539 }
540 }
541
542done:
543 WREG32(i2c_cntl_0, 0);
544 WREG32(i2c_cntl_1, 0);
545 WREG32(i2c_cntl_0, (RADEON_I2C_DONE |
546 RADEON_I2C_NACK |
547 RADEON_I2C_HALT |
548 RADEON_I2C_SOFT_RST));
549
550 if (rdev->is_atom_bios) {
551 tmp = RREG32(RADEON_BIOS_6_SCRATCH);
552 tmp &= ~ATOM_S6_HW_I2C_BUSY_STATE;
553 WREG32(RADEON_BIOS_6_SCRATCH, tmp);
554 }
555
Alex Deucher57fcab62010-02-06 17:06:42 -0500556 mutex_unlock(&rdev->pm.mutex);
Alex Deucher40bacf12009-12-23 03:23:21 -0500557 mutex_unlock(&rdev->dc_hw_i2c_mutex);
558
559 return ret;
560}
561
562/* hw i2c engine for r5xx hardware
563 * hw can buffer up to 15 bytes
564 */
565static int r500_hw_i2c_xfer(struct i2c_adapter *i2c_adap,
566 struct i2c_msg *msgs, int num)
567{
568 struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
569 struct radeon_device *rdev = i2c->dev->dev_private;
570 struct radeon_i2c_bus_rec *rec = &i2c->rec;
571 struct i2c_msg *p;
Alex Deucher40bacf12009-12-23 03:23:21 -0500572 int i, j, remaining, current_count, buffer_offset, ret = num;
Alex Deucher9dad76e2010-02-08 14:34:43 -0500573 u32 prescale;
Alex Deucher40bacf12009-12-23 03:23:21 -0500574 u32 tmp, reg;
575 u32 saved1, saved2;
576
577 mutex_lock(&rdev->dc_hw_i2c_mutex);
Alex Deucher57fcab62010-02-06 17:06:42 -0500578 /* take the pm lock since we need a constant sclk */
579 mutex_lock(&rdev->pm.mutex);
580
Alex Deucher9dad76e2010-02-08 14:34:43 -0500581 prescale = radeon_get_i2c_prescale(rdev);
Alex Deucher40bacf12009-12-23 03:23:21 -0500582
583 /* clear gpio mask bits */
584 tmp = RREG32(rec->mask_clk_reg);
585 tmp &= ~rec->mask_clk_mask;
586 WREG32(rec->mask_clk_reg, tmp);
587 tmp = RREG32(rec->mask_clk_reg);
588
589 tmp = RREG32(rec->mask_data_reg);
590 tmp &= ~rec->mask_data_mask;
591 WREG32(rec->mask_data_reg, tmp);
592 tmp = RREG32(rec->mask_data_reg);
593
594 /* clear pin values */
595 tmp = RREG32(rec->a_clk_reg);
596 tmp &= ~rec->a_clk_mask;
597 WREG32(rec->a_clk_reg, tmp);
598 tmp = RREG32(rec->a_clk_reg);
599
600 tmp = RREG32(rec->a_data_reg);
601 tmp &= ~rec->a_data_mask;
602 WREG32(rec->a_data_reg, tmp);
603 tmp = RREG32(rec->a_data_reg);
604
605 /* set the pins to input */
606 tmp = RREG32(rec->en_clk_reg);
607 tmp &= ~rec->en_clk_mask;
608 WREG32(rec->en_clk_reg, tmp);
609 tmp = RREG32(rec->en_clk_reg);
610
611 tmp = RREG32(rec->en_data_reg);
612 tmp &= ~rec->en_data_mask;
613 WREG32(rec->en_data_reg, tmp);
614 tmp = RREG32(rec->en_data_reg);
615
616 /* */
617 tmp = RREG32(RADEON_BIOS_6_SCRATCH);
618 WREG32(RADEON_BIOS_6_SCRATCH, tmp | ATOM_S6_HW_I2C_BUSY_STATE);
619 saved1 = RREG32(AVIVO_DC_I2C_CONTROL1);
620 saved2 = RREG32(0x494);
621 WREG32(0x494, saved2 | 0x1);
622
623 WREG32(AVIVO_DC_I2C_ARBITRATION, AVIVO_DC_I2C_SW_WANTS_TO_USE_I2C);
624 for (i = 0; i < 50; i++) {
625 udelay(1);
626 if (RREG32(AVIVO_DC_I2C_ARBITRATION) & AVIVO_DC_I2C_SW_CAN_USE_I2C)
627 break;
628 }
629 if (i == 50) {
630 DRM_ERROR("failed to get i2c bus\n");
631 ret = -EBUSY;
632 goto done;
633 }
634
Alex Deucher40bacf12009-12-23 03:23:21 -0500635 reg = AVIVO_DC_I2C_START | AVIVO_DC_I2C_STOP | AVIVO_DC_I2C_EN;
636 switch (rec->mask_clk_reg) {
637 case AVIVO_DC_GPIO_DDC1_MASK:
638 reg |= AVIVO_DC_I2C_PIN_SELECT(AVIVO_SEL_DDC1);
639 break;
640 case AVIVO_DC_GPIO_DDC2_MASK:
641 reg |= AVIVO_DC_I2C_PIN_SELECT(AVIVO_SEL_DDC2);
642 break;
643 case AVIVO_DC_GPIO_DDC3_MASK:
644 reg |= AVIVO_DC_I2C_PIN_SELECT(AVIVO_SEL_DDC3);
645 break;
646 default:
647 DRM_ERROR("gpio not supported with hw i2c\n");
648 ret = -EINVAL;
649 goto done;
650 }
651
652 /* check for bus probe */
653 p = &msgs[0];
654 if ((num == 1) && (p->len == 0)) {
655 WREG32(AVIVO_DC_I2C_STATUS1, (AVIVO_DC_I2C_DONE |
656 AVIVO_DC_I2C_NACK |
657 AVIVO_DC_I2C_HALT));
658 WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_SOFT_RESET);
659 udelay(1);
660 WREG32(AVIVO_DC_I2C_RESET, 0);
661
662 WREG32(AVIVO_DC_I2C_DATA, (p->addr << 1) & 0xff);
663 WREG32(AVIVO_DC_I2C_DATA, 0);
664
665 WREG32(AVIVO_DC_I2C_CONTROL3, AVIVO_DC_I2C_TIME_LIMIT(48));
666 WREG32(AVIVO_DC_I2C_CONTROL2, (AVIVO_DC_I2C_ADDR_COUNT(1) |
667 AVIVO_DC_I2C_DATA_COUNT(1) |
668 (prescale << 16)));
669 WREG32(AVIVO_DC_I2C_CONTROL1, reg);
670 WREG32(AVIVO_DC_I2C_STATUS1, AVIVO_DC_I2C_GO);
671 for (j = 0; j < 200; j++) {
672 udelay(50);
673 tmp = RREG32(AVIVO_DC_I2C_STATUS1);
674 if (tmp & AVIVO_DC_I2C_GO)
675 continue;
676 tmp = RREG32(AVIVO_DC_I2C_STATUS1);
677 if (tmp & AVIVO_DC_I2C_DONE)
678 break;
679 else {
680 DRM_DEBUG("i2c write error 0x%08x\n", tmp);
681 WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_ABORT);
682 ret = -EIO;
683 goto done;
684 }
685 }
686 goto done;
687 }
688
689 for (i = 0; i < num; i++) {
690 p = &msgs[i];
691 remaining = p->len;
692 buffer_offset = 0;
693 if (p->flags & I2C_M_RD) {
694 while (remaining) {
695 if (remaining > 15)
696 current_count = 15;
697 else
698 current_count = remaining;
699 WREG32(AVIVO_DC_I2C_STATUS1, (AVIVO_DC_I2C_DONE |
700 AVIVO_DC_I2C_NACK |
701 AVIVO_DC_I2C_HALT));
702 WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_SOFT_RESET);
703 udelay(1);
704 WREG32(AVIVO_DC_I2C_RESET, 0);
705
706 WREG32(AVIVO_DC_I2C_DATA, ((p->addr << 1) & 0xff) | 0x1);
707 WREG32(AVIVO_DC_I2C_CONTROL3, AVIVO_DC_I2C_TIME_LIMIT(48));
708 WREG32(AVIVO_DC_I2C_CONTROL2, (AVIVO_DC_I2C_ADDR_COUNT(1) |
709 AVIVO_DC_I2C_DATA_COUNT(current_count) |
710 (prescale << 16)));
711 WREG32(AVIVO_DC_I2C_CONTROL1, reg | AVIVO_DC_I2C_RECEIVE);
712 WREG32(AVIVO_DC_I2C_STATUS1, AVIVO_DC_I2C_GO);
713 for (j = 0; j < 200; j++) {
714 udelay(50);
715 tmp = RREG32(AVIVO_DC_I2C_STATUS1);
716 if (tmp & AVIVO_DC_I2C_GO)
717 continue;
718 tmp = RREG32(AVIVO_DC_I2C_STATUS1);
719 if (tmp & AVIVO_DC_I2C_DONE)
720 break;
721 else {
722 DRM_DEBUG("i2c read error 0x%08x\n", tmp);
723 WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_ABORT);
724 ret = -EIO;
725 goto done;
726 }
727 }
728 for (j = 0; j < current_count; j++)
729 p->buf[buffer_offset + j] = RREG32(AVIVO_DC_I2C_DATA) & 0xff;
730 remaining -= current_count;
731 buffer_offset += current_count;
732 }
733 } else {
734 while (remaining) {
735 if (remaining > 15)
736 current_count = 15;
737 else
738 current_count = remaining;
739 WREG32(AVIVO_DC_I2C_STATUS1, (AVIVO_DC_I2C_DONE |
740 AVIVO_DC_I2C_NACK |
741 AVIVO_DC_I2C_HALT));
742 WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_SOFT_RESET);
743 udelay(1);
744 WREG32(AVIVO_DC_I2C_RESET, 0);
745
746 WREG32(AVIVO_DC_I2C_DATA, (p->addr << 1) & 0xff);
747 for (j = 0; j < current_count; j++)
748 WREG32(AVIVO_DC_I2C_DATA, p->buf[buffer_offset + j]);
749
750 WREG32(AVIVO_DC_I2C_CONTROL3, AVIVO_DC_I2C_TIME_LIMIT(48));
751 WREG32(AVIVO_DC_I2C_CONTROL2, (AVIVO_DC_I2C_ADDR_COUNT(1) |
752 AVIVO_DC_I2C_DATA_COUNT(current_count) |
753 (prescale << 16)));
754 WREG32(AVIVO_DC_I2C_CONTROL1, reg);
755 WREG32(AVIVO_DC_I2C_STATUS1, AVIVO_DC_I2C_GO);
756 for (j = 0; j < 200; j++) {
757 udelay(50);
758 tmp = RREG32(AVIVO_DC_I2C_STATUS1);
759 if (tmp & AVIVO_DC_I2C_GO)
760 continue;
761 tmp = RREG32(AVIVO_DC_I2C_STATUS1);
762 if (tmp & AVIVO_DC_I2C_DONE)
763 break;
764 else {
765 DRM_DEBUG("i2c write error 0x%08x\n", tmp);
766 WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_ABORT);
767 ret = -EIO;
768 goto done;
769 }
770 }
771 remaining -= current_count;
772 buffer_offset += current_count;
773 }
774 }
775 }
776
777done:
778 WREG32(AVIVO_DC_I2C_STATUS1, (AVIVO_DC_I2C_DONE |
779 AVIVO_DC_I2C_NACK |
780 AVIVO_DC_I2C_HALT));
781 WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_SOFT_RESET);
782 udelay(1);
783 WREG32(AVIVO_DC_I2C_RESET, 0);
784
785 WREG32(AVIVO_DC_I2C_ARBITRATION, AVIVO_DC_I2C_SW_DONE_USING_I2C);
786 WREG32(AVIVO_DC_I2C_CONTROL1, saved1);
787 WREG32(0x494, saved2);
788 tmp = RREG32(RADEON_BIOS_6_SCRATCH);
789 tmp &= ~ATOM_S6_HW_I2C_BUSY_STATE;
790 WREG32(RADEON_BIOS_6_SCRATCH, tmp);
791
Alex Deucher57fcab62010-02-06 17:06:42 -0500792 mutex_unlock(&rdev->pm.mutex);
Alex Deucher40bacf12009-12-23 03:23:21 -0500793 mutex_unlock(&rdev->dc_hw_i2c_mutex);
794
795 return ret;
796}
797
Alex Deucherac1aade2010-03-14 12:22:44 -0400798static int radeon_hw_i2c_xfer(struct i2c_adapter *i2c_adap,
Alex Deucher40bacf12009-12-23 03:23:21 -0500799 struct i2c_msg *msgs, int num)
Alex Deucher5a6f98f2009-12-22 15:04:48 -0500800{
801 struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
Alex Deucher40bacf12009-12-23 03:23:21 -0500802 struct radeon_device *rdev = i2c->dev->dev_private;
803 struct radeon_i2c_bus_rec *rec = &i2c->rec;
Alex Deucherac1aade2010-03-14 12:22:44 -0400804 int ret = 0;
Alex Deucher40bacf12009-12-23 03:23:21 -0500805
806 switch (rdev->family) {
807 case CHIP_R100:
808 case CHIP_RV100:
809 case CHIP_RS100:
810 case CHIP_RV200:
811 case CHIP_RS200:
812 case CHIP_R200:
813 case CHIP_RV250:
814 case CHIP_RS300:
815 case CHIP_RV280:
816 case CHIP_R300:
817 case CHIP_R350:
818 case CHIP_RV350:
819 case CHIP_RV380:
820 case CHIP_R420:
821 case CHIP_R423:
822 case CHIP_RV410:
823 case CHIP_RS400:
824 case CHIP_RS480:
Alex Deucherac1aade2010-03-14 12:22:44 -0400825 ret = r100_hw_i2c_xfer(i2c_adap, msgs, num);
Alex Deucher40bacf12009-12-23 03:23:21 -0500826 break;
827 case CHIP_RS600:
828 case CHIP_RS690:
829 case CHIP_RS740:
830 /* XXX fill in hw i2c implementation */
Alex Deucher40bacf12009-12-23 03:23:21 -0500831 break;
832 case CHIP_RV515:
833 case CHIP_R520:
834 case CHIP_RV530:
835 case CHIP_RV560:
836 case CHIP_RV570:
837 case CHIP_R580:
Alex Deucherac1aade2010-03-14 12:22:44 -0400838 if (rec->mm_i2c)
839 ret = r100_hw_i2c_xfer(i2c_adap, msgs, num);
840 else
841 ret = r500_hw_i2c_xfer(i2c_adap, msgs, num);
Alex Deucher40bacf12009-12-23 03:23:21 -0500842 break;
843 case CHIP_R600:
844 case CHIP_RV610:
845 case CHIP_RV630:
846 case CHIP_RV670:
847 /* XXX fill in hw i2c implementation */
Alex Deucher40bacf12009-12-23 03:23:21 -0500848 break;
849 case CHIP_RV620:
850 case CHIP_RV635:
851 case CHIP_RS780:
852 case CHIP_RS880:
853 case CHIP_RV770:
854 case CHIP_RV730:
855 case CHIP_RV710:
856 case CHIP_RV740:
857 /* XXX fill in hw i2c implementation */
Alex Deucher40bacf12009-12-23 03:23:21 -0500858 break;
Alex Deucher4c36b672010-02-09 18:22:00 -0500859 case CHIP_CEDAR:
860 case CHIP_REDWOOD:
861 case CHIP_JUNIPER:
862 case CHIP_CYPRESS:
863 case CHIP_HEMLOCK:
864 /* XXX fill in hw i2c implementation */
Alex Deucher4c36b672010-02-09 18:22:00 -0500865 break;
Alex Deucher40bacf12009-12-23 03:23:21 -0500866 default:
867 DRM_ERROR("i2c: unhandled radeon chip\n");
868 ret = -EIO;
869 break;
870 }
871
872 return ret;
873}
874
Alex Deucherac1aade2010-03-14 12:22:44 -0400875static u32 radeon_hw_i2c_func(struct i2c_adapter *adap)
Alex Deucher5a6f98f2009-12-22 15:04:48 -0500876{
877 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
878}
879
880static const struct i2c_algorithm radeon_i2c_algo = {
Alex Deucherac1aade2010-03-14 12:22:44 -0400881 .master_xfer = radeon_hw_i2c_xfer,
882 .functionality = radeon_hw_i2c_func,
Alex Deucher5a6f98f2009-12-22 15:04:48 -0500883};
884
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200885struct radeon_i2c_chan *radeon_i2c_create(struct drm_device *dev,
Alex Deucherab1e9ea2009-11-05 18:27:30 -0500886 struct radeon_i2c_bus_rec *rec,
887 const char *name)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200888{
Alex Deucherac1aade2010-03-14 12:22:44 -0400889 struct radeon_device *rdev = dev->dev_private;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200890 struct radeon_i2c_chan *i2c;
891 int ret;
892
Eric Anholt9a298b22009-03-24 12:23:04 -0700893 i2c = kzalloc(sizeof(struct radeon_i2c_chan), GFP_KERNEL);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200894 if (i2c == NULL)
895 return NULL;
896
Alex Deucher40bacf12009-12-23 03:23:21 -0500897 i2c->rec = *rec;
Alex Deucher5a6f98f2009-12-22 15:04:48 -0500898 i2c->adapter.owner = THIS_MODULE;
Alex Deucher87d7a1f2011-05-03 13:32:36 -0400899 i2c->adapter.class = I2C_CLASS_DDC;
Jean Delvare1ffd57c2012-01-28 11:10:38 +0100900 i2c->adapter.dev.parent = &dev->pdev->dev;
Alex Deucherac1aade2010-03-14 12:22:44 -0400901 i2c->dev = dev;
Alex Deucher5a6f98f2009-12-22 15:04:48 -0500902 i2c_set_adapdata(&i2c->adapter, i2c);
Alex Deuchere2b0a8e2010-03-17 02:07:37 -0400903 if (rec->mm_i2c ||
904 (rec->hw_capable &&
905 radeon_hw_i2c &&
906 ((rdev->family <= CHIP_RS480) ||
907 ((rdev->family >= CHIP_RV515) && (rdev->family <= CHIP_R580))))) {
Alex Deucherac1aade2010-03-14 12:22:44 -0400908 /* set the radeon hw i2c adapter */
Alex Deucher164bcb92010-11-18 11:37:18 -0500909 snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
910 "Radeon i2c hw bus %s", name);
Alex Deucherac1aade2010-03-14 12:22:44 -0400911 i2c->adapter.algo = &radeon_i2c_algo;
912 ret = i2c_add_adapter(&i2c->adapter);
913 if (ret) {
914 DRM_ERROR("Failed to register hw i2c %s\n", name);
915 goto out_free;
916 }
917 } else {
918 /* set the radeon bit adapter */
Alex Deucher164bcb92010-11-18 11:37:18 -0500919 snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
920 "Radeon i2c bit bus %s", name);
Alex Deucherac1aade2010-03-14 12:22:44 -0400921 i2c->adapter.algo_data = &i2c->algo.bit;
922 i2c->algo.bit.pre_xfer = pre_xfer;
923 i2c->algo.bit.post_xfer = post_xfer;
924 i2c->algo.bit.setsda = set_data;
925 i2c->algo.bit.setscl = set_clock;
926 i2c->algo.bit.getsda = get_data;
927 i2c->algo.bit.getscl = get_clock;
928 i2c->algo.bit.udelay = 20;
929 /* vesa says 2.2 ms is enough, 1 jiffy doesn't seem to always
930 * make this, 2 jiffies is a lot more reliable */
931 i2c->algo.bit.timeout = 2;
932 i2c->algo.bit.data = i2c;
933 ret = i2c_bit_add_bus(&i2c->adapter);
934 if (ret) {
935 DRM_ERROR("Failed to register bit i2c %s\n", name);
936 goto out_free;
937 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200938 }
939
940 return i2c;
941out_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700942 kfree(i2c);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200943 return NULL;
944
945}
946
Dave Airlie746c1aa2009-12-08 07:07:28 +1000947struct radeon_i2c_chan *radeon_i2c_create_dp(struct drm_device *dev,
Alex Deucher6a93cb22009-11-23 17:39:28 -0500948 struct radeon_i2c_bus_rec *rec,
949 const char *name)
Dave Airlie746c1aa2009-12-08 07:07:28 +1000950{
951 struct radeon_i2c_chan *i2c;
952 int ret;
953
954 i2c = kzalloc(sizeof(struct radeon_i2c_chan), GFP_KERNEL);
955 if (i2c == NULL)
956 return NULL;
957
Alex Deucher6a93cb22009-11-23 17:39:28 -0500958 i2c->rec = *rec;
Dave Airlie746c1aa2009-12-08 07:07:28 +1000959 i2c->adapter.owner = THIS_MODULE;
Alex Deucher87d7a1f2011-05-03 13:32:36 -0400960 i2c->adapter.class = I2C_CLASS_DDC;
Jean Delvare3f7e3632012-01-31 09:55:21 +0100961 i2c->adapter.dev.parent = &dev->pdev->dev;
Dave Airlie746c1aa2009-12-08 07:07:28 +1000962 i2c->dev = dev;
Alex Deucher164bcb92010-11-18 11:37:18 -0500963 snprintf(i2c->adapter.name, sizeof(i2c->adapter.name),
964 "Radeon aux bus %s", name);
Dave Airlie746c1aa2009-12-08 07:07:28 +1000965 i2c_set_adapdata(&i2c->adapter, i2c);
966 i2c->adapter.algo_data = &i2c->algo.dp;
967 i2c->algo.dp.aux_ch = radeon_dp_i2c_aux_ch;
968 i2c->algo.dp.address = 0;
969 ret = i2c_dp_aux_add_bus(&i2c->adapter);
970 if (ret) {
971 DRM_INFO("Failed to register i2c %s\n", name);
972 goto out_free;
973 }
974
975 return i2c;
976out_free:
977 kfree(i2c);
978 return NULL;
979
980}
981
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200982void radeon_i2c_destroy(struct radeon_i2c_chan *i2c)
983{
984 if (!i2c)
985 return;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200986 i2c_del_adapter(&i2c->adapter);
Eric Anholt9a298b22009-03-24 12:23:04 -0700987 kfree(i2c);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200988}
989
Alex Deucherf376b942010-08-05 21:21:16 -0400990/* Add the default buses */
991void radeon_i2c_init(struct radeon_device *rdev)
992{
993 if (rdev->is_atom_bios)
994 radeon_atombios_i2c_init(rdev);
995 else
996 radeon_combios_i2c_init(rdev);
997}
998
999/* remove all the buses */
1000void radeon_i2c_fini(struct radeon_device *rdev)
1001{
1002 int i;
1003
1004 for (i = 0; i < RADEON_MAX_I2C_BUS; i++) {
1005 if (rdev->i2c_bus[i]) {
1006 radeon_i2c_destroy(rdev->i2c_bus[i]);
1007 rdev->i2c_bus[i] = NULL;
1008 }
1009 }
1010}
1011
1012/* Add additional buses */
1013void radeon_i2c_add(struct radeon_device *rdev,
1014 struct radeon_i2c_bus_rec *rec,
1015 const char *name)
1016{
1017 struct drm_device *dev = rdev->ddev;
1018 int i;
1019
1020 for (i = 0; i < RADEON_MAX_I2C_BUS; i++) {
1021 if (!rdev->i2c_bus[i]) {
1022 rdev->i2c_bus[i] = radeon_i2c_create(dev, rec, name);
1023 return;
1024 }
1025 }
1026}
1027
1028/* looks up bus based on id */
1029struct radeon_i2c_chan *radeon_i2c_lookup(struct radeon_device *rdev,
1030 struct radeon_i2c_bus_rec *i2c_bus)
1031{
1032 int i;
1033
1034 for (i = 0; i < RADEON_MAX_I2C_BUS; i++) {
1035 if (rdev->i2c_bus[i] &&
1036 (rdev->i2c_bus[i]->rec.i2c_id == i2c_bus->i2c_id)) {
1037 return rdev->i2c_bus[i];
1038 }
1039 }
1040 return NULL;
1041}
1042
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001043struct drm_encoder *radeon_best_encoder(struct drm_connector *connector)
1044{
1045 return NULL;
1046}
Alex Deucherfcec5702009-11-10 21:25:07 -05001047
Alex Deucher5a6f98f2009-12-22 15:04:48 -05001048void radeon_i2c_get_byte(struct radeon_i2c_chan *i2c_bus,
1049 u8 slave_addr,
1050 u8 addr,
1051 u8 *val)
Alex Deucherfcec5702009-11-10 21:25:07 -05001052{
1053 u8 out_buf[2];
1054 u8 in_buf[2];
1055 struct i2c_msg msgs[] = {
1056 {
1057 .addr = slave_addr,
1058 .flags = 0,
1059 .len = 1,
1060 .buf = out_buf,
1061 },
1062 {
1063 .addr = slave_addr,
1064 .flags = I2C_M_RD,
1065 .len = 1,
1066 .buf = in_buf,
1067 }
1068 };
1069
1070 out_buf[0] = addr;
1071 out_buf[1] = 0;
1072
1073 if (i2c_transfer(&i2c_bus->adapter, msgs, 2) == 2) {
1074 *val = in_buf[0];
1075 DRM_DEBUG("val = 0x%02x\n", *val);
1076 } else {
Alex Deucherd4864d62011-04-06 13:44:10 -04001077 DRM_DEBUG("i2c 0x%02x 0x%02x read failed\n",
Alex Deucherfcec5702009-11-10 21:25:07 -05001078 addr, *val);
1079 }
1080}
1081
Alex Deucher5a6f98f2009-12-22 15:04:48 -05001082void radeon_i2c_put_byte(struct radeon_i2c_chan *i2c_bus,
1083 u8 slave_addr,
1084 u8 addr,
1085 u8 val)
Alex Deucherfcec5702009-11-10 21:25:07 -05001086{
1087 uint8_t out_buf[2];
1088 struct i2c_msg msg = {
1089 .addr = slave_addr,
1090 .flags = 0,
1091 .len = 2,
1092 .buf = out_buf,
1093 };
1094
1095 out_buf[0] = addr;
1096 out_buf[1] = val;
1097
1098 if (i2c_transfer(&i2c_bus->adapter, &msg, 1) != 1)
Alex Deucherd4864d62011-04-06 13:44:10 -04001099 DRM_DEBUG("i2c 0x%02x 0x%02x write failed\n",
Alex Deucherfcec5702009-11-10 21:25:07 -05001100 addr, val);
1101}
1102
Alex Deucherfb939df2010-11-08 16:08:29 +00001103/* ddc router switching */
1104void radeon_router_select_ddc_port(struct radeon_connector *radeon_connector)
Alex Deucher26b5bc92010-08-05 21:21:18 -04001105{
1106 u8 val;
1107
Alex Deucherfb939df2010-11-08 16:08:29 +00001108 if (!radeon_connector->router.ddc_valid)
Alex Deucher26b5bc92010-08-05 21:21:18 -04001109 return;
1110
Alex Deuchera70882a2011-04-14 17:24:07 -04001111 if (!radeon_connector->router_bus)
1112 return;
1113
Alex Deucher26b5bc92010-08-05 21:21:18 -04001114 radeon_i2c_get_byte(radeon_connector->router_bus,
1115 radeon_connector->router.i2c_addr,
1116 0x3, &val);
Tyson Whiteheadbdd91b22010-11-08 16:08:30 +00001117 val &= ~radeon_connector->router.ddc_mux_control_pin;
Alex Deucher26b5bc92010-08-05 21:21:18 -04001118 radeon_i2c_put_byte(radeon_connector->router_bus,
1119 radeon_connector->router.i2c_addr,
1120 0x3, val);
1121 radeon_i2c_get_byte(radeon_connector->router_bus,
1122 radeon_connector->router.i2c_addr,
1123 0x1, &val);
Tyson Whiteheadbdd91b22010-11-08 16:08:30 +00001124 val &= ~radeon_connector->router.ddc_mux_control_pin;
Alex Deucherfb939df2010-11-08 16:08:29 +00001125 val |= radeon_connector->router.ddc_mux_state;
1126 radeon_i2c_put_byte(radeon_connector->router_bus,
1127 radeon_connector->router.i2c_addr,
1128 0x1, val);
1129}
1130
1131/* clock/data router switching */
1132void radeon_router_select_cd_port(struct radeon_connector *radeon_connector)
1133{
1134 u8 val;
1135
1136 if (!radeon_connector->router.cd_valid)
1137 return;
1138
Alex Deuchera70882a2011-04-14 17:24:07 -04001139 if (!radeon_connector->router_bus)
1140 return;
1141
Alex Deucherfb939df2010-11-08 16:08:29 +00001142 radeon_i2c_get_byte(radeon_connector->router_bus,
1143 radeon_connector->router.i2c_addr,
1144 0x3, &val);
Tyson Whiteheadbdd91b22010-11-08 16:08:30 +00001145 val &= ~radeon_connector->router.cd_mux_control_pin;
Alex Deucherfb939df2010-11-08 16:08:29 +00001146 radeon_i2c_put_byte(radeon_connector->router_bus,
1147 radeon_connector->router.i2c_addr,
1148 0x3, val);
1149 radeon_i2c_get_byte(radeon_connector->router_bus,
1150 radeon_connector->router.i2c_addr,
1151 0x1, &val);
Tyson Whiteheadbdd91b22010-11-08 16:08:30 +00001152 val &= ~radeon_connector->router.cd_mux_control_pin;
Alex Deucherfb939df2010-11-08 16:08:29 +00001153 val |= radeon_connector->router.cd_mux_state;
Alex Deucher26b5bc92010-08-05 21:21:18 -04001154 radeon_i2c_put_byte(radeon_connector->router_bus,
1155 radeon_connector->router.i2c_addr,
1156 0x1, val);
1157}
1158