Linux 2.6.21-rc3
[linux/fpc-iii.git] / drivers / video / savage / savagefb-i2c.c
blob8db066ccca6b8ccd314120851f5e9245246199da
1 /*
2 * linux/drivers/video/savage/savagefb-i2c.c - S3 Savage DDC2
4 * Copyright 2004 Antonino A. Daplas <adaplas @pol.net>
6 * Based partly on rivafb-i2c.c
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.
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>
19 #include <asm/io.h>
20 #include "savagefb.h"
22 #define SAVAGE_DDC 0x50
24 #define VGA_CR_IX 0x3d4
25 #define VGA_CR_DATA 0x3d5
27 #define CR_SERIAL1 0xa0 /* I2C serial communications interface */
28 #define MM_SERIAL1 0xff20
29 #define CR_SERIAL2 0xb1 /* DDC2 monitor communications interface */
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
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
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)
48 static void savage4_gpio_setscl(void *data, int val)
50 struct savagefb_i2c_chan *chan = data;
51 unsigned int r;
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 */
62 static void savage4_gpio_setsda(void *data, int val)
64 struct savagefb_i2c_chan *chan = data;
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 */
76 static int savage4_gpio_getscl(void *data)
78 struct savagefb_i2c_chan *chan = data;
80 return (0 != (readl(chan->ioaddr + chan->reg) & SAVAGE4_I2C_SCL_IN));
83 static int savage4_gpio_getsda(void *data)
85 struct savagefb_i2c_chan *chan = data;
87 return (0 != (readl(chan->ioaddr + chan->reg) & SAVAGE4_I2C_SDA_IN));
90 static void prosavage_gpio_setscl(void* data, int val)
92 struct savagefb_i2c_chan *chan = data;
93 u32 r;
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;
103 SET_CR_DATA(chan->ioaddr, r);
106 static void prosavage_gpio_setsda(void* data, int val)
108 struct savagefb_i2c_chan *chan = data;
109 unsigned int r;
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;
119 SET_CR_DATA(chan->ioaddr, r);
122 static int prosavage_gpio_getscl(void* data)
124 struct savagefb_i2c_chan *chan = data;
126 SET_CR_IX(chan->ioaddr, chan->reg);
127 return (0 != (GET_CR_DATA(chan->ioaddr) & PROSAVAGE_I2C_SCL_IN));
130 static int prosavage_gpio_getsda(void* data)
132 struct savagefb_i2c_chan *chan = data;
134 SET_CR_IX(chan->ioaddr, chan->reg);
135 return (0 != (GET_CR_DATA(chan->ioaddr) & PROSAVAGE_I2C_SDA_IN));
138 static int savage_setup_i2c_bus(struct savagefb_i2c_chan *chan,
139 const char *name)
141 int rc = 0;
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;
153 i2c_set_adapdata(&chan->adapter, chan);
155 /* Raise SCL and SDA */
156 chan->algo.setsda(chan, 1);
157 chan->algo.setscl(chan, 1);
158 udelay(20);
160 rc = i2c_bit_add_bus(&chan->adapter);
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;
171 return rc;
174 void savagefb_create_i2c_busses(struct fb_info *info)
176 struct savagefb_par *par = info->par;
177 par->chan.par = par;
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;
202 savage_setup_i2c_bus(&par->chan, "SAVAGE DDC2");
205 void savagefb_delete_i2c_busses(struct fb_info *info)
207 struct savagefb_par *par = info->par;
209 if (par->chan.par)
210 i2c_del_adapter(&par->chan.adapter);
212 par->chan.par = NULL;
215 int savagefb_probe_i2c_connector(struct fb_info *info, u8 **out_edid)
217 struct savagefb_par *par = info->par;
218 u8 *edid;
220 if (par->chan.par)
221 edid = fb_ddc_read(&par->chan.adapter);
222 else
223 edid = NULL;
225 if (!edid) {
226 /* try to get from firmware */
227 const u8 *e = fb_firmware_edid(info->device);
229 if (e)
230 edid = kmemdup(e, EDID_LENGTH, GFP_KERNEL);
233 *out_edid = edid;
235 return (edid) ? 0 : 1;
238 MODULE_LICENSE("GPL");