blob: cefabe818a12e30eed8090144b8434f68c9e8a12 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 *
3 * Hardware accelerated Matrox Millennium I, II, Mystique, G100, G200, G400 and G450.
4 *
5 * (c) 1998-2002 Petr Vandrovec <vandrove@vc.cvut.cz>
6 *
7 * Version: 1.64 2002/06/10
8 *
9 * See matroxfb_base.c for contributors.
10 *
11 */
12
13#include "matroxfb_base.h"
14#include "matroxfb_maven.h"
15#include <linux/i2c.h>
16#include <linux/i2c-algo-bit.h>
17
18/* MGA-TVO I2C for G200, G400 */
19#define MAT_CLK 0x20
20#define MAT_DATA 0x10
21/* primary head DDC for Mystique(?), G100, G200, G400 */
22#define DDC1_CLK 0x08
23#define DDC1_DATA 0x02
24/* primary head DDC for Millennium, Millennium II */
25#define DDC1B_CLK 0x10
26#define DDC1B_DATA 0x04
27/* secondary head DDC for G400 */
28#define DDC2_CLK 0x04
29#define DDC2_DATA 0x01
30
31/******************************************************/
32
33struct matroxfb_dh_maven_info {
34 struct i2c_bit_adapter maven;
35 struct i2c_bit_adapter ddc1;
36 struct i2c_bit_adapter ddc2;
37};
38
39static int matroxfb_read_gpio(struct matrox_fb_info* minfo) {
40 unsigned long flags;
41 int v;
42
43 matroxfb_DAC_lock_irqsave(flags);
44 v = matroxfb_DAC_in(PMINFO DAC_XGENIODATA);
45 matroxfb_DAC_unlock_irqrestore(flags);
46 return v;
47}
48
49static void matroxfb_set_gpio(struct matrox_fb_info* minfo, int mask, int val) {
50 unsigned long flags;
51 int v;
52
53 matroxfb_DAC_lock_irqsave(flags);
54 v = (matroxfb_DAC_in(PMINFO DAC_XGENIOCTRL) & mask) | val;
55 matroxfb_DAC_out(PMINFO DAC_XGENIOCTRL, v);
56 /* We must reset GENIODATA very often... XFree plays with this register */
57 matroxfb_DAC_out(PMINFO DAC_XGENIODATA, 0x00);
58 matroxfb_DAC_unlock_irqrestore(flags);
59}
60
61/* software I2C functions */
62static inline void matroxfb_i2c_set(struct matrox_fb_info* minfo, int mask, int state) {
63 if (state)
64 state = 0;
65 else
66 state = mask;
67 matroxfb_set_gpio(minfo, ~mask, state);
68}
69
70static void matroxfb_gpio_setsda(void* data, int state) {
71 struct i2c_bit_adapter* b = data;
72 matroxfb_i2c_set(b->minfo, b->mask.data, state);
73}
74
75static void matroxfb_gpio_setscl(void* data, int state) {
76 struct i2c_bit_adapter* b = data;
77 matroxfb_i2c_set(b->minfo, b->mask.clock, state);
78}
79
80static int matroxfb_gpio_getsda(void* data) {
81 struct i2c_bit_adapter* b = data;
82 return (matroxfb_read_gpio(b->minfo) & b->mask.data) ? 1 : 0;
83}
84
85static int matroxfb_gpio_getscl(void* data) {
86 struct i2c_bit_adapter* b = data;
87 return (matroxfb_read_gpio(b->minfo) & b->mask.clock) ? 1 : 0;
88}
89
Jean Delvare73b7d922008-08-12 15:08:54 -070090static const struct i2c_algo_bit_data matrox_i2c_algo_template =
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Jean Delvare8241cb12006-08-13 23:40:09 +020092 .setsda = matroxfb_gpio_setsda,
93 .setscl = matroxfb_gpio_setscl,
94 .getsda = matroxfb_gpio_getsda,
95 .getscl = matroxfb_gpio_getscl,
96 .udelay = 10,
Jean Delvare8241cb12006-08-13 23:40:09 +020097 .timeout = 100,
Linus Torvalds1da177e2005-04-16 15:20:36 -070098};
99
100static int i2c_bus_reg(struct i2c_bit_adapter* b, struct matrox_fb_info* minfo,
Jean Delvarec1b6b4f2008-07-14 22:38:28 +0200101 unsigned int data, unsigned int clock, const char *name,
102 int class)
103{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 int err;
105
106 b->minfo = minfo;
107 b->mask.data = data;
108 b->mask.clock = clock;
Jean Delvare73b7d922008-08-12 15:08:54 -0700109 b->adapter.owner = THIS_MODULE;
David Brownell2096b952007-05-01 23:26:28 +0200110 snprintf(b->adapter.name, sizeof(b->adapter.name), name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 minfo->fbcon.node);
112 i2c_set_adapdata(&b->adapter, b);
Jean Delvarec1b6b4f2008-07-14 22:38:28 +0200113 b->adapter.class = class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 b->adapter.algo_data = &b->bac;
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700115 b->adapter.dev.parent = &minfo->pcidev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 b->bac = matrox_i2c_algo_template;
117 b->bac.data = b;
118 err = i2c_bit_add_bus(&b->adapter);
119 b->initialized = !err;
120 return err;
121}
122
123static void i2c_bit_bus_del(struct i2c_bit_adapter* b) {
124 if (b->initialized) {
Jean Delvare32697112006-12-10 21:21:33 +0100125 i2c_del_adapter(&b->adapter);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 b->initialized = 0;
127 }
128}
129
130static inline void i2c_maven_done(struct matroxfb_dh_maven_info* minfo2) {
131 i2c_bit_bus_del(&minfo2->maven);
132}
133
134static inline void i2c_ddc1_done(struct matroxfb_dh_maven_info* minfo2) {
135 i2c_bit_bus_del(&minfo2->ddc1);
136}
137
138static inline void i2c_ddc2_done(struct matroxfb_dh_maven_info* minfo2) {
139 i2c_bit_bus_del(&minfo2->ddc2);
140}
141
142static void* i2c_matroxfb_probe(struct matrox_fb_info* minfo) {
143 int err;
144 unsigned long flags;
145 struct matroxfb_dh_maven_info* m2info;
146
Jean Delvare2fdbe5c2007-02-12 00:55:07 -0800147 m2info = kzalloc(sizeof(*m2info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (!m2info)
149 return NULL;
150
151 matroxfb_DAC_lock_irqsave(flags);
152 matroxfb_DAC_out(PMINFO DAC_XGENIODATA, 0xFF);
153 matroxfb_DAC_out(PMINFO DAC_XGENIOCTRL, 0x00);
154 matroxfb_DAC_unlock_irqrestore(flags);
155
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700156 switch (minfo->chip) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 case MGA_2064:
158 case MGA_2164:
Jean Delvarec1b6b4f2008-07-14 22:38:28 +0200159 err = i2c_bus_reg(&m2info->ddc1, minfo,
160 DDC1B_DATA, DDC1B_CLK,
161 "DDC:fb%u #0", I2C_CLASS_DDC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 break;
163 default:
Jean Delvarec1b6b4f2008-07-14 22:38:28 +0200164 err = i2c_bus_reg(&m2info->ddc1, minfo,
165 DDC1_DATA, DDC1_CLK,
166 "DDC:fb%u #0", I2C_CLASS_DDC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 break;
168 }
169 if (err)
170 goto fail_ddc1;
Jean Delvarefc2d10d2009-09-22 16:47:48 -0700171 if (minfo->devflags.dualhead) {
Jean Delvarec1b6b4f2008-07-14 22:38:28 +0200172 err = i2c_bus_reg(&m2info->ddc2, minfo,
173 DDC2_DATA, DDC2_CLK,
174 "DDC:fb%u #1", I2C_CLASS_DDC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (err == -ENODEV) {
176 printk(KERN_INFO "i2c-matroxfb: VGA->TV plug detected, DDC unavailable.\n");
177 } else if (err)
178 printk(KERN_INFO "i2c-matroxfb: Could not register secondary output i2c bus. Continuing anyway.\n");
179 /* Register maven bus even on G450/G550 */
Jean Delvarec1b6b4f2008-07-14 22:38:28 +0200180 err = i2c_bus_reg(&m2info->maven, minfo,
181 MAT_DATA, MAT_CLK, "MAVEN:fb%u", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (err)
183 printk(KERN_INFO "i2c-matroxfb: Could not register Maven i2c bus. Continuing anyway.\n");
Jean Delvare10546352008-08-12 15:08:55 -0700184 else {
185 struct i2c_board_info maven_info = {
186 I2C_BOARD_INFO("maven", 0x1b),
187 };
188 unsigned short const addr_list[2] = {
189 0x1b, I2C_CLIENT_END
190 };
191
192 i2c_new_probed_device(&m2info->maven.adapter,
193 &maven_info, addr_list);
194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
196 return m2info;
197fail_ddc1:;
198 kfree(m2info);
199 printk(KERN_ERR "i2c-matroxfb: Could not register primary adapter DDC bus.\n");
200 return NULL;
201}
202
203static void i2c_matroxfb_remove(struct matrox_fb_info* minfo, void* data) {
204 struct matroxfb_dh_maven_info* m2info = data;
205
206 i2c_maven_done(m2info);
207 i2c_ddc2_done(m2info);
208 i2c_ddc1_done(m2info);
209 kfree(m2info);
210}
211
212static struct matroxfb_driver i2c_matroxfb = {
213 .node = LIST_HEAD_INIT(i2c_matroxfb.node),
214 .name = "i2c-matroxfb",
215 .probe = i2c_matroxfb_probe,
216 .remove = i2c_matroxfb_remove,
217};
218
219static int __init i2c_matroxfb_init(void) {
220 if (matroxfb_register_driver(&i2c_matroxfb)) {
221 printk(KERN_ERR "i2c-matroxfb: failed to register driver\n");
222 return -ENXIO;
223 }
224 return 0;
225}
226
227static void __exit i2c_matroxfb_exit(void) {
228 matroxfb_unregister_driver(&i2c_matroxfb);
229}
230
231MODULE_AUTHOR("(c) 1999-2002 Petr Vandrovec <vandrove@vc.cvut.cz>");
232MODULE_DESCRIPTION("Support module providing I2C buses present on Matrox videocards");
233
234module_init(i2c_matroxfb_init);
235module_exit(i2c_matroxfb_exit);
236/* no __setup required */
237MODULE_LICENSE("GPL");