blob: a0e22ac483a3d65161f7e54bdfad322c8918d93c [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static void riva_gpio_setscl(void* data, int state)
28{
Antonino A. Daplasf4a41832006-01-09 20:53:04 -080029 struct riva_i2c_chan *chan = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 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
45static void riva_gpio_setsda(void* data, int state)
46{
Antonino A. Daplasf4a41832006-01-09 20:53:04 -080047 struct riva_i2c_chan *chan = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 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
63static int riva_gpio_getscl(void* data)
64{
Antonino A. Daplasf4a41832006-01-09 20:53:04 -080065 struct riva_i2c_chan *chan = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return val;
74}
75
76static int riva_gpio_getsda(void* data)
77{
Antonino A. Daplasf4a41832006-01-09 20:53:04 -080078 struct riva_i2c_chan *chan = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 struct riva_par *par = chan->par;
80 u32 val = 0;
81
82 VGA_WR08(par->riva.PCIO, 0x3d4, chan->ddc_base);
83 if (VGA_RD08(par->riva.PCIO, 0x3d5) & 0x08)
84 val = 1;
85
86 return val;
87}
88
Jean Delvarea65ff762007-05-08 00:38:20 -070089static int __devinit riva_setup_i2c_bus(struct riva_i2c_chan *chan,
90 const char *name,
91 unsigned int i2c_class)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 int rc;
94
95 strcpy(chan->adapter.name, name);
96 chan->adapter.owner = THIS_MODULE;
Jean Delvare1684a9842005-08-11 23:51:10 +020097 chan->adapter.id = I2C_HW_B_RIVA;
Jean Delvare1e73db22007-05-08 00:38:15 -070098 chan->adapter.class = i2c_class;
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
Jean Delvarea65ff762007-05-08 00:38:20 -0700128void __devinit riva_create_i2c_busses(struct riva_par *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 par->chan[0].par = par;
131 par->chan[1].par = par;
132 par->chan[2].par = par;
133
Antonino A. Daplas04043842007-05-08 00:38:22 -0700134 par->chan[0].ddc_base = 0x36;
135 par->chan[1].ddc_base = 0x3e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 par->chan[2].ddc_base = 0x50;
Antonino A. Daplas04043842007-05-08 00:38:22 -0700137 riva_setup_i2c_bus(&par->chan[0], "BUS1", I2C_CLASS_HWMON);
138 riva_setup_i2c_bus(&par->chan[1], "BUS2", 0);
Jean Delvare1e73db22007-05-08 00:38:15 -0700139 riva_setup_i2c_bus(&par->chan[2], "BUS3", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
142void riva_delete_i2c_busses(struct riva_par *par)
143{
Jean Delvarea65ff762007-05-08 00:38:20 -0700144 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Jean Delvarea65ff762007-05-08 00:38:20 -0700146 for (i = 0; i < 3; i++) {
147 if (!par->chan[i].par)
148 continue;
149 i2c_del_adapter(&par->chan[i].adapter);
150 par->chan[i].par = NULL;
151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
Jean Delvarea65ff762007-05-08 00:38:20 -0700154int __devinit riva_probe_i2c_connector(struct riva_par *par, int conn, u8 **out_edid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 u8 *edid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Jean Delvarea65ff762007-05-08 00:38:20 -0700158 if (par->chan[conn].par)
159 edid = fb_ddc_read(&par->chan[conn].adapter);
Antonino A. Daplasbf5df0a2006-10-03 01:14:44 -0700160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (out_edid)
162 *out_edid = edid;
163 if (!edid)
164 return 1;
165
166 return 0;
167}
168