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/config.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/sched.h>
17 #include <linux/delay.h>
18 #include <linux/pci.h>
24 #define SAVAGE_DDC 0x50
26 #define VGA_CR_IX 0x3d4
27 #define VGA_CR_DATA 0x3d5
29 #define CR_SERIAL1 0xa0 /* I2C serial communications interface */
30 #define MM_SERIAL1 0xff20
31 #define CR_SERIAL2 0xb1 /* DDC2 monitor communications interface */
33 /* based on vt8365 documentation */
34 #define PROSAVAGE_I2C_ENAB 0x10
35 #define PROSAVAGE_I2C_SCL_OUT 0x01
36 #define PROSAVAGE_I2C_SDA_OUT 0x02
37 #define PROSAVAGE_I2C_SCL_IN 0x04
38 #define PROSAVAGE_I2C_SDA_IN 0x08
40 #define SAVAGE4_I2C_ENAB 0x00000020
41 #define SAVAGE4_I2C_SCL_OUT 0x00000001
42 #define SAVAGE4_I2C_SDA_OUT 0x00000002
43 #define SAVAGE4_I2C_SCL_IN 0x00000008
44 #define SAVAGE4_I2C_SDA_IN 0x00000010
46 #define SET_CR_IX(base, val) writeb((val), base + 0x8000 + VGA_CR_IX)
47 #define SET_CR_DATA(base, val) writeb((val), base + 0x8000 + VGA_CR_DATA)
48 #define GET_CR_DATA(base) readb(base + 0x8000 + VGA_CR_DATA)
50 static void savage4_gpio_setscl(void *data
, int val
)
52 struct savagefb_i2c_chan
*chan
= (struct savagefb_i2c_chan
*)data
;
55 r
= readl(chan
->ioaddr
+ chan
->reg
);
57 r
|= SAVAGE4_I2C_SCL_OUT
;
59 r
&= ~SAVAGE4_I2C_SCL_OUT
;
60 writel(r
, chan
->ioaddr
+ chan
->reg
);
61 readl(chan
->ioaddr
+ chan
->reg
); /* flush posted write */
64 static void savage4_gpio_setsda(void *data
, int val
)
66 struct savagefb_i2c_chan
*chan
= (struct savagefb_i2c_chan
*)data
;
69 r
= readl(chan
->ioaddr
+ chan
->reg
);
71 r
|= SAVAGE4_I2C_SDA_OUT
;
73 r
&= ~SAVAGE4_I2C_SDA_OUT
;
74 writel(r
, chan
->ioaddr
+ chan
->reg
);
75 readl(chan
->ioaddr
+ chan
->reg
); /* flush posted write */
78 static int savage4_gpio_getscl(void *data
)
80 struct savagefb_i2c_chan
*chan
= (struct savagefb_i2c_chan
*)data
;
82 return (0 != (readl(chan
->ioaddr
+ chan
->reg
) & SAVAGE4_I2C_SCL_IN
));
85 static int savage4_gpio_getsda(void *data
)
87 struct savagefb_i2c_chan
*chan
= (struct savagefb_i2c_chan
*)data
;
89 return (0 != (readl(chan
->ioaddr
+ chan
->reg
) & SAVAGE4_I2C_SDA_IN
));
92 static void prosavage_gpio_setscl(void* data
, int val
)
94 struct savagefb_i2c_chan
*chan
= (struct savagefb_i2c_chan
*)data
;
97 SET_CR_IX(chan
->ioaddr
, chan
->reg
);
98 r
= GET_CR_DATA(chan
->ioaddr
);
99 r
|= PROSAVAGE_I2C_ENAB
;
101 r
|= PROSAVAGE_I2C_SCL_OUT
;
103 r
&= ~PROSAVAGE_I2C_SCL_OUT
;
105 SET_CR_DATA(chan
->ioaddr
, r
);
108 static void prosavage_gpio_setsda(void* data
, int val
)
110 struct savagefb_i2c_chan
*chan
= (struct savagefb_i2c_chan
*)data
;
113 SET_CR_IX(chan
->ioaddr
, chan
->reg
);
114 r
= GET_CR_DATA(chan
->ioaddr
);
115 r
|= PROSAVAGE_I2C_ENAB
;
117 r
|= PROSAVAGE_I2C_SDA_OUT
;
119 r
&= ~PROSAVAGE_I2C_SDA_OUT
;
121 SET_CR_DATA(chan
->ioaddr
, r
);
124 static int prosavage_gpio_getscl(void* data
)
126 struct savagefb_i2c_chan
*chan
= (struct savagefb_i2c_chan
*)data
;
128 SET_CR_IX(chan
->ioaddr
, chan
->reg
);
129 return (0 != (GET_CR_DATA(chan
->ioaddr
) & PROSAVAGE_I2C_SCL_IN
));
132 static int prosavage_gpio_getsda(void* data
)
134 struct savagefb_i2c_chan
*chan
= (struct savagefb_i2c_chan
*)data
;
136 SET_CR_IX(chan
->ioaddr
, chan
->reg
);
137 return (0 != (GET_CR_DATA(chan
->ioaddr
) & PROSAVAGE_I2C_SDA_IN
));
140 static int savage_setup_i2c_bus(struct savagefb_i2c_chan
*chan
,
143 int (*add_bus
)(struct i2c_adapter
*) = symbol_get(i2c_bit_add_bus
);
146 if (add_bus
&& chan
->par
) {
147 strcpy(chan
->adapter
.name
, name
);
148 chan
->adapter
.owner
= THIS_MODULE
;
149 chan
->adapter
.id
= I2C_HW_B_SAVAGE
;
150 chan
->adapter
.algo_data
= &chan
->algo
;
151 chan
->adapter
.dev
.parent
= &chan
->par
->pcidev
->dev
;
152 chan
->algo
.udelay
= 40;
153 chan
->algo
.mdelay
= 5;
154 chan
->algo
.timeout
= 20;
155 chan
->algo
.data
= chan
;
157 i2c_set_adapdata(&chan
->adapter
, chan
);
159 /* Raise SCL and SDA */
160 chan
->algo
.setsda(chan
, 1);
161 chan
->algo
.setscl(chan
, 1);
164 rc
= add_bus(&chan
->adapter
);
167 dev_dbg(&chan
->par
->pcidev
->dev
,
168 "I2C bus %s registered.\n", name
);
170 dev_warn(&chan
->par
->pcidev
->dev
,
171 "Failed to register I2C bus %s.\n", name
);
173 symbol_put(i2c_bit_add_bus
);
180 void savagefb_create_i2c_busses(struct fb_info
*info
)
182 struct savagefb_par
*par
= (struct savagefb_par
*)info
->par
;
185 switch(info
->fix
.accel
) {
186 case FB_ACCEL_PROSAVAGE_DDRK
:
187 case FB_ACCEL_PROSAVAGE_PM
:
188 par
->chan
.reg
= CR_SERIAL2
;
189 par
->chan
.ioaddr
= par
->mmio
.vbase
;
190 par
->chan
.algo
.setsda
= prosavage_gpio_setsda
;
191 par
->chan
.algo
.setscl
= prosavage_gpio_setscl
;
192 par
->chan
.algo
.getsda
= prosavage_gpio_getsda
;
193 par
->chan
.algo
.getscl
= prosavage_gpio_getscl
;
195 case FB_ACCEL_SAVAGE4
:
196 par
->chan
.reg
= 0xff20;
197 par
->chan
.ioaddr
= par
->mmio
.vbase
;
198 par
->chan
.algo
.setsda
= savage4_gpio_setsda
;
199 par
->chan
.algo
.setscl
= savage4_gpio_setscl
;
200 par
->chan
.algo
.getsda
= savage4_gpio_getsda
;
201 par
->chan
.algo
.getscl
= savage4_gpio_getscl
;
204 par
->chan
.par
= NULL
;
207 savage_setup_i2c_bus(&par
->chan
, "SAVAGE DDC2");
210 void savagefb_delete_i2c_busses(struct fb_info
*info
)
212 struct savagefb_par
*par
= (struct savagefb_par
*)info
->par
;
213 int (*del_bus
)(struct i2c_adapter
*) =
214 symbol_get(i2c_bit_del_bus
);
216 if (del_bus
&& par
->chan
.par
) {
217 del_bus(&par
->chan
.adapter
);
218 symbol_put(i2c_bit_del_bus
);
221 par
->chan
.par
= NULL
;
224 static u8
*savage_do_probe_i2c_edid(struct savagefb_i2c_chan
*chan
)
227 int (*transfer
)(struct i2c_adapter
*, struct i2c_msg
*, int) =
228 symbol_get(i2c_transfer
);
229 struct i2c_msg msgs
[] = {
242 if (transfer
&& chan
->par
) {
243 buf
= kmalloc(EDID_LENGTH
, GFP_KERNEL
);
248 if (transfer(&chan
->adapter
, msgs
, 2) != 2) {
249 dev_dbg(&chan
->par
->pcidev
->dev
,
250 "Unable to read EDID block.\n");
256 symbol_put(i2c_transfer
);
262 int savagefb_probe_i2c_connector(struct fb_info
*info
, u8
**out_edid
)
264 struct savagefb_par
*par
= info
->par
;
268 for (i
= 0; i
< 3; i
++) {
269 /* Do the real work */
270 edid
= savage_do_probe_i2c_edid(&par
->chan
);
276 /* try to get from firmware */
277 const u8
*e
= fb_firmware_edid(info
->device
);
280 edid
= kmalloc(EDID_LENGTH
, GFP_KERNEL
);
282 memcpy(edid
, e
, EDID_LENGTH
);
289 return (edid
) ? 0 : 1;
292 MODULE_LICENSE("GPL");