a bit of code cleanup.. use a single function to get the statusbar height (or lack...
[Rockbox.git] / firmware / drivers / generic_i2c.c
blob8475f00ee6ad253e484c29c11988e9bde7633c79
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
19 #include "config.h"
20 #include "cpu.h"
21 #include <stdbool.h>
22 #include <stdlib.h>
23 #include "debug.h"
24 #include "logf.h"
25 #include "generic_i2c.h"
27 int i2c_num_ifs = 0;
28 struct i2c_interface *i2c_if[5];
30 static void i2c_start(struct i2c_interface *iface)
32 iface->sda_hi();
33 iface->scl_hi();
34 iface->sda_lo();
35 iface->delay_hd_sta();
36 iface->scl_lo();
39 static void i2c_stop(struct i2c_interface *iface)
41 iface->sda_lo();
42 iface->scl_hi();
43 iface->delay_su_sto();
44 iface->sda_hi();
47 static void i2c_ack(struct i2c_interface *iface, bool ack)
49 iface->scl_lo();
50 if ( ack )
51 iface->sda_lo();
52 else
53 iface->sda_hi();
55 iface->delay_su_dat();
56 iface->scl_hi();
57 iface->delay_thigh();
58 iface->scl_lo();
61 static int i2c_getack(struct i2c_interface *iface)
63 int ret = 1;
65 iface->sda_input();
66 iface->delay_su_dat();
67 iface->scl_hi();
69 if (iface->sda())
70 ret = 0; /* ack failed */
72 iface->delay_thigh();
73 iface->scl_lo();
74 iface->sda_hi();
75 iface->sda_output();
76 iface->delay_hd_dat();
77 return ret;
80 static unsigned char i2c_inb(struct i2c_interface *iface, bool ack)
82 int i;
83 unsigned char byte = 0;
85 /* clock in each bit, MSB first */
86 for ( i=0x80; i; i>>=1 ) {
87 iface->sda_input();
88 iface->scl_hi();
89 iface->delay_thigh();
90 if (iface->sda())
91 byte |= i;
92 iface->scl_lo();
93 iface->delay_hd_dat();
94 iface->sda_output();
97 i2c_ack(iface, ack);
99 return byte;
102 static void i2c_outb(struct i2c_interface *iface, unsigned char byte)
104 int i;
106 /* clock out each bit, MSB first */
107 for (i=0x80; i; i>>=1) {
108 if (i & byte)
109 iface->sda_hi();
110 else
111 iface->sda_lo();
112 iface->delay_su_dat();
113 iface->scl_hi();
114 iface->delay_thigh();
115 iface->scl_lo();
116 iface->delay_hd_dat();
119 iface->sda_hi();
122 static struct i2c_interface *find_if(int address)
124 int i;
126 for(i = 0;i < i2c_num_ifs;i++) {
127 if(i2c_if[i]->address == address)
128 return i2c_if[i];
130 return NULL;
133 int i2c_write_data(int bus_address, int address,
134 const unsigned char* buf, int count)
136 int i;
137 int ret = 0;
138 struct i2c_interface *iface = find_if(bus_address);
139 if(!iface)
140 return -1;
142 i2c_start(iface);
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)) {
150 ret = -3;
151 break;
154 } else {
155 ret = -2;
157 } else {
158 logf("i2c_write_data() - no ack\n");
159 ret = -1;
162 i2c_stop(iface);
163 return ret;
166 int i2c_read_data(int bus_address, int address,
167 unsigned char* buf, int count)
169 int i;
170 int ret = 0;
171 struct i2c_interface *iface = find_if(bus_address);
172 if(!iface)
173 return -1;
175 i2c_start(iface);
176 i2c_outb(iface, iface->address & 0xfe);
177 if (i2c_getack(iface)) {
178 i2c_outb(iface, address);
179 if (i2c_getack(iface)) {
180 i2c_start(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);
187 } else {
188 ret = -3;
190 } else {
191 ret = -2;
193 } else {
194 ret = -1;
197 i2c_stop(iface);
198 return ret;
201 void i2c_add_node(struct i2c_interface *iface)
203 i2c_if[i2c_num_ifs++] = iface;