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/sched.h>
16 #include <linux/delay.h>
17 #include <linux/pci.h>
28 static void nvidia_gpio_setscl(void *data, int state)
30 struct nvidia_i2c_chan *chan = data;
31 struct nvidia_par *par = chan->par;
34 VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base + 1);
35 val = VGA_RD08(par->PCIO, 0x3d5) & 0xf0;
42 VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base + 1);
43 VGA_WR08(par->PCIO, 0x3d5, val | 0x1);
46 static void nvidia_gpio_setsda(void *data, int state)
48 struct nvidia_i2c_chan *chan = data;
49 struct nvidia_par *par = chan->par;
52 VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base + 1);
53 val = VGA_RD08(par->PCIO, 0x3d5) & 0xf0;
60 VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base + 1);
61 VGA_WR08(par->PCIO, 0x3d5, val | 0x1);
64 static int nvidia_gpio_getscl(void *data)
66 struct nvidia_i2c_chan *chan = data;
67 struct nvidia_par *par = chan->par;
70 VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base);
71 if (VGA_RD08(par->PCIO, 0x3d5) & 0x04)
74 val = VGA_RD08(par->PCIO, 0x3d5);
79 static int nvidia_gpio_getsda(void *data)
81 struct nvidia_i2c_chan *chan = data;
82 struct nvidia_par *par = chan->par;
85 VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base);
86 if (VGA_RD08(par->PCIO, 0x3d5) & 0x08)
92 static int nvidia_setup_i2c_bus(struct nvidia_i2c_chan *chan, const char *name)
96 strcpy(chan->adapter.name, name);
97 chan->adapter.owner = THIS_MODULE;
98 chan->adapter.id = I2C_HW_B_NVIDIA;
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;
109 i2c_set_adapdata(&chan->adapter, chan);
111 /* Raise SCL and SDA */
112 nvidia_gpio_setsda(chan, 1);
113 nvidia_gpio_setscl(chan, 1);
116 rc = i2c_bit_add_bus(&chan->adapter);
118 dev_dbg(&chan->par->pci_dev->dev,
119 "I2C bus %s registered.\n", name);
121 dev_warn(&chan->par->pci_dev->dev,
122 "Failed to register I2C bus %s.\n", name);
129 void nvidia_create_i2c_busses(struct nvidia_par *par)
133 par->chan[0].par = par;
134 par->chan[1].par = par;
135 par->chan[2].par = par;
137 par->chan[0].ddc_base = 0x3e;
138 nvidia_setup_i2c_bus(&par->chan[0], "nvidia #0");
140 par->chan[1].ddc_base = 0x36;
141 nvidia_setup_i2c_bus(&par->chan[1], "nvidia #1");
143 par->chan[2].ddc_base = 0x50;
144 nvidia_setup_i2c_bus(&par->chan[2], "nvidia #2");
147 void nvidia_delete_i2c_busses(struct nvidia_par *par)
149 if (par->chan[0].par)
150 i2c_bit_del_bus(&par->chan[0].adapter);
151 par->chan[0].par = NULL;
153 if (par->chan[1].par)
154 i2c_bit_del_bus(&par->chan[1].adapter);
155 par->chan[1].par = NULL;
157 if (par->chan[2].par)
158 i2c_bit_del_bus(&par->chan[2].adapter);
159 par->chan[2].par = NULL;
163 static u8 *nvidia_do_probe_i2c_edid(struct nvidia_i2c_chan *chan)
166 struct i2c_msg msgs[] = {
182 buf = kmalloc(EDID_LENGTH, GFP_KERNEL);
184 dev_warn(&chan->par->pci_dev->dev, "Out of memory!\n");
189 if (i2c_transfer(&chan->adapter, msgs, 2) == 2)
191 dev_dbg(&chan->par->pci_dev->dev, "Unable to read EDID block.\n");
196 int nvidia_probe_i2c_connector(struct fb_info *info, int conn, u8 **out_edid)
198 struct nvidia_par *par = info->par;
202 for (i = 0; i < 3; i++) {
203 /* Do the real work */
204 edid = nvidia_do_probe_i2c_edid(&par->chan[conn - 1]);
209 if (!edid && conn == 1) {
210 /* try to get from firmware */
211 const u8 *e = fb_firmware_edid(info->device);
214 edid = kmalloc(EDID_LENGTH, GFP_KERNEL);
216 memcpy(edid, e, EDID_LENGTH);
222 return (edid) ? 0 : 1;