This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / video / riva / rivafb-i2c.c
1 /*
2  * linux/drivers/video/riva/fbdev-i2c.c - nVidia i2c
3  *
4  * Maintained by Ani Joshi <ajoshi@shell.unixbox.com>
5  *
6  * Copyright 2004 Antonino A. Daplas <adaplas @pol.net>
7  *
8  * Based on radeonfb-i2c.c
9  *
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.
13  */
14
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
23 #include <asm/io.h>
24
25 #include "rivafb.h"
26 #include "../edid.h"
27
28 #define RIVA_DDC        0x50
29
30 static void riva_gpio_setscl(void* data, int state)
31 {
32         struct riva_i2c_chan    *chan = (struct riva_i2c_chan *)data;
33         struct riva_par         *par = chan->par;
34         u32                     val;
35
36         VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1);
37         val = VGA_RD08(par->riva.PCIO, 0x3d5) & 0xf0;
38
39         if (state)
40                 val |= 0x20;
41         else
42                 val &= ~0x20;
43
44         VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1);
45         VGA_WR08(par->riva.PCIO, 0x3d5, val | 0x1);
46 }
47
48 static void riva_gpio_setsda(void* data, int state)
49 {
50         struct riva_i2c_chan    *chan = (struct riva_i2c_chan *)data;
51         struct riva_par         *par = chan->par;
52         u32                     val;
53
54         VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1);
55         val = VGA_RD08(par->riva.PCIO, 0x3d5) & 0xf0;
56
57         if (state)
58                 val |= 0x10;
59         else
60                 val &= ~0x10;
61
62         VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1);
63         VGA_WR08(par->riva.PCIO, 0x3d5, val | 0x1);
64 }
65
66 static int riva_gpio_getscl(void* data)
67 {
68         struct riva_i2c_chan    *chan = (struct riva_i2c_chan *)data;
69         struct riva_par         *par = chan->par;
70         u32                     val = 0;
71
72         VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base);
73         if (VGA_RD08(par->riva.PCIO, 0x3d5) & 0x04)
74                 val = 1;
75
76         val = VGA_RD08(par->riva.PCIO, 0x3d5);
77
78         return val;
79 }
80
81 static int riva_gpio_getsda(void* data)
82 {
83         struct riva_i2c_chan    *chan = (struct riva_i2c_chan *)data;
84         struct riva_par         *par = chan->par;
85         u32                     val = 0;
86
87         VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base);
88         if (VGA_RD08(par->riva.PCIO, 0x3d5) & 0x08)
89                 val = 1;
90
91         return val;
92 }
93
94 #define I2C_ALGO_RIVA   0x0e0000
95 static int riva_setup_i2c_bus(struct riva_i2c_chan *chan, const char *name)
96 {
97         int rc;
98
99         strcpy(chan->adapter.name, name);
100         chan->adapter.owner             = THIS_MODULE;
101         chan->adapter.id                = I2C_ALGO_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              = 20;
110         chan->algo.data                 = chan;
111
112         i2c_set_adapdata(&chan->adapter, chan);
113
114         /* Raise SCL and SDA */
115         riva_gpio_setsda(chan, 1);
116         riva_gpio_setscl(chan, 1);
117         udelay(20);
118
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, "Failed to register I2C bus %s.\n", name);
124         return rc;
125 }
126
127 void riva_create_i2c_busses(struct riva_par *par)
128 {
129         par->chan[0].par        = par;
130         par->chan[1].par        = par;
131         par->chan[2].par        = par;
132
133         switch (par->riva.Architecture) {
134 #if 0           /* no support yet for other nVidia chipsets */
135                 par->chan[2].ddc_base   = 0x50;
136                 riva_setup_i2c_bus(&par->chan[2], "BUS2");
137 #endif
138         case NV_ARCH_10:
139         case NV_ARCH_20:
140         case NV_ARCH_04:
141                 par->chan[1].ddc_base   = 0x36;
142                 riva_setup_i2c_bus(&par->chan[1], "BUS1");
143         case NV_ARCH_03:
144                 par->chan[0].ddc_base   = 0x3e;
145                 riva_setup_i2c_bus(&par->chan[0], "BUS0");
146         }
147 }
148
149 void riva_delete_i2c_busses(struct riva_par *par)
150 {
151         if (par->chan[0].par)
152                 i2c_bit_del_bus(&par->chan[0].adapter);
153         par->chan[0].par = NULL;
154
155         if (par->chan[1].par)
156                 i2c_bit_del_bus(&par->chan[1].adapter);
157         par->chan[1].par = NULL;
158
159 }
160
161 static u8 *riva_do_probe_i2c_edid(struct riva_i2c_chan *chan)
162 {
163         u8 start = 0x0;
164         struct i2c_msg msgs[] = {
165                 {
166                         .addr   = RIVA_DDC,
167                         .len    = 1,
168                         .buf    = &start,
169                 }, {
170                         .addr   = RIVA_DDC,
171                         .flags  = I2C_M_RD,
172                         .len    = EDID_LENGTH,
173                 },
174         };
175         u8 *buf;
176
177         buf = kmalloc(EDID_LENGTH, GFP_KERNEL);
178         if (!buf) {
179                 dev_warn(&chan->par->pdev->dev, "Out of memory!\n");
180                 return NULL;
181         }
182         msgs[1].buf = buf;
183
184         if (i2c_transfer(&chan->adapter, msgs, 2) == 2)
185                 return buf;
186         dev_dbg(&chan->par->pdev->dev, "Unable to read EDID block.\n");
187         kfree(buf);
188         return NULL;
189 }
190
191 int riva_probe_i2c_connector(struct riva_par *par, int conn, u8 **out_edid)
192 {
193         u8 *edid = NULL;
194         int i;
195
196         for (i = 0; i < 3; i++) {
197                 /* Do the real work */
198                 edid = riva_do_probe_i2c_edid(&par->chan[conn-1]);
199                 if (edid)
200                         break;
201         }
202         if (out_edid)
203                 *out_edid = edid;
204         if (!edid)
205                 return 1;
206
207         return 0;
208 }
209