Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jlbec...
[linux-2.6-block.git] / drivers / video / aty / radeon_i2c.c
CommitLineData
b0313f89
JD
1#include "radeonfb.h"
2
1da177e4
LT
3#include <linux/module.h>
4#include <linux/kernel.h>
1da177e4 5#include <linux/delay.h>
1da177e4
LT
6#include <linux/fb.h>
7
8
9#include <linux/i2c.h>
10#include <linux/i2c-id.h>
11#include <linux/i2c-algo-bit.h>
12
13#include <asm/io.h>
14
15#include <video/radeon.h>
1da177e4
LT
16#include "../edid.h"
17
1da177e4
LT
18static void radeon_gpio_setscl(void* data, int state)
19{
20 struct radeon_i2c_chan *chan = data;
21 struct radeonfb_info *rinfo = chan->rinfo;
22 u32 val;
23
24 val = INREG(chan->ddc_reg) & ~(VGA_DDC_CLK_OUT_EN);
25 if (!state)
26 val |= VGA_DDC_CLK_OUT_EN;
27
28 OUTREG(chan->ddc_reg, val);
29 (void)INREG(chan->ddc_reg);
30}
31
32static void radeon_gpio_setsda(void* data, int state)
33{
34 struct radeon_i2c_chan *chan = data;
35 struct radeonfb_info *rinfo = chan->rinfo;
36 u32 val;
37
38 val = INREG(chan->ddc_reg) & ~(VGA_DDC_DATA_OUT_EN);
39 if (!state)
40 val |= VGA_DDC_DATA_OUT_EN;
41
42 OUTREG(chan->ddc_reg, val);
43 (void)INREG(chan->ddc_reg);
44}
45
46static int radeon_gpio_getscl(void* data)
47{
48 struct radeon_i2c_chan *chan = data;
49 struct radeonfb_info *rinfo = chan->rinfo;
50 u32 val;
51
52 val = INREG(chan->ddc_reg);
53
54 return (val & VGA_DDC_CLK_INPUT) ? 1 : 0;
55}
56
57static int radeon_gpio_getsda(void* data)
58{
59 struct radeon_i2c_chan *chan = data;
60 struct radeonfb_info *rinfo = chan->rinfo;
61 u32 val;
62
63 val = INREG(chan->ddc_reg);
64
65 return (val & VGA_DDC_DATA_INPUT) ? 1 : 0;
66}
67
68static int radeon_setup_i2c_bus(struct radeon_i2c_chan *chan, const char *name)
69{
70 int rc;
71
9bd6ceb6
JD
72 snprintf(chan->adapter.name, sizeof(chan->adapter.name),
73 "radeonfb %s", name);
1da177e4 74 chan->adapter.owner = THIS_MODULE;
1da177e4
LT
75 chan->adapter.algo_data = &chan->algo;
76 chan->adapter.dev.parent = &chan->rinfo->pdev->dev;
77 chan->algo.setsda = radeon_gpio_setsda;
78 chan->algo.setscl = radeon_gpio_setscl;
79 chan->algo.getsda = radeon_gpio_getsda;
80 chan->algo.getscl = radeon_gpio_getscl;
e0745ae7 81 chan->algo.udelay = 10;
1da177e4
LT
82 chan->algo.timeout = 20;
83 chan->algo.data = chan;
84
85 i2c_set_adapdata(&chan->adapter, chan);
86
87 /* Raise SCL and SDA */
88 radeon_gpio_setsda(chan, 1);
89 radeon_gpio_setscl(chan, 1);
90 udelay(20);
91
92 rc = i2c_bit_add_bus(&chan->adapter);
93 if (rc == 0)
94 dev_dbg(&chan->rinfo->pdev->dev, "I2C bus %s registered.\n", name);
95 else
96 dev_warn(&chan->rinfo->pdev->dev, "Failed to register I2C bus %s.\n", name);
97 return rc;
98}
99
100void radeon_create_i2c_busses(struct radeonfb_info *rinfo)
101{
102 rinfo->i2c[0].rinfo = rinfo;
103 rinfo->i2c[0].ddc_reg = GPIO_MONID;
104 radeon_setup_i2c_bus(&rinfo->i2c[0], "monid");
105
106 rinfo->i2c[1].rinfo = rinfo;
107 rinfo->i2c[1].ddc_reg = GPIO_DVI_DDC;
108 radeon_setup_i2c_bus(&rinfo->i2c[1], "dvi");
109
110 rinfo->i2c[2].rinfo = rinfo;
111 rinfo->i2c[2].ddc_reg = GPIO_VGA_DDC;
112 radeon_setup_i2c_bus(&rinfo->i2c[2], "vga");
113
114 rinfo->i2c[3].rinfo = rinfo;
115 rinfo->i2c[3].ddc_reg = GPIO_CRT2_DDC;
116 radeon_setup_i2c_bus(&rinfo->i2c[3], "crt2");
117}
118
119void radeon_delete_i2c_busses(struct radeonfb_info *rinfo)
120{
121 if (rinfo->i2c[0].rinfo)
3269711b 122 i2c_del_adapter(&rinfo->i2c[0].adapter);
1da177e4
LT
123 rinfo->i2c[0].rinfo = NULL;
124
125 if (rinfo->i2c[1].rinfo)
3269711b 126 i2c_del_adapter(&rinfo->i2c[1].adapter);
1da177e4
LT
127 rinfo->i2c[1].rinfo = NULL;
128
129 if (rinfo->i2c[2].rinfo)
3269711b 130 i2c_del_adapter(&rinfo->i2c[2].adapter);
1da177e4
LT
131 rinfo->i2c[2].rinfo = NULL;
132
133 if (rinfo->i2c[3].rinfo)
3269711b 134 i2c_del_adapter(&rinfo->i2c[3].adapter);
1da177e4
LT
135 rinfo->i2c[3].rinfo = NULL;
136}
137
7a45093b
DM
138int radeon_probe_i2c_connector(struct radeonfb_info *rinfo, int conn,
139 u8 **out_edid)
1da177e4 140{
4f71c5de
BH
141 u8 *edid;
142
4f71c5de 143 edid = fb_ddc_read(&rinfo->i2c[conn-1].adapter);
1da177e4
LT
144
145 if (out_edid)
146 *out_edid = edid;
147 if (!edid) {
bc3bf466 148 pr_debug("radeonfb: I2C (port %d) ... not found\n", conn);
1da177e4
LT
149 return MT_NONE;
150 }
151 if (edid[0x14] & 0x80) {
152 /* Fix detection using BIOS tables */
153 if (rinfo->is_mobility /*&& conn == ddc_dvi*/ &&
154 (INREG(LVDS_GEN_CNTL) & LVDS_ON)) {
bc3bf466 155 pr_debug("radeonfb: I2C (port %d) ... found LVDS panel\n", conn);
1da177e4
LT
156 return MT_LCD;
157 } else {
bc3bf466 158 pr_debug("radeonfb: I2C (port %d) ... found TMDS panel\n", conn);
1da177e4
LT
159 return MT_DFP;
160 }
161 }
bc3bf466 162 pr_debug("radeonfb: I2C (port %d) ... found CRT display\n", conn);
1da177e4
LT
163 return MT_CRT;
164}
165