2004-10-15 21:41:46 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
2007-11-10 19:14:01 +00:00
|
|
|
* $Id:$
|
|
|
|
* Physical interface of the Philips TEA5767 in iriver H100/H300 series
|
2004-10-15 21:41:46 +00:00
|
|
|
*
|
2004-10-17 23:24:18 +00:00
|
|
|
* Copyright (C) 2002 by Linus Nielsen Feltzing
|
2004-10-15 21:41:46 +00:00
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2005-08-13 17:32:15 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "logf.h"
|
2004-10-15 21:41:46 +00:00
|
|
|
#include "system.h"
|
|
|
|
|
2005-08-13 18:13:56 +00:00
|
|
|
#if (CONFIG_TUNER & TEA5767)
|
2006-07-21 08:42:28 +00:00
|
|
|
|
2005-08-13 17:32:15 +00:00
|
|
|
/* cute little functions, atomic read-modify-write */
|
|
|
|
/* SDA is GPIO1,23 */
|
|
|
|
|
2005-12-15 01:11:19 +00:00
|
|
|
#ifdef IRIVER_H300_SERIES
|
|
|
|
|
|
|
|
/* SDA is GPIO57 */
|
2007-10-15 16:45:46 +00:00
|
|
|
#define SDA_OUTINIT and_l(~0x02000000, &GPIO1_OUT)
|
|
|
|
#define SDA_HI_IN and_l(~0x02000000, &GPIO1_ENABLE)
|
|
|
|
#define SDA_LO_OUT or_l( 0x02000000, &GPIO1_ENABLE)
|
|
|
|
#define SDA ( 0x02000000 & GPIO1_READ)
|
2005-12-15 01:11:19 +00:00
|
|
|
|
|
|
|
/* SCL is GPIO56 */
|
|
|
|
#define SCL_INPUT and_l(~0x01000000, &GPIO1_ENABLE)
|
|
|
|
#define SCL_OUTPUT or_l( 0x01000000, &GPIO1_ENABLE)
|
|
|
|
#define SCL_LO and_l(~0x01000000, &GPIO1_OUT)
|
|
|
|
#define SCL_HI or_l( 0x01000000, &GPIO1_OUT)
|
|
|
|
#define SCL ( 0x01000000 & GPIO1_READ)
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
/* SDA is GPIO55 */
|
2007-10-15 16:45:46 +00:00
|
|
|
#define SDA_OUTINIT and_l(~0x00800000, &GPIO1_OUT)
|
|
|
|
#define SDA_HI_IN and_l(~0x00800000, &GPIO1_ENABLE)
|
|
|
|
#define SDA_LO_OUT or_l( 0x00800000, &GPIO1_ENABLE)
|
|
|
|
#define SDA ( 0x00800000 & GPIO1_READ)
|
2005-12-15 01:11:19 +00:00
|
|
|
|
|
|
|
/* SCL is GPIO3 */
|
|
|
|
#define SCL_INPUT and_l(~0x00000008, &GPIO_ENABLE)
|
|
|
|
#define SCL_OUTPUT or_l( 0x00000008, &GPIO_ENABLE)
|
|
|
|
#define SCL_LO and_l(~0x00000008, &GPIO_OUT)
|
|
|
|
#define SCL_HI or_l( 0x00000008, &GPIO_OUT)
|
|
|
|
#define SCL ( 0x00000008 & GPIO_READ)
|
|
|
|
#endif
|
2005-08-13 17:32:15 +00:00
|
|
|
|
|
|
|
/* delay loop to achieve 400kHz at 120MHz CPU frequency */
|
2007-06-27 03:11:49 +00:00
|
|
|
#define DELAY \
|
|
|
|
({ \
|
|
|
|
int _x_; \
|
|
|
|
asm volatile ( \
|
|
|
|
"move.l #21, %[_x_] \r\n" \
|
|
|
|
"1: \r\n" \
|
|
|
|
"subq.l #1, %[_x_] \r\n" \
|
|
|
|
"bhi.b 1b \r\n" \
|
|
|
|
: [_x_]"=&d"(_x_) \
|
|
|
|
); \
|
|
|
|
})
|
2005-08-13 17:32:15 +00:00
|
|
|
|
|
|
|
static void fmradio_i2c_start(void)
|
|
|
|
{
|
|
|
|
SCL_HI;
|
2007-10-15 16:45:46 +00:00
|
|
|
SCL_OUTPUT;
|
|
|
|
SDA_HI_IN;
|
|
|
|
SDA_OUTINIT;
|
2005-08-13 17:32:15 +00:00
|
|
|
DELAY;
|
2007-10-15 16:45:46 +00:00
|
|
|
SDA_LO_OUT;
|
2005-08-13 17:32:15 +00:00
|
|
|
DELAY;
|
|
|
|
SCL_LO;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fmradio_i2c_stop(void)
|
|
|
|
{
|
2007-10-15 16:45:46 +00:00
|
|
|
SDA_LO_OUT;
|
|
|
|
DELAY;
|
2005-08-13 17:32:15 +00:00
|
|
|
SCL_HI;
|
|
|
|
DELAY;
|
2007-10-15 16:45:46 +00:00
|
|
|
SDA_HI_IN;
|
2005-08-13 17:32:15 +00:00
|
|
|
}
|
|
|
|
|
2007-06-27 03:11:49 +00:00
|
|
|
/* Generate ACK or NACK */
|
|
|
|
static void fmradio_i2c_ack(bool nack)
|
2005-08-13 17:32:15 +00:00
|
|
|
{
|
|
|
|
/* Here's the deal. The slave is slow, and sometimes needs to wait
|
|
|
|
before it can receive the acknowledge. Therefore it forces the clock
|
|
|
|
low until it is ready. We need to poll the clock line until it goes
|
|
|
|
high before we release the ack.
|
|
|
|
|
|
|
|
In their infinite wisdom, iriver didn't pull up the SCL line, so
|
|
|
|
we have to drive the SCL high repeatedly to simulate a pullup. */
|
|
|
|
|
2007-06-27 03:11:49 +00:00
|
|
|
if (nack)
|
2007-10-15 16:45:46 +00:00
|
|
|
SDA_HI_IN;
|
2007-06-27 03:11:49 +00:00
|
|
|
else
|
2007-10-15 16:45:46 +00:00
|
|
|
SDA_LO_OUT;
|
|
|
|
DELAY;
|
|
|
|
|
|
|
|
SCL_HI;
|
|
|
|
do
|
2005-08-13 17:32:15 +00:00
|
|
|
{
|
2005-12-15 01:11:19 +00:00
|
|
|
SCL_OUTPUT; /* Set the clock to output */
|
2005-08-13 17:32:15 +00:00
|
|
|
SCL_INPUT; /* Set the clock to input */
|
|
|
|
DELAY;
|
|
|
|
}
|
2007-10-15 16:45:46 +00:00
|
|
|
while(!SCL); /* and wait for the slave to release it */
|
2005-08-13 17:32:15 +00:00
|
|
|
|
|
|
|
SCL_OUTPUT;
|
|
|
|
SCL_LO;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int fmradio_i2c_getack(void)
|
|
|
|
{
|
|
|
|
int ret = 1;
|
2004-10-15 21:41:46 +00:00
|
|
|
|
2005-08-13 17:32:15 +00:00
|
|
|
/* Here's the deal. The slave is slow, and sometimes needs to wait
|
|
|
|
before it can send the acknowledge. Therefore it forces the clock
|
|
|
|
low until it is ready. We need to poll the clock line until it goes
|
|
|
|
high before we read the ack.
|
|
|
|
|
|
|
|
In their infinite wisdom, iriver didn't pull up the SCL line, so
|
|
|
|
we have to drive the SCL high repeatedly to simulate a pullup. */
|
|
|
|
|
2007-10-15 16:45:46 +00:00
|
|
|
SDA_HI_IN;
|
|
|
|
DELAY;
|
|
|
|
|
|
|
|
SCL_HI; /* set clock to high */
|
|
|
|
do
|
2005-08-13 17:32:15 +00:00
|
|
|
{
|
2005-12-15 01:11:19 +00:00
|
|
|
SCL_OUTPUT; /* Set the clock to output */
|
2005-08-13 17:32:15 +00:00
|
|
|
SCL_INPUT; /* Set the clock to input */
|
|
|
|
DELAY;
|
|
|
|
}
|
2007-10-15 16:45:46 +00:00
|
|
|
while(!SCL); /* and wait for the slave to release it */
|
2004-10-17 23:24:18 +00:00
|
|
|
|
2005-08-13 17:32:15 +00:00
|
|
|
if (SDA)
|
2007-10-15 16:45:46 +00:00
|
|
|
ret = 0; /* ack failed */
|
2005-08-13 17:32:15 +00:00
|
|
|
|
|
|
|
SCL_OUTPUT;
|
|
|
|
SCL_LO;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fmradio_i2c_outb(unsigned char byte)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* clock out each bit, MSB first */
|
|
|
|
for ( i=0x80; i; i>>=1 ) {
|
|
|
|
if ( i & byte )
|
2007-10-15 16:45:46 +00:00
|
|
|
SDA_HI_IN;
|
2005-08-13 17:32:15 +00:00
|
|
|
else
|
2007-10-15 16:45:46 +00:00
|
|
|
SDA_LO_OUT;
|
2005-08-13 17:32:15 +00:00
|
|
|
DELAY;
|
|
|
|
SCL_HI;
|
|
|
|
DELAY;
|
|
|
|
SCL_LO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned char fmradio_i2c_inb(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned char byte = 0;
|
|
|
|
|
2007-10-15 16:45:46 +00:00
|
|
|
SDA_HI_IN;
|
2005-08-13 17:32:15 +00:00
|
|
|
/* clock in each bit, MSB first */
|
|
|
|
for ( i=0x80; i; i>>=1 ) {
|
2005-12-15 01:11:19 +00:00
|
|
|
DELAY;
|
2007-10-15 16:45:46 +00:00
|
|
|
SCL_HI;
|
2005-08-13 17:32:15 +00:00
|
|
|
DELAY;
|
|
|
|
if ( SDA )
|
|
|
|
byte |= i;
|
|
|
|
SCL_LO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return byte;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fmradio_i2c_write(int address, const unsigned char* buf, int count)
|
|
|
|
{
|
|
|
|
int i,x=0;
|
|
|
|
|
|
|
|
fmradio_i2c_start();
|
|
|
|
fmradio_i2c_outb(address & 0xfe);
|
|
|
|
if (fmradio_i2c_getack())
|
|
|
|
{
|
|
|
|
for (i=0; i<count; i++)
|
|
|
|
{
|
|
|
|
fmradio_i2c_outb(buf[i]);
|
|
|
|
if (!fmradio_i2c_getack())
|
|
|
|
{
|
|
|
|
x=-2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logf("fmradio_i2c_write() - no ack\n");
|
|
|
|
x=-1;
|
|
|
|
}
|
|
|
|
fmradio_i2c_stop();
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fmradio_i2c_read(int address, unsigned char* buf, int count)
|
|
|
|
{
|
|
|
|
int i,x=0;
|
|
|
|
|
|
|
|
fmradio_i2c_start();
|
|
|
|
fmradio_i2c_outb(address | 1);
|
|
|
|
|
|
|
|
if (fmradio_i2c_getack())
|
|
|
|
{
|
2007-06-27 00:22:46 +00:00
|
|
|
for (i=count; i>0; i--)
|
2005-08-13 17:32:15 +00:00
|
|
|
{
|
2007-06-27 00:22:46 +00:00
|
|
|
*buf++ = fmradio_i2c_inb();
|
2007-06-27 03:11:49 +00:00
|
|
|
fmradio_i2c_ack(i == 1);
|
2005-08-13 17:32:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
x=-1;
|
|
|
|
fmradio_i2c_stop();
|
|
|
|
return x;
|
|
|
|
}
|
2004-10-17 23:24:18 +00:00
|
|
|
|
2005-08-13 17:32:15 +00:00
|
|
|
#endif
|