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