1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
9 * Physical interface of the Philips TEA5767 in Archos Ondio
11 * Copyright (C) 2002 by Linus Nielsen Feltzing
13 * All files in this archive are subject to the GNU General Public License.
14 * See the file COPYING in the source tree root for full license agreement.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 ****************************************************************************/
28 #if (CONFIG_TUNER & TEA5767)
29 #if (CONFIG_I2C == I2C_COLDFIRE)
32 #include "i2c-coldfire.h"
34 int fmradio_i2c_write(unsigned char address
, const unsigned char* buf
,
37 return i2c_write(I2C_IFACE_0
, address
, buf
, count
);
40 int fmradio_i2c_read(unsigned char address
, unsigned char* buf
, int count
)
42 return i2c_read(I2C_IFACE_0
, address
, buf
, count
);
46 /* cute little functions, atomic read-modify-write */
49 #ifdef IRIVER_H300_SERIES
52 #define SDA_OUTINIT and_l(~0x02000000, &GPIO1_OUT)
53 #define SDA_HI_IN and_l(~0x02000000, &GPIO1_ENABLE)
54 #define SDA_LO_OUT or_l( 0x02000000, &GPIO1_ENABLE)
55 #define SDA ( 0x02000000 & GPIO1_READ)
58 #define SCL_INPUT and_l(~0x01000000, &GPIO1_ENABLE)
59 #define SCL_OUTPUT or_l( 0x01000000, &GPIO1_ENABLE)
60 #define SCL_LO and_l(~0x01000000, &GPIO1_OUT)
61 #define SCL_HI or_l( 0x01000000, &GPIO1_OUT)
62 #define SCL ( 0x01000000 & GPIO1_READ)
67 #define SDA_OUTINIT and_l(~0x00800000, &GPIO1_OUT)
68 #define SDA_HI_IN and_l(~0x00800000, &GPIO1_ENABLE)
69 #define SDA_LO_OUT or_l( 0x00800000, &GPIO1_ENABLE)
70 #define SDA ( 0x00800000 & GPIO1_READ)
73 #define SCL_INPUT and_l(~0x00000008, &GPIO_ENABLE)
74 #define SCL_OUTPUT or_l( 0x00000008, &GPIO_ENABLE)
75 #define SCL_LO and_l(~0x00000008, &GPIO_OUT)
76 #define SCL_HI or_l( 0x00000008, &GPIO_OUT)
77 #define SCL ( 0x00000008 & GPIO_READ)
80 /* delay loop to achieve 400kHz at 120MHz CPU frequency */
85 "move.l #21, %[_x_] \r\n" \
87 "subq.l #1, %[_x_] \r\n" \
93 static void fmradio_i2c_start(void)
105 static void fmradio_i2c_stop(void)
114 /* Generate ACK or NACK */
115 static void fmradio_i2c_ack(bool nack
)
117 /* Here's the deal. The slave is slow, and sometimes needs to wait
118 before it can receive the acknowledge. Therefore it forces the clock
119 low until it is ready. We need to poll the clock line until it goes
120 high before we release the ack.
122 In their infinite wisdom, iriver didn't pull up the SCL line, so
123 we have to drive the SCL high repeatedly to simulate a pullup. */
134 SCL_OUTPUT
; /* Set the clock to output */
135 SCL_INPUT
; /* Set the clock to input */
138 while(!SCL
); /* and wait for the slave to release it */
144 static int fmradio_i2c_getack(void)
148 /* Here's the deal. The slave is slow, and sometimes needs to wait
149 before it can send the acknowledge. Therefore it forces the clock
150 low until it is ready. We need to poll the clock line until it goes
151 high before we read the ack.
153 In their infinite wisdom, iriver didn't pull up the SCL line, so
154 we have to drive the SCL high repeatedly to simulate a pullup. */
159 SCL_HI
; /* set clock to high */
162 SCL_OUTPUT
; /* Set the clock to output */
163 SCL_INPUT
; /* Set the clock to input */
166 while(!SCL
); /* and wait for the slave to release it */
169 ret
= 0; /* ack failed */
177 static void fmradio_i2c_outb(unsigned char byte
)
181 /* clock out each bit, MSB first */
182 for ( i
=0x80; i
; i
>>=1 ) {
194 static unsigned char fmradio_i2c_inb(void)
197 unsigned char byte
= 0;
200 /* clock in each bit, MSB first */
201 for ( i
=0x80; i
; i
>>=1 ) {
213 int fmradio_i2c_write(int address
, const unsigned char* buf
, int count
)
218 fmradio_i2c_outb(address
& 0xfe);
219 if (fmradio_i2c_getack())
221 for (i
=0; i
<count
; i
++)
223 fmradio_i2c_outb(buf
[i
]);
224 if (!fmradio_i2c_getack())
233 logf("fmradio_i2c_write() - no ack\n");
240 int fmradio_i2c_read(int address
, unsigned char* buf
, int count
)
245 fmradio_i2c_outb(address
| 1);
247 if (fmradio_i2c_getack())
249 for (i
=count
; i
>0; i
--)
251 *buf
++ = fmradio_i2c_inb();
252 fmradio_i2c_ack(i
== 1);
260 #endif /* ! IAUDIO_X5 */
262 /* cute little functions, atomic read-modify-write */
264 #define SDA_LO and_b(~0x10, &PBDRL)
265 #define SDA_HI or_b(0x10, &PBDRL)
266 #define SDA_INPUT and_b(~0x10, &PBIORL)
267 #define SDA_OUTPUT or_b(0x10, &PBIORL)
268 #define SDA (PBDR & 0x0010)
271 #define SCL_INPUT and_b(~0x02, &PBIORL)
272 #define SCL_OUTPUT or_b(0x02, &PBIORL)
273 #define SCL_LO and_b(~0x02, &PBDRL)
274 #define SCL_HI or_b(0x02, &PBDRL)
275 #define SCL (PBDR & 0x0002)
277 /* arbitrary delay loop */
278 #define DELAY do { int _x; for(_x=0;_x<20;_x++);} while (0)
280 static void fmradio_i2c_start(void)
290 static void fmradio_i2c_stop(void)
299 static void fmradio_i2c_ack(bool nack
)
301 /* Here's the deal. The slave is slow, and sometimes needs to wait
302 before it can receive the acknowledge. Therefore it forces the clock
303 low until it is ready. We need to poll the clock line until it goes
304 high before we release the ack. */
306 SCL_LO
; /* Set the clock low */
313 SCL_INPUT
; /* Set the clock to input */
314 while(!SCL
) /* and wait for the slave to release it */
322 static int fmradio_i2c_getack(void)
326 /* Here's the deal. The slave is slow, and sometimes needs to wait
327 before it can send the acknowledge. Therefore it forces the clock
328 low until it is ready. We need to poll the clock line until it goes
329 high before we read the ack. */
331 SDA_INPUT
; /* And set to input */
332 SCL_INPUT
; /* Set the clock to input */
333 while(!SCL
) /* and wait for the slave to release it */
347 static void fmradio_i2c_outb(unsigned char byte
)
351 /* clock out each bit, MSB first */
352 for ( i
=0x80; i
; i
>>=1 ) {
368 static unsigned char fmradio_i2c_inb(void)
371 unsigned char byte
= 0;
373 /* clock in each bit, MSB first */
374 for ( i
=0x80; i
; i
>>=1 ) {
375 SDA_INPUT
; /* And set to input */
386 int fmradio_i2c_write(int address
, const unsigned char* buf
, int count
)
391 fmradio_i2c_outb(address
& 0xfe);
392 if (fmradio_i2c_getack())
394 for (i
=0; i
<count
; i
++)
396 fmradio_i2c_outb(buf
[i
]);
397 if (!fmradio_i2c_getack())
406 logf("fmradio_i2c_write() - no ack\n");
413 int fmradio_i2c_read(int address
, unsigned char* buf
, int count
)
418 fmradio_i2c_outb(address
| 1);
419 if (fmradio_i2c_getack()) {
420 for (i
=count
; i
>0; i
--)
422 *buf
++ = fmradio_i2c_inb();
423 fmradio_i2c_ack(i
== 1);