blob: 21debed863ac4d459bd9222b544841fe862eb476 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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/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>
19#include <linux/fb.h>
20
21#include <asm/io.h>
22#include "savagefb.h"
23
24#define SAVAGE_DDC 0x50
25
26#define VGA_CR_IX 0x3d4
27#define VGA_CR_DATA 0x3d5
28
29#define CR_SERIAL1 0xa0 /* I2C serial communications interface */
30#define MM_SERIAL1 0xff20
31#define CR_SERIAL2 0xb1 /* DDC2 monitor communications interface */
32
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
39
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
45
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)
49
50static void savage4_gpio_setscl(void *data, int val)
51{
Antonino A. Daplasb8901b02006-01-09 20:53:02 -080052 struct savagefb_i2c_chan *chan = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 unsigned int r;
54
55 r = readl(chan->ioaddr + chan->reg);
56 if(val)
57 r |= SAVAGE4_I2C_SCL_OUT;
58 else
59 r &= ~SAVAGE4_I2C_SCL_OUT;
60 writel(r, chan->ioaddr + chan->reg);
61 readl(chan->ioaddr + chan->reg); /* flush posted write */
62}
63
64static void savage4_gpio_setsda(void *data, int val)
65{
Antonino A. Daplasb8901b02006-01-09 20:53:02 -080066 struct savagefb_i2c_chan *chan = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 unsigned int r;
69 r = readl(chan->ioaddr + chan->reg);
70 if(val)
71 r |= SAVAGE4_I2C_SDA_OUT;
72 else
73 r &= ~SAVAGE4_I2C_SDA_OUT;
74 writel(r, chan->ioaddr + chan->reg);
75 readl(chan->ioaddr + chan->reg); /* flush posted write */
76}
77
78static int savage4_gpio_getscl(void *data)
79{
Antonino A. Daplasb8901b02006-01-09 20:53:02 -080080 struct savagefb_i2c_chan *chan = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 return (0 != (readl(chan->ioaddr + chan->reg) & SAVAGE4_I2C_SCL_IN));
83}
84
85static int savage4_gpio_getsda(void *data)
86{
Antonino A. Daplasb8901b02006-01-09 20:53:02 -080087 struct savagefb_i2c_chan *chan = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 return (0 != (readl(chan->ioaddr + chan->reg) & SAVAGE4_I2C_SDA_IN));
90}
91
92static void prosavage_gpio_setscl(void* data, int val)
93{
Antonino A. Daplasb8901b02006-01-09 20:53:02 -080094 struct savagefb_i2c_chan *chan = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 u32 r;
96
97 SET_CR_IX(chan->ioaddr, chan->reg);
98 r = GET_CR_DATA(chan->ioaddr);
99 r |= PROSAVAGE_I2C_ENAB;
100 if (val) {
101 r |= PROSAVAGE_I2C_SCL_OUT;
102 } else {
103 r &= ~PROSAVAGE_I2C_SCL_OUT;
104 }
105 SET_CR_DATA(chan->ioaddr, r);
106}
107
108static void prosavage_gpio_setsda(void* data, int val)
109{
Antonino A. Daplasb8901b02006-01-09 20:53:02 -0800110 struct savagefb_i2c_chan *chan = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 unsigned int r;
112
113 SET_CR_IX(chan->ioaddr, chan->reg);
114 r = GET_CR_DATA(chan->ioaddr);
115 r |= PROSAVAGE_I2C_ENAB;
116 if (val) {
117 r |= PROSAVAGE_I2C_SDA_OUT;
118 } else {
119 r &= ~PROSAVAGE_I2C_SDA_OUT;
120 }
121 SET_CR_DATA(chan->ioaddr, r);
122}
123
124static int prosavage_gpio_getscl(void* data)
125{
Antonino A. Daplasb8901b02006-01-09 20:53:02 -0800126 struct savagefb_i2c_chan *chan = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 SET_CR_IX(chan->ioaddr, chan->reg);
129 return (0 != (GET_CR_DATA(chan->ioaddr) & PROSAVAGE_I2C_SCL_IN));
130}
131
132static int prosavage_gpio_getsda(void* data)
133{
Antonino A. Daplasb8901b02006-01-09 20:53:02 -0800134 struct savagefb_i2c_chan *chan = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 SET_CR_IX(chan->ioaddr, chan->reg);
137 return (0 != (GET_CR_DATA(chan->ioaddr) & PROSAVAGE_I2C_SDA_IN));
138}
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140static int savage_setup_i2c_bus(struct savagefb_i2c_chan *chan,
141 const char *name)
142{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 int rc = 0;
144
Antonino A. Daplasb8901b02006-01-09 20:53:02 -0800145 if (chan->par) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 strcpy(chan->adapter.name, name);
147 chan->adapter.owner = THIS_MODULE;
Jean Delvare1684a9842005-08-11 23:51:10 +0200148 chan->adapter.id = I2C_HW_B_SAVAGE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 chan->adapter.algo_data = &chan->algo;
150 chan->adapter.dev.parent = &chan->par->pcidev->dev;
151 chan->algo.udelay = 40;
152 chan->algo.mdelay = 5;
153 chan->algo.timeout = 20;
154 chan->algo.data = chan;
155
156 i2c_set_adapdata(&chan->adapter, chan);
157
158 /* Raise SCL and SDA */
159 chan->algo.setsda(chan, 1);
160 chan->algo.setscl(chan, 1);
161 udelay(20);
162
Antonino A. Daplasb8901b02006-01-09 20:53:02 -0800163 rc = i2c_bit_add_bus(&chan->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 if (rc == 0)
166 dev_dbg(&chan->par->pcidev->dev,
167 "I2C bus %s registered.\n", name);
168 else
169 dev_warn(&chan->par->pcidev->dev,
170 "Failed to register I2C bus %s.\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 } else
172 chan->par = NULL;
173
174 return rc;
175}
176
177void savagefb_create_i2c_busses(struct fb_info *info)
178{
Antonino A. Daplasb8901b02006-01-09 20:53:02 -0800179 struct savagefb_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 par->chan.par = par;
181
182 switch(info->fix.accel) {
183 case FB_ACCEL_PROSAVAGE_DDRK:
184 case FB_ACCEL_PROSAVAGE_PM:
185 par->chan.reg = CR_SERIAL2;
186 par->chan.ioaddr = par->mmio.vbase;
187 par->chan.algo.setsda = prosavage_gpio_setsda;
188 par->chan.algo.setscl = prosavage_gpio_setscl;
189 par->chan.algo.getsda = prosavage_gpio_getsda;
190 par->chan.algo.getscl = prosavage_gpio_getscl;
191 break;
192 case FB_ACCEL_SAVAGE4:
Jean Delvarec35dba62006-01-09 20:52:59 -0800193 case FB_ACCEL_SAVAGE2000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 par->chan.reg = 0xff20;
195 par->chan.ioaddr = par->mmio.vbase;
196 par->chan.algo.setsda = savage4_gpio_setsda;
197 par->chan.algo.setscl = savage4_gpio_setscl;
198 par->chan.algo.getsda = savage4_gpio_getsda;
199 par->chan.algo.getscl = savage4_gpio_getscl;
200 break;
201 default:
202 par->chan.par = NULL;
203 }
204
205 savage_setup_i2c_bus(&par->chan, "SAVAGE DDC2");
206}
207
208void savagefb_delete_i2c_busses(struct fb_info *info)
209{
Antonino A. Daplasb8901b02006-01-09 20:53:02 -0800210 struct savagefb_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Antonino A. Daplasb8901b02006-01-09 20:53:02 -0800212 if (par->chan.par)
213 i2c_bit_del_bus(&par->chan.adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 par->chan.par = NULL;
216}
217
218static u8 *savage_do_probe_i2c_edid(struct savagefb_i2c_chan *chan)
219{
220 u8 start = 0x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 struct i2c_msg msgs[] = {
222 {
223 .addr = SAVAGE_DDC,
224 .len = 1,
225 .buf = &start,
226 }, {
227 .addr = SAVAGE_DDC,
228 .flags = I2C_M_RD,
229 .len = EDID_LENGTH,
230 },
231 };
232 u8 *buf = NULL;
233
Antonino A. Daplasb8901b02006-01-09 20:53:02 -0800234 if (chan->par) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 buf = kmalloc(EDID_LENGTH, GFP_KERNEL);
236
237 if (buf) {
238 msgs[1].buf = buf;
239
Antonino A. Daplasb8901b02006-01-09 20:53:02 -0800240 if (i2c_transfer(&chan->adapter, msgs, 2) != 2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 dev_dbg(&chan->par->pcidev->dev,
242 "Unable to read EDID block.\n");
243 kfree(buf);
244 buf = NULL;
245 }
246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248
249 return buf;
250}
251
Antonino A. Daplas13776712005-09-09 13:04:35 -0700252int savagefb_probe_i2c_connector(struct fb_info *info, u8 **out_edid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Antonino A. Daplas13776712005-09-09 13:04:35 -0700254 struct savagefb_par *par = info->par;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 u8 *edid = NULL;
256 int i;
257
258 for (i = 0; i < 3; i++) {
259 /* Do the real work */
260 edid = savage_do_probe_i2c_edid(&par->chan);
261 if (edid)
262 break;
263 }
Antonino A. Daplas13776712005-09-09 13:04:35 -0700264
265 if (!edid) {
266 /* try to get from firmware */
Antonino A. Daplas7b6a1862005-09-15 20:58:57 +0800267 const u8 *e = fb_firmware_edid(info->device);
268
269 if (e) {
270 edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
271 if (edid)
272 memcpy(edid, e, EDID_LENGTH);
273 }
Antonino A. Daplas13776712005-09-09 13:04:35 -0700274 }
275
Antonino A. Daplas0e4be282006-03-27 01:17:34 -0800276 *out_edid = edid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Antonino A. Daplas13776712005-09-09 13:04:35 -0700278 return (edid) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281MODULE_LICENSE("GPL");