fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / 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 static void radeon_gpio_setscl(void* data, int state)
20 {
21         struct radeon_i2c_chan  *chan = data;
22         struct radeonfb_info    *rinfo = chan->rinfo;
23         u32                     val;
24         
25         val = INREG(chan->ddc_reg) & ~(VGA_DDC_CLK_OUT_EN);
26         if (!state)
27                 val |= VGA_DDC_CLK_OUT_EN;
28
29         OUTREG(chan->ddc_reg, val);
30         (void)INREG(chan->ddc_reg);
31 }
32
33 static void radeon_gpio_setsda(void* data, int state)
34 {
35         struct radeon_i2c_chan  *chan = data;
36         struct radeonfb_info    *rinfo = chan->rinfo;
37         u32                     val;
38         
39         val = INREG(chan->ddc_reg) & ~(VGA_DDC_DATA_OUT_EN);
40         if (!state)
41                 val |= VGA_DDC_DATA_OUT_EN;
42
43         OUTREG(chan->ddc_reg, val);
44         (void)INREG(chan->ddc_reg);
45 }
46
47 static int radeon_gpio_getscl(void* data)
48 {
49         struct radeon_i2c_chan  *chan = data;
50         struct radeonfb_info    *rinfo = chan->rinfo;
51         u32                     val;
52         
53         val = INREG(chan->ddc_reg);
54
55         return (val & VGA_DDC_CLK_INPUT) ? 1 : 0;
56 }
57
58 static int radeon_gpio_getsda(void* data)
59 {
60         struct radeon_i2c_chan  *chan = data;
61         struct radeonfb_info    *rinfo = chan->rinfo;
62         u32                     val;
63         
64         val = INREG(chan->ddc_reg);
65
66         return (val & VGA_DDC_DATA_INPUT) ? 1 : 0;
67 }
68
69 static int radeon_setup_i2c_bus(struct radeon_i2c_chan *chan, const char *name)
70 {
71         int rc;
72
73         strcpy(chan->adapter.name, name);
74         chan->adapter.owner             = THIS_MODULE;
75         chan->adapter.id                = I2C_HW_B_RADEON;
76         chan->adapter.algo_data         = &chan->algo;
77         chan->adapter.dev.parent        = &chan->rinfo->pdev->dev;
78         chan->algo.setsda               = radeon_gpio_setsda;
79         chan->algo.setscl               = radeon_gpio_setscl;
80         chan->algo.getsda               = radeon_gpio_getsda;
81         chan->algo.getscl               = radeon_gpio_getscl;
82         chan->algo.udelay               = 40;
83         chan->algo.timeout              = 20;
84         chan->algo.data                 = chan; 
85         
86         i2c_set_adapdata(&chan->adapter, chan);
87         
88         /* Raise SCL and SDA */
89         radeon_gpio_setsda(chan, 1);
90         radeon_gpio_setscl(chan, 1);
91         udelay(20);
92
93         rc = i2c_bit_add_bus(&chan->adapter);
94         if (rc == 0)
95                 dev_dbg(&chan->rinfo->pdev->dev, "I2C bus %s registered.\n", name);
96         else
97                 dev_warn(&chan->rinfo->pdev->dev, "Failed to register I2C bus %s.\n", name);
98         return rc;
99 }
100
101 void radeon_create_i2c_busses(struct radeonfb_info *rinfo)
102 {
103         rinfo->i2c[0].rinfo     = rinfo;
104         rinfo->i2c[0].ddc_reg   = GPIO_MONID;
105         radeon_setup_i2c_bus(&rinfo->i2c[0], "monid");
106
107         rinfo->i2c[1].rinfo     = rinfo;
108         rinfo->i2c[1].ddc_reg   = GPIO_DVI_DDC;
109         radeon_setup_i2c_bus(&rinfo->i2c[1], "dvi");
110
111         rinfo->i2c[2].rinfo     = rinfo;
112         rinfo->i2c[2].ddc_reg   = GPIO_VGA_DDC;
113         radeon_setup_i2c_bus(&rinfo->i2c[2], "vga");
114
115         rinfo->i2c[3].rinfo     = rinfo;
116         rinfo->i2c[3].ddc_reg   = GPIO_CRT2_DDC;
117         radeon_setup_i2c_bus(&rinfo->i2c[3], "crt2");
118 }
119
120 void radeon_delete_i2c_busses(struct radeonfb_info *rinfo)
121 {
122         if (rinfo->i2c[0].rinfo)
123                 i2c_del_adapter(&rinfo->i2c[0].adapter);
124         rinfo->i2c[0].rinfo = NULL;
125
126         if (rinfo->i2c[1].rinfo)
127                 i2c_del_adapter(&rinfo->i2c[1].adapter);
128         rinfo->i2c[1].rinfo = NULL;
129
130         if (rinfo->i2c[2].rinfo)
131                 i2c_del_adapter(&rinfo->i2c[2].adapter);
132         rinfo->i2c[2].rinfo = NULL;
133
134         if (rinfo->i2c[3].rinfo)
135                 i2c_del_adapter(&rinfo->i2c[3].adapter);
136         rinfo->i2c[3].rinfo = NULL;
137 }
138
139 int radeon_probe_i2c_connector(struct radeonfb_info *rinfo, int conn,
140                                u8 **out_edid)
141 {
142         u32 reg = rinfo->i2c[conn-1].ddc_reg;
143         u8 *edid;
144
145         OUTREG(reg, INREG(reg) &
146                         ~(VGA_DDC_DATA_OUTPUT | VGA_DDC_CLK_OUTPUT));
147
148         edid = fb_ddc_read(&rinfo->i2c[conn-1].adapter);
149
150         if (out_edid)
151                 *out_edid = edid;
152         if (!edid) {
153                 RTRACE("radeonfb: I2C (port %d) ... not found\n", conn);
154                 return MT_NONE;
155         }
156         if (edid[0x14] & 0x80) {
157                 /* Fix detection using BIOS tables */
158                 if (rinfo->is_mobility /*&& conn == ddc_dvi*/ &&
159                     (INREG(LVDS_GEN_CNTL) & LVDS_ON)) {
160                         RTRACE("radeonfb: I2C (port %d) ... found LVDS panel\n", conn);
161                         return MT_LCD;
162                 } else {
163                         RTRACE("radeonfb: I2C (port %d) ... found TMDS panel\n", conn);
164                         return MT_DFP;
165                 }
166         }
167         RTRACE("radeonfb: I2C (port %d) ... found CRT display\n", conn);
168         return MT_CRT;
169 }
170