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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | static void savage4_gpio_setscl(void *data, int val) |
| 46 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 47 | struct savagefb_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | unsigned int r; |
| 49 | |
| 50 | r = readl(chan->ioaddr + chan->reg); |
| 51 | if(val) |
| 52 | r |= SAVAGE4_I2C_SCL_OUT; |
| 53 | else |
| 54 | r &= ~SAVAGE4_I2C_SCL_OUT; |
| 55 | writel(r, chan->ioaddr + chan->reg); |
| 56 | readl(chan->ioaddr + chan->reg); /* flush posted write */ |
| 57 | } |
| 58 | |
| 59 | static void savage4_gpio_setsda(void *data, int val) |
| 60 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 61 | struct savagefb_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
| 63 | unsigned int r; |
| 64 | r = readl(chan->ioaddr + chan->reg); |
| 65 | if(val) |
| 66 | r |= SAVAGE4_I2C_SDA_OUT; |
| 67 | else |
| 68 | r &= ~SAVAGE4_I2C_SDA_OUT; |
| 69 | writel(r, chan->ioaddr + chan->reg); |
| 70 | readl(chan->ioaddr + chan->reg); /* flush posted write */ |
| 71 | } |
| 72 | |
| 73 | static int savage4_gpio_getscl(void *data) |
| 74 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 75 | struct savagefb_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
| 77 | return (0 != (readl(chan->ioaddr + chan->reg) & SAVAGE4_I2C_SCL_IN)); |
| 78 | } |
| 79 | |
| 80 | static int savage4_gpio_getsda(void *data) |
| 81 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 82 | struct savagefb_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
| 84 | return (0 != (readl(chan->ioaddr + chan->reg) & SAVAGE4_I2C_SDA_IN)); |
| 85 | } |
| 86 | |
| 87 | static void prosavage_gpio_setscl(void* data, int val) |
| 88 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 89 | struct savagefb_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | u32 r; |
| 91 | |
Antonino A. Daplas | f782915 | 2007-05-08 00:38:34 -0700 | [diff] [blame] | 92 | r = VGArCR(chan->reg, chan->par); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | r |= PROSAVAGE_I2C_ENAB; |
| 94 | if (val) { |
| 95 | r |= PROSAVAGE_I2C_SCL_OUT; |
| 96 | } else { |
| 97 | r &= ~PROSAVAGE_I2C_SCL_OUT; |
| 98 | } |
Antonino A. Daplas | f782915 | 2007-05-08 00:38:34 -0700 | [diff] [blame] | 99 | |
| 100 | VGAwCR(chan->reg, r, chan->par); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | static void prosavage_gpio_setsda(void* data, int val) |
| 104 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 105 | struct savagefb_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | unsigned int r; |
| 107 | |
Antonino A. Daplas | f782915 | 2007-05-08 00:38:34 -0700 | [diff] [blame] | 108 | r = VGArCR(chan->reg, chan->par); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | r |= PROSAVAGE_I2C_ENAB; |
| 110 | if (val) { |
| 111 | r |= PROSAVAGE_I2C_SDA_OUT; |
| 112 | } else { |
| 113 | r &= ~PROSAVAGE_I2C_SDA_OUT; |
| 114 | } |
Antonino A. Daplas | f782915 | 2007-05-08 00:38:34 -0700 | [diff] [blame] | 115 | |
| 116 | VGAwCR(chan->reg, r, chan->par); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static int prosavage_gpio_getscl(void* data) |
| 120 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 121 | struct savagefb_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Antonino A. Daplas | f782915 | 2007-05-08 00:38:34 -0700 | [diff] [blame] | 123 | return (VGArCR(chan->reg, chan->par) & PROSAVAGE_I2C_SCL_IN) ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static int prosavage_gpio_getsda(void* data) |
| 127 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 128 | struct savagefb_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
Antonino A. Daplas | f782915 | 2007-05-08 00:38:34 -0700 | [diff] [blame] | 130 | return (VGArCR(chan->reg, chan->par) & PROSAVAGE_I2C_SDA_IN) ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | static int savage_setup_i2c_bus(struct savagefb_i2c_chan *chan, |
| 134 | const char *name) |
| 135 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | int rc = 0; |
| 137 | |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 138 | if (chan->par) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | strcpy(chan->adapter.name, name); |
| 140 | chan->adapter.owner = THIS_MODULE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | chan->adapter.algo_data = &chan->algo; |
| 142 | chan->adapter.dev.parent = &chan->par->pcidev->dev; |
Jean Delvare | 9201a85 | 2008-04-28 02:15:16 -0700 | [diff] [blame] | 143 | chan->algo.udelay = 10; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | chan->algo.timeout = 20; |
| 145 | chan->algo.data = chan; |
| 146 | |
| 147 | i2c_set_adapdata(&chan->adapter, chan); |
| 148 | |
| 149 | /* Raise SCL and SDA */ |
| 150 | chan->algo.setsda(chan, 1); |
| 151 | chan->algo.setscl(chan, 1); |
| 152 | udelay(20); |
| 153 | |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 154 | rc = i2c_bit_add_bus(&chan->adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
| 156 | if (rc == 0) |
| 157 | dev_dbg(&chan->par->pcidev->dev, |
| 158 | "I2C bus %s registered.\n", name); |
| 159 | else |
| 160 | dev_warn(&chan->par->pcidev->dev, |
| 161 | "Failed to register I2C bus %s.\n", name); |
Tormod Volden | 787dffa5 | 2011-04-03 12:54:06 +0000 | [diff] [blame] | 162 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
| 164 | return rc; |
| 165 | } |
| 166 | |
| 167 | void savagefb_create_i2c_busses(struct fb_info *info) |
| 168 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 169 | struct savagefb_par *par = info->par; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | par->chan.par = par; |
| 171 | |
Tormod Volden | 21cd72e | 2011-04-03 12:54:05 +0000 | [diff] [blame] | 172 | switch (par->chip) { |
| 173 | case S3_PROSAVAGE: |
Tormod Volden | cc40634 | 2011-04-10 20:57:34 +0000 | [diff] [blame] | 174 | case S3_PROSAVAGEDDR: |
| 175 | case S3_TWISTER: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | par->chan.reg = CR_SERIAL2; |
| 177 | par->chan.ioaddr = par->mmio.vbase; |
| 178 | par->chan.algo.setsda = prosavage_gpio_setsda; |
| 179 | par->chan.algo.setscl = prosavage_gpio_setscl; |
| 180 | par->chan.algo.getsda = prosavage_gpio_getsda; |
| 181 | par->chan.algo.getscl = prosavage_gpio_getscl; |
| 182 | break; |
Tormod Volden | 21cd72e | 2011-04-03 12:54:05 +0000 | [diff] [blame] | 183 | case S3_SAVAGE4: |
Ondrej Zary | f0a2f35 | 2010-10-27 15:33:25 -0700 | [diff] [blame] | 184 | par->chan.reg = CR_SERIAL1; |
| 185 | if (par->pcidev->revision > 1 && !(VGArCR(0xa6, par) & 0x40)) |
| 186 | par->chan.reg = CR_SERIAL2; |
| 187 | par->chan.ioaddr = par->mmio.vbase; |
| 188 | par->chan.algo.setsda = prosavage_gpio_setsda; |
| 189 | par->chan.algo.setscl = prosavage_gpio_setscl; |
| 190 | par->chan.algo.getsda = prosavage_gpio_getsda; |
| 191 | par->chan.algo.getscl = prosavage_gpio_getscl; |
| 192 | break; |
Tormod Volden | 21cd72e | 2011-04-03 12:54:05 +0000 | [diff] [blame] | 193 | case S3_SAVAGE2000: |
Tormod Volden | a564d301 | 2011-04-03 12:54:04 +0000 | [diff] [blame] | 194 | par->chan.reg = MM_SERIAL1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 208 | void savagefb_delete_i2c_busses(struct fb_info *info) |
| 209 | { |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 210 | struct savagefb_par *par = info->par; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Antonino A. Daplas | b8901b0 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 212 | if (par->chan.par) |
Jean Delvare | 3269711 | 2006-12-10 21:21:33 +0100 | [diff] [blame] | 213 | i2c_del_adapter(&par->chan.adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
| 215 | par->chan.par = NULL; |
| 216 | } |
| 217 | |
Antonino A. Daplas | 1377671 | 2005-09-09 13:04:35 -0700 | [diff] [blame] | 218 | int savagefb_probe_i2c_connector(struct fb_info *info, u8 **out_edid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | { |
Antonino A. Daplas | 1377671 | 2005-09-09 13:04:35 -0700 | [diff] [blame] | 220 | struct savagefb_par *par = info->par; |
Antonino A. Daplas | 946c4ea | 2006-10-03 01:14:45 -0700 | [diff] [blame] | 221 | u8 *edid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
Antonino A. Daplas | 946c4ea | 2006-10-03 01:14:45 -0700 | [diff] [blame] | 223 | if (par->chan.par) |
| 224 | edid = fb_ddc_read(&par->chan.adapter); |
| 225 | else |
| 226 | edid = NULL; |
Antonino A. Daplas | 1377671 | 2005-09-09 13:04:35 -0700 | [diff] [blame] | 227 | |
| 228 | if (!edid) { |
| 229 | /* try to get from firmware */ |
Antonino A. Daplas | 7b6a186 | 2005-09-15 20:58:57 +0800 | [diff] [blame] | 230 | const u8 *e = fb_firmware_edid(info->device); |
| 231 | |
Alexey Dobriyan | bfba7b3 | 2006-12-08 02:40:46 -0800 | [diff] [blame] | 232 | if (e) |
| 233 | edid = kmemdup(e, EDID_LENGTH, GFP_KERNEL); |
Antonino A. Daplas | 1377671 | 2005-09-09 13:04:35 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Antonino A. Daplas | 0e4be28 | 2006-03-27 01:17:34 -0800 | [diff] [blame] | 236 | *out_edid = edid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
Antonino A. Daplas | 1377671 | 2005-09-09 13:04:35 -0700 | [diff] [blame] | 238 | return (edid) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | MODULE_LICENSE("GPL"); |