9aaca58c074ac6c4926d55608ae98bae7aab0c9f
[safe/jmp/linux-2.6] / drivers / video / aty / radeon_i2c.c
1 #include <linux/module.h>
2 #include <linux/kernel.h>
3 #include <linux/sched.h>
4 #include <linux/delay.h>
5 #include <linux/pci.h>
6 #include <linux/fb.h>
7
8
9 #include <linux/i2c.h>
10 #include <linux/i2c-id.h>
11 #include <linux/i2c-algo-bit.h>
12
13 #include <asm/io.h>
14
15 #include <video/radeon.h>
16 #include "radeonfb.h"
17 #include "../edid.h"
18
19 #define RADEON_DDC      0x50
20
21 static void radeon_gpio_setscl(void* data, int state)
22 {
23         struct radeon_i2c_chan  *chan = data;
24         struct radeonfb_info    *rinfo = chan->rinfo;
25         u32                     val;
26         
27         val = INREG(chan->ddc_reg) & ~(VGA_DDC_CLK_OUT_EN);
28         if (!state)
29                 val |= VGA_DDC_CLK_OUT_EN;
30
31         OUTREG(chan->ddc_reg, val);
32         (void)INREG(chan->ddc_reg);
33 }
34
35 static void radeon_gpio_setsda(void* data, int state)
36 {
37         struct radeon_i2c_chan  *chan = data;
38         struct radeonfb_info    *rinfo = chan->rinfo;
39         u32                     val;
40         
41         val = INREG(chan->ddc_reg) & ~(VGA_DDC_DATA_OUT_EN);
42         if (!state)
43                 val |= VGA_DDC_DATA_OUT_EN;
44
45         OUTREG(chan->ddc_reg, val);
46         (void)INREG(chan->ddc_reg);
47 }
48
49 static int radeon_gpio_getscl(void* data)
50 {
51         struct radeon_i2c_chan  *chan = data;
52         struct radeonfb_info    *rinfo = chan->rinfo;
53         u32                     val;
54         
55         val = INREG(chan->ddc_reg);
56
57         return (val & VGA_DDC_CLK_INPUT) ? 1 : 0;
58 }
59
60 static int radeon_gpio_getsda(void* data)
61 {
62         struct radeon_i2c_chan  *chan = data;
63         struct radeonfb_info    *rinfo = chan->rinfo;
64         u32                     val;
65         
66         val = INREG(chan->ddc_reg);
67
68         return (val & VGA_DDC_DATA_INPUT) ? 1 : 0;
69 }
70
71 static int radeon_setup_i2c_bus(struct radeon_i2c_chan *chan, const char *name)
72 {
73         int rc;
74
75         strcpy(chan->adapter.name, name);
76         chan->adapter.owner             = THIS_MODULE;
77         chan->adapter.id                = I2C_HW_B_RADEON;
78         chan->adapter.algo_data         = &chan->algo;
79         chan->adapter.dev.parent        = &chan->rinfo->pdev->dev;
80         chan->algo.setsda               = radeon_gpio_setsda;
81         chan->algo.setscl               = radeon_gpio_setscl;
82         chan->algo.getsda               = radeon_gpio_getsda;
83         chan->algo.getscl               = radeon_gpio_getscl;
84         chan->algo.udelay               = 40;
85         chan->algo.timeout              = 20;
86         chan->algo.data                 = chan; 
87         
88         i2c_set_adapdata(&chan->adapter, chan);
89         
90         /* Raise SCL and SDA */
91         radeon_gpio_setsda(chan, 1);
92         radeon_gpio_setscl(chan, 1);
93         udelay(20);
94
95         rc = i2c_bit_add_bus(&chan->adapter);
96         if (rc == 0)
97                 dev_dbg(&chan->rinfo->pdev->dev, "I2C bus %s registered.\n", name);
98         else
99                 dev_warn(&chan->rinfo->pdev->dev, "Failed to register I2C bus %s.\n", name);
100         return rc;
101 }
102
103 void radeon_create_i2c_busses(struct radeonfb_info *rinfo)
104 {
105         rinfo->i2c[0].rinfo     = rinfo;
106         rinfo->i2c[0].ddc_reg   = GPIO_MONID;
107         radeon_setup_i2c_bus(&rinfo->i2c[0], "monid");
108
109         rinfo->i2c[1].rinfo     = rinfo;
110         rinfo->i2c[1].ddc_reg   = GPIO_DVI_DDC;
111         radeon_setup_i2c_bus(&rinfo->i2c[1], "dvi");
112
113         rinfo->i2c[2].rinfo     = rinfo;
114         rinfo->i2c[2].ddc_reg   = GPIO_VGA_DDC;
115         radeon_setup_i2c_bus(&rinfo->i2c[2], "vga");
116
117         rinfo->i2c[3].rinfo     = rinfo;
118         rinfo->i2c[3].ddc_reg   = GPIO_CRT2_DDC;
119         radeon_setup_i2c_bus(&rinfo->i2c[3], "crt2");
120 }
121
122 void radeon_delete_i2c_busses(struct radeonfb_info *rinfo)
123 {
124         if (rinfo->i2c[0].rinfo)
125                 i2c_bit_del_bus(&rinfo->i2c[0].adapter);
126         rinfo->i2c[0].rinfo = NULL;
127
128         if (rinfo->i2c[1].rinfo)
129                 i2c_bit_del_bus(&rinfo->i2c[1].adapter);
130         rinfo->i2c[1].rinfo = NULL;
131
132         if (rinfo->i2c[2].rinfo)
133                 i2c_bit_del_bus(&rinfo->i2c[2].adapter);
134         rinfo->i2c[2].rinfo = NULL;
135
136         if (rinfo->i2c[3].rinfo)
137                 i2c_bit_del_bus(&rinfo->i2c[3].adapter);
138         rinfo->i2c[3].rinfo = NULL;
139 }
140
141
142 static u8 *radeon_do_probe_i2c_edid(struct radeon_i2c_chan *chan)
143 {
144         u8 start = 0x0;
145         struct i2c_msg msgs[] = {
146                 {
147                         .addr   = RADEON_DDC,
148                         .len    = 1,
149                         .buf    = &start,
150                 }, {
151                         .addr   = RADEON_DDC,
152                         .flags  = I2C_M_RD,
153                         .len    = EDID_LENGTH,
154                 },
155         };
156         u8 *buf;
157
158         buf = kmalloc(EDID_LENGTH, GFP_KERNEL);
159         if (!buf) {
160                 dev_warn(&chan->rinfo->pdev->dev, "Out of memory!\n");
161                 return NULL;
162         }
163         msgs[1].buf = buf;
164
165         if (i2c_transfer(&chan->adapter, msgs, 2) == 2)
166                 return buf;
167         dev_dbg(&chan->rinfo->pdev->dev, "Unable to read EDID block.\n");
168         kfree(buf);
169         return NULL;
170 }
171
172
173 int radeon_probe_i2c_connector(struct radeonfb_info *rinfo, int conn, u8 **out_edid)
174 {
175         u32 reg = rinfo->i2c[conn-1].ddc_reg;
176         u8 *edid = NULL;
177         int i, j;
178
179         OUTREG(reg, INREG(reg) & 
180                         ~(VGA_DDC_DATA_OUTPUT | VGA_DDC_CLK_OUTPUT));
181
182         OUTREG(reg, INREG(reg) & ~(VGA_DDC_CLK_OUT_EN));
183         (void)INREG(reg);
184
185         for (i = 0; i < 3; i++) {
186                 /* For some old monitors we need the
187                  * following process to initialize/stop DDC
188                  */
189                 OUTREG(reg, INREG(reg) & ~(VGA_DDC_DATA_OUT_EN));
190                 (void)INREG(reg);
191                 msleep(13);
192
193                 OUTREG(reg, INREG(reg) & ~(VGA_DDC_CLK_OUT_EN));
194                 (void)INREG(reg);
195                 for (j = 0; j < 5; j++) {
196                         msleep(10);
197                         if (INREG(reg) & VGA_DDC_CLK_INPUT)
198                                 break;
199                 }
200                 if (j == 5)
201                         continue;
202
203                 OUTREG(reg, INREG(reg) | VGA_DDC_DATA_OUT_EN);
204                 (void)INREG(reg);
205                 msleep(15);
206                 OUTREG(reg, INREG(reg) | VGA_DDC_CLK_OUT_EN);
207                 (void)INREG(reg);
208                 msleep(15);
209                 OUTREG(reg, INREG(reg) & ~(VGA_DDC_DATA_OUT_EN));
210                 (void)INREG(reg);
211                 msleep(15);
212
213                 /* Do the real work */
214                 edid = radeon_do_probe_i2c_edid(&rinfo->i2c[conn-1]);
215
216                 OUTREG(reg, INREG(reg) | 
217                                 (VGA_DDC_DATA_OUT_EN | VGA_DDC_CLK_OUT_EN));
218                 (void)INREG(reg);
219                 msleep(15);
220                 
221                 OUTREG(reg, INREG(reg) & ~(VGA_DDC_CLK_OUT_EN));
222                 (void)INREG(reg);
223                 for (j = 0; j < 10; j++) {
224                         msleep(10);
225                         if (INREG(reg) & VGA_DDC_CLK_INPUT)
226                                 break;
227                 }
228
229                 OUTREG(reg, INREG(reg) & ~(VGA_DDC_DATA_OUT_EN));
230                 (void)INREG(reg);
231                 msleep(15);
232                 OUTREG(reg, INREG(reg) |
233                                 (VGA_DDC_DATA_OUT_EN | VGA_DDC_CLK_OUT_EN));
234                 (void)INREG(reg);
235                 if (edid)
236                         break;
237         }
238         /* Release the DDC lines when done or the Apple Cinema HD display
239          * will switch off
240          */
241         OUTREG(reg, INREG(reg) & ~(VGA_DDC_CLK_OUT_EN | VGA_DDC_DATA_OUT_EN));
242         (void)INREG(reg);
243
244         if (out_edid)
245                 *out_edid = edid;
246         if (!edid) {
247                 RTRACE("radeonfb: I2C (port %d) ... not found\n", conn);
248                 return MT_NONE;
249         }
250         if (edid[0x14] & 0x80) {
251                 /* Fix detection using BIOS tables */
252                 if (rinfo->is_mobility /*&& conn == ddc_dvi*/ &&
253                     (INREG(LVDS_GEN_CNTL) & LVDS_ON)) {
254                         RTRACE("radeonfb: I2C (port %d) ... found LVDS panel\n", conn);
255                         return MT_LCD;
256                 } else {
257                         RTRACE("radeonfb: I2C (port %d) ... found TMDS panel\n", conn);
258                         return MT_DFP;
259                 }
260         }
261         RTRACE("radeonfb: I2C (port %d) ... found CRT display\n", conn);
262         return MT_CRT;
263 }
264