fuzev2: prevent button light flickering when accessing µSD
[kugel-rb.git] / firmware / drivers / generic_i2c.c
blob87e9091a3d18ad80199b0b263ddceb7fc6e7d9a5
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
21 #include "config.h"
22 #include "cpu.h"
23 #include <stdbool.h>
24 #include <stdlib.h>
25 #include "debug.h"
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)
35 iface->sda_output();
37 iface->sda_hi();
38 iface->scl_hi();
39 iface->delay_su_sta();
40 iface->sda_lo();
41 iface->delay_hd_sta();
42 iface->scl_lo();
43 iface->delay_hd_dat();
46 static void i2c_stop(const struct i2c_interface *iface)
48 iface->sda_output();
50 iface->sda_lo();
51 iface->delay_su_dat();
52 iface->scl_hi();
53 iface->delay_su_sto();
54 iface->sda_hi();
57 static void i2c_ack(const struct i2c_interface *iface, bool ack)
59 iface->sda_output();
60 if ( ack )
61 iface->sda_lo();
62 else
63 iface->sda_hi();
65 iface->delay_su_dat();
66 iface->scl_hi();
67 iface->delay_thigh();
68 iface->scl_lo();
69 iface->delay_hd_dat();
72 static int i2c_getack(const struct i2c_interface *iface)
74 int ret = 1;
76 iface->sda_input();
77 iface->delay_su_dat();
78 iface->scl_hi();
79 iface->delay_thigh();
80 if (iface->sda())
81 ret = 0; /* ack failed */
82 iface->scl_lo();
83 iface->delay_hd_dat();
84 return ret;
87 static unsigned char i2c_inb(const struct i2c_interface *iface, bool ack)
89 int i;
90 unsigned char byte = 0;
92 iface->sda_input();
94 /* clock in each bit, MSB first */
95 for ( i=0x80; i; i>>=1 ) {
96 iface->delay_su_dat();
97 iface->scl_hi();
98 iface->delay_thigh();
99 if (iface->sda())
100 byte |= i;
101 iface->scl_lo();
102 iface->delay_hd_dat();
105 i2c_ack(iface, ack);
107 return byte;
110 static int i2c_outb(const struct i2c_interface *iface, unsigned char byte)
112 int i;
114 iface->sda_output();
116 /* clock out each bit, MSB first */
117 for (i=0x80; i; i>>=1) {
118 if (i & byte)
119 iface->sda_hi();
120 else
121 iface->sda_lo();
122 iface->delay_su_dat();
123 iface->scl_hi();
124 iface->delay_thigh();
125 iface->scl_lo();
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)
134 int i;
135 int ret = 0;
136 const struct i2c_interface *iface = i2c_if[bus_index];
138 i2c_start(iface);
139 if (!i2c_outb(iface, bus_address & 0xfe))
141 ret = -2;
142 goto end;
145 if (address != -1)
147 if (!i2c_outb(iface, address))
149 ret = -3;
150 goto end;
154 for(i = 0;i < count;i++)
156 if (!i2c_outb(iface, buf[i]))
158 ret = -4;
159 break;
163 end:
164 i2c_stop(iface);
165 return ret;
168 int i2c_read_data(int bus_index, int bus_address, int address,
169 unsigned char* buf, int count)
171 int i;
172 int ret = 0;
173 const struct i2c_interface *iface = i2c_if[bus_index];
175 if (address != -1)
177 i2c_start(iface);
178 if (!i2c_outb(iface, bus_address & 0xfe))
180 ret = -2;
181 goto end;
183 if (!i2c_outb(iface, address))
185 ret = -3;
186 goto end;
190 i2c_start(iface);
191 if (!i2c_outb(iface, bus_address | 1))
193 ret = -4;
194 goto end;
197 for(i = 0;i < count-1;i++)
198 buf[i] = i2c_inb(iface, true);
200 buf[i] = i2c_inb(iface, false);
202 end:
203 i2c_stop(iface);
204 return ret;
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)
210 int bus_index;
212 if (i2c_num_ifs == MAX_I2C_INTERFACES)
213 return -1;
215 bus_index = i2c_num_ifs++;
216 i2c_if[bus_index] = iface;
218 iface->scl_output();
220 return bus_index;