blob: 8454adf2d178abb9cc49bebf56226d17db804c77 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/video/nvidia/nvidia-i2c.c - nVidia i2c
3 *
4 * Copyright 2004 Antonino A. Daplas <adaplas @pol.net>
5 *
6 * Based on rivafb-i2c.c
7 *
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file COPYING in the main directory of this archive
10 * for more details.
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/sched.h>
16#include <linux/delay.h>
17#include <linux/pci.h>
18#include <linux/fb.h>
19
20#include <asm/io.h>
21
22#include "nv_type.h"
23#include "nv_local.h"
24#include "nv_proto.h"
25
26#include "../edid.h"
27
28static void nvidia_gpio_setscl(void *data, int state)
29{
30 struct nvidia_i2c_chan *chan = data;
31 struct nvidia_par *par = chan->par;
32 u32 val;
33
34 VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base + 1);
35 val = VGA_RD08(par->PCIO, 0x3d5) & 0xf0;
36
37 if (state)
38 val |= 0x20;
39 else
40 val &= ~0x20;
41
42 VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base + 1);
43 VGA_WR08(par->PCIO, 0x3d5, val | 0x1);
44}
45
46static void nvidia_gpio_setsda(void *data, int state)
47{
Antonino A. Daplasc439e342006-01-09 20:53:02 -080048 struct nvidia_i2c_chan *chan = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 struct nvidia_par *par = chan->par;
50 u32 val;
51
52 VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base + 1);
53 val = VGA_RD08(par->PCIO, 0x3d5) & 0xf0;
54
55 if (state)
56 val |= 0x10;
57 else
58 val &= ~0x10;
59
60 VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base + 1);
61 VGA_WR08(par->PCIO, 0x3d5, val | 0x1);
62}
63
64static int nvidia_gpio_getscl(void *data)
65{
Antonino A. Daplasc439e342006-01-09 20:53:02 -080066 struct nvidia_i2c_chan *chan = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 struct nvidia_par *par = chan->par;
68 u32 val = 0;
69
70 VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base);
71 if (VGA_RD08(par->PCIO, 0x3d5) & 0x04)
72 val = 1;
73
74 val = VGA_RD08(par->PCIO, 0x3d5);
75
76 return val;
77}
78
79static int nvidia_gpio_getsda(void *data)
80{
Antonino A. Daplasc439e342006-01-09 20:53:02 -080081 struct nvidia_i2c_chan *chan = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 struct nvidia_par *par = chan->par;
83 u32 val = 0;
84
85 VGA_WR08(par->PCIO, 0x3d4, chan->ddc_base);
86 if (VGA_RD08(par->PCIO, 0x3d5) & 0x08)
87 val = 1;
88
89 return val;
90}
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int nvidia_setup_i2c_bus(struct nvidia_i2c_chan *chan, const char *name)
93{
94 int rc;
95
96 strcpy(chan->adapter.name, name);
97 chan->adapter.owner = THIS_MODULE;
Jean Delvare1684a9842005-08-11 23:51:10 +020098 chan->adapter.id = I2C_HW_B_NVIDIA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 chan->adapter.algo_data = &chan->algo;
100 chan->adapter.dev.parent = &chan->par->pci_dev->dev;
101 chan->algo.setsda = nvidia_gpio_setsda;
102 chan->algo.setscl = nvidia_gpio_setscl;
103 chan->algo.getsda = nvidia_gpio_getsda;
104 chan->algo.getscl = nvidia_gpio_getscl;
105 chan->algo.udelay = 40;
106 chan->algo.timeout = msecs_to_jiffies(2);
107 chan->algo.data = chan;
108
109 i2c_set_adapdata(&chan->adapter, chan);
110
111 /* Raise SCL and SDA */
112 nvidia_gpio_setsda(chan, 1);
113 nvidia_gpio_setscl(chan, 1);
114 udelay(20);
115
116 rc = i2c_bit_add_bus(&chan->adapter);
117 if (rc == 0)
118 dev_dbg(&chan->par->pci_dev->dev,
119 "I2C bus %s registered.\n", name);
120 else {
121 dev_warn(&chan->par->pci_dev->dev,
122 "Failed to register I2C bus %s.\n", name);
123 chan->par = NULL;
124 }
125
126 return rc;
127}
128
129void nvidia_create_i2c_busses(struct nvidia_par *par)
130{
131 par->bus = 3;
132
133 par->chan[0].par = par;
134 par->chan[1].par = par;
135 par->chan[2].par = par;
136
137 par->chan[0].ddc_base = 0x3e;
Alessandro Zummo2610acc2006-01-09 20:52:57 -0800138 nvidia_setup_i2c_bus(&par->chan[0], "nvidia #0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 par->chan[1].ddc_base = 0x36;
Alessandro Zummo2610acc2006-01-09 20:52:57 -0800141 nvidia_setup_i2c_bus(&par->chan[1], "nvidia #1");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 par->chan[2].ddc_base = 0x50;
Alessandro Zummo2610acc2006-01-09 20:52:57 -0800144 nvidia_setup_i2c_bus(&par->chan[2], "nvidia #2");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
147void nvidia_delete_i2c_busses(struct nvidia_par *par)
148{
149 if (par->chan[0].par)
Jean Delvare32697112006-12-10 21:21:33 +0100150 i2c_del_adapter(&par->chan[0].adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 par->chan[0].par = NULL;
152
153 if (par->chan[1].par)
Jean Delvare32697112006-12-10 21:21:33 +0100154 i2c_del_adapter(&par->chan[1].adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 par->chan[1].par = NULL;
156
157 if (par->chan[2].par)
Jean Delvare32697112006-12-10 21:21:33 +0100158 i2c_del_adapter(&par->chan[2].adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 par->chan[2].par = NULL;
160
161}
162
Andrew Morton7e4910922006-10-11 01:20:35 -0700163static u8 *nvidia_do_probe_i2c_edid(struct nvidia_i2c_chan *chan)
164{
165 u8 start = 0x0;
166 struct i2c_msg msgs[] = {
167 {
168 .addr = 0x50,
169 .len = 1,
170 .buf = &start,
171 }, {
172 .addr = 0x50,
173 .flags = I2C_M_RD,
174 .len = EDID_LENGTH,
175 },
176 };
177 u8 *buf;
178
179 if (!chan->par)
180 return NULL;
181
182 buf = kmalloc(EDID_LENGTH, GFP_KERNEL);
183 if (!buf) {
184 dev_warn(&chan->par->pci_dev->dev, "Out of memory!\n");
185 return NULL;
186 }
187 msgs[1].buf = buf;
188
189 if (i2c_transfer(&chan->adapter, msgs, 2) == 2)
190 return buf;
191 dev_dbg(&chan->par->pci_dev->dev, "Unable to read EDID block.\n");
192 kfree(buf);
193 return NULL;
194}
195
Antonino A. Daplas094bb652005-09-09 13:04:36 -0700196int nvidia_probe_i2c_connector(struct fb_info *info, int conn, u8 **out_edid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Antonino A. Daplas094bb652005-09-09 13:04:36 -0700198 struct nvidia_par *par = info->par;
Andrew Morton7e4910922006-10-11 01:20:35 -0700199 u8 *edid = NULL;
200 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Andrew Morton7e4910922006-10-11 01:20:35 -0700202 for (i = 0; i < 3; i++) {
203 /* Do the real work */
204 edid = nvidia_do_probe_i2c_edid(&par->chan[conn - 1]);
205 if (edid)
206 break;
207 }
Antonino A. Daplas094bb652005-09-09 13:04:36 -0700208
209 if (!edid && conn == 1) {
210 /* try to get from firmware */
Antonino A. Daplas0ed8e042005-09-14 14:19:15 -0700211 const u8 *e = fb_firmware_edid(info->device);
212
Alexey Dobriyanbfba7b32006-12-08 02:40:46 -0800213 if (e != NULL)
214 edid = kmemdup(e, EDID_LENGTH, GFP_KERNEL);
Antonino A. Daplas094bb652005-09-09 13:04:36 -0700215 }
216
Antonino A. Daplas3d5b1912006-03-27 01:17:33 -0800217 *out_edid = edid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Antonino A. Daplas094bb652005-09-09 13:04:36 -0700219 return (edid) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}