Fix no-backlight colours for H100 series and M3.
[kugel-rb.git] / firmware / drivers / generic_i2c.c
blobdcbe46f6543daf014b6d6285f2b387efc45c14d0
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 struct i2c_interface *i2c_if[MAX_I2C_INTERFACES];
33 static void i2c_start(struct i2c_interface *iface)
35 iface->sda_output();
37 iface->sda_hi();
38 iface->scl_hi();
39 iface->sda_lo();
40 iface->delay_hd_sta();
41 iface->scl_lo();
44 static void i2c_stop(struct i2c_interface *iface)
46 iface->sda_output();
48 iface->sda_lo();
49 iface->scl_hi();
50 iface->delay_su_sto();
51 iface->sda_hi();
54 static void i2c_ack(struct i2c_interface *iface, bool ack)
56 iface->sda_output();
57 iface->scl_lo();
58 if ( ack )
59 iface->sda_lo();
60 else
61 iface->sda_hi();
63 iface->delay_su_dat();
64 iface->scl_hi();
65 iface->delay_thigh();
66 iface->scl_lo();
69 static int i2c_getack(struct i2c_interface *iface)
71 int ret = 1;
73 iface->sda_input();
74 iface->delay_su_dat();
75 iface->scl_hi();
77 if (iface->sda())
78 ret = 0; /* ack failed */
80 iface->delay_thigh();
81 iface->scl_lo();
82 iface->sda_hi();
83 iface->sda_output();
84 iface->delay_hd_dat();
85 return ret;
88 static unsigned char i2c_inb(struct i2c_interface *iface, bool ack)
90 int i;
91 unsigned char byte = 0;
93 iface->sda_input();
95 /* clock in each bit, MSB first */
96 for ( i=0x80; i; i>>=1 ) {
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(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();
126 iface->delay_hd_dat();
129 iface->sda_hi();
131 return i2c_getack(iface);
134 static struct i2c_interface *find_if(int address)
136 int i;
138 for(i = 0;i < i2c_num_ifs;i++) {
139 if(i2c_if[i]->address == address)
140 return i2c_if[i];
142 return NULL;
145 int i2c_write_data(int bus_address, int address,
146 const unsigned char* buf, int count)
148 int i;
149 int ret = 0;
150 struct i2c_interface *iface = find_if(bus_address);
151 if(!iface)
152 return -1;
154 i2c_start(iface);
155 if (!i2c_outb(iface, iface->address & 0xfe))
157 ret = -2;
158 goto end;
161 if (address != -1)
163 if (!i2c_outb(iface, address))
165 ret = -3;
166 goto end;
170 for(i = 0;i < count;i++)
172 if (!i2c_outb(iface, buf[i]))
174 ret = -4;
175 break;
179 end:
180 i2c_stop(iface);
181 return ret;
184 int i2c_read_data(int bus_address, int address,
185 unsigned char* buf, int count)
187 int i;
188 int ret = 0;
189 struct i2c_interface *iface = find_if(bus_address);
190 if(!iface)
191 return -1;
193 if (address != -1)
195 i2c_start(iface);
196 if (!i2c_outb(iface, iface->address & 0xfe))
198 ret = -2;
199 goto end;
201 if (!i2c_outb(iface, address))
203 ret = -3;
204 goto end;
208 i2c_start(iface);
209 if (!i2c_outb(iface, iface->address | 1))
211 ret = -4;
212 goto end;
215 for(i = 0;i < count-1;i++)
216 buf[i] = i2c_inb(iface, true);
218 buf[i] = i2c_inb(iface, false);
220 end:
221 i2c_stop(iface);
222 return ret;
225 int i2c_add_node(struct i2c_interface *iface)
227 if (i2c_num_ifs == MAX_I2C_INTERFACES)
228 return -1;
230 i2c_if[i2c_num_ifs++] = iface;
232 iface->scl_output();
234 return 0;