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(iface
->delay_su_sta
);
41 iface
->delay(iface
->delay_hd_sta
);
43 iface
->delay(iface
->delay_hd_dat
);
46 static void i2c_stop(const struct i2c_interface
*iface
)
51 iface
->delay(iface
->delay_su_dat
);
53 iface
->delay(iface
->delay_su_sto
);
57 static void i2c_ack(const struct i2c_interface
*iface
, bool ack
)
61 iface
->delay(iface
->delay_su_dat
);
63 iface
->delay(iface
->delay_thigh
);
65 iface
->delay(iface
->delay_hd_dat
);
68 static int i2c_getack(const struct i2c_interface
*iface
)
72 iface
->sda_dir(false);
73 iface
->delay(iface
->delay_su_dat
);
75 iface
->delay(iface
->delay_thigh
);
77 ret
= 0; /* ack failed */
79 iface
->delay(iface
->delay_hd_dat
);
83 static unsigned char i2c_inb(const struct i2c_interface
*iface
, bool ack
)
86 unsigned char byte
= 0;
88 iface
->sda_dir(false);
90 /* clock in each bit, MSB first */
91 for ( i
=0x80; i
; i
>>=1 ) {
92 iface
->delay(iface
->delay_su_dat
);
94 iface
->delay(iface
->delay_thigh
);
98 iface
->delay(iface
->delay_hd_dat
);
106 static int i2c_outb(const struct i2c_interface
*iface
, unsigned char byte
)
110 iface
->sda_dir(true);
112 /* clock out each bit, MSB first */
113 for (i
=0x80; i
; i
>>=1) {
114 iface
->sda_out(i
& byte
);
115 iface
->delay(iface
->delay_su_dat
);
117 iface
->delay(iface
->delay_thigh
);
119 iface
->delay(iface
->delay_hd_dat
);
122 return i2c_getack(iface
);
125 int i2c_write_data(int bus_index
, int bus_address
, int address
,
126 const unsigned char* buf
, int count
)
130 const struct i2c_interface
*iface
= i2c_if
[bus_index
];
133 if (!i2c_outb(iface
, bus_address
& 0xfe))
141 if (!i2c_outb(iface
, address
))
148 for(i
= 0;i
< count
;i
++)
150 if (!i2c_outb(iface
, buf
[i
]))
162 int i2c_read_data(int bus_index
, int bus_address
, int address
,
163 unsigned char* buf
, int count
)
167 const struct i2c_interface
*iface
= i2c_if
[bus_index
];
172 if (!i2c_outb(iface
, bus_address
& 0xfe))
177 if (!i2c_outb(iface
, address
))
185 if (!i2c_outb(iface
, bus_address
| 1))
191 for(i
= 0;i
< count
-1;i
++)
192 buf
[i
] = i2c_inb(iface
, true);
194 buf
[i
] = i2c_inb(iface
, false);
201 /* returns bus index which can be used as a handle, or <0 on error */
202 int i2c_add_node(const struct i2c_interface
*iface
)
206 if (i2c_num_ifs
== MAX_I2C_INTERFACES
)
209 bus_index
= i2c_num_ifs
++;
210 i2c_if
[bus_index
] = iface
;
212 iface
->scl_dir(true);