2006-02-22 14:50:57 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 by Linus Nielsen Feltzing
|
|
|
|
*
|
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-02-22 14:50:57 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include "config.h"
|
|
|
|
#include "cpu.h"
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "debug.h"
|
|
|
|
#include "generic_i2c.h"
|
|
|
|
|
2008-12-07 19:25:51 +00:00
|
|
|
#define MAX_I2C_INTERFACES 5
|
|
|
|
|
2008-12-08 19:01:00 +00:00
|
|
|
static int i2c_num_ifs = 0;
|
2009-03-03 18:00:17 +00:00
|
|
|
static const struct i2c_interface *i2c_if[MAX_I2C_INTERFACES];
|
2006-02-22 14:50:57 +00:00
|
|
|
|
2009-03-03 18:00:17 +00:00
|
|
|
static void i2c_start(const struct i2c_interface *iface)
|
2006-02-22 14:50:57 +00:00
|
|
|
{
|
2010-06-21 21:41:07 +00:00
|
|
|
iface->sda_dir(true);
|
|
|
|
|
|
|
|
iface->sda_out(1);
|
|
|
|
iface->scl_out(1);
|
|
|
|
iface->delay(iface->delay_su_sta);
|
|
|
|
iface->sda_out(0);
|
|
|
|
iface->delay(iface->delay_hd_sta);
|
|
|
|
iface->scl_out(0);
|
|
|
|
iface->delay(iface->delay_hd_dat);
|
2006-02-22 14:50:57 +00:00
|
|
|
}
|
|
|
|
|
2009-03-03 18:00:17 +00:00
|
|
|
static void i2c_stop(const struct i2c_interface *iface)
|
2006-02-22 14:50:57 +00:00
|
|
|
{
|
2010-06-21 21:41:07 +00:00
|
|
|
iface->sda_dir(true);
|
2008-12-07 19:25:51 +00:00
|
|
|
|
2010-06-21 21:41:07 +00:00
|
|
|
iface->sda_out(0);
|
|
|
|
iface->delay(iface->delay_su_dat);
|
|
|
|
iface->scl_out(1);
|
|
|
|
iface->delay(iface->delay_su_sto);
|
|
|
|
iface->sda_out(1);
|
2006-02-22 14:50:57 +00:00
|
|
|
}
|
|
|
|
|
2009-03-03 18:00:17 +00:00
|
|
|
static void i2c_ack(const struct i2c_interface *iface, bool ack)
|
2006-02-22 14:50:57 +00:00
|
|
|
{
|
2010-06-21 21:41:07 +00:00
|
|
|
iface->sda_dir(true);
|
|
|
|
iface->sda_out(!ack);
|
|
|
|
iface->delay(iface->delay_su_dat);
|
|
|
|
iface->scl_out(1);
|
|
|
|
iface->delay(iface->delay_thigh);
|
|
|
|
iface->scl_out(0);
|
|
|
|
iface->delay(iface->delay_hd_dat);
|
2006-02-22 14:50:57 +00:00
|
|
|
}
|
|
|
|
|
2009-03-03 18:00:17 +00:00
|
|
|
static int i2c_getack(const struct i2c_interface *iface)
|
2006-02-22 14:50:57 +00:00
|
|
|
{
|
|
|
|
int ret = 1;
|
|
|
|
|
2010-06-21 21:41:07 +00:00
|
|
|
iface->sda_dir(false);
|
|
|
|
iface->delay(iface->delay_su_dat);
|
|
|
|
iface->scl_out(1);
|
|
|
|
iface->delay(iface->delay_thigh);
|
|
|
|
if (iface->sda_in())
|
2006-02-22 14:50:57 +00:00
|
|
|
ret = 0; /* ack failed */
|
2010-06-21 21:41:07 +00:00
|
|
|
iface->scl_out(0);
|
|
|
|
iface->delay(iface->delay_hd_dat);
|
2006-02-22 14:50:57 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-03-03 18:00:17 +00:00
|
|
|
static unsigned char i2c_inb(const struct i2c_interface *iface, bool ack)
|
2006-02-22 14:50:57 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned char byte = 0;
|
2008-12-07 19:25:51 +00:00
|
|
|
|
2010-06-21 21:41:07 +00:00
|
|
|
iface->sda_dir(false);
|
2008-12-07 19:25:51 +00:00
|
|
|
|
2006-02-22 14:50:57 +00:00
|
|
|
/* clock in each bit, MSB first */
|
|
|
|
for ( i=0x80; i; i>>=1 ) {
|
2010-06-21 21:41:07 +00:00
|
|
|
iface->delay(iface->delay_su_dat);
|
|
|
|
iface->scl_out(1);
|
|
|
|
iface->delay(iface->delay_thigh);
|
|
|
|
if (iface->sda_in())
|
2006-02-22 14:50:57 +00:00
|
|
|
byte |= i;
|
2010-06-21 21:41:07 +00:00
|
|
|
iface->scl_out(0);
|
|
|
|
iface->delay(iface->delay_hd_dat);
|
2006-02-22 14:50:57 +00:00
|
|
|
}
|
2008-12-07 19:25:51 +00:00
|
|
|
|
2006-02-22 14:50:57 +00:00
|
|
|
i2c_ack(iface, ack);
|
2008-12-07 19:25:51 +00:00
|
|
|
|
2006-02-22 14:50:57 +00:00
|
|
|
return byte;
|
|
|
|
}
|
|
|
|
|
2009-03-03 18:00:17 +00:00
|
|
|
static int i2c_outb(const struct i2c_interface *iface, unsigned char byte)
|
2006-02-22 14:50:57 +00:00
|
|
|
{
|
2008-12-07 19:25:51 +00:00
|
|
|
int i;
|
|
|
|
|
2010-06-21 21:41:07 +00:00
|
|
|
iface->sda_dir(true);
|
2008-12-07 19:25:51 +00:00
|
|
|
|
|
|
|
/* clock out each bit, MSB first */
|
|
|
|
for (i=0x80; i; i>>=1) {
|
2010-06-21 21:41:07 +00:00
|
|
|
iface->sda_out(i & byte);
|
|
|
|
iface->delay(iface->delay_su_dat);
|
|
|
|
iface->scl_out(1);
|
|
|
|
iface->delay(iface->delay_thigh);
|
|
|
|
iface->scl_out(0);
|
|
|
|
iface->delay(iface->delay_hd_dat);
|
2006-02-22 14:50:57 +00:00
|
|
|
}
|
|
|
|
|
2009-03-03 18:00:17 +00:00
|
|
|
return i2c_getack(iface);
|
2006-02-22 14:50:57 +00:00
|
|
|
}
|
|
|
|
|
2009-03-03 18:00:17 +00:00
|
|
|
int i2c_write_data(int bus_index, int bus_address, int address,
|
2006-02-22 14:50:57 +00:00
|
|
|
const unsigned char* buf, int count)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int ret = 0;
|
2009-03-04 18:15:06 +00:00
|
|
|
const struct i2c_interface *iface = i2c_if[bus_index];
|
2008-12-07 19:25:51 +00:00
|
|
|
|
2006-02-22 14:50:57 +00:00
|
|
|
i2c_start(iface);
|
2009-03-03 18:00:17 +00:00
|
|
|
if (!i2c_outb(iface, bus_address & 0xfe))
|
2008-12-07 19:25:51 +00:00
|
|
|
{
|
|
|
|
ret = -2;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (address != -1)
|
|
|
|
{
|
2008-12-08 19:01:00 +00:00
|
|
|
if (!i2c_outb(iface, address))
|
2008-12-07 19:25:51 +00:00
|
|
|
{
|
|
|
|
ret = -3;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i = 0;i < count;i++)
|
|
|
|
{
|
2008-12-08 19:01:00 +00:00
|
|
|
if (!i2c_outb(iface, buf[i]))
|
2008-12-07 19:25:51 +00:00
|
|
|
{
|
|
|
|
ret = -4;
|
|
|
|
break;
|
2006-02-22 14:50:57 +00:00
|
|
|
}
|
|
|
|
}
|
2008-12-07 19:25:51 +00:00
|
|
|
|
|
|
|
end:
|
2006-02-22 14:50:57 +00:00
|
|
|
i2c_stop(iface);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-03-03 18:00:17 +00:00
|
|
|
int i2c_read_data(int bus_index, int bus_address, int address,
|
2006-02-22 14:50:57 +00:00
|
|
|
unsigned char* buf, int count)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int ret = 0;
|
2009-03-04 18:15:06 +00:00
|
|
|
const struct i2c_interface *iface = i2c_if[bus_index];
|
2008-12-07 19:25:51 +00:00
|
|
|
|
|
|
|
if (address != -1)
|
|
|
|
{
|
|
|
|
i2c_start(iface);
|
2009-03-03 18:00:17 +00:00
|
|
|
if (!i2c_outb(iface, bus_address & 0xfe))
|
2008-12-07 19:25:51 +00:00
|
|
|
{
|
2006-02-22 14:50:57 +00:00
|
|
|
ret = -2;
|
2008-12-07 19:25:51 +00:00
|
|
|
goto end;
|
|
|
|
}
|
2008-12-08 19:01:00 +00:00
|
|
|
if (!i2c_outb(iface, address))
|
2008-12-07 19:25:51 +00:00
|
|
|
{
|
|
|
|
ret = -3;
|
|
|
|
goto end;
|
2006-02-22 14:50:57 +00:00
|
|
|
}
|
|
|
|
}
|
2008-12-07 19:25:51 +00:00
|
|
|
|
|
|
|
i2c_start(iface);
|
2009-03-03 18:00:17 +00:00
|
|
|
if (!i2c_outb(iface, bus_address | 1))
|
2008-12-07 19:25:51 +00:00
|
|
|
{
|
|
|
|
ret = -4;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i = 0;i < count-1;i++)
|
|
|
|
buf[i] = i2c_inb(iface, true);
|
|
|
|
|
|
|
|
buf[i] = i2c_inb(iface, false);
|
|
|
|
|
|
|
|
end:
|
2006-02-22 14:50:57 +00:00
|
|
|
i2c_stop(iface);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-07-10 06:56:32 +00:00
|
|
|
int i2c_write_read_data(int bus_index, int bus_address,
|
|
|
|
const unsigned char* buf_write, int count_write,
|
|
|
|
unsigned char* buf_read, int count_read)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int ret = 0;
|
|
|
|
const struct i2c_interface *iface = i2c_if[bus_index];
|
|
|
|
|
|
|
|
i2c_start(iface);
|
|
|
|
if (!i2c_outb(iface, bus_address))
|
|
|
|
{
|
|
|
|
ret = -2;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i = 0;i < count_write;i++)
|
|
|
|
{
|
|
|
|
if (!i2c_outb(iface, buf_write[i]))
|
|
|
|
{
|
|
|
|
ret = -3;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(i = 0;i < count_read-1;i++)
|
|
|
|
buf_read[i] = i2c_inb(iface, true);
|
|
|
|
|
|
|
|
buf_read[i] = i2c_inb(iface, false);
|
|
|
|
|
|
|
|
end:
|
|
|
|
i2c_stop(iface);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-03-03 18:00:17 +00:00
|
|
|
/* returns bus index which can be used as a handle, or <0 on error */
|
|
|
|
int i2c_add_node(const struct i2c_interface *iface)
|
2006-02-22 14:50:57 +00:00
|
|
|
{
|
2009-03-03 18:00:17 +00:00
|
|
|
int bus_index;
|
|
|
|
|
2008-12-07 19:25:51 +00:00
|
|
|
if (i2c_num_ifs == MAX_I2C_INTERFACES)
|
|
|
|
return -1;
|
|
|
|
|
2009-03-03 18:00:17 +00:00
|
|
|
bus_index = i2c_num_ifs++;
|
|
|
|
i2c_if[bus_index] = iface;
|
2008-12-07 19:25:51 +00:00
|
|
|
|
2010-06-21 21:41:07 +00:00
|
|
|
iface->scl_dir(true);
|
2008-12-07 19:25:51 +00:00
|
|
|
|
2009-03-03 18:00:17 +00:00
|
|
|
return bus_index;
|
2006-02-22 14:50:57 +00:00
|
|
|
}
|