1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2006 by Linus Nielsen Feltzing
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
26 #include "generic_i2c.h"
28 #define MAX_I2C_INTERFACES 5
30 static int i2c_num_ifs
= 0;
31 static const struct i2c_interface
*i2c_if
[MAX_I2C_INTERFACES
];
33 static void i2c_start(const struct i2c_interface
*iface
)
39 iface
->delay_su_sta();
41 iface
->delay_hd_sta();
43 iface
->delay_hd_dat();
46 static void i2c_stop(const struct i2c_interface
*iface
)
51 iface
->delay_su_dat();
53 iface
->delay_su_sto();
57 static void i2c_ack(const struct i2c_interface
*iface
, bool ack
)
65 iface
->delay_su_dat();
69 iface
->delay_hd_dat();
72 static int i2c_getack(const struct i2c_interface
*iface
)
77 iface
->delay_su_dat();
81 ret
= 0; /* ack failed */
83 iface
->delay_hd_dat();
87 static unsigned char i2c_inb(const struct i2c_interface
*iface
, bool ack
)
90 unsigned char byte
= 0;
94 /* clock in each bit, MSB first */
95 for ( i
=0x80; i
; i
>>=1 ) {
96 iface
->delay_su_dat();
102 iface
->delay_hd_dat();
110 static int i2c_outb(const struct i2c_interface
*iface
, unsigned char byte
)
116 /* clock out each bit, MSB first */
117 for (i
=0x80; i
; i
>>=1) {
122 iface
->delay_su_dat();
124 iface
->delay_thigh();
128 return i2c_getack(iface
);
131 int i2c_write_data(int bus_index
, int bus_address
, int address
,
132 const unsigned char* buf
, int count
)
136 const struct i2c_interface
*iface
= i2c_if
[bus_index
];
139 if (!i2c_outb(iface
, bus_address
& 0xfe))
147 if (!i2c_outb(iface
, address
))
154 for(i
= 0;i
< count
;i
++)
156 if (!i2c_outb(iface
, buf
[i
]))
168 int i2c_read_data(int bus_index
, int bus_address
, int address
,
169 unsigned char* buf
, int count
)
173 const struct i2c_interface
*iface
= i2c_if
[bus_index
];
178 if (!i2c_outb(iface
, bus_address
& 0xfe))
183 if (!i2c_outb(iface
, address
))
191 if (!i2c_outb(iface
, bus_address
| 1))
197 for(i
= 0;i
< count
-1;i
++)
198 buf
[i
] = i2c_inb(iface
, true);
200 buf
[i
] = i2c_inb(iface
, false);
207 /* returns bus index which can be used as a handle, or <0 on error */
208 int i2c_add_node(const struct i2c_interface
*iface
)
212 if (i2c_num_ifs
== MAX_I2C_INTERFACES
)
215 bus_index
= i2c_num_ifs
++;
216 i2c_if
[bus_index
] = iface
;