blob: 5d93418f9fcaa43869168a8e0b6588140934d851 [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 */
26#include "drmP.h"
27#include "radeon_drm.h"
28#include "radeon.h"
Alex Deucher40bacf12009-12-23 03:23:21 -050029#include "atom.h"
Jerome Glisse771fe6b2009-06-05 14:42:42 +020030
31/**
32 * radeon_ddc_probe
33 *
34 */
35bool radeon_ddc_probe(struct radeon_connector *radeon_connector)
36{
37 u8 out_buf[] = { 0x0, 0x0};
38 u8 buf[2];
39 int ret;
40 struct i2c_msg msgs[] = {
41 {
42 .addr = 0x50,
43 .flags = 0,
44 .len = 1,
45 .buf = out_buf,
46 },
47 {
48 .addr = 0x50,
49 .flags = I2C_M_RD,
50 .len = 1,
51 .buf = buf,
52 }
53 };
54
55 ret = i2c_transfer(&radeon_connector->ddc_bus->adapter, msgs, 2);
56 if (ret == 2)
57 return true;
58
59 return false;
60}
61
62
Alex Deucher5a6f98f2009-12-22 15:04:48 -050063static void radeon_i2c_do_lock(struct radeon_i2c_chan *i2c, int lock_state)
Jerome Glisse771fe6b2009-06-05 14:42:42 +020064{
Alex Deucherab1e9ea2009-11-05 18:27:30 -050065 struct radeon_device *rdev = i2c->dev->dev_private;
66 struct radeon_i2c_bus_rec *rec = &i2c->rec;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020067 uint32_t temp;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020068
69 /* RV410 appears to have a bug where the hw i2c in reset
70 * holds the i2c port in a bad state - switch hw i2c away before
71 * doing DDC - do this for all r200s/r300s/r400s for safety sake
72 */
Alex Deucher6a93cb22009-11-23 17:39:28 -050073 if (rec->hw_capable) {
74 if ((rdev->family >= CHIP_R200) && !ASIC_IS_AVIVO(rdev)) {
Alex Deucher40bacf12009-12-23 03:23:21 -050075 u32 reg;
76
77 if (rdev->family >= CHIP_RV350)
78 reg = RADEON_GPIO_MONID;
79 else if ((rdev->family == CHIP_R300) ||
80 (rdev->family == CHIP_R350))
81 reg = RADEON_GPIO_DVI_DDC;
82 else
83 reg = RADEON_GPIO_CRT2_DDC;
84
85 mutex_lock(&rdev->dc_hw_i2c_mutex);
86 if (rec->a_clk_reg == reg) {
Alex Deucher6a93cb22009-11-23 17:39:28 -050087 WREG32(RADEON_DVI_I2C_CNTL_0, (RADEON_I2C_SOFT_RST |
88 R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1)));
89 } else {
90 WREG32(RADEON_DVI_I2C_CNTL_0, (RADEON_I2C_SOFT_RST |
91 R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3)));
92 }
Alex Deucher40bacf12009-12-23 03:23:21 -050093 mutex_unlock(&rdev->dc_hw_i2c_mutex);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020094 }
95 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +020096
Alex Deucher9b9fe722009-11-10 15:59:44 -050097 /* clear the output pin values */
98 temp = RREG32(rec->a_clk_reg) & ~rec->a_clk_mask;
99 WREG32(rec->a_clk_reg, temp);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200100
Alex Deucher9b9fe722009-11-10 15:59:44 -0500101 temp = RREG32(rec->a_data_reg) & ~rec->a_data_mask;
102 WREG32(rec->a_data_reg, temp);
103
Alex Deucher6a93cb22009-11-23 17:39:28 -0500104 /* set the pins to input */
105 temp = RREG32(rec->en_clk_reg) & ~rec->en_clk_mask;
106 WREG32(rec->en_clk_reg, temp);
107
108 temp = RREG32(rec->en_data_reg) & ~rec->en_data_mask;
109 WREG32(rec->en_data_reg, temp);
Alex Deucher9b9fe722009-11-10 15:59:44 -0500110
111 /* mask the gpio pins for software use */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200112 temp = RREG32(rec->mask_clk_reg);
113 if (lock_state)
114 temp |= rec->mask_clk_mask;
115 else
116 temp &= ~rec->mask_clk_mask;
117 WREG32(rec->mask_clk_reg, temp);
118 temp = RREG32(rec->mask_clk_reg);
119
120 temp = RREG32(rec->mask_data_reg);
121 if (lock_state)
122 temp |= rec->mask_data_mask;
123 else
124 temp &= ~rec->mask_data_mask;
125 WREG32(rec->mask_data_reg, temp);
126 temp = RREG32(rec->mask_data_reg);
127}
128
129static int get_clock(void *i2c_priv)
130{
131 struct radeon_i2c_chan *i2c = i2c_priv;
132 struct radeon_device *rdev = i2c->dev->dev_private;
133 struct radeon_i2c_bus_rec *rec = &i2c->rec;
134 uint32_t val;
135
Alex Deucher9b9fe722009-11-10 15:59:44 -0500136 /* read the value off the pin */
137 val = RREG32(rec->y_clk_reg);
138 val &= rec->y_clk_mask;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200139
140 return (val != 0);
141}
142
143
144static int get_data(void *i2c_priv)
145{
146 struct radeon_i2c_chan *i2c = i2c_priv;
147 struct radeon_device *rdev = i2c->dev->dev_private;
148 struct radeon_i2c_bus_rec *rec = &i2c->rec;
149 uint32_t val;
150
Alex Deucher9b9fe722009-11-10 15:59:44 -0500151 /* read the value off the pin */
152 val = RREG32(rec->y_data_reg);
153 val &= rec->y_data_mask;
154
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200155 return (val != 0);
156}
157
158static void set_clock(void *i2c_priv, int clock)
159{
160 struct radeon_i2c_chan *i2c = i2c_priv;
161 struct radeon_device *rdev = i2c->dev->dev_private;
162 struct radeon_i2c_bus_rec *rec = &i2c->rec;
163 uint32_t val;
164
Alex Deucher9b9fe722009-11-10 15:59:44 -0500165 /* set pin direction */
166 val = RREG32(rec->en_clk_reg) & ~rec->en_clk_mask;
167 val |= clock ? 0 : rec->en_clk_mask;
168 WREG32(rec->en_clk_reg, val);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200169}
170
171static void set_data(void *i2c_priv, int data)
172{
173 struct radeon_i2c_chan *i2c = i2c_priv;
174 struct radeon_device *rdev = i2c->dev->dev_private;
175 struct radeon_i2c_bus_rec *rec = &i2c->rec;
176 uint32_t val;
177
Alex Deucher9b9fe722009-11-10 15:59:44 -0500178 /* set pin direction */
179 val = RREG32(rec->en_data_reg) & ~rec->en_data_mask;
180 val |= data ? 0 : rec->en_data_mask;
181 WREG32(rec->en_data_reg, val);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200182}
183
Alex Deucher9dad76e2010-02-08 14:34:43 -0500184static u32 radeon_get_i2c_prescale(struct radeon_device *rdev)
185{
186 struct radeon_pll *spll = &rdev->clock.spll;
187 u32 sclk = radeon_get_engine_clock(rdev);
188 u32 prescale = 0;
189 u32 n, m;
190 u8 loop;
191 int i2c_clock;
192
193 switch (rdev->family) {
194 case CHIP_R100:
195 case CHIP_RV100:
196 case CHIP_RS100:
197 case CHIP_RV200:
198 case CHIP_RS200:
199 case CHIP_R200:
200 case CHIP_RV250:
201 case CHIP_RS300:
202 case CHIP_RV280:
203 case CHIP_R300:
204 case CHIP_R350:
205 case CHIP_RV350:
206 n = (spll->reference_freq) / (4 * 6);
207 for (loop = 1; loop < 255; loop++) {
208 if ((loop * (loop - 1)) > n)
209 break;
210 }
211 m = loop - 1;
212 prescale = m | (loop << 8);
213 break;
214 case CHIP_RV380:
215 case CHIP_RS400:
216 case CHIP_RS480:
217 case CHIP_R420:
218 case CHIP_R423:
219 case CHIP_RV410:
220 sclk = radeon_get_engine_clock(rdev);
221 prescale = (((sclk * 10)/(4 * 128 * 100) + 1) << 8) + 128;
222 break;
223 case CHIP_RS600:
224 case CHIP_RS690:
225 case CHIP_RS740:
226 /* todo */
227 break;
228 case CHIP_RV515:
229 case CHIP_R520:
230 case CHIP_RV530:
231 case CHIP_RV560:
232 case CHIP_RV570:
233 case CHIP_R580:
234 i2c_clock = 50;
235 sclk = radeon_get_engine_clock(rdev);
236 if (rdev->family == CHIP_R520)
237 prescale = (127 << 8) + ((sclk * 10) / (4 * 127 * i2c_clock));
238 else
239 prescale = (((sclk * 10)/(4 * 128 * 100) + 1) << 8) + 128;
240 break;
241 case CHIP_R600:
242 case CHIP_RV610:
243 case CHIP_RV630:
244 case CHIP_RV670:
245 /* todo */
246 break;
247 case CHIP_RV620:
248 case CHIP_RV635:
249 case CHIP_RS780:
250 case CHIP_RS880:
251 case CHIP_RV770:
252 case CHIP_RV730:
253 case CHIP_RV710:
254 case CHIP_RV740:
255 /* todo */
256 break;
Alex Deucher4c36b672010-02-09 18:22:00 -0500257 case CHIP_CEDAR:
258 case CHIP_REDWOOD:
259 case CHIP_JUNIPER:
260 case CHIP_CYPRESS:
261 case CHIP_HEMLOCK:
262 /* todo */
263 break;
Alex Deucher9dad76e2010-02-08 14:34:43 -0500264 default:
265 DRM_ERROR("i2c: unhandled radeon chip\n");
266 break;
267 }
268 return prescale;
269}
270
271
Alex Deucher40bacf12009-12-23 03:23:21 -0500272/* hw i2c engine for r1xx-4xx hardware
273 * hw can buffer up to 15 bytes
274 */
275static int r100_hw_i2c_xfer(struct i2c_adapter *i2c_adap,
276 struct i2c_msg *msgs, int num)
277{
278 struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
279 struct radeon_device *rdev = i2c->dev->dev_private;
280 struct radeon_i2c_bus_rec *rec = &i2c->rec;
281 struct i2c_msg *p;
282 int i, j, k, ret = num;
Alex Deucher9dad76e2010-02-08 14:34:43 -0500283 u32 prescale;
Alex Deucher40bacf12009-12-23 03:23:21 -0500284 u32 i2c_cntl_0, i2c_cntl_1, i2c_data;
285 u32 tmp, reg;
286
287 mutex_lock(&rdev->dc_hw_i2c_mutex);
Alex Deucher57fcab62010-02-06 17:06:42 -0500288 /* take the pm lock since we need a constant sclk */
289 mutex_lock(&rdev->pm.mutex);
290
Alex Deucher9dad76e2010-02-08 14:34:43 -0500291 prescale = radeon_get_i2c_prescale(rdev);
Alex Deucher40bacf12009-12-23 03:23:21 -0500292
293 reg = ((prescale << RADEON_I2C_PRESCALE_SHIFT) |
Alex Deucherae088192010-03-11 13:28:14 -0500294 RADEON_I2C_DRIVE_EN |
Alex Deucher40bacf12009-12-23 03:23:21 -0500295 RADEON_I2C_START |
296 RADEON_I2C_STOP |
297 RADEON_I2C_GO);
298
299 if (rdev->is_atom_bios) {
300 tmp = RREG32(RADEON_BIOS_6_SCRATCH);
301 WREG32(RADEON_BIOS_6_SCRATCH, tmp | ATOM_S6_HW_I2C_BUSY_STATE);
302 }
303
304 if (rec->mm_i2c) {
305 i2c_cntl_0 = RADEON_I2C_CNTL_0;
306 i2c_cntl_1 = RADEON_I2C_CNTL_1;
307 i2c_data = RADEON_I2C_DATA;
308 } else {
309 i2c_cntl_0 = RADEON_DVI_I2C_CNTL_0;
310 i2c_cntl_1 = RADEON_DVI_I2C_CNTL_1;
311 i2c_data = RADEON_DVI_I2C_DATA;
312
313 switch (rdev->family) {
314 case CHIP_R100:
315 case CHIP_RV100:
316 case CHIP_RS100:
317 case CHIP_RV200:
318 case CHIP_RS200:
319 case CHIP_RS300:
320 switch (rec->mask_clk_reg) {
321 case RADEON_GPIO_DVI_DDC:
322 /* no gpio select bit */
323 break;
324 default:
325 DRM_ERROR("gpio not supported with hw i2c\n");
326 ret = -EINVAL;
327 goto done;
328 }
329 break;
330 case CHIP_R200:
331 /* only bit 4 on r200 */
332 switch (rec->mask_clk_reg) {
333 case RADEON_GPIO_DVI_DDC:
334 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1);
335 break;
336 case RADEON_GPIO_MONID:
337 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3);
338 break;
339 default:
340 DRM_ERROR("gpio not supported with hw i2c\n");
341 ret = -EINVAL;
342 goto done;
343 }
344 break;
345 case CHIP_RV250:
346 case CHIP_RV280:
347 /* bits 3 and 4 */
348 switch (rec->mask_clk_reg) {
349 case RADEON_GPIO_DVI_DDC:
350 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1);
351 break;
352 case RADEON_GPIO_VGA_DDC:
353 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC2);
354 break;
355 case RADEON_GPIO_CRT2_DDC:
356 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3);
357 break;
358 default:
359 DRM_ERROR("gpio not supported with hw i2c\n");
360 ret = -EINVAL;
361 goto done;
362 }
363 break;
364 case CHIP_R300:
365 case CHIP_R350:
366 /* only bit 4 on r300/r350 */
367 switch (rec->mask_clk_reg) {
368 case RADEON_GPIO_VGA_DDC:
369 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1);
370 break;
371 case RADEON_GPIO_DVI_DDC:
372 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3);
373 break;
374 default:
375 DRM_ERROR("gpio not supported with hw i2c\n");
376 ret = -EINVAL;
377 goto done;
378 }
379 break;
380 case CHIP_RV350:
381 case CHIP_RV380:
382 case CHIP_R420:
383 case CHIP_R423:
384 case CHIP_RV410:
385 case CHIP_RS400:
386 case CHIP_RS480:
387 /* bits 3 and 4 */
388 switch (rec->mask_clk_reg) {
389 case RADEON_GPIO_VGA_DDC:
390 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC1);
391 break;
392 case RADEON_GPIO_DVI_DDC:
393 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC2);
394 break;
395 case RADEON_GPIO_MONID:
396 reg |= R200_DVI_I2C_PIN_SEL(R200_SEL_DDC3);
397 break;
398 default:
399 DRM_ERROR("gpio not supported with hw i2c\n");
400 ret = -EINVAL;
401 goto done;
402 }
403 break;
404 default:
405 DRM_ERROR("unsupported asic\n");
406 ret = -EINVAL;
407 goto done;
408 break;
409 }
410 }
411
412 /* check for bus probe */
413 p = &msgs[0];
414 if ((num == 1) && (p->len == 0)) {
415 WREG32(i2c_cntl_0, (RADEON_I2C_DONE |
416 RADEON_I2C_NACK |
417 RADEON_I2C_HALT |
418 RADEON_I2C_SOFT_RST));
419 WREG32(i2c_data, (p->addr << 1) & 0xff);
420 WREG32(i2c_data, 0);
421 WREG32(i2c_cntl_1, ((1 << RADEON_I2C_DATA_COUNT_SHIFT) |
422 (1 << RADEON_I2C_ADDR_COUNT_SHIFT) |
423 RADEON_I2C_EN |
424 (48 << RADEON_I2C_TIME_LIMIT_SHIFT)));
425 WREG32(i2c_cntl_0, reg);
426 for (k = 0; k < 32; k++) {
427 udelay(10);
428 tmp = RREG32(i2c_cntl_0);
429 if (tmp & RADEON_I2C_GO)
430 continue;
431 tmp = RREG32(i2c_cntl_0);
432 if (tmp & RADEON_I2C_DONE)
433 break;
434 else {
435 DRM_DEBUG("i2c write error 0x%08x\n", tmp);
436 WREG32(i2c_cntl_0, tmp | RADEON_I2C_ABORT);
437 ret = -EIO;
438 goto done;
439 }
440 }
441 goto done;
442 }
443
444 for (i = 0; i < num; i++) {
445 p = &msgs[i];
446 for (j = 0; j < p->len; j++) {
447 if (p->flags & I2C_M_RD) {
448 WREG32(i2c_cntl_0, (RADEON_I2C_DONE |
449 RADEON_I2C_NACK |
450 RADEON_I2C_HALT |
451 RADEON_I2C_SOFT_RST));
452 WREG32(i2c_data, ((p->addr << 1) & 0xff) | 0x1);
453 WREG32(i2c_cntl_1, ((1 << RADEON_I2C_DATA_COUNT_SHIFT) |
454 (1 << RADEON_I2C_ADDR_COUNT_SHIFT) |
455 RADEON_I2C_EN |
456 (48 << RADEON_I2C_TIME_LIMIT_SHIFT)));
457 WREG32(i2c_cntl_0, reg | RADEON_I2C_RECEIVE);
458 for (k = 0; k < 32; k++) {
459 udelay(10);
460 tmp = RREG32(i2c_cntl_0);
461 if (tmp & RADEON_I2C_GO)
462 continue;
463 tmp = RREG32(i2c_cntl_0);
464 if (tmp & RADEON_I2C_DONE)
465 break;
466 else {
467 DRM_DEBUG("i2c read error 0x%08x\n", tmp);
468 WREG32(i2c_cntl_0, tmp | RADEON_I2C_ABORT);
469 ret = -EIO;
470 goto done;
471 }
472 }
473 p->buf[j] = RREG32(i2c_data) & 0xff;
474 } else {
475 WREG32(i2c_cntl_0, (RADEON_I2C_DONE |
476 RADEON_I2C_NACK |
477 RADEON_I2C_HALT |
478 RADEON_I2C_SOFT_RST));
479 WREG32(i2c_data, (p->addr << 1) & 0xff);
480 WREG32(i2c_data, p->buf[j]);
481 WREG32(i2c_cntl_1, ((1 << RADEON_I2C_DATA_COUNT_SHIFT) |
482 (1 << RADEON_I2C_ADDR_COUNT_SHIFT) |
483 RADEON_I2C_EN |
484 (48 << RADEON_I2C_TIME_LIMIT_SHIFT)));
485 WREG32(i2c_cntl_0, reg);
486 for (k = 0; k < 32; k++) {
487 udelay(10);
488 tmp = RREG32(i2c_cntl_0);
489 if (tmp & RADEON_I2C_GO)
490 continue;
491 tmp = RREG32(i2c_cntl_0);
492 if (tmp & RADEON_I2C_DONE)
493 break;
494 else {
495 DRM_DEBUG("i2c write error 0x%08x\n", tmp);
496 WREG32(i2c_cntl_0, tmp | RADEON_I2C_ABORT);
497 ret = -EIO;
498 goto done;
499 }
500 }
501 }
502 }
503 }
504
505done:
506 WREG32(i2c_cntl_0, 0);
507 WREG32(i2c_cntl_1, 0);
508 WREG32(i2c_cntl_0, (RADEON_I2C_DONE |
509 RADEON_I2C_NACK |
510 RADEON_I2C_HALT |
511 RADEON_I2C_SOFT_RST));
512
513 if (rdev->is_atom_bios) {
514 tmp = RREG32(RADEON_BIOS_6_SCRATCH);
515 tmp &= ~ATOM_S6_HW_I2C_BUSY_STATE;
516 WREG32(RADEON_BIOS_6_SCRATCH, tmp);
517 }
518
Alex Deucher57fcab62010-02-06 17:06:42 -0500519 mutex_unlock(&rdev->pm.mutex);
Alex Deucher40bacf12009-12-23 03:23:21 -0500520 mutex_unlock(&rdev->dc_hw_i2c_mutex);
521
522 return ret;
523}
524
525/* hw i2c engine for r5xx hardware
526 * hw can buffer up to 15 bytes
527 */
528static int r500_hw_i2c_xfer(struct i2c_adapter *i2c_adap,
529 struct i2c_msg *msgs, int num)
530{
531 struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
532 struct radeon_device *rdev = i2c->dev->dev_private;
533 struct radeon_i2c_bus_rec *rec = &i2c->rec;
534 struct i2c_msg *p;
Alex Deucher40bacf12009-12-23 03:23:21 -0500535 int i, j, remaining, current_count, buffer_offset, ret = num;
Alex Deucher9dad76e2010-02-08 14:34:43 -0500536 u32 prescale;
Alex Deucher40bacf12009-12-23 03:23:21 -0500537 u32 tmp, reg;
538 u32 saved1, saved2;
539
540 mutex_lock(&rdev->dc_hw_i2c_mutex);
Alex Deucher57fcab62010-02-06 17:06:42 -0500541 /* take the pm lock since we need a constant sclk */
542 mutex_lock(&rdev->pm.mutex);
543
Alex Deucher9dad76e2010-02-08 14:34:43 -0500544 prescale = radeon_get_i2c_prescale(rdev);
Alex Deucher40bacf12009-12-23 03:23:21 -0500545
546 /* clear gpio mask bits */
547 tmp = RREG32(rec->mask_clk_reg);
548 tmp &= ~rec->mask_clk_mask;
549 WREG32(rec->mask_clk_reg, tmp);
550 tmp = RREG32(rec->mask_clk_reg);
551
552 tmp = RREG32(rec->mask_data_reg);
553 tmp &= ~rec->mask_data_mask;
554 WREG32(rec->mask_data_reg, tmp);
555 tmp = RREG32(rec->mask_data_reg);
556
557 /* clear pin values */
558 tmp = RREG32(rec->a_clk_reg);
559 tmp &= ~rec->a_clk_mask;
560 WREG32(rec->a_clk_reg, tmp);
561 tmp = RREG32(rec->a_clk_reg);
562
563 tmp = RREG32(rec->a_data_reg);
564 tmp &= ~rec->a_data_mask;
565 WREG32(rec->a_data_reg, tmp);
566 tmp = RREG32(rec->a_data_reg);
567
568 /* set the pins to input */
569 tmp = RREG32(rec->en_clk_reg);
570 tmp &= ~rec->en_clk_mask;
571 WREG32(rec->en_clk_reg, tmp);
572 tmp = RREG32(rec->en_clk_reg);
573
574 tmp = RREG32(rec->en_data_reg);
575 tmp &= ~rec->en_data_mask;
576 WREG32(rec->en_data_reg, tmp);
577 tmp = RREG32(rec->en_data_reg);
578
579 /* */
580 tmp = RREG32(RADEON_BIOS_6_SCRATCH);
581 WREG32(RADEON_BIOS_6_SCRATCH, tmp | ATOM_S6_HW_I2C_BUSY_STATE);
582 saved1 = RREG32(AVIVO_DC_I2C_CONTROL1);
583 saved2 = RREG32(0x494);
584 WREG32(0x494, saved2 | 0x1);
585
586 WREG32(AVIVO_DC_I2C_ARBITRATION, AVIVO_DC_I2C_SW_WANTS_TO_USE_I2C);
587 for (i = 0; i < 50; i++) {
588 udelay(1);
589 if (RREG32(AVIVO_DC_I2C_ARBITRATION) & AVIVO_DC_I2C_SW_CAN_USE_I2C)
590 break;
591 }
592 if (i == 50) {
593 DRM_ERROR("failed to get i2c bus\n");
594 ret = -EBUSY;
595 goto done;
596 }
597
Alex Deucher40bacf12009-12-23 03:23:21 -0500598 reg = AVIVO_DC_I2C_START | AVIVO_DC_I2C_STOP | AVIVO_DC_I2C_EN;
599 switch (rec->mask_clk_reg) {
600 case AVIVO_DC_GPIO_DDC1_MASK:
601 reg |= AVIVO_DC_I2C_PIN_SELECT(AVIVO_SEL_DDC1);
602 break;
603 case AVIVO_DC_GPIO_DDC2_MASK:
604 reg |= AVIVO_DC_I2C_PIN_SELECT(AVIVO_SEL_DDC2);
605 break;
606 case AVIVO_DC_GPIO_DDC3_MASK:
607 reg |= AVIVO_DC_I2C_PIN_SELECT(AVIVO_SEL_DDC3);
608 break;
609 default:
610 DRM_ERROR("gpio not supported with hw i2c\n");
611 ret = -EINVAL;
612 goto done;
613 }
614
615 /* check for bus probe */
616 p = &msgs[0];
617 if ((num == 1) && (p->len == 0)) {
618 WREG32(AVIVO_DC_I2C_STATUS1, (AVIVO_DC_I2C_DONE |
619 AVIVO_DC_I2C_NACK |
620 AVIVO_DC_I2C_HALT));
621 WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_SOFT_RESET);
622 udelay(1);
623 WREG32(AVIVO_DC_I2C_RESET, 0);
624
625 WREG32(AVIVO_DC_I2C_DATA, (p->addr << 1) & 0xff);
626 WREG32(AVIVO_DC_I2C_DATA, 0);
627
628 WREG32(AVIVO_DC_I2C_CONTROL3, AVIVO_DC_I2C_TIME_LIMIT(48));
629 WREG32(AVIVO_DC_I2C_CONTROL2, (AVIVO_DC_I2C_ADDR_COUNT(1) |
630 AVIVO_DC_I2C_DATA_COUNT(1) |
631 (prescale << 16)));
632 WREG32(AVIVO_DC_I2C_CONTROL1, reg);
633 WREG32(AVIVO_DC_I2C_STATUS1, AVIVO_DC_I2C_GO);
634 for (j = 0; j < 200; j++) {
635 udelay(50);
636 tmp = RREG32(AVIVO_DC_I2C_STATUS1);
637 if (tmp & AVIVO_DC_I2C_GO)
638 continue;
639 tmp = RREG32(AVIVO_DC_I2C_STATUS1);
640 if (tmp & AVIVO_DC_I2C_DONE)
641 break;
642 else {
643 DRM_DEBUG("i2c write error 0x%08x\n", tmp);
644 WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_ABORT);
645 ret = -EIO;
646 goto done;
647 }
648 }
649 goto done;
650 }
651
652 for (i = 0; i < num; i++) {
653 p = &msgs[i];
654 remaining = p->len;
655 buffer_offset = 0;
656 if (p->flags & I2C_M_RD) {
657 while (remaining) {
658 if (remaining > 15)
659 current_count = 15;
660 else
661 current_count = remaining;
662 WREG32(AVIVO_DC_I2C_STATUS1, (AVIVO_DC_I2C_DONE |
663 AVIVO_DC_I2C_NACK |
664 AVIVO_DC_I2C_HALT));
665 WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_SOFT_RESET);
666 udelay(1);
667 WREG32(AVIVO_DC_I2C_RESET, 0);
668
669 WREG32(AVIVO_DC_I2C_DATA, ((p->addr << 1) & 0xff) | 0x1);
670 WREG32(AVIVO_DC_I2C_CONTROL3, AVIVO_DC_I2C_TIME_LIMIT(48));
671 WREG32(AVIVO_DC_I2C_CONTROL2, (AVIVO_DC_I2C_ADDR_COUNT(1) |
672 AVIVO_DC_I2C_DATA_COUNT(current_count) |
673 (prescale << 16)));
674 WREG32(AVIVO_DC_I2C_CONTROL1, reg | AVIVO_DC_I2C_RECEIVE);
675 WREG32(AVIVO_DC_I2C_STATUS1, AVIVO_DC_I2C_GO);
676 for (j = 0; j < 200; j++) {
677 udelay(50);
678 tmp = RREG32(AVIVO_DC_I2C_STATUS1);
679 if (tmp & AVIVO_DC_I2C_GO)
680 continue;
681 tmp = RREG32(AVIVO_DC_I2C_STATUS1);
682 if (tmp & AVIVO_DC_I2C_DONE)
683 break;
684 else {
685 DRM_DEBUG("i2c read error 0x%08x\n", tmp);
686 WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_ABORT);
687 ret = -EIO;
688 goto done;
689 }
690 }
691 for (j = 0; j < current_count; j++)
692 p->buf[buffer_offset + j] = RREG32(AVIVO_DC_I2C_DATA) & 0xff;
693 remaining -= current_count;
694 buffer_offset += current_count;
695 }
696 } else {
697 while (remaining) {
698 if (remaining > 15)
699 current_count = 15;
700 else
701 current_count = remaining;
702 WREG32(AVIVO_DC_I2C_STATUS1, (AVIVO_DC_I2C_DONE |
703 AVIVO_DC_I2C_NACK |
704 AVIVO_DC_I2C_HALT));
705 WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_SOFT_RESET);
706 udelay(1);
707 WREG32(AVIVO_DC_I2C_RESET, 0);
708
709 WREG32(AVIVO_DC_I2C_DATA, (p->addr << 1) & 0xff);
710 for (j = 0; j < current_count; j++)
711 WREG32(AVIVO_DC_I2C_DATA, p->buf[buffer_offset + j]);
712
713 WREG32(AVIVO_DC_I2C_CONTROL3, AVIVO_DC_I2C_TIME_LIMIT(48));
714 WREG32(AVIVO_DC_I2C_CONTROL2, (AVIVO_DC_I2C_ADDR_COUNT(1) |
715 AVIVO_DC_I2C_DATA_COUNT(current_count) |
716 (prescale << 16)));
717 WREG32(AVIVO_DC_I2C_CONTROL1, reg);
718 WREG32(AVIVO_DC_I2C_STATUS1, AVIVO_DC_I2C_GO);
719 for (j = 0; j < 200; j++) {
720 udelay(50);
721 tmp = RREG32(AVIVO_DC_I2C_STATUS1);
722 if (tmp & AVIVO_DC_I2C_GO)
723 continue;
724 tmp = RREG32(AVIVO_DC_I2C_STATUS1);
725 if (tmp & AVIVO_DC_I2C_DONE)
726 break;
727 else {
728 DRM_DEBUG("i2c write error 0x%08x\n", tmp);
729 WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_ABORT);
730 ret = -EIO;
731 goto done;
732 }
733 }
734 remaining -= current_count;
735 buffer_offset += current_count;
736 }
737 }
738 }
739
740done:
741 WREG32(AVIVO_DC_I2C_STATUS1, (AVIVO_DC_I2C_DONE |
742 AVIVO_DC_I2C_NACK |
743 AVIVO_DC_I2C_HALT));
744 WREG32(AVIVO_DC_I2C_RESET, AVIVO_DC_I2C_SOFT_RESET);
745 udelay(1);
746 WREG32(AVIVO_DC_I2C_RESET, 0);
747
748 WREG32(AVIVO_DC_I2C_ARBITRATION, AVIVO_DC_I2C_SW_DONE_USING_I2C);
749 WREG32(AVIVO_DC_I2C_CONTROL1, saved1);
750 WREG32(0x494, saved2);
751 tmp = RREG32(RADEON_BIOS_6_SCRATCH);
752 tmp &= ~ATOM_S6_HW_I2C_BUSY_STATE;
753 WREG32(RADEON_BIOS_6_SCRATCH, tmp);
754
Alex Deucher57fcab62010-02-06 17:06:42 -0500755 mutex_unlock(&rdev->pm.mutex);
Alex Deucher40bacf12009-12-23 03:23:21 -0500756 mutex_unlock(&rdev->dc_hw_i2c_mutex);
757
758 return ret;
759}
760
761static int radeon_sw_i2c_xfer(struct i2c_adapter *i2c_adap,
762 struct i2c_msg *msgs, int num)
Alex Deucher5a6f98f2009-12-22 15:04:48 -0500763{
764 struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
765 int ret;
766
767 radeon_i2c_do_lock(i2c, 1);
Alex Deucher40bacf12009-12-23 03:23:21 -0500768 ret = i2c_transfer(&i2c->algo.radeon.bit_adapter, msgs, num);
Alex Deucher5a6f98f2009-12-22 15:04:48 -0500769 radeon_i2c_do_lock(i2c, 0);
770
771 return ret;
772}
773
Alex Deucher40bacf12009-12-23 03:23:21 -0500774static int radeon_i2c_xfer(struct i2c_adapter *i2c_adap,
775 struct i2c_msg *msgs, int num)
776{
777 struct radeon_i2c_chan *i2c = i2c_get_adapdata(i2c_adap);
778 struct radeon_device *rdev = i2c->dev->dev_private;
779 struct radeon_i2c_bus_rec *rec = &i2c->rec;
780 int ret;
781
782 switch (rdev->family) {
783 case CHIP_R100:
784 case CHIP_RV100:
785 case CHIP_RS100:
786 case CHIP_RV200:
787 case CHIP_RS200:
788 case CHIP_R200:
789 case CHIP_RV250:
790 case CHIP_RS300:
791 case CHIP_RV280:
792 case CHIP_R300:
793 case CHIP_R350:
794 case CHIP_RV350:
795 case CHIP_RV380:
796 case CHIP_R420:
797 case CHIP_R423:
798 case CHIP_RV410:
799 case CHIP_RS400:
800 case CHIP_RS480:
801 if (rec->hw_capable)
802 ret = r100_hw_i2c_xfer(i2c_adap, msgs, num);
803 else
804 ret = radeon_sw_i2c_xfer(i2c_adap, msgs, num);
805 break;
806 case CHIP_RS600:
807 case CHIP_RS690:
808 case CHIP_RS740:
809 /* XXX fill in hw i2c implementation */
810 ret = radeon_sw_i2c_xfer(i2c_adap, msgs, num);
811 break;
812 case CHIP_RV515:
813 case CHIP_R520:
814 case CHIP_RV530:
815 case CHIP_RV560:
816 case CHIP_RV570:
817 case CHIP_R580:
818 if (rec->hw_capable) {
819 if (rec->mm_i2c)
820 ret = r100_hw_i2c_xfer(i2c_adap, msgs, num);
821 else
822 ret = r500_hw_i2c_xfer(i2c_adap, msgs, num);
823 } else
824 ret = radeon_sw_i2c_xfer(i2c_adap, msgs, num);
825 break;
826 case CHIP_R600:
827 case CHIP_RV610:
828 case CHIP_RV630:
829 case CHIP_RV670:
830 /* XXX fill in hw i2c implementation */
831 ret = radeon_sw_i2c_xfer(i2c_adap, msgs, num);
832 break;
833 case CHIP_RV620:
834 case CHIP_RV635:
835 case CHIP_RS780:
836 case CHIP_RS880:
837 case CHIP_RV770:
838 case CHIP_RV730:
839 case CHIP_RV710:
840 case CHIP_RV740:
841 /* XXX fill in hw i2c implementation */
842 ret = radeon_sw_i2c_xfer(i2c_adap, msgs, num);
843 break;
Alex Deucher4c36b672010-02-09 18:22:00 -0500844 case CHIP_CEDAR:
845 case CHIP_REDWOOD:
846 case CHIP_JUNIPER:
847 case CHIP_CYPRESS:
848 case CHIP_HEMLOCK:
849 /* XXX fill in hw i2c implementation */
850 ret = radeon_sw_i2c_xfer(i2c_adap, msgs, num);
851 break;
Alex Deucher40bacf12009-12-23 03:23:21 -0500852 default:
853 DRM_ERROR("i2c: unhandled radeon chip\n");
854 ret = -EIO;
855 break;
856 }
857
858 return ret;
859}
860
Alex Deucher5a6f98f2009-12-22 15:04:48 -0500861static u32 radeon_i2c_func(struct i2c_adapter *adap)
862{
863 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
864}
865
866static const struct i2c_algorithm radeon_i2c_algo = {
867 .master_xfer = radeon_i2c_xfer,
868 .functionality = radeon_i2c_func,
869};
870
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200871struct radeon_i2c_chan *radeon_i2c_create(struct drm_device *dev,
Alex Deucherab1e9ea2009-11-05 18:27:30 -0500872 struct radeon_i2c_bus_rec *rec,
873 const char *name)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200874{
875 struct radeon_i2c_chan *i2c;
876 int ret;
877
Eric Anholt9a298b22009-03-24 12:23:04 -0700878 i2c = kzalloc(sizeof(struct radeon_i2c_chan), GFP_KERNEL);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200879 if (i2c == NULL)
880 return NULL;
881
Alex Deucher5a6f98f2009-12-22 15:04:48 -0500882 /* set the internal bit adapter */
883 i2c->algo.radeon.bit_adapter.owner = THIS_MODULE;
884 i2c_set_adapdata(&i2c->algo.radeon.bit_adapter, i2c);
885 sprintf(i2c->algo.radeon.bit_adapter.name, "Radeon internal i2c bit bus %s", name);
886 i2c->algo.radeon.bit_adapter.algo_data = &i2c->algo.radeon.bit_data;
887 i2c->algo.radeon.bit_data.setsda = set_data;
888 i2c->algo.radeon.bit_data.setscl = set_clock;
889 i2c->algo.radeon.bit_data.getsda = get_data;
890 i2c->algo.radeon.bit_data.getscl = get_clock;
891 i2c->algo.radeon.bit_data.udelay = 20;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200892 /* vesa says 2.2 ms is enough, 1 jiffy doesn't seem to always
893 * make this, 2 jiffies is a lot more reliable */
Alex Deucher5a6f98f2009-12-22 15:04:48 -0500894 i2c->algo.radeon.bit_data.timeout = 2;
895 i2c->algo.radeon.bit_data.data = i2c;
896 ret = i2c_bit_add_bus(&i2c->algo.radeon.bit_adapter);
897 if (ret) {
Alex Deucher40bacf12009-12-23 03:23:21 -0500898 DRM_ERROR("Failed to register internal bit i2c %s\n", name);
Alex Deucher5a6f98f2009-12-22 15:04:48 -0500899 goto out_free;
900 }
901 /* set the radeon i2c adapter */
Alex Deucher40bacf12009-12-23 03:23:21 -0500902 i2c->dev = dev;
903 i2c->rec = *rec;
Alex Deucher5a6f98f2009-12-22 15:04:48 -0500904 i2c->adapter.owner = THIS_MODULE;
905 i2c_set_adapdata(&i2c->adapter, i2c);
906 sprintf(i2c->adapter.name, "Radeon i2c %s", name);
907 i2c->adapter.algo_data = &i2c->algo.radeon;
908 i2c->adapter.algo = &radeon_i2c_algo;
909 ret = i2c_add_adapter(&i2c->adapter);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200910 if (ret) {
Alex Deucher40bacf12009-12-23 03:23:21 -0500911 DRM_ERROR("Failed to register i2c %s\n", name);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200912 goto out_free;
913 }
914
915 return i2c;
916out_free:
Eric Anholt9a298b22009-03-24 12:23:04 -0700917 kfree(i2c);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200918 return NULL;
919
920}
921
Dave Airlie746c1aa2009-12-08 07:07:28 +1000922struct radeon_i2c_chan *radeon_i2c_create_dp(struct drm_device *dev,
Alex Deucher6a93cb22009-11-23 17:39:28 -0500923 struct radeon_i2c_bus_rec *rec,
924 const char *name)
Dave Airlie746c1aa2009-12-08 07:07:28 +1000925{
926 struct radeon_i2c_chan *i2c;
927 int ret;
928
929 i2c = kzalloc(sizeof(struct radeon_i2c_chan), GFP_KERNEL);
930 if (i2c == NULL)
931 return NULL;
932
Alex Deucher6a93cb22009-11-23 17:39:28 -0500933 i2c->rec = *rec;
Dave Airlie746c1aa2009-12-08 07:07:28 +1000934 i2c->adapter.owner = THIS_MODULE;
935 i2c->dev = dev;
936 i2c_set_adapdata(&i2c->adapter, i2c);
937 i2c->adapter.algo_data = &i2c->algo.dp;
938 i2c->algo.dp.aux_ch = radeon_dp_i2c_aux_ch;
939 i2c->algo.dp.address = 0;
940 ret = i2c_dp_aux_add_bus(&i2c->adapter);
941 if (ret) {
942 DRM_INFO("Failed to register i2c %s\n", name);
943 goto out_free;
944 }
945
946 return i2c;
947out_free:
948 kfree(i2c);
949 return NULL;
950
951}
952
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200953void radeon_i2c_destroy(struct radeon_i2c_chan *i2c)
954{
955 if (!i2c)
956 return;
Alex Deucher5a6f98f2009-12-22 15:04:48 -0500957 i2c_del_adapter(&i2c->algo.radeon.bit_adapter);
958 i2c_del_adapter(&i2c->adapter);
959 kfree(i2c);
960}
961
962void radeon_i2c_destroy_dp(struct radeon_i2c_chan *i2c)
963{
964 if (!i2c)
965 return;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200966
967 i2c_del_adapter(&i2c->adapter);
Eric Anholt9a298b22009-03-24 12:23:04 -0700968 kfree(i2c);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200969}
970
971struct drm_encoder *radeon_best_encoder(struct drm_connector *connector)
972{
973 return NULL;
974}
Alex Deucherfcec5702009-11-10 21:25:07 -0500975
Alex Deucher5a6f98f2009-12-22 15:04:48 -0500976void radeon_i2c_get_byte(struct radeon_i2c_chan *i2c_bus,
977 u8 slave_addr,
978 u8 addr,
979 u8 *val)
Alex Deucherfcec5702009-11-10 21:25:07 -0500980{
981 u8 out_buf[2];
982 u8 in_buf[2];
983 struct i2c_msg msgs[] = {
984 {
985 .addr = slave_addr,
986 .flags = 0,
987 .len = 1,
988 .buf = out_buf,
989 },
990 {
991 .addr = slave_addr,
992 .flags = I2C_M_RD,
993 .len = 1,
994 .buf = in_buf,
995 }
996 };
997
998 out_buf[0] = addr;
999 out_buf[1] = 0;
1000
1001 if (i2c_transfer(&i2c_bus->adapter, msgs, 2) == 2) {
1002 *val = in_buf[0];
1003 DRM_DEBUG("val = 0x%02x\n", *val);
1004 } else {
1005 DRM_ERROR("i2c 0x%02x 0x%02x read failed\n",
1006 addr, *val);
1007 }
1008}
1009
Alex Deucher5a6f98f2009-12-22 15:04:48 -05001010void radeon_i2c_put_byte(struct radeon_i2c_chan *i2c_bus,
1011 u8 slave_addr,
1012 u8 addr,
1013 u8 val)
Alex Deucherfcec5702009-11-10 21:25:07 -05001014{
1015 uint8_t out_buf[2];
1016 struct i2c_msg msg = {
1017 .addr = slave_addr,
1018 .flags = 0,
1019 .len = 2,
1020 .buf = out_buf,
1021 };
1022
1023 out_buf[0] = addr;
1024 out_buf[1] = val;
1025
1026 if (i2c_transfer(&i2c_bus->adapter, &msg, 1) != 1)
1027 DRM_ERROR("i2c 0x%02x 0x%02x write failed\n",
1028 addr, val);
1029}
1030