2 * Copyright 1998-2008 VIA Technologies, Inc. All Rights Reserved.
3 * Copyright 2001-2008 S3 Graphics, Inc. All Rights Reserved.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public
7 * License as published by the Free Software Foundation;
8 * either version 2, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTIES OR REPRESENTATIONS; without even
12 * the implied warranty of MERCHANTABILITY or FITNESS FOR
13 * A PARTICULAR PURPOSE.See the GNU General Public License
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 static void via_i2c_setscl(void *data
, int state
)
27 struct via_i2c_stuff
*via_i2c_chan
= (struct via_i2c_stuff
*)data
;
29 val
= viafb_read_reg(VIASR
, via_i2c_chan
->i2c_port
) & 0xF0;
34 switch (via_i2c_chan
->i2c_port
) {
42 DEBUG_MSG("via_i2c: specify wrong i2c port.\n");
44 viafb_write_reg(via_i2c_chan
->i2c_port
, VIASR
, val
);
47 static int via_i2c_getscl(void *data
)
49 struct via_i2c_stuff
*via_i2c_chan
= (struct via_i2c_stuff
*)data
;
51 if (viafb_read_reg(VIASR
, via_i2c_chan
->i2c_port
) & 0x08)
56 static int via_i2c_getsda(void *data
)
58 struct via_i2c_stuff
*via_i2c_chan
= (struct via_i2c_stuff
*)data
;
60 if (viafb_read_reg(VIASR
, via_i2c_chan
->i2c_port
) & 0x04)
65 static void via_i2c_setsda(void *data
, int state
)
68 struct via_i2c_stuff
*via_i2c_chan
= (struct via_i2c_stuff
*)data
;
70 val
= viafb_read_reg(VIASR
, via_i2c_chan
->i2c_port
) & 0xF0;
75 switch (via_i2c_chan
->i2c_port
) {
83 DEBUG_MSG("via_i2c: specify wrong i2c port.\n");
85 viafb_write_reg(via_i2c_chan
->i2c_port
, VIASR
, val
);
88 int viafb_i2c_readbyte(u8 slave_addr
, u8 index
, u8
*pdata
)
91 struct i2c_msg msgs
[2];
95 msgs
[1].flags
= I2C_M_RD
;
96 msgs
[0].addr
= msgs
[1].addr
= slave_addr
/ 2;
98 msgs
[0].len
= 1; msgs
[1].len
= 1;
99 msgs
[0].buf
= mm1
; msgs
[1].buf
= pdata
;
100 i2c_transfer(&viaparinfo
->shared
->i2c_stuff
.adapter
, msgs
, 2);
105 int viafb_i2c_writebyte(u8 slave_addr
, u8 index
, u8 data
)
107 u8 msg
[2] = { index
, data
};
111 msgs
.addr
= slave_addr
/ 2;
114 return i2c_transfer(&viaparinfo
->shared
->i2c_stuff
.adapter
, &msgs
, 1);
117 int viafb_i2c_readbytes(u8 slave_addr
, u8 index
, u8
*buff
, int buff_len
)
120 struct i2c_msg msgs
[2];
123 msgs
[1].flags
= I2C_M_RD
;
124 msgs
[0].addr
= msgs
[1].addr
= slave_addr
/ 2;
126 msgs
[0].len
= 1; msgs
[1].len
= buff_len
;
127 msgs
[0].buf
= mm1
; msgs
[1].buf
= buff
;
128 i2c_transfer(&viaparinfo
->shared
->i2c_stuff
.adapter
, msgs
, 2);
132 int viafb_create_i2c_bus(void *viapar
)
135 struct via_i2c_stuff
*i2c_stuff
=
136 &((struct viafb_par
*)viapar
)->shared
->i2c_stuff
;
138 strcpy(i2c_stuff
->adapter
.name
, "via_i2c");
139 i2c_stuff
->i2c_port
= 0x0;
140 i2c_stuff
->adapter
.owner
= THIS_MODULE
;
141 i2c_stuff
->adapter
.id
= 0x01FFFF;
142 i2c_stuff
->adapter
.class = 0;
143 i2c_stuff
->adapter
.algo_data
= &i2c_stuff
->algo
;
144 i2c_stuff
->adapter
.dev
.parent
= NULL
;
145 i2c_stuff
->algo
.setsda
= via_i2c_setsda
;
146 i2c_stuff
->algo
.setscl
= via_i2c_setscl
;
147 i2c_stuff
->algo
.getsda
= via_i2c_getsda
;
148 i2c_stuff
->algo
.getscl
= via_i2c_getscl
;
149 i2c_stuff
->algo
.udelay
= 40;
150 i2c_stuff
->algo
.timeout
= 20;
151 i2c_stuff
->algo
.data
= i2c_stuff
;
153 i2c_set_adapdata(&i2c_stuff
->adapter
, i2c_stuff
);
155 /* Raise SCL and SDA */
156 i2c_stuff
->i2c_port
= I2CPORTINDEX
;
157 via_i2c_setsda(i2c_stuff
, 1);
158 via_i2c_setscl(i2c_stuff
, 1);
160 i2c_stuff
->i2c_port
= GPIOPORTINDEX
;
161 via_i2c_setsda(i2c_stuff
, 1);
162 via_i2c_setscl(i2c_stuff
, 1);
165 ret
= i2c_bit_add_bus(&i2c_stuff
->adapter
);
167 DEBUG_MSG("I2C bus %s registered.\n", i2c_stuff
->adapter
.name
);
169 DEBUG_MSG("Failed to register I2C bus %s.\n",
170 i2c_stuff
->adapter
.name
);
174 void viafb_delete_i2c_buss(void *par
)
176 i2c_del_adapter(&((struct viafb_par
*)par
)->shared
->i2c_stuff
.adapter
);