Linux 2.6.21-rc3
[linux/fpc-iii.git] / drivers / video / riva / rivafb-i2c.c
blob0405e839ff939f5dc9e13d33f02dd4f1cd51ca06
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/module.h>
16 #include <linux/kernel.h>
17 #include <linux/delay.h>
18 #include <linux/pci.h>
19 #include <linux/fb.h>
20 #include <linux/jiffies.h>
22 #include <asm/io.h>
24 #include "rivafb.h"
25 #include "../edid.h"
27 static void riva_gpio_setscl(void* data, int state)
29 struct riva_i2c_chan *chan = data;
30 struct riva_par *par = chan->par;
31 u32 val;
33 VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1);
34 val = VGA_RD08(par->riva.PCIO, 0x3d5) & 0xf0;
36 if (state)
37 val |= 0x20;
38 else
39 val &= ~0x20;
41 VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1);
42 VGA_WR08(par->riva.PCIO, 0x3d5, val | 0x1);
45 static void riva_gpio_setsda(void* data, int state)
47 struct riva_i2c_chan *chan = data;
48 struct riva_par *par = chan->par;
49 u32 val;
51 VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1);
52 val = VGA_RD08(par->riva.PCIO, 0x3d5) & 0xf0;
54 if (state)
55 val |= 0x10;
56 else
57 val &= ~0x10;
59 VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1);
60 VGA_WR08(par->riva.PCIO, 0x3d5, val | 0x1);
63 static int riva_gpio_getscl(void* data)
65 struct riva_i2c_chan *chan = data;
66 struct riva_par *par = chan->par;
67 u32 val = 0;
69 VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base);
70 if (VGA_RD08(par->riva.PCIO, 0x3d5) & 0x04)
71 val = 1;
73 val = VGA_RD08(par->riva.PCIO, 0x3d5);
75 return val;
78 static int riva_gpio_getsda(void* data)
80 struct riva_i2c_chan *chan = data;
81 struct riva_par *par = chan->par;
82 u32 val = 0;
84 VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base);
85 if (VGA_RD08(par->riva.PCIO, 0x3d5) & 0x08)
86 val = 1;
88 return val;
91 static int riva_setup_i2c_bus(struct riva_i2c_chan *chan, const char *name)
93 int rc;
95 strcpy(chan->adapter.name, name);
96 chan->adapter.owner = THIS_MODULE;
97 chan->adapter.id = I2C_HW_B_RIVA;
98 chan->adapter.algo_data = &chan->algo;
99 chan->adapter.dev.parent = &chan->par->pdev->dev;
100 chan->algo.setsda = riva_gpio_setsda;
101 chan->algo.setscl = riva_gpio_setscl;
102 chan->algo.getsda = riva_gpio_getsda;
103 chan->algo.getscl = riva_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 riva_gpio_setsda(chan, 1);
112 riva_gpio_setscl(chan, 1);
113 udelay(20);
115 rc = i2c_bit_add_bus(&chan->adapter);
116 if (rc == 0)
117 dev_dbg(&chan->par->pdev->dev, "I2C bus %s registered.\n", name);
118 else {
119 dev_warn(&chan->par->pdev->dev,
120 "Failed to register I2C bus %s.\n", name);
121 chan->par = NULL;
124 return rc;
127 void riva_create_i2c_busses(struct riva_par *par)
129 par->bus = 3;
131 par->chan[0].par = par;
132 par->chan[1].par = par;
133 par->chan[2].par = par;
135 par->chan[0].ddc_base = 0x3e;
136 par->chan[1].ddc_base = 0x36;
137 par->chan[2].ddc_base = 0x50;
138 riva_setup_i2c_bus(&par->chan[0], "BUS1");
139 riva_setup_i2c_bus(&par->chan[1], "BUS2");
140 riva_setup_i2c_bus(&par->chan[2], "BUS3");
143 void riva_delete_i2c_busses(struct riva_par *par)
145 if (par->chan[0].par)
146 i2c_del_adapter(&par->chan[0].adapter);
147 par->chan[0].par = NULL;
149 if (par->chan[1].par)
150 i2c_del_adapter(&par->chan[1].adapter);
151 par->chan[1].par = NULL;
153 if (par->chan[2].par)
154 i2c_del_adapter(&par->chan[2].adapter);
155 par->chan[2].par = NULL;
158 int riva_probe_i2c_connector(struct riva_par *par, int conn, u8 **out_edid)
160 u8 *edid = NULL;
162 edid = fb_ddc_read(&par->chan[conn-1].adapter);
164 if (out_edid)
165 *out_edid = edid;
166 if (!edid)
167 return 1;
169 return 0;