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
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/delay.h>
17 #include <linux/pci.h>
23 #define SAVAGE_DDC 0x50
25 #define VGA_CR_IX 0x3d4
26 #define VGA_CR_DATA 0x3d5
28 #define CR_SERIAL1 0xa0 /* I2C serial communications interface */
29 #define MM_SERIAL1 0xff20
30 #define CR_SERIAL2 0xb1 /* DDC2 monitor communications interface */
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
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
45 #define SET_CR_IX(base, val) writeb((val), base + 0x8000 + VGA_CR_IX)
46 #define SET_CR_DATA(base, val) writeb((val), base + 0x8000 + VGA_CR_DATA)
47 #define GET_CR_DATA(base) readb(base + 0x8000 + VGA_CR_DATA)
49 static void savage4_gpio_setscl(void *data, int val)
51 struct savagefb_i2c_chan *chan = data;
54 r = readl(chan->ioaddr + chan->reg);
56 r |= SAVAGE4_I2C_SCL_OUT;
58 r &= ~SAVAGE4_I2C_SCL_OUT;
59 writel(r, chan->ioaddr + chan->reg);
60 readl(chan->ioaddr + chan->reg); /* flush posted write */
63 static void savage4_gpio_setsda(void *data, int val)
65 struct savagefb_i2c_chan *chan = data;
68 r = readl(chan->ioaddr + chan->reg);
70 r |= SAVAGE4_I2C_SDA_OUT;
72 r &= ~SAVAGE4_I2C_SDA_OUT;
73 writel(r, chan->ioaddr + chan->reg);
74 readl(chan->ioaddr + chan->reg); /* flush posted write */
77 static int savage4_gpio_getscl(void *data)
79 struct savagefb_i2c_chan *chan = data;
81 return (0 != (readl(chan->ioaddr + chan->reg) & SAVAGE4_I2C_SCL_IN));
84 static int savage4_gpio_getsda(void *data)
86 struct savagefb_i2c_chan *chan = data;
88 return (0 != (readl(chan->ioaddr + chan->reg) & SAVAGE4_I2C_SDA_IN));
91 static void prosavage_gpio_setscl(void* data, int val)
93 struct savagefb_i2c_chan *chan = data;
96 SET_CR_IX(chan->ioaddr, chan->reg);
97 r = GET_CR_DATA(chan->ioaddr);
98 r |= PROSAVAGE_I2C_ENAB;
100 r |= PROSAVAGE_I2C_SCL_OUT;
102 r &= ~PROSAVAGE_I2C_SCL_OUT;
104 SET_CR_DATA(chan->ioaddr, r);
107 static void prosavage_gpio_setsda(void* data, int val)
109 struct savagefb_i2c_chan *chan = data;
112 SET_CR_IX(chan->ioaddr, chan->reg);
113 r = GET_CR_DATA(chan->ioaddr);
114 r |= PROSAVAGE_I2C_ENAB;
116 r |= PROSAVAGE_I2C_SDA_OUT;
118 r &= ~PROSAVAGE_I2C_SDA_OUT;
120 SET_CR_DATA(chan->ioaddr, r);
123 static int prosavage_gpio_getscl(void* data)
125 struct savagefb_i2c_chan *chan = data;
127 SET_CR_IX(chan->ioaddr, chan->reg);
128 return (0 != (GET_CR_DATA(chan->ioaddr) & PROSAVAGE_I2C_SCL_IN));
131 static int prosavage_gpio_getsda(void* data)
133 struct savagefb_i2c_chan *chan = data;
135 SET_CR_IX(chan->ioaddr, chan->reg);
136 return (0 != (GET_CR_DATA(chan->ioaddr) & PROSAVAGE_I2C_SDA_IN));
139 static int savage_setup_i2c_bus(struct savagefb_i2c_chan *chan,
145 strcpy(chan->adapter.name, name);
146 chan->adapter.owner = THIS_MODULE;
147 chan->adapter.id = I2C_HW_B_SAVAGE;
148 chan->adapter.algo_data = &chan->algo;
149 chan->adapter.dev.parent = &chan->par->pcidev->dev;
150 chan->algo.udelay = 40;
151 chan->algo.mdelay = 5;
152 chan->algo.timeout = 20;
153 chan->algo.data = chan;
155 i2c_set_adapdata(&chan->adapter, chan);
157 /* Raise SCL and SDA */
158 chan->algo.setsda(chan, 1);
159 chan->algo.setscl(chan, 1);
162 rc = i2c_bit_add_bus(&chan->adapter);
165 dev_dbg(&chan->par->pcidev->dev,
166 "I2C bus %s registered.\n", name);
168 dev_warn(&chan->par->pcidev->dev,
169 "Failed to register I2C bus %s.\n", name);
176 void savagefb_create_i2c_busses(struct fb_info *info)
178 struct savagefb_par *par = info->par;
181 switch(info->fix.accel) {
182 case FB_ACCEL_PROSAVAGE_DDRK:
183 case FB_ACCEL_PROSAVAGE_PM:
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;
191 case FB_ACCEL_SAVAGE4:
192 case FB_ACCEL_SAVAGE2000:
193 par->chan.reg = 0xff20;
194 par->chan.ioaddr = par->mmio.vbase;
195 par->chan.algo.setsda = savage4_gpio_setsda;
196 par->chan.algo.setscl = savage4_gpio_setscl;
197 par->chan.algo.getsda = savage4_gpio_getsda;
198 par->chan.algo.getscl = savage4_gpio_getscl;
201 par->chan.par = NULL;
204 savage_setup_i2c_bus(&par->chan, "SAVAGE DDC2");
207 void savagefb_delete_i2c_busses(struct fb_info *info)
209 struct savagefb_par *par = info->par;
212 i2c_bit_del_bus(&par->chan.adapter);
214 par->chan.par = NULL;
217 static u8 *savage_do_probe_i2c_edid(struct savagefb_i2c_chan *chan)
220 struct i2c_msg msgs[] = {
234 buf = kmalloc(EDID_LENGTH, GFP_KERNEL);
239 if (i2c_transfer(&chan->adapter, msgs, 2) != 2) {
240 dev_dbg(&chan->par->pcidev->dev,
241 "Unable to read EDID block.\n");
251 int savagefb_probe_i2c_connector(struct fb_info *info, u8 **out_edid)
253 struct savagefb_par *par = info->par;
257 for (i = 0; i < 3; i++) {
258 /* Do the real work */
259 edid = savage_do_probe_i2c_edid(&par->chan);
265 /* try to get from firmware */
266 const u8 *e = fb_firmware_edid(info->device);
269 edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
271 memcpy(edid, e, EDID_LENGTH);
277 return (edid) ? 0 : 1;
280 MODULE_LICENSE("GPL");