1 #include <linux/config.h>
2 #include <linux/module.h>
3 #include <linux/kernel.h>
4 #include <linux/sched.h>
5 #include <linux/delay.h>
10 #include <linux/i2c.h>
11 #include <linux/i2c-id.h>
12 #include <linux/i2c-algo-bit.h>
16 #include <video/radeon.h>
20 #define RADEON_DDC 0x50
22 static void radeon_gpio_setscl(void* data, int state)
24 struct radeon_i2c_chan *chan = data;
25 struct radeonfb_info *rinfo = chan->rinfo;
28 val = INREG(chan->ddc_reg) & ~(VGA_DDC_CLK_OUT_EN);
30 val |= VGA_DDC_CLK_OUT_EN;
32 OUTREG(chan->ddc_reg, val);
33 (void)INREG(chan->ddc_reg);
36 static void radeon_gpio_setsda(void* data, int state)
38 struct radeon_i2c_chan *chan = data;
39 struct radeonfb_info *rinfo = chan->rinfo;
42 val = INREG(chan->ddc_reg) & ~(VGA_DDC_DATA_OUT_EN);
44 val |= VGA_DDC_DATA_OUT_EN;
46 OUTREG(chan->ddc_reg, val);
47 (void)INREG(chan->ddc_reg);
50 static int radeon_gpio_getscl(void* data)
52 struct radeon_i2c_chan *chan = data;
53 struct radeonfb_info *rinfo = chan->rinfo;
56 val = INREG(chan->ddc_reg);
58 return (val & VGA_DDC_CLK_INPUT) ? 1 : 0;
61 static int radeon_gpio_getsda(void* data)
63 struct radeon_i2c_chan *chan = data;
64 struct radeonfb_info *rinfo = chan->rinfo;
67 val = INREG(chan->ddc_reg);
69 return (val & VGA_DDC_DATA_INPUT) ? 1 : 0;
72 static int radeon_setup_i2c_bus(struct radeon_i2c_chan *chan, const char *name)
76 strcpy(chan->adapter.name, name);
77 chan->adapter.owner = THIS_MODULE;
78 chan->adapter.id = I2C_ALGO_ATI;
79 chan->adapter.algo_data = &chan->algo;
80 chan->adapter.dev.parent = &chan->rinfo->pdev->dev;
81 chan->algo.setsda = radeon_gpio_setsda;
82 chan->algo.setscl = radeon_gpio_setscl;
83 chan->algo.getsda = radeon_gpio_getsda;
84 chan->algo.getscl = radeon_gpio_getscl;
85 chan->algo.udelay = 40;
86 chan->algo.timeout = 20;
87 chan->algo.data = chan;
89 i2c_set_adapdata(&chan->adapter, chan);
91 /* Raise SCL and SDA */
92 radeon_gpio_setsda(chan, 1);
93 radeon_gpio_setscl(chan, 1);
96 rc = i2c_bit_add_bus(&chan->adapter);
98 dev_dbg(&chan->rinfo->pdev->dev, "I2C bus %s registered.\n", name);
100 dev_warn(&chan->rinfo->pdev->dev, "Failed to register I2C bus %s.\n", name);
104 void radeon_create_i2c_busses(struct radeonfb_info *rinfo)
106 rinfo->i2c[0].rinfo = rinfo;
107 rinfo->i2c[0].ddc_reg = GPIO_MONID;
108 radeon_setup_i2c_bus(&rinfo->i2c[0], "monid");
110 rinfo->i2c[1].rinfo = rinfo;
111 rinfo->i2c[1].ddc_reg = GPIO_DVI_DDC;
112 radeon_setup_i2c_bus(&rinfo->i2c[1], "dvi");
114 rinfo->i2c[2].rinfo = rinfo;
115 rinfo->i2c[2].ddc_reg = GPIO_VGA_DDC;
116 radeon_setup_i2c_bus(&rinfo->i2c[2], "vga");
118 rinfo->i2c[3].rinfo = rinfo;
119 rinfo->i2c[3].ddc_reg = GPIO_CRT2_DDC;
120 radeon_setup_i2c_bus(&rinfo->i2c[3], "crt2");
123 void radeon_delete_i2c_busses(struct radeonfb_info *rinfo)
125 if (rinfo->i2c[0].rinfo)
126 i2c_bit_del_bus(&rinfo->i2c[0].adapter);
127 rinfo->i2c[0].rinfo = NULL;
129 if (rinfo->i2c[1].rinfo)
130 i2c_bit_del_bus(&rinfo->i2c[1].adapter);
131 rinfo->i2c[1].rinfo = NULL;
133 if (rinfo->i2c[2].rinfo)
134 i2c_bit_del_bus(&rinfo->i2c[2].adapter);
135 rinfo->i2c[2].rinfo = NULL;
137 if (rinfo->i2c[3].rinfo)
138 i2c_bit_del_bus(&rinfo->i2c[3].adapter);
139 rinfo->i2c[3].rinfo = NULL;
143 static u8 *radeon_do_probe_i2c_edid(struct radeon_i2c_chan *chan)
146 struct i2c_msg msgs[] = {
159 buf = kmalloc(EDID_LENGTH, GFP_KERNEL);
161 dev_warn(&chan->rinfo->pdev->dev, "Out of memory!\n");
166 if (i2c_transfer(&chan->adapter, msgs, 2) == 2)
168 dev_dbg(&chan->rinfo->pdev->dev, "Unable to read EDID block.\n");
174 int radeon_probe_i2c_connector(struct radeonfb_info *rinfo, int conn, u8 **out_edid)
176 u32 reg = rinfo->i2c[conn-1].ddc_reg;
180 OUTREG(reg, INREG(reg) &
181 ~(VGA_DDC_DATA_OUTPUT | VGA_DDC_CLK_OUTPUT));
183 OUTREG(reg, INREG(reg) & ~(VGA_DDC_CLK_OUT_EN));
186 for (i = 0; i < 3; i++) {
187 /* For some old monitors we need the
188 * following process to initialize/stop DDC
190 OUTREG(reg, INREG(reg) & ~(VGA_DDC_DATA_OUT_EN));
194 OUTREG(reg, INREG(reg) & ~(VGA_DDC_CLK_OUT_EN));
196 for (j = 0; j < 5; j++) {
198 if (INREG(reg) & VGA_DDC_CLK_INPUT)
204 OUTREG(reg, INREG(reg) | VGA_DDC_DATA_OUT_EN);
207 OUTREG(reg, INREG(reg) | VGA_DDC_CLK_OUT_EN);
210 OUTREG(reg, INREG(reg) & ~(VGA_DDC_DATA_OUT_EN));
214 /* Do the real work */
215 edid = radeon_do_probe_i2c_edid(&rinfo->i2c[conn-1]);
217 OUTREG(reg, INREG(reg) |
218 (VGA_DDC_DATA_OUT_EN | VGA_DDC_CLK_OUT_EN));
222 OUTREG(reg, INREG(reg) & ~(VGA_DDC_CLK_OUT_EN));
224 for (j = 0; j < 10; j++) {
226 if (INREG(reg) & VGA_DDC_CLK_INPUT)
230 OUTREG(reg, INREG(reg) & ~(VGA_DDC_DATA_OUT_EN));
233 OUTREG(reg, INREG(reg) |
234 (VGA_DDC_DATA_OUT_EN | VGA_DDC_CLK_OUT_EN));
239 /* Release the DDC lines when done or the Apple Cinema HD display
242 OUTREG(reg, INREG(reg) & ~(VGA_DDC_CLK_OUT_EN | VGA_DDC_DATA_OUT_EN));
248 RTRACE("radeonfb: I2C (port %d) ... not found\n", conn);
251 if (edid[0x14] & 0x80) {
252 /* Fix detection using BIOS tables */
253 if (rinfo->is_mobility /*&& conn == ddc_dvi*/ &&
254 (INREG(LVDS_GEN_CNTL) & LVDS_ON)) {
255 RTRACE("radeonfb: I2C (port %d) ... found LVDS panel\n", conn);
258 RTRACE("radeonfb: I2C (port %d) ... found TMDS panel\n", conn);
262 RTRACE("radeonfb: I2C (port %d) ... found CRT display\n", conn);