lcd dimensions from configure
[kugel-rb.git] / firmware / drivers / i2c.c
blob8cc7c78a2da5deb08514ddfa0d4c3a85cd6a29e1
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 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 "lcd.h"
22 #include "cpu.h"
23 #include "kernel.h"
24 #include "thread.h"
25 #include "debug.h"
26 #include "system.h"
27 #include "i2c.h"
29 /* cute little functions, atomic read-modify-write */
31 /* SDA is PB7 */
32 #define SDA_LO and_b(~0x80, &PBDRL)
33 #define SDA_HI or_b(0x80, &PBDRL)
34 #define SDA_INPUT and_b(~0x80, &PBIORL)
35 #define SDA_OUTPUT or_b(0x80, &PBIORL)
36 #define SDA (PBDRL & 0x80)
38 #if CONFIG_I2C == I2C_ONDIO
39 /* Ondio pinout, SCL moved to PB6 */
40 #define SCL_INPUT and_b(~0x40, &PBIORL)
41 #define SCL_OUTPUT or_b(0x40, &PBIORL)
42 #define SCL_LO and_b(~0x40, &PBDRL)
43 #define SCL_HI or_b(0x40, &PBDRL)
44 #define SCL (PBDRL & 0x40)
45 #else
46 /* "classic" pinout, SCL is PB13 */
47 #define SCL_INPUT and_b(~0x20, &PBIORH)
48 #define SCL_OUTPUT or_b(0x20, &PBIORH)
49 #define SCL_LO and_b(~0x20, &PBDRH)
50 #define SCL_HI or_b(0x20, &PBDRH)
51 #define SCL (PBDRH & 0x20)
52 #endif
54 /* arbitrary delay loop */
55 #define DELAY do { int _x; for(_x=0;_x<20;_x++);} while (0)
57 static struct mutex i2c_mtx;
59 void i2c_begin(void)
61 mutex_lock(&i2c_mtx);
64 void i2c_end(void)
66 mutex_unlock(&i2c_mtx);
69 void i2c_start(void)
71 SDA_OUTPUT;
72 SDA_HI;
73 SCL_HI;
74 SDA_LO;
75 DELAY;
76 SCL_LO;
79 void i2c_stop(void)
81 SDA_LO;
82 SCL_HI;
83 DELAY;
84 SDA_HI;
87 void i2c_init(void)
89 int i;
91 mutex_init(&i2c_mtx);
93 #if CONFIG_I2C == I2C_ONDIO
94 /* make PB6 & PB7 general I/O */
95 PBCR2 &= ~0xf000;
96 #else /* not Ondio */
97 /* make PB7 & PB13 general I/O */
98 PBCR1 &= ~0x0c00; /* PB13 */
99 PBCR2 &= ~0xc000; /* PB7 */
100 #endif
102 SCL_OUTPUT;
103 SDA_OUTPUT;
104 SDA_HI;
105 SCL_LO;
106 for (i=0;i<3;i++)
107 i2c_stop();
110 void i2c_ack(int bit)
112 /* Here's the deal. The MAS is slow, and sometimes needs to wait
113 before it can receive the acknowledge. Therefore it forces the clock
114 low until it is ready. We need to poll the clock line until it goes
115 high before we release the ack. */
117 SCL_LO; /* Set the clock low */
118 if ( bit )
120 SDA_HI;
122 else
124 SDA_LO;
127 SCL_INPUT; /* Set the clock to input */
128 while(!SCL) /* and wait for the MAS to release it */
129 sleep(0);
131 DELAY;
132 SCL_OUTPUT;
133 SCL_LO;
136 int i2c_getack(void)
138 int ret = 1;
140 /* Here's the deal. The MAS is slow, and sometimes needs to wait
141 before it can send the acknowledge. Therefore it forces the clock
142 low until it is ready. We need to poll the clock line until it goes
143 high before we read the ack. */
145 #ifdef HAVE_I2C_LOW_FIRST
146 SDA_LO; /* First, discharge the data line */
147 #endif
148 SDA_INPUT; /* And set to input */
149 SCL_INPUT; /* Set the clock to input */
150 while(!SCL) /* and wait for the MAS to release it */
151 sleep(0);
153 if (SDA)
154 /* ack failed */
155 ret = 0;
157 SCL_OUTPUT;
158 SCL_LO;
159 SDA_HI;
160 SDA_OUTPUT;
161 return ret;
164 void i2c_outb(unsigned char byte)
166 int i;
168 /* clock out each bit, MSB first */
169 for ( i=0x80; i; i>>=1 ) {
170 if ( i & byte )
172 SDA_HI;
174 else
176 SDA_LO;
178 SCL_HI;
179 SCL_LO;
182 SDA_HI;
185 unsigned char i2c_inb(int ack)
187 int i;
188 unsigned char byte = 0;
190 /* clock in each bit, MSB first */
191 for ( i=0x80; i; i>>=1 ) {
192 #ifdef HAVE_I2C_LOW_FIRST
193 /* Tricky business. Here we discharge the data line by driving it low
194 and then set it to input to see if it stays low or goes high */
195 SDA_LO; /* First, discharge the data line */
196 #endif
197 SDA_INPUT; /* And set to input */
198 SCL_HI;
199 if ( SDA )
200 byte |= i;
201 SCL_LO;
202 SDA_OUTPUT;
205 i2c_ack(ack);
207 return byte;
210 int i2c_write(int address, const unsigned char* buf, int count )
212 int i,x=0;
214 i2c_start();
215 i2c_outb(address & 0xfe);
216 if (i2c_getack())
218 for (i=0; i<count; i++)
220 i2c_outb(buf[i]);
221 if (!i2c_getack())
223 x=-2;
224 break;
228 else
230 debugf("i2c_write() - no ack\n");
231 x=-1;
233 i2c_stop();
234 return x;
237 int i2c_read(int address, unsigned char* buf, int count )
239 int i,x=0;
241 i2c_start();
242 i2c_outb(address | 1);
243 if (i2c_getack()) {
244 for (i=0; i<count; i++) {
245 buf[i] = i2c_inb(0);
248 else
249 x=-1;
250 i2c_stop();
251 return x;