2005-11-07 23:07:19 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
2007-01-27 17:17:52 +00:00
|
|
|
* PP502X and PP5002 I2C driver
|
2005-12-19 14:30:52 +00:00
|
|
|
*
|
|
|
|
* Based on code from the ipodlinux project - http://ipodlinux.org/
|
|
|
|
* Adapted for Rockbox in November 2005
|
|
|
|
*
|
|
|
|
* Original file: linux/arch/armnommu/mach-ipod/hardware.c
|
|
|
|
*
|
|
|
|
* Copyright (c) 2003-2005 Bernard Leach (leachbj@bouncycastle.org)
|
2005-11-07 23:07:19 +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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "kernel.h"
|
2007-04-30 07:37:26 +00:00
|
|
|
#include "thread.h"
|
2005-11-07 23:07:19 +00:00
|
|
|
#include "logf.h"
|
|
|
|
#include "system.h"
|
2007-01-27 20:09:57 +00:00
|
|
|
#include "i2c-pp.h"
|
2007-10-25 09:03:47 +00:00
|
|
|
#include "as3514.h"
|
2005-11-07 23:07:19 +00:00
|
|
|
|
|
|
|
/* Local functions definitions */
|
2007-11-12 07:58:54 +00:00
|
|
|
static struct mutex i2c_mtx NOCACHEBSS_ATTR;
|
2005-11-07 23:07:19 +00:00
|
|
|
|
|
|
|
#define POLL_TIMEOUT (HZ)
|
|
|
|
|
2006-10-27 11:24:23 +00:00
|
|
|
static int pp_i2c_wait_not_busy(void)
|
2005-11-07 23:07:19 +00:00
|
|
|
{
|
2005-11-11 17:51:35 +00:00
|
|
|
unsigned long timeout;
|
2005-12-11 10:52:17 +00:00
|
|
|
timeout = current_tick + POLL_TIMEOUT;
|
|
|
|
while (TIME_BEFORE(current_tick, timeout)) {
|
2007-01-27 14:22:24 +00:00
|
|
|
if (!(I2C_STATUS & I2C_BUSY)) {
|
2005-11-07 23:07:19 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2007-04-30 07:37:26 +00:00
|
|
|
priority_yield();
|
2005-11-07 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
2005-12-11 10:52:17 +00:00
|
|
|
return -1;
|
2005-11-07 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-27 11:24:23 +00:00
|
|
|
static int pp_i2c_read_byte(unsigned int addr, unsigned int *data)
|
2005-11-07 23:07:19 +00:00
|
|
|
{
|
2006-10-27 11:24:23 +00:00
|
|
|
if (pp_i2c_wait_not_busy() < 0)
|
2005-12-11 10:52:17 +00:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2005-11-07 23:07:19 +00:00
|
|
|
|
2006-03-05 22:14:53 +00:00
|
|
|
{
|
2006-03-09 19:47:12 +00:00
|
|
|
unsigned int byte;
|
2006-03-05 22:14:53 +00:00
|
|
|
int old_irq_level = set_irq_level(HIGHEST_IRQ_LEVEL);
|
2005-11-07 23:07:19 +00:00
|
|
|
|
2006-03-05 22:14:53 +00:00
|
|
|
/* clear top 15 bits, left shift 1, or in 0x1 for a read */
|
2007-01-27 14:22:24 +00:00
|
|
|
I2C_ADDR = ((addr << 17) >> 16) | 0x1 ;
|
2005-11-07 23:07:19 +00:00
|
|
|
|
2007-01-27 14:22:24 +00:00
|
|
|
I2C_CTRL |= 0x20;
|
2005-11-07 23:07:19 +00:00
|
|
|
|
2007-01-27 14:22:24 +00:00
|
|
|
I2C_CTRL |= I2C_SEND;
|
2005-11-07 23:07:19 +00:00
|
|
|
|
2006-03-09 19:47:12 +00:00
|
|
|
set_irq_level(old_irq_level);
|
2006-10-27 11:24:23 +00:00
|
|
|
if (pp_i2c_wait_not_busy() < 0)
|
2006-03-05 22:14:53 +00:00
|
|
|
{
|
2006-03-09 19:47:12 +00:00
|
|
|
return -1;
|
2006-03-05 22:14:53 +00:00
|
|
|
}
|
2006-03-09 19:47:12 +00:00
|
|
|
old_irq_level = set_irq_level(HIGHEST_IRQ_LEVEL);
|
|
|
|
|
2007-01-27 14:22:24 +00:00
|
|
|
byte = I2C_DATA(0);
|
2006-03-09 19:47:12 +00:00
|
|
|
|
|
|
|
if (data)
|
|
|
|
*data = byte;
|
|
|
|
|
2006-03-08 22:59:41 +00:00
|
|
|
set_irq_level(old_irq_level);
|
2005-12-11 10:52:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2005-11-07 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-27 11:24:23 +00:00
|
|
|
static int pp_i2c_send_bytes(unsigned int addr, unsigned int len, unsigned char *data)
|
2005-11-07 23:07:19 +00:00
|
|
|
{
|
2005-12-11 10:52:17 +00:00
|
|
|
unsigned int i;
|
2005-11-07 23:07:19 +00:00
|
|
|
|
2005-12-11 10:52:17 +00:00
|
|
|
if (len < 1 || len > 4)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2005-11-07 23:07:19 +00:00
|
|
|
|
2006-10-27 11:24:23 +00:00
|
|
|
if (pp_i2c_wait_not_busy() < 0)
|
2005-12-11 10:52:17 +00:00
|
|
|
{
|
|
|
|
return -2;
|
|
|
|
}
|
2005-11-07 23:07:19 +00:00
|
|
|
|
2006-03-05 22:14:53 +00:00
|
|
|
{
|
|
|
|
int old_irq_level = set_irq_level(HIGHEST_IRQ_LEVEL);
|
2005-11-07 23:07:19 +00:00
|
|
|
|
2006-03-05 22:14:53 +00:00
|
|
|
/* clear top 15 bits, left shift 1 */
|
2007-01-27 14:22:24 +00:00
|
|
|
I2C_ADDR = (addr << 17) >> 16;
|
2005-11-07 23:07:19 +00:00
|
|
|
|
2007-01-27 14:22:24 +00:00
|
|
|
I2C_CTRL &= ~0x20;
|
2006-03-05 22:14:53 +00:00
|
|
|
|
|
|
|
for ( i = 0; i < len; i++ )
|
|
|
|
{
|
2007-01-27 14:22:24 +00:00
|
|
|
I2C_DATA(i) = *data++;
|
2006-03-05 22:14:53 +00:00
|
|
|
}
|
2005-11-07 23:07:19 +00:00
|
|
|
|
2007-01-27 14:22:24 +00:00
|
|
|
I2C_CTRL = (I2C_CTRL & ~0x26) | ((len-1) << 1);
|
2005-11-07 23:07:19 +00:00
|
|
|
|
2007-01-27 14:22:24 +00:00
|
|
|
I2C_CTRL |= I2C_SEND;
|
2006-03-05 22:14:53 +00:00
|
|
|
|
|
|
|
set_irq_level(old_irq_level);
|
|
|
|
}
|
2005-11-07 23:07:19 +00:00
|
|
|
|
2005-12-11 10:52:17 +00:00
|
|
|
return 0x0;
|
2005-11-07 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-27 11:24:23 +00:00
|
|
|
static int pp_i2c_send_byte(unsigned int addr, int data0)
|
2005-11-07 23:07:19 +00:00
|
|
|
{
|
2005-12-11 10:52:17 +00:00
|
|
|
unsigned char data[1];
|
2005-11-07 23:07:19 +00:00
|
|
|
|
2005-12-11 10:52:17 +00:00
|
|
|
data[0] = data0;
|
2005-11-07 23:07:19 +00:00
|
|
|
|
2006-10-27 11:24:23 +00:00
|
|
|
return pp_i2c_send_bytes(addr, 1, data);
|
2005-12-11 10:52:17 +00:00
|
|
|
}
|
2005-11-07 23:07:19 +00:00
|
|
|
|
2006-03-05 22:14:53 +00:00
|
|
|
/* Public functions */
|
2007-11-12 07:58:54 +00:00
|
|
|
void i2c_lock(void)
|
|
|
|
{
|
|
|
|
mutex_lock(&i2c_mtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void i2c_unlock(void)
|
|
|
|
{
|
|
|
|
mutex_unlock(&i2c_mtx);
|
|
|
|
}
|
2005-11-07 23:07:19 +00:00
|
|
|
|
2006-03-09 19:47:12 +00:00
|
|
|
int i2c_readbytes(unsigned int dev_addr, int addr, int len, unsigned char *data) {
|
|
|
|
unsigned int temp;
|
|
|
|
int i;
|
2007-11-12 07:58:54 +00:00
|
|
|
mutex_lock(&i2c_mtx);
|
2006-10-27 11:24:23 +00:00
|
|
|
pp_i2c_send_byte(dev_addr, addr);
|
2006-03-09 19:47:12 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
2006-10-27 11:24:23 +00:00
|
|
|
pp_i2c_read_byte(dev_addr, &temp);
|
2006-03-09 19:47:12 +00:00
|
|
|
data[i] = temp;
|
|
|
|
}
|
2007-11-12 07:58:54 +00:00
|
|
|
mutex_unlock(&i2c_mtx);
|
2006-03-09 19:47:12 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2005-12-11 10:52:17 +00:00
|
|
|
int i2c_readbyte(unsigned int dev_addr, int addr)
|
|
|
|
{
|
|
|
|
int data;
|
2005-11-07 23:07:19 +00:00
|
|
|
|
2007-11-12 07:58:54 +00:00
|
|
|
mutex_lock(&i2c_mtx);
|
2006-10-27 11:24:23 +00:00
|
|
|
pp_i2c_send_byte(dev_addr, addr);
|
|
|
|
pp_i2c_read_byte(dev_addr, &data);
|
2007-11-12 07:58:54 +00:00
|
|
|
mutex_unlock(&i2c_mtx);
|
2005-11-07 23:07:19 +00:00
|
|
|
|
2005-12-11 10:52:17 +00:00
|
|
|
return data;
|
2005-11-07 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
2006-10-27 11:24:23 +00:00
|
|
|
int pp_i2c_send(unsigned int addr, int data0, int data1)
|
2005-11-07 23:07:19 +00:00
|
|
|
{
|
2007-01-27 14:22:24 +00:00
|
|
|
int retval;
|
|
|
|
unsigned char data[2];
|
2005-11-07 23:07:19 +00:00
|
|
|
|
2007-01-27 14:22:24 +00:00
|
|
|
data[0] = data0;
|
|
|
|
data[1] = data1;
|
2005-11-07 23:07:19 +00:00
|
|
|
|
2007-11-12 07:58:54 +00:00
|
|
|
mutex_lock(&i2c_mtx);
|
2007-01-27 14:22:24 +00:00
|
|
|
retval = pp_i2c_send_bytes(addr, 2, data);
|
2007-11-12 07:58:54 +00:00
|
|
|
mutex_unlock(&i2c_mtx);
|
2006-03-05 22:14:53 +00:00
|
|
|
|
2007-01-27 14:22:24 +00:00
|
|
|
return retval;
|
2005-12-11 10:52:17 +00:00
|
|
|
}
|
2005-11-07 23:07:19 +00:00
|
|
|
|
2005-12-11 10:52:17 +00:00
|
|
|
void i2c_init(void)
|
2005-11-07 23:07:19 +00:00
|
|
|
{
|
2007-01-27 14:22:24 +00:00
|
|
|
/* From ipodlinux */
|
2008-03-12 10:03:52 +00:00
|
|
|
mutex_init(&i2c_mtx);
|
2005-11-07 23:07:19 +00:00
|
|
|
|
2006-04-08 09:08:38 +00:00
|
|
|
#ifdef IPOD_MINI
|
2007-01-27 14:22:24 +00:00
|
|
|
/* GPIO port C disable port 0x10 */
|
|
|
|
GPIOC_ENABLE &= ~0x10;
|
2006-02-27 12:35:05 +00:00
|
|
|
|
2007-01-27 14:22:24 +00:00
|
|
|
/* GPIO port C disable port 0x20 */
|
|
|
|
GPIOC_ENABLE &= ~0x20;
|
2006-02-27 12:35:05 +00:00
|
|
|
#endif
|
|
|
|
|
2007-01-27 17:17:52 +00:00
|
|
|
#if CONFIG_I2C == I2C_PP5002
|
|
|
|
DEV_EN |= 0x2;
|
|
|
|
#else
|
2007-01-27 15:20:30 +00:00
|
|
|
DEV_EN |= DEV_I2C; /* Enable I2C */
|
2007-01-27 17:17:52 +00:00
|
|
|
#endif
|
2007-01-27 15:20:30 +00:00
|
|
|
DEV_RS |= DEV_I2C; /* Start I2C Reset */
|
|
|
|
DEV_RS &=~DEV_I2C; /* End I2C Reset */
|
2007-01-27 14:22:24 +00:00
|
|
|
|
2007-01-27 17:17:52 +00:00
|
|
|
#if CONFIG_I2C == I2C_PP5020
|
2007-01-27 14:22:24 +00:00
|
|
|
outl(0x0, 0x600060a4);
|
|
|
|
outl(0x80 | (0 << 8), 0x600060a4);
|
2007-05-17 22:49:27 +00:00
|
|
|
#elif CONFIG_I2C == I2C_PP5024
|
2007-09-06 03:28:58 +00:00
|
|
|
#ifdef SANSA_E200
|
2007-05-17 22:49:27 +00:00
|
|
|
/* Sansa OF sets this to 0x20 first, communicates with the AS3514
|
|
|
|
then sets it to 0x23 - this still works fine though */
|
|
|
|
outl(0x0, 0x600060a4);
|
|
|
|
outl(0x23, 0x600060a4);
|
2007-09-06 03:28:58 +00:00
|
|
|
#elif defined(SANSA_C200)
|
|
|
|
/* This is the init sequence from the Sansa c200 bootloader.
|
|
|
|
I'm not sure what's really necessary. */
|
|
|
|
pp_i2c_wait_not_busy();
|
|
|
|
|
|
|
|
outl(0, 0x600060a4);
|
|
|
|
outl(0x64, 0x600060a4);
|
|
|
|
|
|
|
|
outl(0x55, 0x7000c02c);
|
|
|
|
outl(0x54, 0x7000c030);
|
|
|
|
|
|
|
|
outl(0, 0x600060a4);
|
|
|
|
outl(0x1e, 0x600060a4);
|
|
|
|
|
2007-10-25 09:03:47 +00:00
|
|
|
pp_i2c_send(AS3514_I2C_ADDR, SUPERVISOR, 5);
|
2007-09-06 03:28:58 +00:00
|
|
|
#endif
|
2007-01-27 17:17:52 +00:00
|
|
|
#endif
|
2005-11-07 23:07:19 +00:00
|
|
|
|
2007-01-27 14:22:24 +00:00
|
|
|
i2c_readbyte(0x8, 0);
|
2005-11-07 23:07:19 +00:00
|
|
|
}
|