Merge branch 'for-2.6.22' of git://git.kernel.dk/data/git/linux-2.6-block
[linux-2.6] / 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/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>
21
22 #include <asm/io.h>
23
24 #include "rivafb.h"
25 #include "../edid.h"
26
27 static void riva_gpio_setscl(void* data, int state)
28 {
29         struct riva_i2c_chan    *chan = data;
30         struct riva_par         *par = chan->par;
31         u32                     val;
32
33         VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1);
34         val = VGA_RD08(par->riva.PCIO, 0x3d5) & 0xf0;
35
36         if (state)
37                 val |= 0x20;
38         else
39                 val &= ~0x20;
40
41         VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1);
42         VGA_WR08(par->riva.PCIO, 0x3d5, val | 0x1);
43 }
44
45 static void riva_gpio_setsda(void* data, int state)
46 {
47         struct riva_i2c_chan    *chan = data;
48         struct riva_par         *par = chan->par;
49         u32                     val;
50
51         VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1);
52         val = VGA_RD08(par->riva.PCIO, 0x3d5) & 0xf0;
53
54         if (state)
55                 val |= 0x10;
56         else
57                 val &= ~0x10;
58
59         VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1);
60         VGA_WR08(par->riva.PCIO, 0x3d5, val | 0x1);
61 }
62
63 static int riva_gpio_getscl(void* data)
64 {
65         struct riva_i2c_chan    *chan = data;
66         struct riva_par         *par = chan->par;
67         u32                     val = 0;
68
69         VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base);
70         if (VGA_RD08(par->riva.PCIO, 0x3d5) & 0x04)
71                 val = 1;
72
73         val = VGA_RD08(par->riva.PCIO, 0x3d5);
74
75         return val;
76 }
77
78 static int riva_gpio_getsda(void* data)
79 {
80         struct riva_i2c_chan    *chan = data;
81         struct riva_par         *par = chan->par;
82         u32                     val = 0;
83
84         VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base);
85         if (VGA_RD08(par->riva.PCIO, 0x3d5) & 0x08)
86                 val = 1;
87
88         return val;
89 }
90
91 static int __devinit riva_setup_i2c_bus(struct riva_i2c_chan *chan,
92                                         const char *name,
93                                         unsigned int i2c_class)
94 {
95         int rc;
96
97         strcpy(chan->adapter.name, name);
98         chan->adapter.owner             = THIS_MODULE;
99         chan->adapter.id                = I2C_HW_B_RIVA;
100         chan->adapter.class             = i2c_class;
101         chan->adapter.algo_data         = &chan->algo;
102         chan->adapter.dev.parent        = &chan->par->pdev->dev;
103         chan->algo.setsda               = riva_gpio_setsda;
104         chan->algo.setscl               = riva_gpio_setscl;
105         chan->algo.getsda               = riva_gpio_getsda;
106         chan->algo.getscl               = riva_gpio_getscl;
107         chan->algo.udelay               = 40;
108         chan->algo.timeout              = msecs_to_jiffies(2);
109         chan->algo.data                 = chan;
110
111         i2c_set_adapdata(&chan->adapter, chan);
112
113         /* Raise SCL and SDA */
114         riva_gpio_setsda(chan, 1);
115         riva_gpio_setscl(chan, 1);
116         udelay(20);
117
118         rc = i2c_bit_add_bus(&chan->adapter);
119         if (rc == 0)
120                 dev_dbg(&chan->par->pdev->dev, "I2C bus %s registered.\n", name);
121         else {
122                 dev_warn(&chan->par->pdev->dev,
123                          "Failed to register I2C bus %s.\n", name);
124                 chan->par = NULL;
125         }
126
127         return rc;
128 }
129
130 void __devinit riva_create_i2c_busses(struct riva_par *par)
131 {
132         par->chan[0].par        = par;
133         par->chan[1].par        = par;
134         par->chan[2].par        = par;
135
136         par->chan[0].ddc_base = 0x36;
137         par->chan[1].ddc_base = 0x3e;
138         par->chan[2].ddc_base = 0x50;
139         riva_setup_i2c_bus(&par->chan[0], "BUS1", I2C_CLASS_HWMON);
140         riva_setup_i2c_bus(&par->chan[1], "BUS2", 0);
141         riva_setup_i2c_bus(&par->chan[2], "BUS3", 0);
142 }
143
144 void riva_delete_i2c_busses(struct riva_par *par)
145 {
146         int i;
147
148         for (i = 0; i < 3; i++) {
149                 if (!par->chan[i].par)
150                         continue;
151                 i2c_del_adapter(&par->chan[i].adapter);
152                 par->chan[i].par = NULL;
153         }
154 }
155
156 int __devinit riva_probe_i2c_connector(struct riva_par *par, int conn, u8 **out_edid)
157 {
158         u8 *edid = NULL;
159
160         if (par->chan[conn].par)
161                 edid = fb_ddc_read(&par->chan[conn].adapter);
162
163         if (out_edid)
164                 *out_edid = edid;
165         if (!edid)
166                 return 1;
167
168         return 0;
169 }
170