blob: a101fb1fa9eb2cc39065de50ba643f56699d18e8 [file] [log] [blame]
Barry Wardell0d8111c2007-11-10 19:14:01 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
Michael Sevakised8445f2008-03-20 15:20:06 +00008 * $Id$
Barry Wardell0d8111c2007-11-10 19:14:01 +00009 * Physical interface of the Philips TEA5767 in Archos Ondio
10 *
11 * Copyright (C) 2002 by Linus Nielsen Feltzing
12 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000013 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
Barry Wardell0d8111c2007-11-10 19:14:01 +000017 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ****************************************************************************/
22#include "config.h"
23#include "cpu.h"
24#include "logf.h"
25#include "system.h"
26
27#if (CONFIG_TUNER & TEA5767)
28
29/* cute little functions, atomic read-modify-write */
30/* SDA is PB4 */
31#define SDA_LO and_b(~0x10, &PBDRL)
32#define SDA_HI or_b(0x10, &PBDRL)
33#define SDA_INPUT and_b(~0x10, &PBIORL)
34#define SDA_OUTPUT or_b(0x10, &PBIORL)
35#define SDA (PBDR & 0x0010)
36
37/* SCL is PB1 */
38#define SCL_INPUT and_b(~0x02, &PBIORL)
39#define SCL_OUTPUT or_b(0x02, &PBIORL)
40#define SCL_LO and_b(~0x02, &PBDRL)
41#define SCL_HI or_b(0x02, &PBDRL)
42#define SCL (PBDR & 0x0002)
43
44/* arbitrary delay loop */
45#define DELAY do { int _x; for(_x=0;_x<20;_x++);} while (0)
46
47static void fmradio_i2c_start(void)
48{
49 SDA_OUTPUT;
50 SDA_HI;
51 SCL_HI;
52 SDA_LO;
53 DELAY;
54 SCL_LO;
55}
56
57static void fmradio_i2c_stop(void)
58{
59 SDA_LO;
60 SCL_HI;
61 DELAY;
62 SDA_HI;
63}
64
65
66static void fmradio_i2c_ack(bool nack)
67{
68 /* Here's the deal. The slave is slow, and sometimes needs to wait
69 before it can receive the acknowledge. Therefore it forces the clock
70 low until it is ready. We need to poll the clock line until it goes
71 high before we release the ack. */
72
73 SCL_LO; /* Set the clock low */
74
75 if (nack)
76 SDA_HI;
77 else
78 SDA_LO;
79
80 SCL_INPUT; /* Set the clock to input */
81 while(!SCL) /* and wait for the slave to release it */
Michael Sevakised8445f2008-03-20 15:20:06 +000082 sleep(0);
Barry Wardell0d8111c2007-11-10 19:14:01 +000083
84 DELAY;
85 SCL_OUTPUT;
86 SCL_LO;
87}
88
89static int fmradio_i2c_getack(void)
90{
91 int ret = 1;
92
93 /* Here's the deal. The slave is slow, and sometimes needs to wait
94 before it can send the acknowledge. Therefore it forces the clock
95 low until it is ready. We need to poll the clock line until it goes
96 high before we read the ack. */
97
98 SDA_INPUT; /* And set to input */
99 SCL_INPUT; /* Set the clock to input */
100 while(!SCL) /* and wait for the slave to release it */
Michael Sevakised8445f2008-03-20 15:20:06 +0000101 sleep(0);
Barry Wardell0d8111c2007-11-10 19:14:01 +0000102
103 if (SDA)
104 /* ack failed */
105 ret = 0;
106
107 SCL_OUTPUT;
108 SCL_LO;
109 SDA_HI;
110 SDA_OUTPUT;
111 return ret;
112}
113
114static void fmradio_i2c_outb(unsigned char byte)
115{
116 int i;
117
118 /* clock out each bit, MSB first */
119 for ( i=0x80; i; i>>=1 ) {
120 if ( i & byte )
121 {
122 SDA_HI;
123 }
124 else
125 {
126 SDA_LO;
127 }
128 SCL_HI;
129 SCL_LO;
130 }
131
132 SDA_HI;
133}
134
135static unsigned char fmradio_i2c_inb(void)
136{
137 int i;
138 unsigned char byte = 0;
139
140 /* clock in each bit, MSB first */
141 for ( i=0x80; i; i>>=1 ) {
142 SDA_INPUT; /* And set to input */
143 SCL_HI;
144 if ( SDA )
145 byte |= i;
146 SCL_LO;
147 SDA_OUTPUT;
148 }
149
150 return byte;
151}
152
153int fmradio_i2c_write(int address, const unsigned char* buf, int count)
154{
155 int i,x=0;
156
157 fmradio_i2c_start();
158 fmradio_i2c_outb(address & 0xfe);
159 if (fmradio_i2c_getack())
160 {
161 for (i=0; i<count; i++)
162 {
163 fmradio_i2c_outb(buf[i]);
164 if (!fmradio_i2c_getack())
165 {
166 x=-2;
167 break;
168 }
169 }
170 }
171 else
172 {
173 logf("fmradio_i2c_write() - no ack\n");
174 x=-1;
175 }
176 fmradio_i2c_stop();
177 return x;
178}
179
180int fmradio_i2c_read(int address, unsigned char* buf, int count)
181{
182 int i,x=0;
183
184 fmradio_i2c_start();
185 fmradio_i2c_outb(address | 1);
186 if (fmradio_i2c_getack()) {
187 for (i=count; i>0; i--)
188 {
189 *buf++ = fmradio_i2c_inb();
190 fmradio_i2c_ack(i == 1);
191 }
192 }
193 else
194 x=-1;
195 fmradio_i2c_stop();
196 return x;
197}
198
199#endif