summaryrefslogtreecommitdiffstats
path: root/drivers/video/matrox/i2c-matroxfb.c
blob: bbb695223dc36b59d6107284e2eaf91177bce3f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include "matroxfb_base.h"
#include "matroxfb_maven.h"
#include <linux/i2c.h>
#include <linux/i2c-algo-bit.h>

/* MGA-TVO I2C for G200, G400 */
#define MAT_CLK		0x20
#define MAT_DATA	0x10
/* primary head DDC for Mystique(?), G100, G200, G400 */
#define DDC1_CLK	0x08
#define DDC1_DATA	0x02
/* primary head DDC for Millennium, Millennium II */
#define DDC1B_CLK	0x10
#define DDC1B_DATA	0x04
/* secondary head DDC for G400 */
#define DDC2_CLK	0x04
#define DDC2_DATA	0x01

/******************************************************/

static int matroxfb_read_gpio(struct matrox_fb_info* minfo) {
	unsigned long flags;
	int v;

	matroxfb_DAC_lock_irqsave(flags);
	v = matroxfb_DAC_in(PMINFO DAC_XGENIODATA);
	matroxfb_DAC_unlock_irqrestore(flags);
	return v;
}

static inline void matroxfb_set_gpio(struct matrox_fb_info* minfo, int mask, int val) {
	unsigned long flags;
	int v;

	matroxfb_DAC_lock_irqsave(flags);
	v = (matroxfb_DAC_in(PMINFO DAC_XGENIOCTRL) & mask) | val;
	matroxfb_DAC_out(PMINFO DAC_XGENIOCTRL, v);
	/* We must reset GENIODATA very often... XFree plays with this register */
	matroxfb_DAC_out(PMINFO DAC_XGENIODATA, 0x00);
	matroxfb_DAC_unlock_irqrestore(flags);
}

/* software I2C functions */
static void matroxfb_i2c_set(struct matrox_fb_info* minfo, int mask, int state) {
	if (state)
		state = 0;
	else
		state = mask;
	matroxfb_set_gpio(minfo, ~mask, state);
}

static void matroxfb_maven_setsda(void* data, int state) {
	matroxfb_i2c_set(data, MAT_DATA, state);
}

static void matroxfb_maven_setscl(void* data, int state) {
	matroxfb_i2c_set(data, MAT_CLK, state);
}

static int matroxfb_maven_getsda(void* data) {
	return (matroxfb_read_gpio(data) & MAT_DATA) ? 1 : 0;
}

static int matroxfb_maven_getscl(void* data) {
	return (matroxfb_read_gpio(data) & MAT_CLK) ? 1 : 0;
}

static void matroxfb_ddc1_setsda(void* data, int state) {
	matroxfb_i2c_set(data, DDC1_DATA, state);
}

static void matroxfb_ddc1_setscl(void* data, int state) {
	matroxfb_i2c_set(data, DDC1_CLK, state);
}

static int matroxfb_ddc1_getsda(void* data) {
	return (matroxfb_read_gpio(data) & DDC1_DATA) ? 1 : 0;
}

static int matroxfb_ddc1_getscl(void* data) {
	return (matroxfb_read_gpio(data) & DDC1_CLK) ? 1 : 0;
}

static void matroxfb_ddc1b_setsda(void* data, int state) {
	matroxfb_i2c_set(data, DDC1B_DATA, state);
}

static void matroxfb_ddc1b_setscl(void* data, int state) {
	matroxfb_i2c_set(data, DDC1B_CLK, state);
}

static int matroxfb_ddc1b_getsda(void* data) {
	return (matroxfb_read_gpio(data) & DDC1B_DATA) ? 1 : 0;
}

static int matroxfb_ddc1b_getscl(void* data) {
	return (matroxfb_read_gpio(data) & DDC1B_CLK) ? 1 : 0;
}

static void matroxfb_ddc2_setsda(void* data, int state) {
	matroxfb_i2c_set(data, DDC2_DATA, state);
}

static void matroxfb_ddc2_setscl(void* data, int state) {
	matroxfb_i2c_set(data, DDC2_CLK, state);
}

static int matroxfb_ddc2_getsda(void* data) {
	return (matroxfb_read_gpio(data) & DDC2_DATA) ? 1 : 0;
}

static int matroxfb_ddc2_getscl(void* data) {
	return (matroxfb_read_gpio(data) & DDC2_CLK) ? 1 : 0;
}

