3 * Hardware accelerated Matrox Millennium I, II, Mystique, G100, G200, G400 and G450.
5 * (c) 1998-2002 Petr Vandrovec <vandrove@vc.cvut.cz>
7 * Version: 1.64 2002/06/10
9 * See matroxfb_base.c for contributors.
13 #include "matroxfb_base.h"
14 #include "matroxfb_maven.h"
15 #include <linux/i2c.h>
16 #include <linux/i2c-algo-bit.h>
18 /* MGA-TVO I2C for G200, G400 */
21 /* primary head DDC for Mystique(?), G100, G200, G400 */
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 */
29 #define DDC2_DATA 0x01
31 /******************************************************/
33 struct matroxfb_dh_maven_info
{
34 struct i2c_bit_adapter maven
;
35 struct i2c_bit_adapter ddc1
;
36 struct i2c_bit_adapter ddc2
;
39 static int matroxfb_read_gpio(struct matrox_fb_info
* minfo
) {
43 matroxfb_DAC_lock_irqsave(flags
);
44 v
= matroxfb_DAC_in(PMINFO DAC_XGENIODATA
);
45 matroxfb_DAC_unlock_irqrestore(flags
);
49 static void matroxfb_set_gpio(struct matrox_fb_info
* minfo
, int mask
, int val
) {
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
);
61 /* software I2C functions */
62 static inline void matroxfb_i2c_set(struct matrox_fb_info
* minfo
, int mask
, int state
) {
67 matroxfb_set_gpio(minfo
, ~mask
, 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
);
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
);
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;
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;
90 static const struct i2c_algo_bit_data matrox_i2c_algo_template
=
92 .setsda
= matroxfb_gpio_setsda
,
93 .setscl
= matroxfb_gpio_setscl
,
94 .getsda
= matroxfb_gpio_getsda
,
95 .getscl
= matroxfb_gpio_getscl
,
100 static int i2c_bus_reg(struct i2c_bit_adapter
* b
, struct matrox_fb_info
* minfo
,
101 unsigned int data
, unsigned int clock
, const char *name
,
108 b
->mask
.clock
= clock
;
109 b
->adapter
.owner
= THIS_MODULE
;
110 b
->adapter
.id
= I2C_HW_B_G400
;
111 snprintf(b
->adapter
.name
, sizeof(b
->adapter
.name
), name
,
113 i2c_set_adapdata(&b
->adapter
, b
);
114 b
->adapter
.class = class;
115 b
->adapter
.algo_data
= &b
->bac
;
116 b
->adapter
.dev
.parent
= &ACCESS_FBINFO(pcidev
)->dev
;
117 b
->bac
= matrox_i2c_algo_template
;
119 err
= i2c_bit_add_bus(&b
->adapter
);
120 b
->initialized
= !err
;
124 static void i2c_bit_bus_del(struct i2c_bit_adapter
* b
) {
125 if (b
->initialized
) {
126 i2c_del_adapter(&b
->adapter
);
131 static inline void i2c_maven_done(struct matroxfb_dh_maven_info
* minfo2
) {
132 i2c_bit_bus_del(&minfo2
->maven
);
135 static inline void i2c_ddc1_done(struct matroxfb_dh_maven_info
* minfo2
) {
136 i2c_bit_bus_del(&minfo2
->ddc1
);
139 static inline void i2c_ddc2_done(struct matroxfb_dh_maven_info
* minfo2
) {
140 i2c_bit_bus_del(&minfo2
->ddc2
);
143 static void* i2c_matroxfb_probe(struct matrox_fb_info
* minfo
) {
146 struct matroxfb_dh_maven_info
* m2info
;
148 m2info
= kzalloc(sizeof(*m2info
), GFP_KERNEL
);
152 matroxfb_DAC_lock_irqsave(flags
);
153 matroxfb_DAC_out(PMINFO DAC_XGENIODATA
, 0xFF);
154 matroxfb_DAC_out(PMINFO DAC_XGENIOCTRL
, 0x00);
155 matroxfb_DAC_unlock_irqrestore(flags
);
157 switch (ACCESS_FBINFO(chip
)) {
160 err
= i2c_bus_reg(&m2info
->ddc1
, minfo
,
161 DDC1B_DATA
, DDC1B_CLK
,
162 "DDC:fb%u #0", I2C_CLASS_DDC
);
165 err
= i2c_bus_reg(&m2info
->ddc1
, minfo
,
167 "DDC:fb%u #0", I2C_CLASS_DDC
);
172 if (ACCESS_FBINFO(devflags
.dualhead
)) {
173 err
= i2c_bus_reg(&m2info
->ddc2
, minfo
,
175 "DDC:fb%u #1", I2C_CLASS_DDC
);
176 if (err
== -ENODEV
) {
177 printk(KERN_INFO
"i2c-matroxfb: VGA->TV plug detected, DDC unavailable.\n");
179 printk(KERN_INFO
"i2c-matroxfb: Could not register secondary output i2c bus. Continuing anyway.\n");
180 /* Register maven bus even on G450/G550 */
181 err
= i2c_bus_reg(&m2info
->maven
, minfo
,
182 MAT_DATA
, MAT_CLK
, "MAVEN:fb%u", 0);
184 printk(KERN_INFO
"i2c-matroxfb: Could not register Maven i2c bus. Continuing anyway.\n");
189 printk(KERN_ERR
"i2c-matroxfb: Could not register primary adapter DDC bus.\n");
193 static void i2c_matroxfb_remove(struct matrox_fb_info
* minfo
, void* data
) {
194 struct matroxfb_dh_maven_info
* m2info
= data
;
196 i2c_maven_done(m2info
);
197 i2c_ddc2_done(m2info
);
198 i2c_ddc1_done(m2info
);
202 static struct matroxfb_driver i2c_matroxfb
= {
203 .node
= LIST_HEAD_INIT(i2c_matroxfb
.node
),
204 .name
= "i2c-matroxfb",
205 .probe
= i2c_matroxfb_probe
,
206 .remove
= i2c_matroxfb_remove
,
209 static int __init
i2c_matroxfb_init(void) {
210 if (matroxfb_register_driver(&i2c_matroxfb
)) {
211 printk(KERN_ERR
"i2c-matroxfb: failed to register driver\n");
217 static void __exit
i2c_matroxfb_exit(void) {
218 matroxfb_unregister_driver(&i2c_matroxfb
);
221 MODULE_AUTHOR("(c) 1999-2002 Petr Vandrovec <vandrove@vc.cvut.cz>");
222 MODULE_DESCRIPTION("Support module providing I2C buses present on Matrox videocards");
224 module_init(i2c_matroxfb_init
);
225 module_exit(i2c_matroxfb_exit
);
226 /* no __setup required */
227 MODULE_LICENSE("GPL");