1 /*
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 
33 struct matroxfb_dh_maven_info {
34 	struct i2c_bit_adapter	maven;
35 	struct i2c_bit_adapter	ddc1;
36 	struct i2c_bit_adapter	ddc2;
37 };
38 
matroxfb_read_gpio(struct matrox_fb_info * minfo)39 static 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 
matroxfb_set_gpio(struct matrox_fb_info * minfo,int mask,int val)49 static 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 */
matroxfb_i2c_set(struct matrox_fb_info * minfo,int mask,int state)62 static 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 
matroxfb_gpio_setsda(void * data,int state)70 static 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 
matroxfb_gpio_setscl(void * data,int state)75 static 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 
matroxfb_gpio_getsda(void * data)80 static 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 
matroxfb_gpio_getscl(void * data)85 static 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 
matroxfb_dh_inc_use(struct i2c_adapter * dummy)90 static void matroxfb_dh_inc_use(struct i2c_adapter* dummy) {
91 	MOD_INC_USE_COUNT;
92 }
93 
matroxfb_dh_dec_use(struct i2c_adapter * dummy)94 static void matroxfb_dh_dec_use(struct i2c_adapter* dummy) {
95 	MOD_DEC_USE_COUNT;
96 }
97 
98 static struct i2c_adapter matrox_i2c_adapter_template =
99 {
100 	.id =		I2C_HW_B_G400,
101 	.inc_use =	matroxfb_dh_inc_use,
102 	.dec_use =	matroxfb_dh_dec_use,
103 };
104 
105 static struct i2c_algo_bit_data matrox_i2c_algo_template =
106 {
107 	.setsda		= matroxfb_gpio_setsda,
108 	.setscl		= matroxfb_gpio_setscl,
109 	.getsda		= matroxfb_gpio_getsda,
110 	.getscl		= matroxfb_gpio_getscl,
111 	.udelay		= 10,
112 	.mdelay		= 10,
113 	.timeout	= 100,
114 };
115 
i2c_bus_reg(struct i2c_bit_adapter * b,struct matrox_fb_info * minfo,unsigned int data,unsigned int clock,const char * name)116 static int i2c_bus_reg(struct i2c_bit_adapter* b, struct matrox_fb_info* minfo,
117 		unsigned int data, unsigned int clock, const char* name) {
118 	int err;
119 
120 	b->minfo = minfo;
121 	b->mask.data = data;
122 	b->mask.clock = clock;
123 	b->adapter = matrox_i2c_adapter_template;
124 	sprintf(b->adapter.name, name, GET_FB_IDX(minfo->fbcon.node));
125 	b->adapter.data = b;
126 	b->adapter.algo_data = &b->bac;
127 	b->bac = matrox_i2c_algo_template;
128 	b->bac.data = b;
129 	err = i2c_bit_add_bus(&b->adapter);
130 	b->initialized = !err;
131 	return err;
132 }
133 
i2c_bit_bus_del(struct i2c_bit_adapter * b)134 static void i2c_bit_bus_del(struct i2c_bit_adapter* b) {
135 	if (b->initialized) {
136 		i2c_bit_del_bus(&b->adapter);
137 		b->initialized = 0;
138 	}
139 }
140 
i2c_maven_done(struct matroxfb_dh_maven_info * minfo2)141 static inline void i2c_maven_done(struct matroxfb_dh_maven_info* minfo2) {
142 	i2c_bit_bus_del(&minfo2->maven);
143 }
144 
i2c_ddc1_done(struct matroxfb_dh_maven_info * minfo2)145 static inline void i2c_ddc1_done(struct matroxfb_dh_maven_info* minfo2) {
146 	i2c_bit_bus_del(&minfo2->ddc1);
147 }
148 
i2c_ddc2_done(struct matroxfb_dh_maven_info * minfo2)149 static inline void i2c_ddc2_done(struct matroxfb_dh_maven_info* minfo2) {
150 	i2c_bit_bus_del(&minfo2->ddc2);
151 }
152 
i2c_matroxfb_probe(struct matrox_fb_info * minfo)153 static void* i2c_matroxfb_probe(struct matrox_fb_info* minfo) {
154 	int err;
155 	unsigned long flags;
156 	struct matroxfb_dh_maven_info* m2info;
157 
158 	m2info = (struct matroxfb_dh_maven_info*)kmalloc(sizeof(*m2info), GFP_KERNEL);
159 	if (!m2info)
160 		return NULL;
161 
162 	matroxfb_DAC_lock_irqsave(flags);
163 	matroxfb_DAC_out(PMINFO DAC_XGENIODATA, 0xFF);
164 	matroxfb_DAC_out(PMINFO DAC_XGENIOCTRL, 0x00);
165 	matroxfb_DAC_unlock_irqrestore(flags);
166 
167 	memset(m2info, 0, sizeof(*m2info));
168 
169 	switch (ACCESS_FBINFO(chip)) {
170 		case MGA_2064:
171 		case MGA_2164:
172 			err = i2c_bus_reg(&m2info->ddc1, minfo, DDC1B_DATA, DDC1B_CLK, "DDC:fb%u #0 on i2c-matroxfb");
173 			break;
174 		default:
175 			err = i2c_bus_reg(&m2info->ddc1, minfo, DDC1_DATA, DDC1_CLK, "DDC:fb%u #0 on i2c-matroxfb");
176 			break;
177 	}
178 	if (err)
179 		goto fail_ddc1;
180 	if (ACCESS_FBINFO(devflags.dualhead)) {
181 		err = i2c_bus_reg(&m2info->ddc2, minfo, DDC2_DATA, DDC2_CLK, "DDC:fb%u #1 on i2c-matroxfb");
182 		if (err == -ENODEV) {
183 			printk(KERN_INFO "i2c-matroxfb: VGA->TV plug detected, DDC unavailable.\n");
184 		} else if (err)
185 			printk(KERN_INFO "i2c-matroxfb: Could not register secondary output i2c bus. Continuing anyway.\n");
186 		/* Register maven bus even on G450/G550 */
187 		err = i2c_bus_reg(&m2info->maven, minfo, MAT_DATA, MAT_CLK, "MAVEN:fb%u on i2c-matroxfb");
188 		if (err)
189 			printk(KERN_INFO "i2c-matroxfb: Could not register Maven i2c bus. Continuing anyway.\n");
190 	}
191 	return m2info;
192 fail_ddc1:;
193 	kfree(m2info);
194 	printk(KERN_ERR "i2c-matroxfb: Could not register primary adapter DDC bus.\n");
195 	return NULL;
196 }
197 
i2c_matroxfb_remove(struct matrox_fb_info * minfo,void * data)198 static void i2c_matroxfb_remove(struct matrox_fb_info* minfo, void* data) {
199 	struct matroxfb_dh_maven_info* m2info = data;
200 
201 	i2c_maven_done(m2info);
202 	i2c_ddc2_done(m2info);
203 	i2c_ddc1_done(m2info);
204 	kfree(m2info);
205 }
206 
207 static struct matroxfb_driver i2c_matroxfb = {
208 	.node =		LIST_HEAD_INIT(i2c_matroxfb.node),
209 	.name =		"i2c-matroxfb",
210 	.probe = 	i2c_matroxfb_probe,
211 	.remove =	i2c_matroxfb_remove,
212 };
213 
i2c_matroxfb_init(void)214 static int __init i2c_matroxfb_init(void) {
215 	if (matroxfb_register_driver(&i2c_matroxfb)) {
216 		printk(KERN_ERR "i2c-matroxfb: failed to register driver\n");
217 		return -ENXIO;
218 	}
219 	return 0;
220 }
221 
i2c_matroxfb_exit(void)222 static void __exit i2c_matroxfb_exit(void) {
223 	matroxfb_unregister_driver(&i2c_matroxfb);
224 }
225 
226 MODULE_AUTHOR("(c) 1999-2002 Petr Vandrovec <vandrove@vc.cvut.cz>");
227 MODULE_DESCRIPTION("Support module providing I2C buses present on Matrox videocards");
228 
229 module_init(i2c_matroxfb_init);
230 module_exit(i2c_matroxfb_exit);
231 /* no __setup required */
232 MODULE_LICENSE("GPL");
233