2 * linux/drivers/video/nvidia/nvidia-i2c.c - nVidia i2c
4 * Copyright 2004 Antonino A. Daplas <adaplas @pol.net>
6 * Based on rivafb-i2c.c
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
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/delay.h>
16 #include <linux/pci.h>
27 static void nvidia_gpio_setscl(void *data, int state)
29 struct nvidia_i2c_chan *chan = data;
30 struct nvidia_par *par = chan->par;
33 VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base + 1);
34 val = VGA_RD08(par->PCIO, 0x3d5) & 0xf0;
41 VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base + 1);
42 VGA_WR08(par->PCIO, 0x3d5, val | 0x1);
45 static void nvidia_gpio_setsda(void *data, int state)
47 struct nvidia_i2c_chan *chan = data;
48 struct nvidia_par *par = chan->par;
51 VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base + 1);
52 val = VGA_RD08(par->PCIO, 0x3d5) & 0xf0;
59 VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base + 1);
60 VGA_WR08(par->PCIO, 0x3d5, val | 0x1);
63 static int nvidia_gpio_getscl(void *data)
65 struct nvidia_i2c_chan *chan = data;
66 struct nvidia_par *par = chan->par;
69 VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base);
70 if (VGA_RD08(par->PCIO, 0x3d5) & 0x04)
73 val = VGA_RD08(par->PCIO, 0x3d5);
78 static int nvidia_gpio_getsda(void *data)
80 struct nvidia_i2c_chan *chan = data;
81 struct nvidia_par *par = chan->par;
84 VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base);
85 if (VGA_RD08(par->PCIO, 0x3d5) & 0x08)
91 static int nvidia_setup_i2c_bus(struct nvidia_i2c_chan *chan, const char *name)
95 strcpy(chan->adapter.name, name);
96 chan->adapter.owner = THIS_MODULE;
97 chan->adapter.id = I2C_HW_B_NVIDIA;
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;
108 i2c_set_adapdata(&chan->adapter, chan);
110 /* Raise SCL and SDA */
111 nvidia_gpio_setsda(chan, 1);
112 nvidia_gpio_setscl(chan, 1);
115 rc = i2c_bit_add_bus(&chan->adapter);
117 dev_dbg(&chan->par->pci_dev->dev,
118 "I2C bus %s registered.\n", name);
120 dev_warn(&chan->par->pci_dev->dev,
121 "Failed to register I2C bus %s.\n", name);
128 void nvidia_create_i2c_busses(struct nvidia_par *par)
132 par->chan[0].par = par;
133 par->chan[1].par = par;
134 par->chan[2].par = par;
136 par->chan[0].ddc_base = 0x3e;
137 nvidia_setup_i2c_bus(&par->chan[0], "nvidia #0");
139 par->chan[1].ddc_base = 0x36;
140 nvidia_setup_i2c_bus(&par->chan[1], "nvidia #1");
142 par->chan[2].ddc_base = 0x50;
143 nvidia_setup_i2c_bus(&par->chan[2], "nvidia #2");
146 void nvidia_delete_i2c_busses(struct nvidia_par *par)
148 if (par->chan[0].par)
149 i2c_del_adapter(&par->chan[0].adapter);
150 par->chan[0].par = NULL;
152 if (par->chan[1].par)
153 i2c_del_adapter(&par->chan[1].adapter);
154 par->chan[1].par = NULL;
156 if (par->chan[2].par)
157 i2c_del_adapter(&par->chan[2].adapter);
158 par->chan[2].par = NULL;
162 static u8 *nvidia_do_probe_i2c_edid(struct nvidia_i2c_chan *chan)
165 struct i2c_msg msgs[] = {
181 buf = kmalloc(EDID_LENGTH, GFP_KERNEL);
183 dev_warn(&chan->par->pci_dev->dev, "Out of memory!\n");
188 if (i2c_transfer(&chan->adapter, msgs, 2) == 2)
190 dev_dbg(&chan->par->pci_dev->dev, "Unable to read EDID block.\n");
195 int nvidia_probe_i2c_connector(struct fb_info *info, int conn, u8 **out_edid)
197 struct nvidia_par *par = info->par;
201 for (i = 0; i < 3; i++) {
202 /* Do the real work */
203 edid = nvidia_do_probe_i2c_edid(&par->chan[conn - 1]);
208 if (!edid && conn == 1) {
209 /* try to get from firmware */
210 const u8 *e = fb_firmware_edid(info->device);
213 edid = kmemdup(e, EDID_LENGTH, GFP_KERNEL);
218 return (edid) ? 0 : 1;