2006-09-29 10:54:01 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
2007-06-26 02:11:30 +00:00
|
|
|
* Copyright (C) 2007 by Michael Sevakis
|
2006-09-29 10:54:01 +00:00
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2006-09-29 10:54:01 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include "system.h"
|
2007-06-26 02:11:30 +00:00
|
|
|
#include "i2c-meg-fx.h"
|
|
|
|
|
2008-03-30 05:38:28 +00:00
|
|
|
static struct wakeup i2c_wake; /* Transfer completion signal */
|
|
|
|
static struct mutex i2c_mtx; /* Mutual exclusion */
|
|
|
|
static unsigned char *buf_ptr; /* Next byte to transfer */
|
|
|
|
static int buf_count; /* Number of bytes remaining to transfer */
|
2006-09-29 10:54:01 +00:00
|
|
|
|
2007-06-26 02:11:30 +00:00
|
|
|
static void i2c_stop(void)
|
2006-09-29 10:54:01 +00:00
|
|
|
{
|
2007-06-26 02:11:30 +00:00
|
|
|
/* Generate STOP */
|
|
|
|
IICSTAT = I2C_MODE_MASTER | I2C_MODE_TX | I2C_RXTX_ENB;
|
2006-09-29 10:54:01 +00:00
|
|
|
|
2008-03-30 05:38:28 +00:00
|
|
|
/* No more interrupts, clear pending interrupt to continue */
|
|
|
|
IICCON &= ~(I2C_TXRX_INTPND | I2C_TXRX_INTENB);
|
2006-09-29 10:54:01 +00:00
|
|
|
}
|
|
|
|
|
2007-06-26 02:11:30 +00:00
|
|
|
void i2c_write(int addr, const unsigned char *buf, int count)
|
2006-09-29 10:54:01 +00:00
|
|
|
{
|
2008-03-30 05:38:28 +00:00
|
|
|
if (count <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mutex_lock(&i2c_mtx);
|
|
|
|
|
2007-06-26 02:11:30 +00:00
|
|
|
/* Turn on I2C clock */
|
2008-12-04 15:06:48 +00:00
|
|
|
s3c_regset32(&CLKCON, 1 << 16);
|
2006-09-29 10:54:01 +00:00
|
|
|
|
2007-06-26 02:11:30 +00:00
|
|
|
/* Set mode to master transmitter and enable lines */
|
|
|
|
IICSTAT = I2C_MODE_MASTER | I2C_MODE_TX | I2C_RXTX_ENB;
|
2006-09-29 10:54:01 +00:00
|
|
|
|
2008-03-30 05:38:28 +00:00
|
|
|
/* Set buffer start and count */
|
|
|
|
buf_ptr = (unsigned char *)buf;
|
|
|
|
buf_count = count;
|
2006-09-29 10:54:01 +00:00
|
|
|
|
2008-03-30 05:38:28 +00:00
|
|
|
/* Send slave address and then data */
|
|
|
|
SRCPND = IIC_MASK;
|
|
|
|
INTPND = IIC_MASK;
|
2006-09-29 10:54:01 +00:00
|
|
|
|
2008-03-30 05:38:28 +00:00
|
|
|
IICCON |= I2C_TXRX_INTENB;
|
2006-09-29 10:54:01 +00:00
|
|
|
|
2008-03-30 05:38:28 +00:00
|
|
|
/* Load slave address into shift register */
|
|
|
|
IICDS = addr & 0xfe;
|
|
|
|
|
|
|
|
/* Generate START */
|
|
|
|
IICSTAT = I2C_MODE_MASTER | I2C_MODE_TX | I2C_START | I2C_RXTX_ENB;
|
2007-01-04 03:31:03 +00:00
|
|
|
|
2008-04-01 03:55:02 +00:00
|
|
|
if (wakeup_wait(&i2c_wake, HZ) != OBJ_WAIT_SUCCEEDED)
|
2008-03-30 05:38:28 +00:00
|
|
|
{
|
|
|
|
/* Something went wrong - stop transmission */
|
|
|
|
int oldlevel = disable_irq_save();
|
|
|
|
i2c_stop();
|
|
|
|
restore_irq(oldlevel);
|
2007-06-26 02:11:30 +00:00
|
|
|
}
|
2007-01-20 22:16:01 +00:00
|
|
|
|
2007-06-26 02:11:30 +00:00
|
|
|
/* Go back to slave receive mode and disable lines */
|
|
|
|
IICSTAT = 0;
|
2007-01-04 03:31:03 +00:00
|
|
|
|
2007-06-26 02:11:30 +00:00
|
|
|
/* Turn off I2C clock */
|
2008-12-04 15:06:48 +00:00
|
|
|
s3c_regclr32(&CLKCON, 1 << 16);
|
2008-03-30 05:38:28 +00:00
|
|
|
|
|
|
|
mutex_unlock(&i2c_mtx);
|
2006-09-29 10:54:01 +00:00
|
|
|
}
|
|
|
|
|
2007-06-26 02:11:30 +00:00
|
|
|
void i2c_init(void)
|
|
|
|
{
|
2008-03-30 05:38:28 +00:00
|
|
|
/* Init kernel objects */
|
|
|
|
wakeup_init(&i2c_wake);
|
|
|
|
mutex_init(&i2c_mtx);
|
|
|
|
|
|
|
|
/* Clear pending source */
|
|
|
|
SRCPND = IIC_MASK;
|
|
|
|
INTPND = IIC_MASK;
|
|
|
|
|
|
|
|
/* Enable i2c interrupt in controller */
|
2008-12-04 15:06:48 +00:00
|
|
|
s3c_regclr32(&INTMOD, IIC_MASK);
|
|
|
|
s3c_regclr32(&INTMSK, IIC_MASK);
|
2007-01-20 22:16:01 +00:00
|
|
|
|
2007-06-26 02:11:30 +00:00
|
|
|
/* Turn on I2C clock */
|
2008-12-04 15:06:48 +00:00
|
|
|
s3c_regset32(&CLKCON, 1 << 16);
|
2007-01-20 22:16:01 +00:00
|
|
|
|
2007-06-26 02:11:30 +00:00
|
|
|
/* Set GPE15 (IICSDA) and GPE14 (IICSCL) to IIC */
|
|
|
|
GPECON = (GPECON & ~((3 << 30) | (3 << 28))) |
|
|
|
|
((2 << 30) | (2 << 28));
|
2006-09-29 10:54:01 +00:00
|
|
|
|
2007-06-26 02:11:30 +00:00
|
|
|
/* Bus ACK, IICCLK: fPCLK / 16, Rx/Tx Int: Disable, Tx clock: IICCLK/8 */
|
|
|
|
/* OF PCLK: 49.1568MHz / 16 / 8 = 384.0375 kHz */
|
|
|
|
IICCON = (7 << 0);
|
2006-09-29 10:54:01 +00:00
|
|
|
|
2007-06-26 02:11:30 +00:00
|
|
|
/* SDA line delayed 0 PCLKs */
|
|
|
|
IICLC = (0 << 0);
|
2006-09-29 10:54:01 +00:00
|
|
|
|
2007-06-26 02:11:30 +00:00
|
|
|
/* Turn off I2C clock */
|
2008-12-04 15:06:48 +00:00
|
|
|
s3c_regclr32(&CLKCON, 1 << 16);
|
2006-09-29 10:54:01 +00:00
|
|
|
}
|
2008-03-30 05:38:28 +00:00
|
|
|
|
|
|
|
void IIC(void)
|
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
/* If ack was received from last byte and bytes are remaining */
|
|
|
|
if (--buf_count >= 0 && (IICSTAT & I2C_ACK_L) == 0)
|
|
|
|
{
|
|
|
|
/* Write next byte to shift register */
|
|
|
|
IICDS = *buf_ptr++;
|
|
|
|
|
|
|
|
/* Clear pending interrupt to continue */
|
|
|
|
IICCON &= ~I2C_TXRX_INTPND;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finished */
|
|
|
|
|
|
|
|
/* Generate STOP */
|
|
|
|
i2c_stop();
|
|
|
|
|
|
|
|
/* Signal thread */
|
|
|
|
wakeup_signal(&i2c_wake);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ack */
|
|
|
|
SRCPND = IIC_MASK;
|
|
|
|
INTPND = IIC_MASK;
|
|
|
|
}
|