[PATCH] 8139too deadlock fix
[linux-2.6/linux-loongson.git] / drivers / video / aty / radeon_i2c.c
blob9aaca58c074ac6c4926d55608ae98bae7aab0c9f
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>
9 #include <linux/i2c.h>
10 #include <linux/i2c-id.h>
11 #include <linux/i2c-algo-bit.h>
13 #include <asm/io.h>
15 #include <video/radeon.h>
16 #include "radeonfb.h"
17 #include "../edid.h"
19 #define RADEON_DDC 0x50
21 static void radeon_gpio_setscl(void* data, int state)
23 struct radeon_i2c_chan *chan = data;
24 struct radeonfb_info *rinfo = chan->rinfo;
25 u32 val;
27 val = INREG(chan->ddc_reg) & ~(VGA_DDC_CLK_OUT_EN);
28 if (!state)
29 val |= VGA_DDC_CLK_OUT_EN;
31 OUTREG(chan->ddc_reg, val);
32 (void)INREG(chan->ddc_reg);
35 static void radeon_gpio_setsda(void* data, int state)
37 struct radeon_i2c_chan *chan = data;
38 struct radeonfb_info *rinfo = chan->rinfo;
39 u32 val;
41 val = INREG(chan->ddc_reg) & ~(VGA_DDC_DATA_OUT_EN);
42 if (!state)
43 val |= VGA_DDC_DATA_OUT_EN;
45 OUTREG(chan->ddc_reg, val);
46 (void)INREG(chan->ddc_reg);
49 static int radeon_gpio_getscl(void* data)
51 struct radeon_i2c_chan *chan = data;
52 struct radeonfb_info *rinfo = chan->rinfo;
53 u32 val;
55 val = INREG(chan->ddc_reg);
57 return (val & VGA_DDC_CLK_INPUT) ? 1 : 0;
60 static int radeon_gpio_getsda(void* data)
62 struct radeon_i2c_chan *chan = data;
63 struct radeonfb_info *rinfo = chan->rinfo;
64 u32 val;
66 val = INREG(chan->ddc_reg);
68 return (val & VGA_DDC_DATA_INPUT) ? 1 : 0;
71 static int radeon_setup_i2c_bus(struct radeon_i2c_chan *chan, const char *name)
73 int rc;
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;
88 i2c_set_adapdata(&chan->adapter, chan);
90 /* Raise SCL and SDA */
91 radeon_gpio_setsda(chan, 1);
92 radeon_gpio_setscl(chan, 1);
93 udelay(20);
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;
103 void radeon_create_i2c_busses(struct radeonfb_info *rinfo)
105 rinfo->i2c[0].rinfo = rinfo;
106 rinfo->i2c[0].ddc_reg = GPIO_MONID;
107 radeon_setup_i2c_bus(&rinfo->i2c[0], "monid");
109 rinfo->i2c[1].rinfo = rinfo;
110 rinfo->i2c[1].ddc_reg = GPIO_DVI_DDC;
111 radeon_setup_i2c_bus(&rinfo->i2c[1], "dvi");
113 rinfo->i2c[2].rinfo = rinfo;
114 rinfo->i2c[2].ddc_reg = GPIO_VGA_DDC;
115 radeon_setup_i2c_bus(&rinfo->i2c[2], "vga");
117 rinfo->i2c[3].rinfo = rinfo;
118 rinfo->i2c[3].ddc_reg = GPIO_CRT2_DDC;
119 radeon_setup_i2c_bus(&rinfo->i2c[3], "crt2");
122 void radeon_delete_i2c_busses(struct radeonfb_info *rinfo)
124 if (rinfo->i2c[0].rinfo)
125 i2c_bit_del_bus(&rinfo->i2c[0].adapter);
126 rinfo->i2c[0].rinfo = NULL;
128 if (rinfo->i2c[1].rinfo)
129 i2c_bit_del_bus(&rinfo->i2c[1].adapter);
130 rinfo->i2c[1].rinfo = NULL;
132 if (rinfo->i2c[2].rinfo)
133 i2c_bit_del_bus(&rinfo->i2c[2].adapter);
134 rinfo->i2c[2].rinfo = NULL;
136 if (rinfo->i2c[3].rinfo)
137 i2c_bit_del_bus(&rinfo->i2c[3].adapter);
138 rinfo->i2c[3].rinfo = NULL;
142 static u8 *radeon_do_probe_i2c_edid(struct radeon_i2c_chan *chan)
144 u8 start = 0x0;
145 struct i2c_msg msgs[] = {
147 .addr = RADEON_DDC,
148 .len = 1,
149 .buf = &start,
150 }, {
151 .addr = RADEON_DDC,
152 .flags = I2C_M_RD,
153 .len = EDID_LENGTH,
156 u8 *buf;
158 buf = kmalloc(EDID_LENGTH, GFP_KERNEL);
159 if (!buf) {
160 dev_warn(&chan->rinfo->pdev->dev, "Out of memory!\n");
161 return NULL;
163 msgs[1].buf = buf;
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;
173 int radeon_probe_i2c_connector(struct radeonfb_info *rinfo, int conn, u8 **out_edid)
175 u32 reg = rinfo->i2c[conn-1].ddc_reg;
176 u8 *edid = NULL;
177 int i, j;
179 OUTREG(reg, INREG(reg) &
180 ~(VGA_DDC_DATA_OUTPUT | VGA_DDC_CLK_OUTPUT));
182 OUTREG(reg, INREG(reg) & ~(VGA_DDC_CLK_OUT_EN));
183 (void)INREG(reg);
185 for (i = 0; i < 3; i++) {
186 /* For some old monitors we need the
187 * following process to initialize/stop DDC
189 OUTREG(reg, INREG(reg) & ~(VGA_DDC_DATA_OUT_EN));
190 (void)INREG(reg);
191 msleep(13);
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;
200 if (j == 5)
201 continue;
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);
213 /* Do the real work */
214 edid = radeon_do_probe_i2c_edid(&rinfo->i2c[conn-1]);
216 OUTREG(reg, INREG(reg) |
217 (VGA_DDC_DATA_OUT_EN | VGA_DDC_CLK_OUT_EN));
218 (void)INREG(reg);
219 msleep(15);
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;
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;
238 /* Release the DDC lines when done or the Apple Cinema HD display
239 * will switch off
241 OUTREG(reg, INREG(reg) & ~(VGA_DDC_CLK_OUT_EN | VGA_DDC_DATA_OUT_EN));
242 (void)INREG(reg);
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;
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;
261 RTRACE("radeonfb: I2C (port %d) ... found CRT display\n", conn);
262 return MT_CRT;