Pull release into acpica branch
[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/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>
23
24 #include <asm/io.h>
25
26 #include "rivafb.h"
27 #include "../edid.h"
28
29 #define RIVA_DDC        0x50
30
31 static void riva_gpio_setscl(void* data, int state)
32 {
33         struct riva_i2c_chan    *chan = data;
34         struct riva_par         *par = chan->par;
35         u32                     val;
36
37         VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1);
38         val = VGA_RD08(par->riva.PCIO, 0x3d5) & 0xf0;
39
40         if (state)
41                 val |= 0x20;
42         else
43                 val &= ~0x20;
44
45         VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1);
46         VGA_WR08(par->riva.PCIO, 0x3d5, val | 0x1);
47 }
48
49 static void riva_gpio_setsda(void* data, int state)
50 {
51         struct riva_i2c_chan    *chan = data;
52         struct riva_par         *par = chan->par;
53         u32                     val;
54
55         VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1);
56         val = VGA_RD08(par->riva.PCIO, 0x3d5) & 0xf0;
57
58         if (state)
59                 val |= 0x10;
60         else
61                 val &= ~0x10;
62
63         VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1);
64         VGA_WR08(par->riva.PCIO, 0x3d5, val | 0x1);
65 }
66
67 static int riva_gpio_getscl(void* data)
68 {
69         struct riva_i2c_chan    *chan = data;
70         struct riva_par         *par = chan->par;
71         u32                     val = 0;
72
73         VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base);
74         if (VGA_RD08(par->riva.PCIO, 0x3d5) & 0x04)
75                 val = 1;
76
77         val = VGA_RD08(par->riva.PCIO, 0x3d5);
78
79         return val;
80 }
81
82 static int riva_gpio_getsda(void* data)
83 {
84         struct riva_i2c_chan    *chan = data;
85         struct riva_par         *par = chan->par;
86         u32                     val = 0;
87
88         VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base);
89         if (VGA_RD08(par->riva.PCIO, 0x3d5) & 0x08)
90                 val = 1;
91
92         return val;
93 }
94
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_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;
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,
124                          "Failed to register I2C bus %s.\n", name);
125                 chan->par = NULL;
126         }
127
128         return rc;
129 }
130
131 void riva_create_i2c_busses(struct riva_par *par)
132 {
133         par->bus = 3;
134
135         par->chan[0].par        = par;
136         par->chan[1].par        = par;
137         par->chan[2].par        = par;
138
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");
145 }
146
147 void riva_delete_i2c_busses(struct riva_par *par)
148 {
149         if (par->chan[0].par)
150                 i2c_bit_del_bus(&par->chan[0].adapter);
151         par->chan[0].par = NULL;
152
153         if (par->chan[1].par)
154                 i2c_bit_del_bus(&par->chan[1].adapter);
155         par->chan[1].par = NULL;
156
157         if (par->chan[2].par)
158                 i2c_bit_del_bus(&par->chan[2].adapter);
159         par->chan[2].par = NULL;
160 }
161
162 static u8 *riva_do_probe_i2c_edid(struct riva_i2c_chan *chan)
163 {
164         u8 start = 0x0;
165         struct i2c_msg msgs[] = {
166                 {
167                         .addr   = RIVA_DDC,
168                         .len    = 1,
169                         .buf    = &start,
170                 }, {
171                         .addr   = RIVA_DDC,
172                         .flags  = I2C_M_RD,
173                         .len    = EDID_LENGTH,
174                 },
175         };
176         u8 *buf;
177
178         if (!chan->par)
179                 return NULL;
180
181         buf = kmalloc(EDID_LENGTH, GFP_KERNEL);
182         if (!buf) {
183                 dev_warn(&chan->par->pdev->dev, "Out of memory!\n");
184                 return NULL;
185         }
186         msgs[1].buf = buf;
187
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;
193 }
194
195 int riva_probe_i2c_connector(struct riva_par *par, int conn, u8 **out_edid)
196 {
197         u8 *edid = NULL;
198         int i;
199
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;
205         }
206         if (out_edid)
207                 *out_edid = edid;
208         if (!edid)
209                 return 1;
210
211         return 0;
212 }
213