summaryrefslogtreecommitdiff
path: root/firmware/target/arm/iriver/h10/fmradio_i2c-h10.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/target/arm/iriver/h10/fmradio_i2c-h10.c')
-rw-r--r--firmware/target/arm/iriver/h10/fmradio_i2c-h10.c206
1 files changed, 206 insertions, 0 deletions
diff --git a/firmware/target/arm/iriver/h10/fmradio_i2c-h10.c b/firmware/target/arm/iriver/h10/fmradio_i2c-h10.c
new file mode 100644
index 0000000000..f3d3075df9
--- /dev/null
+++ b/firmware/target/arm/iriver/h10/fmradio_i2c-h10.c
@@ -0,0 +1,206 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 * Physical interface of the Philips TEA5767 in iriver H10 series
10 *
11 * Copyright (C) 2002 by Linus Nielsen Feltzing
12 *
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.
15 *
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
18 *
19 ****************************************************************************/
20#include "config.h"
21#include "cpu.h"
22#include "logf.h"
23#include "system.h"
24
25/* cute little functions, atomic read-modify-write */
26
27#define SDA_OUTINIT GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_VAL, 0x08)
28#define SDA_HI_IN GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_EN, 0x08)
29#define SDA_LO_OUT GPIO_SET_BITWISE(GPIOD_OUTPUT_EN, 0x08)
30#define SDA (GPIOD_INPUT_VAL & 0x08)
31
32#define SCL_INPUT GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_EN, 0x10)
33#define SCL_OUTPUT GPIO_SET_BITWISE(GPIOD_OUTPUT_EN, 0x10)
34#define SCL_LO GPIO_CLEAR_BITWISE(GPIOD_OUTPUT_VAL, 0x10)
35#define SCL_HI GPIO_SET_BITWISE(GPIOD_OUTPUT_VAL,0x10)
36#define SCL (GPIOD_INPUT_VAL & 0x10)
37
38#define DELAY udelay(2)
39
40static void fmradio_i2c_start(void)
41{
42 SCL_HI;
43 SCL_OUTPUT;
44 SDA_HI_IN;
45 SDA_OUTINIT;
46 DELAY;
47 SDA_LO_OUT;
48 DELAY;
49 SCL_LO;
50}
51
52static void fmradio_i2c_stop(void)
53{
54 SDA_LO_OUT;
55 DELAY;
56 SCL_HI;
57 DELAY;
58 SDA_HI_IN;
59}
60
61/* Generate ACK or NACK */
62static void fmradio_i2c_ack(bool nack)
63{
64 /* Here's the deal. The slave is slow, and sometimes needs to wait
65 before it can receive the acknowledge. Therefore it forces the clock
66 low until it is ready. We need to poll the clock line until it goes
67 high before we release the ack.
68
69 In their infinite wisdom, iriver didn't pull up the SCL line, so
70 we have to drive the SCL high repeatedly to simulate a pullup. */
71
72 if (nack)
73 SDA_HI_IN;
74 else
75 SDA_LO_OUT;
76 DELAY;
77
78 SCL_HI;
79 do
80 {
81 SCL_OUTPUT; /* Set the clock to output */
82 SCL_INPUT; /* Set the clock to input */
83 DELAY;
84 }
85 while(!SCL); /* and wait for the slave to release it */
86
87 SCL_OUTPUT;
88 SCL_LO;
89}
90
91static int fmradio_i2c_getack(void)
92{
93 int ret = 1;
94
95 /* Here's the deal. The slave is slow, and sometimes needs to wait
96 before it can send the acknowledge. Therefore it forces the clock
97 low until it is ready. We need to poll the clock line until it goes
98 high before we read the ack.
99
100 In their infinite wisdom, iriver didn't pull up the SCL line, so
101 we have to drive the SCL high repeatedly to simulate a pullup. */
102
103 SDA_HI_IN;
104 DELAY;
105
106 SCL_HI; /* set clock to high */
107 do
108 {
109 SCL_OUTPUT; /* Set the clock to output */
110 SCL_INPUT; /* Set the clock to input */
111 DELAY;
112 }
113 while(!SCL); /* and wait for the slave to release it */
114
115 if (SDA)
116 ret = 0; /* ack failed */
117
118 SCL_OUTPUT;
119 SCL_LO;
120
121 return ret;
122}
123
124static void fmradio_i2c_outb(unsigned char byte)
125{
126 int i;
127
128 /* clock out each bit, MSB first */
129 for ( i=0x80; i; i>>=1 ) {
130 if ( i & byte )
131 SDA_HI_IN;
132 else
133 SDA_LO_OUT;
134 DELAY;
135 SCL_HI;
136 DELAY;
137 SCL_LO;
138 }
139}
140
141static unsigned char fmradio_i2c_inb(void)
142{
143 int i;
144 unsigned char byte = 0;
145
146 SDA_HI_IN;
147 /* clock in each bit, MSB first */
148 for ( i=0x80; i; i>>=1 ) {
149 DELAY;
150 SCL_HI;
151 DELAY;
152 if ( SDA )
153 byte |= i;
154 SCL_LO;
155 }
156
157 return byte;
158}
159
160int fmradio_i2c_write(int address, const unsigned char* buf, int count)
161{
162 int i,x=0;
163
164 fmradio_i2c_start();
165 fmradio_i2c_outb(address & 0xfe);
166 if (fmradio_i2c_getack())
167 {
168 for (i=0; i<count; i++)
169 {
170 fmradio_i2c_outb(buf[i]);
171 if (!fmradio_i2c_getack())
172 {
173 x=-2;
174 break;
175 }
176 }
177 }
178 else
179 {
180 logf("fmradio_i2c_write() - no ack\n");
181 x=-1;
182 }
183 fmradio_i2c_stop();
184 return x;
185}
186
187int fmradio_i2c_read(int address, unsigned char* buf, int count)
188{
189 int i,x=0;
190
191 fmradio_i2c_start();
192 fmradio_i2c_outb(address | 1);
193
194 if (fmradio_i2c_getack())
195 {
196 for (i=count; i>0; i--)
197 {
198 *buf++ = fmradio_i2c_inb();
199 fmradio_i2c_ack(i == 1);
200 }
201 }
202 else
203 x=-1;
204 fmradio_i2c_stop();
205 return x;
206}