Merge master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[wrt350n-kernel.git] / drivers / video / riva / rivafb-i2c.c
blob8b1967fc116ad19636309da4bd0cd77f4ed7ed9a
1 /*
2 * linux/drivers/video/riva/fbdev-i2c.c - nVidia i2c
4 * Maintained by Ani Joshi <ajoshi@shell.unixbox.com>
6 * Copyright 2004 Antonino A. Daplas <adaplas @pol.net>
8 * Based on radeonfb-i2c.c
10 * This file is subject to the terms and conditions of the GNU General Public
11 * License. See the file COPYING in the main directory of this archive
12 * for more details.
15 #include <linux/config.h>
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/sched.h>
19 #include <linux/delay.h>
20 #include <linux/pci.h>
21 #include <linux/fb.h>
22 #include <linux/jiffies.h>
24 #include <asm/io.h>
26 #include "rivafb.h"
27 #include "../edid.h"
29 #define RIVA_DDC 0x50
31 static void riva_gpio_setscl(void* data, int state)
33 struct riva_i2c_chan *chan = data;
34 struct riva_par *par = chan->par;
35 u32 val;
37 VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1);
38 val = VGA_RD08(par->riva.PCIO, 0x3d5) & 0xf0;
40 if (state)
41 val |= 0x20;
42 else
43 val &= ~0x20;
45 VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1);
46 VGA_WR08(par->riva.PCIO, 0x3d5, val | 0x1);
49 static void riva_gpio_setsda(void* data, int state)
51 struct riva_i2c_chan *chan = data;
52 struct riva_par *par = chan->par;
53 u32 val;
55 VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1);
56 val = VGA_RD08(par->riva.PCIO, 0x3d5) & 0xf0;
58 if (state)
59 val |= 0x10;
60 else
61 val &= ~0x10;
63 VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1);
64 VGA_WR08(par->riva.PCIO, 0x3d5, val | 0x1);
67 static int riva_gpio_getscl(void* data)
69 struct riva_i2c_chan *chan = data;
70 struct riva_par *par = chan->par;
71 u32 val = 0;
73 VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base);
74 if (VGA_RD08(par->riva.PCIO, 0x3d5) & 0x04)
75 val = 1;
77 val = VGA_RD08(par->riva.PCIO, 0x3d5);
79 return val;
82 static int riva_gpio_getsda(void* data)
84 struct riva_i2c_chan *chan = data;
85 struct riva_par *par = chan->par;
86 u32 val = 0;
88 VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base);
89 if (VGA_RD08(par->riva.PCIO, 0x3d5) & 0x08)
90 val = 1;
92 return val;
95 static int riva_setup_i2c_bus(struct riva_i2c_chan *chan, const char *name)
97 int rc;
99 strcpy(chan->adapter.name, name);
100 chan->adapter.owner = THIS_MODULE;
101 chan->adapter.id = I2C_HW_B_RIVA;
102 chan->adapter.algo_data = &chan->algo;
103 chan->adapter.dev.parent = &chan->par->pdev->dev;
104 chan->algo.setsda = riva_gpio_setsda;
105 chan->algo.setscl = riva_gpio_setscl;
106 chan->algo.getsda = riva_gpio_getsda;
107 chan->algo.getscl = riva_gpio_getscl;
108 chan->algo.udelay = 40;
109 chan->algo.timeout = msecs_to_jiffies(2);
110 chan->algo.data = chan;
112 i2c_set_adapdata(&chan->adapter, chan);
114 /* Raise SCL and SDA */
115 riva_gpio_setsda(chan, 1);
116 riva_gpio_setscl(chan, 1);
117 udelay(20);
119 rc = i2c_bit_add_bus(&chan->adapter);
120 if (rc == 0)
121 dev_dbg(&chan->par->pdev->dev, "I2C bus %s registered.\n", name);
122 else {
123 dev_warn(&chan->par->pdev->dev,
124 "Failed to register I2C bus %s.\n", name);
125 chan->par = NULL;
128 return rc;
131 void riva_create_i2c_busses(struct riva_par *par)
133 par->bus = 3;
135 par->chan[0].par = par;
136 par->chan[1].par = par;
137 par->chan[2].par = par;
139 par->chan[0].ddc_base = 0x3e;
140 par->chan[1].ddc_base = 0x36;
141 par->chan[2].ddc_base = 0x50;
142 riva_setup_i2c_bus(&par->chan[0], "BUS1");
143 riva_setup_i2c_bus(&par->chan[1], "BUS2");
144 riva_setup_i2c_bus(&par->chan[2], "BUS3");
147 void riva_delete_i2c_busses(struct riva_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;
162 static u8 *riva_do_probe_i2c_edid(struct riva_i2c_chan *chan)
164 u8 start = 0x0;
165 struct i2c_msg msgs[] = {
167 .addr = RIVA_DDC,
168 .len = 1,
169 .buf = &start,
170 }, {
171 .addr = RIVA_DDC,
172 .flags = I2C_M_RD,
173 .len = EDID_LENGTH,
176 u8 *buf;
178 if (!chan->par)
179 return NULL;
181 buf = kmalloc(EDID_LENGTH, GFP_KERNEL);
182 if (!buf) {
183 dev_warn(&chan->par->pdev->dev, "Out of memory!\n");
184 return NULL;
186 msgs[1].buf = buf;
188 if (i2c_transfer(&chan->adapter, msgs, 2) == 2)
189 return buf;
190 dev_dbg(&chan->par->pdev->dev, "Unable to read EDID block.\n");
191 kfree(buf);
192 return NULL;
195 int riva_probe_i2c_connector(struct riva_par *par, int conn, u8 **out_edid)
197 u8 *edid = NULL;
198 int i;
200 for (i = 0; i < 3; i++) {
201 /* Do the real work */
202 edid = riva_do_probe_i2c_edid(&par->chan[conn-1]);
203 if (edid)
204 break;
206 if (out_edid)
207 *out_edid = edid;
208 if (!edid)
209 return 1;
211 return 0;