bb71fea07284e547cbf2632141748b8155691a74
[pandora-kernel.git] / drivers / video / savage / savagefb-i2c.c
1 /*
2  * linux/drivers/video/savage/savagefb-i2c.c - S3 Savage DDC2
3  *
4  * Copyright 2004 Antonino A. Daplas <adaplas @pol.net>
5  *
6  * Based partly on rivafb-i2c.c
7  *
8  * This file is subject to the terms and conditions of the GNU General Public
9  * License.  See the file COPYING in the main directory of this archive
10  * for more details.
11  */
12
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/delay.h>
16 #include <linux/gfp.h>
17 #include <linux/pci.h>
18 #include <linux/fb.h>
19
20 #include <asm/io.h>
21 #include "savagefb.h"
22
23 #define SAVAGE_DDC      0x50
24
25 #define VGA_CR_IX       0x3d4
26 #define VGA_CR_DATA     0x3d5
27
28 #define CR_SERIAL1      0xa0    /* I2C serial communications interface */
29 #define MM_SERIAL1      0xff20
30 #define CR_SERIAL2      0xb1    /* DDC2 monitor communications interface */
31
32 /* based on vt8365 documentation */
33 #define PROSAVAGE_I2C_ENAB      0x10
34 #define PROSAVAGE_I2C_SCL_OUT   0x01
35 #define PROSAVAGE_I2C_SDA_OUT   0x02
36 #define PROSAVAGE_I2C_SCL_IN    0x04
37 #define PROSAVAGE_I2C_SDA_IN    0x08
38
39 #define SAVAGE4_I2C_ENAB        0x00000020
40 #define SAVAGE4_I2C_SCL_OUT     0x00000001
41 #define SAVAGE4_I2C_SDA_OUT     0x00000002
42 #define SAVAGE4_I2C_SCL_IN      0x00000008
43 #define SAVAGE4_I2C_SDA_IN      0x00000010
44
45 static void savage4_gpio_setscl(void *data, int val)
46 {
47         struct savagefb_i2c_chan *chan = data;
48         unsigned int r;
49
50         r = readl(chan->ioaddr + chan->reg);
51         if(val)
52                 r |= SAVAGE4_I2C_SCL_OUT;
53         else
54                 r &= ~SAVAGE4_I2C_SCL_OUT;
55         writel(r, chan->ioaddr + chan->reg);
56         readl(chan->ioaddr + chan->reg);        /* flush posted write */
57 }
58
59 static void savage4_gpio_setsda(void *data, int val)
60 {
61         struct savagefb_i2c_chan *chan = data;
62
63         unsigned int r;
64         r = readl(chan->ioaddr + chan->reg);
65         if(val)
66                 r |= SAVAGE4_I2C_SDA_OUT;
67         else
68                 r &= ~SAVAGE4_I2C_SDA_OUT;
69         writel(r, chan->ioaddr + chan->reg);
70         readl(chan->ioaddr + chan->reg);        /* flush posted write */
71 }
72
73 static int savage4_gpio_getscl(void *data)
74 {
75         struct savagefb_i2c_chan *chan = data;
76
77         return (0 != (readl(chan->ioaddr + chan->reg) & SAVAGE4_I2C_SCL_IN));
78 }
79
80 static int savage4_gpio_getsda(void *data)
81 {
82         struct savagefb_i2c_chan *chan = data;
83
84         return (0 != (readl(chan->ioaddr + chan->reg) & SAVAGE4_I2C_SDA_IN));
85 }
86
87 static void prosavage_gpio_setscl(void* data, int val)
88 {
89         struct savagefb_i2c_chan *chan = data;
90         u32                       r;
91
92         r = VGArCR(chan->reg, chan->par);
93         r |= PROSAVAGE_I2C_ENAB;
94         if (val) {
95                 r |= PROSAVAGE_I2C_SCL_OUT;
96         } else {
97                 r &= ~PROSAVAGE_I2C_SCL_OUT;
98         }
99
100         VGAwCR(chan->reg, r, chan->par);
101 }
102
103 static void prosavage_gpio_setsda(void* data, int val)
104 {
105         struct savagefb_i2c_chan *chan = data;
106         unsigned int r;
107
108         r = VGArCR(chan->reg, chan->par);
109         r |= PROSAVAGE_I2C_ENAB;
110         if (val) {
111                 r |= PROSAVAGE_I2C_SDA_OUT;
112         } else {
113                 r &= ~PROSAVAGE_I2C_SDA_OUT;
114         }
115
116         VGAwCR(chan->reg, r, chan->par);
117 }
118
119 static int prosavage_gpio_getscl(void* data)
120 {
121         struct savagefb_i2c_chan *chan = data;
122
123         return (VGArCR(chan->reg, chan->par) & PROSAVAGE_I2C_SCL_IN) ? 1 : 0;
124 }
125
126 static int prosavage_gpio_getsda(void* data)
127 {
128         struct savagefb_i2c_chan *chan = data;
129
130         return (VGArCR(chan->reg, chan->par) & PROSAVAGE_I2C_SDA_IN) ? 1 : 0;
131 }
132
133 static int savage_setup_i2c_bus(struct savagefb_i2c_chan *chan,
134                                 const char *name)
135 {
136         int rc = 0;
137
138         if (chan->par) {
139                 strcpy(chan->adapter.name, name);
140                 chan->adapter.owner             = THIS_MODULE;
141                 chan->adapter.algo_data         = &chan->algo;
142                 chan->adapter.dev.parent        = &chan->par->pcidev->dev;
143                 chan->algo.udelay               = 10;
144                 chan->algo.timeout              = 20;
145                 chan->algo.data                 = chan;
146
147                 i2c_set_adapdata(&chan->adapter, chan);
148
149                 /* Raise SCL and SDA */
150                 chan->algo.setsda(chan, 1);
151                 chan->algo.setscl(chan, 1);
152                 udelay(20);
153
154                 rc = i2c_bit_add_bus(&chan->adapter);
155
156                 if (rc == 0)
157                         dev_dbg(&chan->par->pcidev->dev,
158                                 "I2C bus %s registered.\n", name);
159                 else
160                         dev_warn(&chan->par->pcidev->dev,
161                                  "Failed to register I2C bus %s.\n", name);
162         }
163
164         return rc;
165 }
166
167 void savagefb_create_i2c_busses(struct fb_info *info)
168 {
169         struct savagefb_par *par = info->par;
170         par->chan.par   = par;
171
172         switch (par->chip) {
173         case S3_PROSAVAGE:
174                 par->chan.reg         = CR_SERIAL2;
175                 par->chan.ioaddr      = par->mmio.vbase;
176                 par->chan.algo.setsda = prosavage_gpio_setsda;
177                 par->chan.algo.setscl = prosavage_gpio_setscl;
178                 par->chan.algo.getsda = prosavage_gpio_getsda;
179                 par->chan.algo.getscl = prosavage_gpio_getscl;
180                 break;
181         case S3_SAVAGE4:
182                 par->chan.reg = CR_SERIAL1;
183                 if (par->pcidev->revision > 1 && !(VGArCR(0xa6, par) & 0x40))
184                         par->chan.reg = CR_SERIAL2;
185                 par->chan.ioaddr      = par->mmio.vbase;
186                 par->chan.algo.setsda = prosavage_gpio_setsda;
187                 par->chan.algo.setscl = prosavage_gpio_setscl;
188                 par->chan.algo.getsda = prosavage_gpio_getsda;
189                 par->chan.algo.getscl = prosavage_gpio_getscl;
190                 break;
191         case S3_SAVAGE2000:
192                 par->chan.reg         = MM_SERIAL1;
193                 par->chan.ioaddr      = par->mmio.vbase;
194                 par->chan.algo.setsda = savage4_gpio_setsda;
195                 par->chan.algo.setscl = savage4_gpio_setscl;
196                 par->chan.algo.getsda = savage4_gpio_getsda;
197                 par->chan.algo.getscl = savage4_gpio_getscl;
198                 break;
199         default:
200                 par->chan.par = NULL;
201         }
202
203         savage_setup_i2c_bus(&par->chan, "SAVAGE DDC2");
204 }
205
206 void savagefb_delete_i2c_busses(struct fb_info *info)
207 {
208         struct savagefb_par *par = info->par;
209
210         if (par->chan.par)
211                 i2c_del_adapter(&par->chan.adapter);
212
213         par->chan.par = NULL;
214 }
215
216 int savagefb_probe_i2c_connector(struct fb_info *info, u8 **out_edid)
217 {
218         struct savagefb_par *par = info->par;
219         u8 *edid;
220
221         if (par->chan.par)
222                 edid = fb_ddc_read(&par->chan.adapter);
223         else
224                 edid = NULL;
225
226         if (!edid) {
227                 /* try to get from firmware */
228                 const u8 *e = fb_firmware_edid(info->device);
229
230                 if (e)
231                         edid = kmemdup(e, EDID_LENGTH, GFP_KERNEL);
232         }
233
234         *out_edid = edid;
235
236         return (edid) ? 0 : 1;
237 }
238
239 MODULE_LICENSE("GPL");