1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 by Linus Nielsen Feltzing
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
25 #include "generic_i2c.h"
28 struct i2c_interface
*i2c_if
[5];
30 static void i2c_start(struct i2c_interface
*iface
)
35 iface
->delay_hd_sta();
39 static void i2c_stop(struct i2c_interface
*iface
)
43 iface
->delay_su_sto();
47 static void i2c_ack(struct i2c_interface
*iface
, bool ack
)
55 iface
->delay_su_dat();
61 static int i2c_getack(struct i2c_interface
*iface
)
66 iface
->delay_su_dat();
70 ret
= 0; /* ack failed */
76 iface
->delay_hd_dat();
80 static unsigned char i2c_inb(struct i2c_interface
*iface
, bool ack
)
83 unsigned char byte
= 0;
85 /* clock in each bit, MSB first */
86 for ( i
=0x80; i
; i
>>=1 ) {
93 iface
->delay_hd_dat();
102 static void i2c_outb(struct i2c_interface
*iface
, unsigned char byte
)
106 /* clock out each bit, MSB first */
107 for (i
=0x80; i
; i
>>=1) {
112 iface
->delay_su_dat();
114 iface
->delay_thigh();
116 iface
->delay_hd_dat();
122 static struct i2c_interface
*find_if(int address
)
126 for(i
= 0;i
< i2c_num_ifs
;i
++) {
127 if(i2c_if
[i
]->address
== address
)
133 int i2c_write_data(int bus_address
, int address
,
134 const unsigned char* buf
, int count
)
138 struct i2c_interface
*iface
= find_if(bus_address
);
143 i2c_outb(iface
, iface
->address
& 0xfe);
144 if (i2c_getack(iface
)) {
145 i2c_outb(iface
, address
);
146 if (i2c_getack(iface
)) {
147 for(i
= 0;i
< count
;i
++) {
148 i2c_outb(iface
, buf
[i
]);
149 if (!i2c_getack(iface
)) {
158 logf("i2c_write_data() - no ack\n");
166 int i2c_read_data(int bus_address
, int address
,
167 unsigned char* buf
, int count
)
171 struct i2c_interface
*iface
= find_if(bus_address
);
176 i2c_outb(iface
, iface
->address
& 0xfe);
177 if (i2c_getack(iface
)) {
178 i2c_outb(iface
, address
);
179 if (i2c_getack(iface
)) {
181 i2c_outb(iface
, iface
->address
| 1);
182 if (i2c_getack(iface
)) {
183 for(i
= 0;i
< count
-1;i
++)
184 buf
[i
] = i2c_inb(iface
, true);
186 buf
[i
] = i2c_inb(iface
, false);
201 void i2c_add_node(struct i2c_interface
*iface
)
203 i2c_if
[i2c_num_ifs
++] = iface
;