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
->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
->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
->i2c_stuff
.adapter
, msgs
, 2);
132 int viafb_create_i2c_bus(void *viapar
)
135 struct viafb_par
*par
= (struct viafb_par
*)viapar
;
137 strcpy(par
->i2c_stuff
.adapter
.name
, "via_i2c");
138 par
->i2c_stuff
.i2c_port
= 0x0;
139 par
->i2c_stuff
.adapter
.owner
= THIS_MODULE
;
140 par
->i2c_stuff
.adapter
.id
= 0x01FFFF;
141 par
->i2c_stuff
.adapter
.class = 0;
142 par
->i2c_stuff
.adapter
.algo_data
= &par
->i2c_stuff
.algo
;
143 par
->i2c_stuff
.adapter
.dev
.parent
= NULL
;
144 par
->i2c_stuff
.algo
.setsda
= via_i2c_setsda
;
145 par
->i2c_stuff
.algo
.setscl
= via_i2c_setscl
;
146 par
->i2c_stuff
.algo
.getsda
= via_i2c_getsda
;
147 par
->i2c_stuff
.algo
.getscl
= via_i2c_getscl
;
148 par
->i2c_stuff
.algo
.udelay
= 40;
149 par
->i2c_stuff
.algo
.timeout
= 20;
150 par
->i2c_stuff
.algo
.data
= &par
->i2c_stuff
;
152 i2c_set_adapdata(&par
->i2c_stuff
.adapter
, &par
->i2c_stuff
);
154 /* Raise SCL and SDA */
155 par
->i2c_stuff
.i2c_port
= I2CPORTINDEX
;
156 via_i2c_setsda(&par
->i2c_stuff
, 1);
157 via_i2c_setscl(&par
->i2c_stuff
, 1);
159 par
->i2c_stuff
.i2c_port
= GPIOPORTINDEX
;
160 via_i2c_setsda(&par
->i2c_stuff
, 1);
161 via_i2c_setscl(&par
->i2c_stuff
, 1);
164 ret
= i2c_bit_add_bus(&par
->i2c_stuff
.adapter
);
166 DEBUG_MSG("I2C bus %s registered.\n",
167 par
->i2c_stuff
.adapter
.name
);
169 DEBUG_MSG("Failed to register I2C bus %s.\n",
170 par
->i2c_stuff
.adapter
.name
);
174 void viafb_delete_i2c_buss(void *par
)
176 i2c_del_adapter(&((struct viafb_par
*)par
)->i2c_stuff
.adapter
);