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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2014-01-05 19:32:09 +00:00
|
|
|
|
2006-09-29 10:54:01 +00:00
|
|
|
#include "system.h"
|
2014-01-05 19:32:09 +00:00
|
|
|
#include "kernel.h"
|
2009-10-26 18:16:58 +00:00
|
|
|
#include "i2c-s3c2440.h"
|
2007-06-26 02:11:30 +00:00
|
|
|
|
2011-03-02 08:49:38 +00:00
|
|
|
static struct semaphore i2c_complete; /* Transfer completion signal */
|
2008-03-30 05:38:28 +00:00
|
|
|
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 */
|
2010-06-30 02:02:46 +00:00
|
|
|
bitset32(&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
|
|
|
|
2011-03-02 08:49:38 +00:00
|
|
|
if (semaphore_wait(&i2c_complete, 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 */
|
2010-06-30 02:02:46 +00:00
|
|
|
bitclr32(&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 */
|
2011-03-02 08:49:38 +00:00
|
|
|
semaphore_init(&i2c_complete, 1, 0);
|
2008-03-30 05:38:28 +00:00
|
|
|
mutex_init(&i2c_mtx);
|
|
|
|
|
|
|
|
/* Clear pending source */
|
|
|
|
SRCPND = IIC_MASK;
|
|
|
|
INTPND = IIC_MASK;
|
|
|
|
|
|
|
|
/* Enable i2c interrupt in controller */
|
2010-06-30 02:02:46 +00:00
|
|
|
bitclr32(&INTMOD, IIC_MASK);
|
|
|
|
bitclr32(&INTMSK, IIC_MASK);
|
2007-01-20 22:16:01 +00:00
|
|
|
|
2007-06-26 02:11:30 +00:00
|
|
|
/* Turn on I2C clock */
|
2010-06-30 02:02:46 +00:00
|
|
|
bitset32(&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 */
|
2010-06-30 02:02:46 +00:00
|
|
|
bitclr32(&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 */
|
2011-03-02 08:49:38 +00:00
|
|
|
semaphore_release(&i2c_complete);
|
2008-03-30 05:38:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ack */
|
|
|
|
SRCPND = IIC_MASK;
|
|
|
|
INTPND = IIC_MASK;
|
|
|
|
}
|