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