RTC s35380a - add support for alarm function. Currently this powers up the device...
[maemo-rb.git] / firmware / drivers / generic_i2c.c
blobeffb5372b460c991818cc48a46c8eee2f80ac27f
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_dir(true);
37 iface->sda_out(1);
38 iface->scl_out(1);
39 iface->delay(iface->delay_su_sta);
40 iface->sda_out(0);
41 iface->delay(iface->delay_hd_sta);
42 iface->scl_out(0);
43 iface->delay(iface->delay_hd_dat);
46 static void i2c_stop(const struct i2c_interface *iface)
48 iface->sda_dir(true);
50 iface->sda_out(0);
51 iface->delay(iface->delay_su_dat);
52 iface->scl_out(1);
53 iface->delay(iface->delay_su_sto);
54 iface->sda_out(1);
57 static void i2c_ack(const struct i2c_interface *iface, bool ack)
59 iface->sda_dir(true);
60 iface->sda_out(!ack);
61 iface->delay(iface->delay_su_dat);
62 iface->scl_out(1);
63 iface->delay(iface->delay_thigh);
64 iface->scl_out(0);
65 iface->delay(iface->delay_hd_dat);
68 static int i2c_getack(const struct i2c_interface *iface)
70 int ret = 1;
72 iface->sda_dir(false);
73 iface->delay(iface->delay_su_dat);
74 iface->scl_out(1);
75 iface->delay(iface->delay_thigh);
76 if (iface->sda_in())
77 ret = 0; /* ack failed */
78 iface->scl_out(0);
79 iface->delay(iface->delay_hd_dat);
80 return ret;
83 static unsigned char i2c_inb(const struct i2c_interface *iface, bool ack)
85 int i;
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);
93 iface->scl_out(1);
94 iface->delay(iface->delay_thigh);
95 if (iface->sda_in())
96 byte |= i;
97 iface->scl_out(0);
98 iface->delay(iface->delay_hd_dat);
101 i2c_ack(iface, ack);
103 return byte;
106 static int i2c_outb(const struct i2c_interface *iface, unsigned char byte)
108 int i;
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);
116 iface->scl_out(1);
117 iface->delay(iface->delay_thigh);
118 iface->scl_out(0);
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)
128 int i;
129 int ret = 0;
130 const struct i2c_interface *iface = i2c_if[bus_index];
132 i2c_start(iface);
133 if (!i2c_outb(iface, bus_address & 0xfe))
135 ret = -2;
136 goto end;
139 if (address != -1)
141 if (!i2c_outb(iface, address))
143 ret = -3;
144 goto end;
148 for(i = 0;i < count;i++)
150 if (!i2c_outb(iface, buf[i]))
152 ret = -4;
153 break;
157 end:
158 i2c_stop(iface);
159 return ret;
162 int i2c_read_data(int bus_index, int bus_address, int address,
163 unsigned char* buf, int count)
165 int i;
166 int ret = 0;
167 const struct i2c_interface *iface = i2c_if[bus_index];
169 if (address != -1)
171 i2c_start(iface);
172 if (!i2c_outb(iface, bus_address & 0xfe))
174 ret = -2;
175 goto end;
177 if (!i2c_outb(iface, address))
179 ret = -3;
180 goto end;
184 i2c_start(iface);
185 if (!i2c_outb(iface, bus_address | 1))
187 ret = -4;
188 goto end;
191 for(i = 0;i < count-1;i++)
192 buf[i] = i2c_inb(iface, true);
194 buf[i] = i2c_inb(iface, false);
196 end:
197 i2c_stop(iface);
198 return ret;
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)
204 int bus_index;
206 if (i2c_num_ifs == MAX_I2C_INTERFACES)
207 return -1;
209 bus_index = i2c_num_ifs++;
210 i2c_if[bus_index] = iface;
212 iface->scl_dir(true);
214 return bus_index;