static void matroxfb_dh_inc_use(struct i2c_adapter* dummy) {
	MOD_INC_USE_COUNT;
}

static void matroxfb_dh_dec_use(struct i2c_adapter* dummy) {
	MOD_DEC_USE_COUNT;
}

static struct i2c_adapter matroxmaven_i2c_adapter_template =
{
	"",
	I2C_HW_B_G400,

	NULL,
	NULL,

	matroxfb_dh_inc_use,
	matroxfb_dh_dec_use,
	NULL,
	NULL,
	NULL,
};

static struct i2c_algo_bit_data matroxmaven_i2c_algo_template =
{
	NULL,
	matroxfb_maven_setsda,
	matroxfb_maven_setscl,
	matroxfb_maven_getsda,
	matroxfb_maven_getscl,
	10, 10, 100,
};

static struct i2c_adapter matrox_ddc1_adapter_template =
{
	"",
	I2C_HW_B_G400, /* DDC */

	NULL,
	NULL,

	matroxfb_dh_inc_use,
	matroxfb_dh_dec_use,
	NULL,
	NULL,
	NULL,
};

static struct i2c_algo_bit_data matrox_ddc1_algo_template =
{
	NULL,
	matroxfb_ddc1_setsda,
	matroxfb_ddc1_setscl,
	matroxfb_ddc1_getsda,
	matroxfb_ddc1_getscl,
	10, 10, 100,
};

static struct i2c_algo_bit_data matrox_ddc1b_algo_template =
{
	NULL,
	matroxfb_ddc1b_setsda,
	matroxfb_ddc1b_setscl,
	matroxfb_ddc1b_getsda,
	matroxfb_ddc1b_getscl,
	10, 10, 100,
};

static struct i2c_adapter matrox_ddc2_adapter_template =
{
	"",
	I2C_HW_B_G400,	/* DDC */

	NULL,
	NULL,

	matroxfb_dh_inc_use, /* should increment matroxfb_maven usage too, this DDC is coupled with maven_client */
	matroxfb_dh_dec_use, /* should decrement matroxfb_maven usage too */
	NULL,
	NULL,
	NULL,
};

static struct i2c_algo_bit_data matrox_ddc2_algo_template =
{
	NULL,
	matroxfb_ddc2_setsda,
	matroxfb_ddc2_setscl,
	matroxfb_ddc2_getsda,
	matroxfb_ddc2_getscl,
	10, 10, 100,
};

static int i2c_bus_reg(struct i2c_bit_adapter* b, struct matrox_fb_info* minfo) {
	int err;

	b->adapter.data = minfo;
	b->adapter.algo_data = &b->bac;
	b->bac.data = minfo;
	err = i2c_bit_add_bus(&b->adapter);
	b->initialized = !err;
	return err;
}

static void i2c_bit_bus_del(struct i2c_bit_adapter* b) {
	if (b->initialized) {
		i2c_bit_del_bus(&b->adapter);
		b->initialized = 0;
	}
}

static inline int i2c_maven_init(struct matroxfb_dh_maven_info* minfo2) {
	struct i2c_bit_adapter *b = &minfo2->maven;

	b->adapter = matroxmaven_i2c_adapter_template;
	b->bac = matroxmaven_i2c_algo_template;
	sprintf(b->adapter.name, "MAVEN:fb%u on i2c-matroxfb", GET_FB_IDX(minfo2->primary_dev->fbcon.node));
	return i2c_bus_reg(b, minfo2->primary_dev);
}

static inline void i2c_maven_done(struct matroxfb_dh_maven_info* minfo2) {
	i2c_bit_bus_del(&minfo2->maven);
}

static inline int i2c_ddc1_init(struct matroxfb_dh_maven_info* minfo2) {
	struct i2c_bit_adapter *b = &minfo2->ddc1;

	b->adapter = matrox_ddc1_adapter_template;
	b->bac = matrox_ddc1_algo_template;
	sprintf(b->adapter.name, "DDC:fb%u #0 on i2c-matroxfb", GET_FB_IDX(minfo2->primary_dev->fbcon.node));
	return i2c_bus_reg(b, minfo2->primary_dev);
}

