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> |
| 15 | #include <linux/sched.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/pci.h> |
| 18 | #include <linux/fb.h> |
| 19 | |
| 20 | #include <asm/io.h> |
| 21 | #include "savagefb.h" |
| 22 | |
| 23 | #define SAVAGE_DDC 0x50 |
| 24 | |
| 25 | #define VGA_CR_IX 0x3d4 |
| 26 | #define VGA_CR_DATA 0x3d5 |
| 27 | |
| 28 | #define CR_SERIAL1 0xa0 /* I2C serial communications interface */ |
| 29 | #define MM_SERIAL1 0xff20 |
| 30 | #define CR_SERIAL2 0xb1 /* DDC2 monitor communications interface */ |
| 31 | |
| 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 |
| 38 | |
| 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 |
| 44 | |
| 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) |
| 48 | |
| 49 | static void savage4_gpio_setscl(void *data, int val) |
| 50 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 51 | struct savagefb_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | unsigned int r; |
| 53 | |
| 54 | r = readl(chan->ioaddr + chan->reg); |
| 55 | if(val) |
| 56 | r |= SAVAGE4_I2C_SCL_OUT; |
| 57 | else |
| 58 | r &= ~SAVAGE4_I2C_SCL_OUT; |
| 59 | writel(r, chan->ioaddr + chan->reg); |
| 60 | readl(chan->ioaddr + chan->reg); /* flush posted write */ |
| 61 | } |
| 62 | |
| 63 | static void savage4_gpio_setsda(void *data, int val) |
| 64 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 65 | struct savagefb_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
| 67 | unsigned int r; |
| 68 | r = readl(chan->ioaddr + chan->reg); |
| 69 | if(val) |
| 70 | r |= SAVAGE4_I2C_SDA_OUT; |
| 71 | else |
| 72 | r &= ~SAVAGE4_I2C_SDA_OUT; |
| 73 | writel(r, chan->ioaddr + chan->reg); |
| 74 | readl(chan->ioaddr + chan->reg); /* flush posted write */ |
| 75 | } |
| 76 | |
| 77 | static int savage4_gpio_getscl(void *data) |
| 78 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 79 | struct savagefb_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | return (0 != (readl(chan->ioaddr + chan->reg) & SAVAGE4_I2C_SCL_IN)); |
| 82 | } |
| 83 | |
| 84 | static int savage4_gpio_getsda(void *data) |
| 85 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 86 | struct savagefb_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
| 88 | return (0 != (readl(chan->ioaddr + chan->reg) & SAVAGE4_I2C_SDA_IN)); |
| 89 | } |
| 90 | |
| 91 | static void prosavage_gpio_setscl(void* data, int val) |
| 92 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 93 | struct savagefb_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | u32 r; |
| 95 | |
| 96 | SET_CR_IX(chan->ioaddr, chan->reg); |
| 97 | r = GET_CR_DATA(chan->ioaddr); |
| 98 | r |= PROSAVAGE_I2C_ENAB; |
| 99 | if (val) { |
| 100 | r |= PROSAVAGE_I2C_SCL_OUT; |
| 101 | } else { |
| 102 | r &= ~PROSAVAGE_I2C_SCL_OUT; |
| 103 | } |
| 104 | SET_CR_DATA(chan->ioaddr, r); |
| 105 | } |
| 106 | |
| 107 | static void prosavage_gpio_setsda(void* data, int val) |
| 108 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 109 | struct savagefb_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | unsigned int r; |
| 111 | |
| 112 | SET_CR_IX(chan->ioaddr, chan->reg); |
| 113 | r = GET_CR_DATA(chan->ioaddr); |
| 114 | r |= PROSAVAGE_I2C_ENAB; |
| 115 | if (val) { |
| 116 | r |= PROSAVAGE_I2C_SDA_OUT; |
| 117 | } else { |
| 118 | r &= ~PROSAVAGE_I2C_SDA_OUT; |
| 119 | } |
| 120 | SET_CR_DATA(chan->ioaddr, r); |
| 121 | } |
| 122 | |
| 123 | static int prosavage_gpio_getscl(void* data) |
| 124 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 125 | struct savagefb_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | SET_CR_IX(chan->ioaddr, chan->reg); |
| 128 | return (0 != (GET_CR_DATA(chan->ioaddr) & PROSAVAGE_I2C_SCL_IN)); |
| 129 | } |
| 130 | |
| 131 | static int prosavage_gpio_getsda(void* data) |
| 132 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 133 | struct savagefb_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
| 135 | SET_CR_IX(chan->ioaddr, chan->reg); |
| 136 | return (0 != (GET_CR_DATA(chan->ioaddr) & PROSAVAGE_I2C_SDA_IN)); |
| 137 | } |
| 138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | static int savage_setup_i2c_bus(struct savagefb_i2c_chan *chan, |
| 140 | const char *name) |
| 141 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | int rc = 0; |
| 143 | |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 144 | if (chan->par) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | strcpy(chan->adapter.name, name); |
| 146 | chan->adapter.owner = THIS_MODULE; |
Jean Delvare | 1684a984 | 2005-08-11 23:51:10 +0200 | [diff] [blame] | 147 | chan->adapter.id = I2C_HW_B_SAVAGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | chan->adapter.algo_data = &chan->algo; |
| 149 | chan->adapter.dev.parent = &chan->par->pcidev->dev; |
| 150 | chan->algo.udelay = 40; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | chan->algo.timeout = 20; |
| 152 | chan->algo.data = chan; |
| 153 | |
| 154 | i2c_set_adapdata(&chan->adapter, chan); |
| 155 | |
| 156 | /* Raise SCL and SDA */ |
| 157 | chan->algo.setsda(chan, 1); |
| 158 | chan->algo.setscl(chan, 1); |
| 159 | udelay(20); |
| 160 | |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 161 | rc = i2c_bit_add_bus(&chan->adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
| 163 | if (rc == 0) |
| 164 | dev_dbg(&chan->par->pcidev->dev, |
| 165 | "I2C bus %s registered.\n", name); |
| 166 | else |
| 167 | dev_warn(&chan->par->pcidev->dev, |
| 168 | "Failed to register I2C bus %s.\n", name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } else |
| 170 | chan->par = NULL; |
| 171 | |
| 172 | return rc; |
| 173 | } |
| 174 | |
| 175 | void savagefb_create_i2c_busses(struct fb_info *info) |
| 176 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 177 | struct savagefb_par *par = info->par; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | par->chan.par = par; |
| 179 | |
| 180 | switch(info->fix.accel) { |
| 181 | case FB_ACCEL_PROSAVAGE_DDRK: |
| 182 | case FB_ACCEL_PROSAVAGE_PM: |
| 183 | par->chan.reg = CR_SERIAL2; |
| 184 | par->chan.ioaddr = par->mmio.vbase; |
| 185 | par->chan.algo.setsda = prosavage_gpio_setsda; |
| 186 | par->chan.algo.setscl = prosavage_gpio_setscl; |
| 187 | par->chan.algo.getsda = prosavage_gpio_getsda; |
| 188 | par->chan.algo.getscl = prosavage_gpio_getscl; |
| 189 | break; |
| 190 | case FB_ACCEL_SAVAGE4: |
Jean Delvare | c35dba6 | 2006-01-09 20:52:59 -0800 | [diff] [blame] | 191 | case FB_ACCEL_SAVAGE2000: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | par->chan.reg = 0xff20; |
| 193 | par->chan.ioaddr = par->mmio.vbase; |
| 194 | par->chan.algo.setsda = savage4_gpio_setsda; |
| 195 | par->chan.algo.setscl = savage4_gpio_setscl; |
| 196 | par->chan.algo.getsda = savage4_gpio_getsda; |
| 197 | par->chan.algo.getscl = savage4_gpio_getscl; |
| 198 | break; |
| 199 | default: |
| 200 | par->chan.par = NULL; |
| 201 | } |
| 202 | |
| 203 | savage_setup_i2c_bus(&par->chan, "SAVAGE DDC2"); |
| 204 | } |
| 205 | |
| 206 | void savagefb_delete_i2c_busses(struct fb_info *info) |
| 207 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 208 | struct savagefb_par *par = info->par; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 210 | if (par->chan.par) |
| 211 | i2c_bit_del_bus(&par->chan.adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
| 213 | par->chan.par = NULL; |
| 214 | } |
| 215 | |
Antonino A. Daplas | 1377671 | 2005-09-09 13:04:35 -0700 | [diff] [blame] | 216 | int savagefb_probe_i2c_connector(struct fb_info *info, u8 **out_edid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | { |
Antonino A. Daplas | 1377671 | 2005-09-09 13:04:35 -0700 | [diff] [blame] | 218 | struct savagefb_par *par = info->par; |
Antonino A. Daplas | 946c4ea | 2006-10-03 01:14:45 -0700 | [diff] [blame] | 219 | u8 *edid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
Antonino A. Daplas | 946c4ea | 2006-10-03 01:14:45 -0700 | [diff] [blame] | 221 | if (par->chan.par) |
| 222 | edid = fb_ddc_read(&par->chan.adapter); |
| 223 | else |
| 224 | edid = NULL; |
Antonino A. Daplas | 1377671 | 2005-09-09 13:04:35 -0700 | [diff] [blame] | 225 | |
| 226 | if (!edid) { |
| 227 | /* try to get from firmware */ |
Antonino A. Daplas | 7b6a186 | 2005-09-15 20:58:57 +0800 | [diff] [blame] | 228 | const u8 *e = fb_firmware_edid(info->device); |
| 229 | |
| 230 | if (e) { |
| 231 | edid = kmalloc(EDID_LENGTH, GFP_KERNEL); |
| 232 | if (edid) |
| 233 | memcpy(edid, e, EDID_LENGTH); |
| 234 | } |
Antonino A. Daplas | 1377671 | 2005-09-09 13:04:35 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Antonino A. Daplas | 0e4be28 | 2006-03-27 01:17:34 -0800 | [diff] [blame] | 237 | *out_edid = edid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Antonino A. Daplas | 1377671 | 2005-09-09 13:04:35 -0700 | [diff] [blame] | 239 | return (edid) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | MODULE_LICENSE("GPL"); |