blob: c15b259af64401f770b83cdcda69db5a30bab20a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/module.h>
16#include <linux/kernel.h>
17#include <linux/sched.h>
18#include <linux/delay.h>
19#include <linux/pci.h>
20#include <linux/fb.h>
21#include <linux/jiffies.h>
22
23#include <asm/io.h>
24
25#include "rivafb.h"
26#include "../edid.h"
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static void riva_gpio_setscl(void* data, int state)
29{
Antonino A. Daplasf4a41832006-01-09 20:53:04 -080030 struct riva_i2c_chan *chan = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 struct riva_par *par = chan->par;
32 u32 val;
33
34 VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1);
35 val = VGA_RD08(par->riva.PCIO, 0x3d5) & 0xf0;
36
37 if (state)
38 val |= 0x20;
39 else
40 val &= ~0x20;
41
42 VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1);
43 VGA_WR08(par->riva.PCIO, 0x3d5, val | 0x1);
44}
45
46static void riva_gpio_setsda(void* data, int state)
47{
Antonino A. Daplasf4a41832006-01-09 20:53:04 -080048 struct riva_i2c_chan *chan = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 struct riva_par *par = chan->par;
50 u32 val;
51
52 VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1);
53 val = VGA_RD08(par->riva.PCIO, 0x3d5) & 0xf0;
54
55 if (state)
56 val |= 0x10;
57 else
58 val &= ~0x10;
59
60 VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base + 1);
61 VGA_WR08(par->riva.PCIO, 0x3d5, val | 0x1);
62}
63
64static int riva_gpio_getscl(void* data)
65{
Antonino A. Daplasf4a41832006-01-09 20:53:04 -080066 struct riva_i2c_chan *chan = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 struct riva_par *par = chan->par;
68 u32 val = 0;
69
70 VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base);
71 if (VGA_RD08(par->riva.PCIO, 0x3d5) & 0x04)
72 val = 1;
73
74 val = VGA_RD08(par->riva.PCIO, 0x3d5);
75
76 return val;
77}
78
79static int riva_gpio_getsda(void* data)
80{
Antonino A. Daplasf4a41832006-01-09 20:53:04 -080081 struct riva_i2c_chan *chan = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 struct riva_par *par = chan->par;
83 u32 val = 0;
84
85 VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base);
86 if (VGA_RD08(par->riva.PCIO, 0x3d5) & 0x08)
87 val = 1;
88
89 return val;
90}
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int riva_setup_i2c_bus(struct riva_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_RIVA;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 chan->adapter.algo_data = &chan->algo;
100 chan->adapter.dev.parent = &chan->par->pdev->dev;
101 chan->algo.setsda = riva_gpio_setsda;
102 chan->algo.setscl = riva_gpio_setscl;
103 chan->algo.getsda = riva_gpio_getsda;
104 chan->algo.getscl = riva_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 riva_gpio_setsda(chan, 1);
113 riva_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->pdev->dev, "I2C bus %s registered.\n", name);
119 else {
120 dev_warn(&chan->par->pdev->dev,
121 "Failed to register I2C bus %s.\n", name);
122 chan->par = NULL;
123 }
124
125 return rc;
126}
127
128void riva_create_i2c_busses(struct riva_par *par)
129{
130 par->bus = 3;
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 = 0x3e;
137 par->chan[1].ddc_base = 0x36;
138 par->chan[2].ddc_base = 0x50;
139 riva_setup_i2c_bus(&par->chan[0], "BUS1");
140 riva_setup_i2c_bus(&par->chan[1], "BUS2");
141 riva_setup_i2c_bus(&par->chan[2], "BUS3");
142}
143
144void riva_delete_i2c_busses(struct riva_par *par)
145{
146 if (par->chan[0].par)
147 i2c_bit_del_bus(&par->chan[0].adapter);
148 par->chan[0].par = NULL;
149
150 if (par->chan[1].par)
151 i2c_bit_del_bus(&par->chan[1].adapter);
152 par->chan[1].par = NULL;
153
154 if (par->chan[2].par)
155 i2c_bit_del_bus(&par->chan[2].adapter);
156 par->chan[2].par = NULL;
157}
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159int riva_probe_i2c_connector(struct riva_par *par, int conn, u8 **out_edid)
160{
161 u8 *edid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Antonino A. Daplasbf5df0a2006-10-03 01:14:44 -0700163 edid = fb_ddc_read(&par->chan[conn-1].adapter);
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (out_edid)
166 *out_edid = edid;
167 if (!edid)
168 return 1;
169
170 return 0;
171}
172