Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/video/nvidia/nvidia-i2c.c - nVidia i2c |
| 3 | * |
| 4 | * Copyright 2004 Antonino A. Daplas <adaplas @pol.net> |
| 5 | * |
| 6 | * Based 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 | |
| 21 | #include "nv_type.h" |
| 22 | #include "nv_local.h" |
| 23 | #include "nv_proto.h" |
| 24 | |
| 25 | #include "../edid.h" |
| 26 | |
| 27 | static void nvidia_gpio_setscl(void *data, int state) |
| 28 | { |
| 29 | struct nvidia_i2c_chan *chan = data; |
| 30 | struct nvidia_par *par = chan->par; |
| 31 | u32 val; |
| 32 | |
| 33 | VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base + 1); |
| 34 | val = VGA_RD08(par->PCIO, 0x3d5) & 0xf0; |
| 35 | |
| 36 | if (state) |
| 37 | val |= 0x20; |
| 38 | else |
| 39 | val &= ~0x20; |
| 40 | |
| 41 | VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base + 1); |
| 42 | VGA_WR08(par->PCIO, 0x3d5, val | 0x1); |
| 43 | } |
| 44 | |
| 45 | static void nvidia_gpio_setsda(void *data, int state) |
| 46 | { |
Antonino A. Daplas | c439e34 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 47 | struct nvidia_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | struct nvidia_par *par = chan->par; |
| 49 | u32 val; |
| 50 | |
| 51 | VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base + 1); |
| 52 | val = VGA_RD08(par->PCIO, 0x3d5) & 0xf0; |
| 53 | |
| 54 | if (state) |
| 55 | val |= 0x10; |
| 56 | else |
| 57 | val &= ~0x10; |
| 58 | |
| 59 | VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base + 1); |
| 60 | VGA_WR08(par->PCIO, 0x3d5, val | 0x1); |
| 61 | } |
| 62 | |
| 63 | static int nvidia_gpio_getscl(void *data) |
| 64 | { |
Antonino A. Daplas | c439e34 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 65 | struct nvidia_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | struct nvidia_par *par = chan->par; |
| 67 | u32 val = 0; |
| 68 | |
| 69 | VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base); |
| 70 | if (VGA_RD08(par->PCIO, 0x3d5) & 0x04) |
| 71 | val = 1; |
| 72 | |
| 73 | val = VGA_RD08(par->PCIO, 0x3d5); |
| 74 | |
| 75 | return val; |
| 76 | } |
| 77 | |
| 78 | static int nvidia_gpio_getsda(void *data) |
| 79 | { |
Antonino A. Daplas | c439e34 | 2006-01-09 20:53:02 -0800 | [diff] [blame] | 80 | struct nvidia_i2c_chan *chan = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | struct nvidia_par *par = chan->par; |
| 82 | u32 val = 0; |
| 83 | |
| 84 | VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base); |
| 85 | if (VGA_RD08(par->PCIO, 0x3d5) & 0x08) |
| 86 | val = 1; |
| 87 | |
| 88 | return val; |
| 89 | } |
| 90 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | static int nvidia_setup_i2c_bus(struct nvidia_i2c_chan *chan, const char *name) |
| 92 | { |
| 93 | int rc; |
| 94 | |
| 95 | strcpy(chan->adapter.name, name); |
| 96 | chan->adapter.owner = THIS_MODULE; |
Jean Delvare | 1684a984 | 2005-08-11 23:51:10 +0200 | [diff] [blame] | 97 | chan->adapter.id = I2C_HW_B_NVIDIA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | chan->adapter.algo_data = &chan->algo; |
| 99 | chan->adapter.dev.parent = &chan->par->pci_dev->dev; |
| 100 | chan->algo.setsda = nvidia_gpio_setsda; |
| 101 | chan->algo.setscl = nvidia_gpio_setscl; |
| 102 | chan->algo.getsda = nvidia_gpio_getsda; |
| 103 | chan->algo.getscl = nvidia_gpio_getscl; |
| 104 | chan->algo.udelay = 40; |
| 105 | chan->algo.timeout = msecs_to_jiffies(2); |
| 106 | chan->algo.data = chan; |
| 107 | |
| 108 | i2c_set_adapdata(&chan->adapter, chan); |
| 109 | |
| 110 | /* Raise SCL and SDA */ |
| 111 | nvidia_gpio_setsda(chan, 1); |
| 112 | nvidia_gpio_setscl(chan, 1); |
| 113 | udelay(20); |
| 114 | |
| 115 | rc = i2c_bit_add_bus(&chan->adapter); |
| 116 | if (rc == 0) |
| 117 | dev_dbg(&chan->par->pci_dev->dev, |
| 118 | "I2C bus %s registered.\n", name); |
| 119 | else { |
| 120 | dev_warn(&chan->par->pci_dev->dev, |
| 121 | "Failed to register I2C bus %s.\n", name); |
| 122 | chan->par = NULL; |
| 123 | } |
| 124 | |
| 125 | return rc; |
| 126 | } |
| 127 | |
| 128 | void nvidia_create_i2c_busses(struct nvidia_par *par) |
| 129 | { |
| 130 | par->bus = 3; |
| 131 | |
| 132 | par->chan[0].par = par; |
| 133 | par->chan[1].par = par; |
| 134 | par->chan[2].par = par; |
| 135 | |
| 136 | par->chan[0].ddc_base = 0x3e; |
Alessandro Zummo | 2610acc | 2006-01-09 20:52:57 -0800 | [diff] [blame] | 137 | nvidia_setup_i2c_bus(&par->chan[0], "nvidia #0"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
| 139 | par->chan[1].ddc_base = 0x36; |
Alessandro Zummo | 2610acc | 2006-01-09 20:52:57 -0800 | [diff] [blame] | 140 | nvidia_setup_i2c_bus(&par->chan[1], "nvidia #1"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | par->chan[2].ddc_base = 0x50; |
Alessandro Zummo | 2610acc | 2006-01-09 20:52:57 -0800 | [diff] [blame] | 143 | nvidia_setup_i2c_bus(&par->chan[2], "nvidia #2"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void nvidia_delete_i2c_busses(struct nvidia_par *par) |
| 147 | { |
| 148 | if (par->chan[0].par) |
Jean Delvare | 3269711 | 2006-12-10 21:21:33 +0100 | [diff] [blame] | 149 | i2c_del_adapter(&par->chan[0].adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | par->chan[0].par = NULL; |
| 151 | |
| 152 | if (par->chan[1].par) |
Jean Delvare | 3269711 | 2006-12-10 21:21:33 +0100 | [diff] [blame] | 153 | i2c_del_adapter(&par->chan[1].adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | par->chan[1].par = NULL; |
| 155 | |
| 156 | if (par->chan[2].par) |
Jean Delvare | 3269711 | 2006-12-10 21:21:33 +0100 | [diff] [blame] | 157 | i2c_del_adapter(&par->chan[2].adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | par->chan[2].par = NULL; |
| 159 | |
| 160 | } |
| 161 | |
Andrew Morton | 7e491092 | 2006-10-11 01:20:35 -0700 | [diff] [blame] | 162 | static u8 *nvidia_do_probe_i2c_edid(struct nvidia_i2c_chan *chan) |
| 163 | { |
| 164 | u8 start = 0x0; |
| 165 | struct i2c_msg msgs[] = { |
| 166 | { |
| 167 | .addr = 0x50, |
| 168 | .len = 1, |
| 169 | .buf = &start, |
| 170 | }, { |
| 171 | .addr = 0x50, |
| 172 | .flags = I2C_M_RD, |
| 173 | .len = EDID_LENGTH, |
| 174 | }, |
| 175 | }; |
| 176 | u8 *buf; |
| 177 | |
| 178 | if (!chan->par) |
| 179 | return NULL; |
| 180 | |
| 181 | buf = kmalloc(EDID_LENGTH, GFP_KERNEL); |
| 182 | if (!buf) { |
| 183 | dev_warn(&chan->par->pci_dev->dev, "Out of memory!\n"); |
| 184 | return NULL; |
| 185 | } |
| 186 | msgs[1].buf = buf; |
| 187 | |
| 188 | if (i2c_transfer(&chan->adapter, msgs, 2) == 2) |
| 189 | return buf; |
| 190 | dev_dbg(&chan->par->pci_dev->dev, "Unable to read EDID block.\n"); |
| 191 | kfree(buf); |
| 192 | return NULL; |
| 193 | } |
| 194 | |
Antonino A. Daplas | 094bb65 | 2005-09-09 13:04:36 -0700 | [diff] [blame] | 195 | int nvidia_probe_i2c_connector(struct fb_info *info, int conn, u8 **out_edid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | { |
Antonino A. Daplas | 094bb65 | 2005-09-09 13:04:36 -0700 | [diff] [blame] | 197 | struct nvidia_par *par = info->par; |
Andrew Morton | 7e491092 | 2006-10-11 01:20:35 -0700 | [diff] [blame] | 198 | u8 *edid = NULL; |
| 199 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
Andrew Morton | 7e491092 | 2006-10-11 01:20:35 -0700 | [diff] [blame] | 201 | for (i = 0; i < 3; i++) { |
| 202 | /* Do the real work */ |
| 203 | edid = nvidia_do_probe_i2c_edid(&par->chan[conn - 1]); |
| 204 | if (edid) |
| 205 | break; |
| 206 | } |
Antonino A. Daplas | 094bb65 | 2005-09-09 13:04:36 -0700 | [diff] [blame] | 207 | |
| 208 | if (!edid && conn == 1) { |
| 209 | /* try to get from firmware */ |
Antonino A. Daplas | 0ed8e04 | 2005-09-14 14:19:15 -0700 | [diff] [blame] | 210 | const u8 *e = fb_firmware_edid(info->device); |
| 211 | |
Alexey Dobriyan | bfba7b3 | 2006-12-08 02:40:46 -0800 | [diff] [blame] | 212 | if (e != NULL) |
| 213 | edid = kmemdup(e, EDID_LENGTH, GFP_KERNEL); |
Antonino A. Daplas | 094bb65 | 2005-09-09 13:04:36 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Antonino A. Daplas | 3d5b191 | 2006-03-27 01:17:33 -0800 | [diff] [blame] | 216 | *out_edid = edid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
Antonino A. Daplas | 094bb65 | 2005-09-09 13:04:36 -0700 | [diff] [blame] | 218 | return (edid) ? 0 : 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | } |