Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/module.h> |
| 14 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 50 | struct savagefb_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 64 | struct savagefb_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 78 | struct savagefb_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | return (0 != (readl(chan->ioaddr + chan->reg) & SAVAGE4_I2C_SCL_IN)); |
| 81 | } |
| 82 | |
| 83 | static int savage4_gpio_getsda(void *data) |
| 84 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 85 | struct savagefb_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 92 | struct savagefb_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 108 | struct savagefb_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 124 | struct savagefb_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 132 | struct savagefb_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | |
| 134 | SET_CR_IX(chan->ioaddr, chan->reg); |
| 135 | return (0 != (GET_CR_DATA(chan->ioaddr) & PROSAVAGE_I2C_SDA_IN)); |
| 136 | } |
| 137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | static int savage_setup_i2c_bus(struct savagefb_i2c_chan *chan, |
| 139 | const char *name) |
| 140 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | int rc = 0; |
| 142 | |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 143 | if (chan->par) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | strcpy(chan->adapter.name, name); |
| 145 | chan->adapter.owner = THIS_MODULE; |
Jean Delvare | 1684a984 | 2005-08-11 23:51:10 +0200 | [diff] [blame] | 146 | chan->adapter.id = I2C_HW_B_SAVAGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | chan->adapter.algo_data = &chan->algo; |
| 148 | chan->adapter.dev.parent = &chan->par->pcidev->dev; |
| 149 | chan->algo.udelay = 40; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 160 | rc = i2c_bit_add_bus(&chan->adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | } else |
| 169 | chan->par = NULL; |
| 170 | |
| 171 | return rc; |
| 172 | } |
| 173 | |
| 174 | void savagefb_create_i2c_busses(struct fb_info *info) |
| 175 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 176 | struct savagefb_par *par = info->par; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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: |
Jean Delvare | c35dba6 | 2006-01-09 20:52:59 -0800 | [diff] [blame] | 190 | case FB_ACCEL_SAVAGE2000: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 207 | struct savagefb_par *par = info->par; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 209 | if (par->chan.par) |
Jean Delvare | 3269711 | 2006-12-10 21:21:33 +0100 | [diff] [blame] | 210 | i2c_del_adapter(&par->chan.adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
| 212 | par->chan.par = NULL; |
| 213 | } |
| 214 | |
Antonino A. Daplas | 1377671 | 2005-09-09 13:04:35 -0700 | [diff] [blame] | 215 | int savagefb_probe_i2c_connector(struct fb_info *info, u8 **out_edid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | { |
Antonino A. Daplas | 1377671 | 2005-09-09 13:04:35 -0700 | [diff] [blame] | 217 | struct savagefb_par *par = info->par; |
Antonino A. Daplas | 946c4ea | 2006-10-03 01:14:45 -0700 | [diff] [blame] | 218 | u8 *edid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
Antonino A. Daplas | 946c4ea | 2006-10-03 01:14:45 -0700 | [diff] [blame] | 220 | if (par->chan.par) |
| 221 | edid = fb_ddc_read(&par->chan.adapter); |
| 222 | else |
| 223 | edid = NULL; |
Antonino A. Daplas | 1377671 | 2005-09-09 13:04:35 -0700 | [diff] [blame] | 224 | |
| 225 | if (!edid) { |
| 226 | /* try to get from firmware */ |
Antonino A. Daplas | 7b6a186 | 2005-09-15 20:58:57 +0800 | [diff] [blame] | 227 | const u8 *e = fb_firmware_edid(info->device); |
| 228 | |
Alexey Dobriyan | bfba7b3 | 2006-12-08 02:40:46 -0800 | [diff] [blame] | 229 | if (e) |
| 230 | edid = kmemdup(e, EDID_LENGTH, GFP_KERNEL); |
Antonino A. Daplas | 1377671 | 2005-09-09 13:04:35 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Antonino A. Daplas | 0e4be28 | 2006-03-27 01:17:34 -0800 | [diff] [blame] | 233 | *out_edid = edid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
Antonino A. Daplas | 1377671 | 2005-09-09 13:04:35 -0700 | [diff] [blame] | 235 | return (edid) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | MODULE_LICENSE("GPL"); |