Prevent file descriptor leakage or overuse
[Rockbox.git] / firmware / drivers / fmradio_i2c.c
blob9f890ca34a588dc096565abe66ff94a4106130a5
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
20 #include "config.h"
21 #include "cpu.h"
22 #include "kernel.h"
23 #include "thread.h"
24 #include "logf.h"
25 #include "system.h"
26 #include "string.h"
28 #if (CONFIG_TUNER & TEA5767)
29 #if (CONFIG_I2C == I2C_COLDFIRE)
31 #ifdef IAUDIO_X5
32 #include "i2c-coldfire.h"
34 int fmradio_i2c_write(unsigned char address, const unsigned char* buf,
35 int count)
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);
44 #else
46 /* cute little functions, atomic read-modify-write */
47 /* SDA is GPIO1,23 */
49 #ifdef IRIVER_H300_SERIES
51 /* SDA is GPIO57 */
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)
57 /* SCL is GPIO56 */
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)
64 #else
66 /* SDA is GPIO55 */
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)
72 /* SCL is GPIO3 */
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)
78 #endif
80 /* delay loop to achieve 400kHz at 120MHz CPU frequency */
81 #define DELAY \
82 ({ \
83 int _x_; \
84 asm volatile ( \
85 "move.l #21, %[_x_] \r\n" \
86 "1: \r\n" \
87 "subq.l #1, %[_x_] \r\n" \
88 "bhi.b 1b \r\n" \
89 : [_x_]"=&d"(_x_) \
90 ); \
93 static void fmradio_i2c_start(void)
95 SCL_HI;
96 SCL_OUTPUT;
97 SDA_HI_IN;
98 SDA_OUTINIT;
99 DELAY;
100 SDA_LO_OUT;
101 DELAY;
102 SCL_LO;
105 static void fmradio_i2c_stop(void)
107 SDA_LO_OUT;
108 DELAY;
109 SCL_HI;
110 DELAY;
111 SDA_HI_IN;
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. */
125 if (nack)
126 SDA_HI_IN;
127 else
128 SDA_LO_OUT;
129 DELAY;
131 SCL_HI;
134 SCL_OUTPUT; /* Set the clock to output */
135 SCL_INPUT; /* Set the clock to input */
136 DELAY;
138 while(!SCL); /* and wait for the slave to release it */
140 SCL_OUTPUT;
141 SCL_LO;
144 static int fmradio_i2c_getack(void)
146 int ret = 1;
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. */
156 SDA_HI_IN;
157 DELAY;
159 SCL_HI; /* set clock to high */
162 SCL_OUTPUT; /* Set the clock to output */
163 SCL_INPUT; /* Set the clock to input */
164 DELAY;
166 while(!SCL); /* and wait for the slave to release it */
168 if (SDA)
169 ret = 0; /* ack failed */
171 SCL_OUTPUT;
172 SCL_LO;
174 return ret;
177 static void fmradio_i2c_outb(unsigned char byte)
179 int i;
181 /* clock out each bit, MSB first */
182 for ( i=0x80; i; i>>=1 ) {
183 if ( i & byte )
184 SDA_HI_IN;
185 else
186 SDA_LO_OUT;
187 DELAY;
188 SCL_HI;
189 DELAY;
190 SCL_LO;
194 static unsigned char fmradio_i2c_inb(void)
196 int i;
197 unsigned char byte = 0;
199 SDA_HI_IN;
200 /* clock in each bit, MSB first */
201 for ( i=0x80; i; i>>=1 ) {
202 DELAY;
203 SCL_HI;
204 DELAY;
205 if ( SDA )
206 byte |= i;
207 SCL_LO;
210 return byte;
213 int fmradio_i2c_write(int address, const unsigned char* buf, int count)
215 int i,x=0;
217 fmradio_i2c_start();
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())
226 x=-2;
227 break;
231 else
233 logf("fmradio_i2c_write() - no ack\n");
234 x=-1;
236 fmradio_i2c_stop();
237 return x;
240 int fmradio_i2c_read(int address, unsigned char* buf, int count)
242 int i,x=0;
244 fmradio_i2c_start();
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);
255 else
256 x=-1;
257 fmradio_i2c_stop();
258 return x;
260 #endif /* ! IAUDIO_X5 */
261 #else
262 /* cute little functions, atomic read-modify-write */
263 /* SDA is PB4 */
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)
270 /* SCL is PB1 */
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)
282 SDA_OUTPUT;
283 SDA_HI;
284 SCL_HI;
285 SDA_LO;
286 DELAY;
287 SCL_LO;
290 static void fmradio_i2c_stop(void)
292 SDA_LO;
293 SCL_HI;
294 DELAY;
295 SDA_HI;
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 */
308 if (nack)
309 SDA_HI;
310 else
311 SDA_LO;
313 SCL_INPUT; /* Set the clock to input */
314 while(!SCL) /* and wait for the slave to release it */
315 sleep_thread(0);
317 DELAY;
318 SCL_OUTPUT;
319 SCL_LO;
322 static int fmradio_i2c_getack(void)
324 int ret = 1;
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 */
334 sleep_thread(0);
336 if (SDA)
337 /* ack failed */
338 ret = 0;
340 SCL_OUTPUT;
341 SCL_LO;
342 SDA_HI;
343 SDA_OUTPUT;
344 return ret;
347 static void fmradio_i2c_outb(unsigned char byte)
349 int i;
351 /* clock out each bit, MSB first */
352 for ( i=0x80; i; i>>=1 ) {
353 if ( i & byte )
355 SDA_HI;
357 else
359 SDA_LO;
361 SCL_HI;
362 SCL_LO;
365 SDA_HI;
368 static unsigned char fmradio_i2c_inb(void)
370 int i;
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 */
376 SCL_HI;
377 if ( SDA )
378 byte |= i;
379 SCL_LO;
380 SDA_OUTPUT;
383 return byte;
386 int fmradio_i2c_write(int address, const unsigned char* buf, int count)
388 int i,x=0;
390 fmradio_i2c_start();
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())
399 x=-2;
400 break;
404 else
406 logf("fmradio_i2c_write() - no ack\n");
407 x=-1;
409 fmradio_i2c_stop();
410 return x;
413 int fmradio_i2c_read(int address, unsigned char* buf, int count)
415 int i,x=0;
417 fmradio_i2c_start();
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);
426 else
427 x=-1;
428 fmradio_i2c_stop();
429 return x;
432 #endif
433 #endif