static inline int i2c_ddc1b_init(struct matroxfb_dh_maven_info* minfo2) {
	struct i2c_bit_adapter *b = &minfo2->ddc1;

	b->adapter = matrox_ddc1_adapter_template;
	b->bac = matrox_ddc1b_algo_template;
	sprintf(b->adapter.name, "DDC:fb%u #0 on i2c-matroxfb", GET_FB_IDX(minfo2->primary_dev->fbcon.node));
	return i2c_bus_reg(b, minfo2->primary_dev);
}

static inline void i2c_ddc1_done(struct matroxfb_dh_maven_info* minfo2) {
	i2c_bit_bus_del(&minfo2->ddc1);
}

static inline int i2c_ddc2_init(struct matroxfb_dh_maven_info* minfo2) {
	struct i2c_bit_adapter *b = &minfo2->ddc2;

	b->adapter = matrox_ddc2_adapter_template;
	b->bac = matrox_ddc2_algo_template;
	sprintf(b->adapter.name, "DDC:fb%u #1 on i2c-matroxfb", GET_FB_IDX(minfo2->primary_dev->fbcon.node));
	return i2c_bus_reg(b, minfo2->primary_dev);
}

static inline void i2c_ddc2_done(struct matroxfb_dh_maven_info* minfo2) {
	i2c_bit_bus_del(&minfo2->ddc2);
}

static void* i2c_matroxfb_probe(struct matrox_fb_info* minfo) {
	int err;
	unsigned long flags;
	struct matroxfb_dh_maven_info* m2info;

	m2info = (struct matroxfb_dh_maven_info*)kmalloc(sizeof(*m2info), GFP_KERNEL);
	if (!m2info)
		return NULL;

	matroxfb_DAC_lock_irqsave(flags);
	matroxfb_DAC_out(PMINFO DAC_XGENIODATA, 0x00);
	matroxfb_DAC_out(PMINFO DAC_XGENIOCTRL, 0xFF);
	matroxfb_DAC_unlock_irqrestore(flags);

	memset(m2info, 0, sizeof(*m2info));
	m2info->primary_dev = minfo;

	if (ACCESS_FBINFO(devflags.accelerator) == FB_ACCEL_MATROX_MGA2064W ||
	    ACCESS_FBINFO(devflags.accelerator) == FB_ACCEL_MATROX_MGA2164W ||
	    ACCESS_FBINFO(devflags.accelerator) == FB_ACCEL_MATROX_MGA2164W_AGP)
		err = i2c_ddc1b_init(m2info);
	else
		err = i2c_ddc1_init(m2info);
	if (err)
		goto fail_ddc1;
	if (ACCESS_FBINFO(devflags.maven_capable)) {
		err = i2c_ddc2_init(m2info);
		if (err)
			printk(KERN_INFO "i2c-matroxfb: Could not register secondary output i2c bus. Continuing anyway.\n");
		err = i2c_maven_init(m2info);
		if (err)
			printk(KERN_INFO "i2c-matroxfb: Could not register Maven i2c bus. Continuing anyway.\n");
	}
	return m2info;
fail_ddc1:;
	kfree(m2info);
	printk(KERN_ERR "i2c-matroxfb: Could not register primary adapter DDC bus.\n");
	return NULL;
}

static void i2c_matroxfb_remove(struct matrox_fb_info* minfo, void* data) {
	struct matroxfb_dh_maven_info* m2info = data;

	i2c_maven_done(m2info);
	i2c_ddc2_done(m2info);
	i2c_ddc1_done(m2info);
	kfree(m2info);
}

static struct matroxfb_driver i2c_matroxfb = {
	LIST_HEAD_INIT(i2c_matroxfb.node),
	"i2c-matroxfb",
	i2c_matroxfb_probe,
	i2c_matroxfb_remove,
};

static int __init i2c_matroxfb_init(void) {
	if (matroxfb_register_driver(&i2c_matroxfb)) {
		printk(KERN_ERR "i2c-matroxfb: failed to register driver\n");
		return -ENXIO;
	}
	return 0;
}

static void __exit i2c_matroxfb_exit(void) {
	matroxfb_unregister_driver(&i2c_matroxfb);
}

MODULE_AUTHOR("(c) 1999 Petr Vandrovec <vandrove@vc.cvut.cz>");
MODULE_DESCRIPTION("Support module providing I2C buses present on Matrox videocards");

module_init(i2c_matroxfb_init);
module_exit(i2c_matroxfb_exit);
/* no __setup required */