xref: /linux/drivers/video/fbdev/matrox/i2c-matroxfb.c (revision 06d07429858317ded2db7986113a9e0129cd599b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *
4  * Hardware accelerated Matrox Millennium I, II, Mystique, G100, G200, G400 and G450.
5  *
6  * (c) 1998-2002 Petr Vandrovec <vandrove@vc.cvut.cz>
7  *
8  * Version: 1.64 2002/06/10
9  *
10  * See matroxfb_base.c for contributors.
11  *
12  */
13 
14 #include "matroxfb_base.h"
15 #include "matroxfb_maven.h"
16 #include <linux/i2c.h>
17 #include <linux/slab.h>
18 #include <linux/i2c-algo-bit.h>
19 
20 /* MGA-TVO I2C for G200, G400 */
21 #define MAT_CLK		0x20
22 #define MAT_DATA	0x10
23 /* primary head DDC for Mystique(?), G100, G200, G400 */
24 #define DDC1_CLK	0x08
25 #define DDC1_DATA	0x02
26 /* primary head DDC for Millennium, Millennium II */
27 #define DDC1B_CLK	0x10
28 #define DDC1B_DATA	0x04
29 /* secondary head DDC for G400 */
30 #define DDC2_CLK	0x04
31 #define DDC2_DATA	0x01
32 
33 /******************************************************/
34 
35 struct matroxfb_dh_maven_info {
36 	struct i2c_bit_adapter	maven;
37 	struct i2c_bit_adapter	ddc1;
38 	struct i2c_bit_adapter	ddc2;
39 };
40 
matroxfb_read_gpio(struct matrox_fb_info * minfo)41 static int matroxfb_read_gpio(struct matrox_fb_info* minfo) {
42 	unsigned long flags;
43 	int v;
44 
45 	matroxfb_DAC_lock_irqsave(flags);
46 	v = matroxfb_DAC_in(minfo, DAC_XGENIODATA);
47 	matroxfb_DAC_unlock_irqrestore(flags);
48 	return v;
49 }
50 
matroxfb_set_gpio(struct matrox_fb_info * minfo,int mask,int val)51 static void matroxfb_set_gpio(struct matrox_fb_info* minfo, int mask, int val) {
52 	unsigned long flags;
53 	int v;
54 
55 	matroxfb_DAC_lock_irqsave(flags);
56 	v = (matroxfb_DAC_in(minfo, DAC_XGENIOCTRL) & mask) | val;
57 	matroxfb_DAC_out(minfo, DAC_XGENIOCTRL, v);
58 	/* We must reset GENIODATA very often... XFree plays with this register */
59 	matroxfb_DAC_out(minfo, DAC_XGENIODATA, 0x00);
60 	matroxfb_DAC_unlock_irqrestore(flags);
61 }
62 
63 /* software I2C functions */
matroxfb_i2c_set(struct matrox_fb_info * minfo,int mask,int state)64 static inline void matroxfb_i2c_set(struct matrox_fb_info* minfo, int mask, int state) {
65 	if (state)
66 		state = 0;
67 	else
68 		state = mask;
69 	matroxfb_set_gpio(minfo, ~mask, state);
70 }
71 
matroxfb_gpio_setsda(void * data,int state)72 static void matroxfb_gpio_setsda(void* data, int state) {
73 	struct i2c_bit_adapter* b = data;
74 	matroxfb_i2c_set(b->minfo, b->mask.data, state);
75 }
76 
matroxfb_gpio_setscl(void * data,int state)77 static void matroxfb_gpio_setscl(void* data, int state) {
78 	struct i2c_bit_adapter* b = data;
79 	matroxfb_i2c_set(b->minfo, b->mask.clock, state);
80 }
81 
matroxfb_gpio_getsda(void * data)82 static int matroxfb_gpio_getsda(void* data) {
83 	struct i2c_bit_adapter* b = data;
84 	return (matroxfb_read_gpio(b->minfo) & b->mask.data) ? 1 : 0;
85 }
86 
matroxfb_gpio_getscl(void * data)87 static int matroxfb_gpio_getscl(void* data) {
88 	struct i2c_bit_adapter* b = data;
89 	return (matroxfb_read_gpio(b->minfo) & b->mask.clock) ? 1 : 0;
90 }
91 
92 static const struct i2c_algo_bit_data matrox_i2c_algo_template =
93 {
94 	.setsda		= matroxfb_gpio_setsda,
95 	.setscl		= matroxfb_gpio_setscl,
96 	.getsda		= matroxfb_gpio_getsda,
97 	.getscl		= matroxfb_gpio_getscl,
98 	.udelay		= 10,
99 	.timeout	= 100,
100 };
101 
i2c_bus_reg(struct i2c_bit_adapter * b,struct matrox_fb_info * minfo,unsigned int data,unsigned int clock,const char * name)102 static int i2c_bus_reg(struct i2c_bit_adapter* b, struct matrox_fb_info* minfo,
103 		unsigned int data, unsigned int clock, const char *name)
104 {
105 	int err;
106 
107 	b->minfo = minfo;
108 	b->mask.data = data;
109 	b->mask.clock = clock;
110 	b->adapter.owner = THIS_MODULE;
111 	snprintf(b->adapter.name, sizeof(b->adapter.name), name,
112 		minfo->fbcon.node);
113 	i2c_set_adapdata(&b->adapter, b);
114 	b->adapter.algo_data = &b->bac;
115 	b->adapter.dev.parent = &minfo->pcidev->dev;
116 	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 
i2c_bit_bus_del(struct i2c_bit_adapter * b)123 static void i2c_bit_bus_del(struct i2c_bit_adapter* b) {
124 	if (b->initialized) {
125 		i2c_del_adapter(&b->adapter);
126 		b->initialized = 0;
127 	}
128 }
129 
i2c_maven_done(struct matroxfb_dh_maven_info * minfo2)130 static inline void i2c_maven_done(struct matroxfb_dh_maven_info* minfo2) {
131 	i2c_bit_bus_del(&minfo2->maven);
132 }
133 
i2c_ddc1_done(struct matroxfb_dh_maven_info * minfo2)134 static inline void i2c_ddc1_done(struct matroxfb_dh_maven_info* minfo2) {
135 	i2c_bit_bus_del(&minfo2->ddc1);
136 }
137 
i2c_ddc2_done(struct matroxfb_dh_maven_info * minfo2)138 static inline void i2c_ddc2_done(struct matroxfb_dh_maven_info* minfo2) {
139 	i2c_bit_bus_del(&minfo2->ddc2);
140 }
141 
i2c_matroxfb_probe(struct matrox_fb_info * minfo)142 static void* i2c_matroxfb_probe(struct matrox_fb_info* minfo) {
143 	int err;
144 	unsigned long flags;
145 	struct matroxfb_dh_maven_info* m2info;
146 
147 	m2info = kzalloc(sizeof(*m2info), GFP_KERNEL);
148 	if (!m2info)
149 		return NULL;
150 
151 	matroxfb_DAC_lock_irqsave(flags);
152 	matroxfb_DAC_out(minfo, DAC_XGENIODATA, 0xFF);
153 	matroxfb_DAC_out(minfo, DAC_XGENIOCTRL, 0x00);
154 	matroxfb_DAC_unlock_irqrestore(flags);
155 
156 	switch (minfo->chip) {
157 		case MGA_2064:
158 		case MGA_2164:
159 			err = i2c_bus_reg(&m2info->ddc1, minfo,
160 					  DDC1B_DATA, DDC1B_CLK,
161 					  "DDC:fb%u #0");
162 			break;
163 		default:
164 			err = i2c_bus_reg(&m2info->ddc1, minfo,
165 					  DDC1_DATA, DDC1_CLK,
166 					  "DDC:fb%u #0");
167 			break;
168 	}
169 	if (err)
170 		goto fail_ddc1;
171 	if (minfo->devflags.dualhead) {
172 		err = i2c_bus_reg(&m2info->ddc2, minfo, DDC2_DATA, DDC2_CLK, "DDC:fb%u #1");
173 		if (err == -ENODEV) {
174 			printk(KERN_INFO "i2c-matroxfb: VGA->TV plug detected, DDC unavailable.\n");
175 		} else if (err)
176 			printk(KERN_INFO "i2c-matroxfb: Could not register secondary output i2c bus. Continuing anyway.\n");
177 		/* Register maven bus even on G450/G550 */
178 		err = i2c_bus_reg(&m2info->maven, minfo, MAT_DATA, MAT_CLK, "MAVEN:fb%u");
179 		if (err)
180 			printk(KERN_INFO "i2c-matroxfb: Could not register Maven i2c bus. Continuing anyway.\n");
181 		else {
182 			struct i2c_board_info maven_info = {
183 				I2C_BOARD_INFO("maven", 0x1b),
184 			};
185 			unsigned short const addr_list[2] = {
186 				0x1b, I2C_CLIENT_END
187 			};
188 
189 			i2c_new_scanned_device(&m2info->maven.adapter,
190 					       &maven_info, addr_list, NULL);
191 		}
192 	}
193 	return m2info;
194 fail_ddc1:;
195 	kfree(m2info);
196 	printk(KERN_ERR "i2c-matroxfb: Could not register primary adapter DDC bus.\n");
197 	return NULL;
198 }
199 
i2c_matroxfb_remove(struct matrox_fb_info * minfo,void * data)200 static void i2c_matroxfb_remove(struct matrox_fb_info* minfo, void* data) {
201 	struct matroxfb_dh_maven_info* m2info = data;
202 
203 	i2c_maven_done(m2info);
204 	i2c_ddc2_done(m2info);
205 	i2c_ddc1_done(m2info);
206 	kfree(m2info);
207 }
208 
209 static struct matroxfb_driver i2c_matroxfb = {
210 	.node =		LIST_HEAD_INIT(i2c_matroxfb.node),
211 	.name =		"i2c-matroxfb",
212 	.probe = 	i2c_matroxfb_probe,
213 	.remove =	i2c_matroxfb_remove,
214 };
215 
i2c_matroxfb_init(void)216 static int __init i2c_matroxfb_init(void) {
217 	if (matroxfb_register_driver(&i2c_matroxfb)) {
218 		printk(KERN_ERR "i2c-matroxfb: failed to register driver\n");
219 		return -ENXIO;
220 	}
221 	return 0;
222 }
223 
i2c_matroxfb_exit(void)224 static void __exit i2c_matroxfb_exit(void) {
225 	matroxfb_unregister_driver(&i2c_matroxfb);
226 }
227 
228 MODULE_AUTHOR("(c) 1999-2002 Petr Vandrovec <vandrove@vc.cvut.cz>");
229 MODULE_DESCRIPTION("Support module providing I2C buses present on Matrox videocards");
230 
231 module_init(i2c_matroxfb_init);
232 module_exit(i2c_matroxfb_exit);
233 /* no __setup required */
234 MODULE_LICENSE("GPL");
235