2015-03-31 16:57:18 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2015, Samsung Electronics
|
|
|
|
* Przemyslaw Marczak <p.marczak@samsung.com>
|
|
|
|
*
|
|
|
|
* This file is based on: drivers/i2c/soft-i2c.c,
|
|
|
|
* with added driver-model support and code cleanup.
|
|
|
|
*/
|
|
|
|
#include <common.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <dm.h>
|
|
|
|
#include <i2c.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2015-03-31 16:57:18 +00:00
|
|
|
#include <asm/gpio.h>
|
2020-05-10 17:40:11 +00:00
|
|
|
#include <linux/delay.h>
|
2015-03-31 16:57:18 +00:00
|
|
|
|
|
|
|
#define DEFAULT_UDELAY 5
|
|
|
|
#define RETRIES 0
|
|
|
|
#define I2C_ACK 0
|
|
|
|
#define I2C_NOACK 1
|
|
|
|
|
|
|
|
enum {
|
|
|
|
PIN_SDA = 0,
|
|
|
|
PIN_SCL,
|
|
|
|
PIN_COUNT,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct i2c_gpio_bus {
|
|
|
|
/**
|
|
|
|
* udelay - delay [us] between GPIO toggle operations,
|
|
|
|
* which is 1/4 of I2C speed clock period.
|
|
|
|
*/
|
|
|
|
int udelay;
|
|
|
|
/* sda, scl */
|
|
|
|
struct gpio_desc gpios[PIN_COUNT];
|
2020-02-07 16:55:31 +00:00
|
|
|
|
|
|
|
int (*get_sda)(struct i2c_gpio_bus *bus);
|
|
|
|
void (*set_sda)(struct i2c_gpio_bus *bus, int bit);
|
|
|
|
void (*set_scl)(struct i2c_gpio_bus *bus, int bit);
|
2015-03-31 16:57:18 +00:00
|
|
|
};
|
|
|
|
|
2020-02-07 16:55:31 +00:00
|
|
|
static int i2c_gpio_sda_get(struct i2c_gpio_bus *bus)
|
2015-03-31 16:57:18 +00:00
|
|
|
{
|
2020-02-07 16:55:31 +00:00
|
|
|
struct gpio_desc *sda = &bus->gpios[PIN_SDA];
|
|
|
|
|
2015-03-31 16:57:18 +00:00
|
|
|
return dm_gpio_get_value(sda);
|
|
|
|
}
|
|
|
|
|
2020-02-07 16:55:31 +00:00
|
|
|
static void i2c_gpio_sda_set(struct i2c_gpio_bus *bus, int bit)
|
2015-03-31 16:57:18 +00:00
|
|
|
{
|
2020-02-07 16:55:31 +00:00
|
|
|
struct gpio_desc *sda = &bus->gpios[PIN_SDA];
|
|
|
|
|
2015-04-26 03:05:38 +00:00
|
|
|
if (bit)
|
2015-03-31 16:57:18 +00:00
|
|
|
dm_gpio_set_dir_flags(sda, GPIOD_IS_IN);
|
2015-04-26 03:05:38 +00:00
|
|
|
else
|
2015-03-31 16:57:18 +00:00
|
|
|
dm_gpio_set_dir_flags(sda, GPIOD_IS_OUT);
|
|
|
|
}
|
|
|
|
|
2020-02-07 16:55:31 +00:00
|
|
|
static void i2c_gpio_scl_set(struct i2c_gpio_bus *bus, int bit)
|
2020-03-16 06:55:06 +00:00
|
|
|
{
|
|
|
|
struct gpio_desc *scl = &bus->gpios[PIN_SCL];
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
if (bit) {
|
|
|
|
dm_gpio_set_dir_flags(scl, GPIOD_IS_IN);
|
|
|
|
while (!dm_gpio_get_value(scl) && count++ < 100000)
|
|
|
|
udelay(1);
|
|
|
|
|
|
|
|
if (!dm_gpio_get_value(scl))
|
|
|
|
pr_err("timeout waiting on slave to release scl\n");
|
|
|
|
} else {
|
|
|
|
dm_gpio_set_dir_flags(scl, GPIOD_IS_OUT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* variant for output only gpios which cannot support clock stretching */
|
|
|
|
static void i2c_gpio_scl_set_output_only(struct i2c_gpio_bus *bus, int bit)
|
2015-03-31 16:57:18 +00:00
|
|
|
{
|
2020-02-07 16:55:31 +00:00
|
|
|
struct gpio_desc *scl = &bus->gpios[PIN_SCL];
|
2015-04-26 03:05:38 +00:00
|
|
|
ulong flags = GPIOD_IS_OUT;
|
|
|
|
|
|
|
|
if (bit)
|
|
|
|
flags |= GPIOD_IS_OUT_ACTIVE;
|
|
|
|
dm_gpio_set_dir_flags(scl, flags);
|
2015-03-31 16:57:18 +00:00
|
|
|
}
|
|
|
|
|
2020-02-07 16:55:31 +00:00
|
|
|
static void i2c_gpio_write_bit(struct i2c_gpio_bus *bus, int delay, uchar bit)
|
2015-03-31 16:57:18 +00:00
|
|
|
{
|
2020-02-07 16:55:31 +00:00
|
|
|
bus->set_scl(bus, 0);
|
2015-03-31 16:57:18 +00:00
|
|
|
udelay(delay);
|
2020-02-07 16:55:31 +00:00
|
|
|
bus->set_sda(bus, bit);
|
2015-03-31 16:57:18 +00:00
|
|
|
udelay(delay);
|
2020-02-07 16:55:31 +00:00
|
|
|
bus->set_scl(bus, 1);
|
2015-03-31 16:57:18 +00:00
|
|
|
udelay(2 * delay);
|
|
|
|
}
|
|
|
|
|
2020-02-07 16:55:31 +00:00
|
|
|
static int i2c_gpio_read_bit(struct i2c_gpio_bus *bus, int delay)
|
2015-03-31 16:57:18 +00:00
|
|
|
{
|
|
|
|
int value;
|
|
|
|
|
2020-02-07 16:55:31 +00:00
|
|
|
bus->set_scl(bus, 1);
|
2015-03-31 16:57:18 +00:00
|
|
|
udelay(delay);
|
2020-02-07 16:55:31 +00:00
|
|
|
value = bus->get_sda(bus);
|
2015-03-31 16:57:18 +00:00
|
|
|
udelay(delay);
|
2020-02-07 16:55:31 +00:00
|
|
|
bus->set_scl(bus, 0);
|
2015-03-31 16:57:18 +00:00
|
|
|
udelay(2 * delay);
|
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* START: High -> Low on SDA while SCL is High */
|
2020-02-07 16:55:31 +00:00
|
|
|
static void i2c_gpio_send_start(struct i2c_gpio_bus *bus, int delay)
|
2015-03-31 16:57:18 +00:00
|
|
|
{
|
|
|
|
udelay(delay);
|
2020-02-07 16:55:31 +00:00
|
|
|
bus->set_sda(bus, 1);
|
2015-03-31 16:57:18 +00:00
|
|
|
udelay(delay);
|
2020-02-07 16:55:31 +00:00
|
|
|
bus->set_scl(bus, 1);
|
2015-03-31 16:57:18 +00:00
|
|
|
udelay(delay);
|
2020-02-07 16:55:31 +00:00
|
|
|
bus->set_sda(bus, 0);
|
2015-03-31 16:57:18 +00:00
|
|
|
udelay(delay);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* STOP: Low -> High on SDA while SCL is High */
|
2020-02-07 16:55:31 +00:00
|
|
|
static void i2c_gpio_send_stop(struct i2c_gpio_bus *bus, int delay)
|
2015-03-31 16:57:18 +00:00
|
|
|
{
|
2020-02-07 16:55:31 +00:00
|
|
|
bus->set_scl(bus, 0);
|
2015-03-31 16:57:18 +00:00
|
|
|
udelay(delay);
|
2020-02-07 16:55:31 +00:00
|
|
|
bus->set_sda(bus, 0);
|
2015-03-31 16:57:18 +00:00
|
|
|
udelay(delay);
|
2020-02-07 16:55:31 +00:00
|
|
|
bus->set_scl(bus, 1);
|
2015-03-31 16:57:18 +00:00
|
|
|
udelay(delay);
|
2020-02-07 16:55:31 +00:00
|
|
|
bus->set_sda(bus, 1);
|
2015-03-31 16:57:18 +00:00
|
|
|
udelay(delay);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ack should be I2C_ACK or I2C_NOACK */
|
2020-02-07 16:55:31 +00:00
|
|
|
static void i2c_gpio_send_ack(struct i2c_gpio_bus *bus, int delay, int ack)
|
2015-03-31 16:57:18 +00:00
|
|
|
{
|
2020-02-07 16:55:31 +00:00
|
|
|
i2c_gpio_write_bit(bus, delay, ack);
|
|
|
|
bus->set_scl(bus, 0);
|
2015-03-31 16:57:18 +00:00
|
|
|
udelay(delay);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a reset sequence consisting of 9 clocks with the data signal high
|
|
|
|
* to clock any confused device back into an idle state. Also send a
|
|
|
|
* <stop> at the end of the sequence for belts & suspenders.
|
|
|
|
*/
|
2020-02-07 16:55:31 +00:00
|
|
|
static void i2c_gpio_send_reset(struct i2c_gpio_bus *bus, int delay)
|
2015-03-31 16:57:18 +00:00
|
|
|
{
|
|
|
|
int j;
|
|
|
|
|
|
|
|
for (j = 0; j < 9; j++)
|
2020-02-07 16:55:31 +00:00
|
|
|
i2c_gpio_write_bit(bus, delay, 1);
|
2015-03-31 16:57:18 +00:00
|
|
|
|
2020-02-07 16:55:31 +00:00
|
|
|
i2c_gpio_send_stop(bus, delay);
|
2015-03-31 16:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set sda high with low clock, before reading slave data */
|
2020-02-07 16:55:31 +00:00
|
|
|
static void i2c_gpio_sda_high(struct i2c_gpio_bus *bus, int delay)
|
2015-03-31 16:57:18 +00:00
|
|
|
{
|
2020-02-07 16:55:31 +00:00
|
|
|
bus->set_scl(bus, 0);
|
2015-03-31 16:57:18 +00:00
|
|
|
udelay(delay);
|
2020-02-07 16:55:31 +00:00
|
|
|
bus->set_sda(bus, 1);
|
2015-03-31 16:57:18 +00:00
|
|
|
udelay(delay);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send 8 bits and look for an acknowledgement */
|
2020-02-07 16:55:31 +00:00
|
|
|
static int i2c_gpio_write_byte(struct i2c_gpio_bus *bus, int delay, uchar data)
|
2015-03-31 16:57:18 +00:00
|
|
|
{
|
|
|
|
int j;
|
|
|
|
int nack;
|
|
|
|
|
|
|
|
for (j = 0; j < 8; j++) {
|
2020-02-07 16:55:31 +00:00
|
|
|
i2c_gpio_write_bit(bus, delay, data & 0x80);
|
2015-03-31 16:57:18 +00:00
|
|
|
data <<= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
udelay(delay);
|
|
|
|
|
|
|
|
/* Look for an <ACK>(negative logic) and return it */
|
2020-02-07 16:55:31 +00:00
|
|
|
i2c_gpio_sda_high(bus, delay);
|
|
|
|
nack = i2c_gpio_read_bit(bus, delay);
|
2015-03-31 16:57:18 +00:00
|
|
|
|
|
|
|
return nack; /* not a nack is an ack */
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* if ack == I2C_ACK, ACK the byte so can continue reading, else
|
|
|
|
* send I2C_NOACK to end the read.
|
|
|
|
*/
|
2020-02-07 16:55:31 +00:00
|
|
|
static uchar i2c_gpio_read_byte(struct i2c_gpio_bus *bus, int delay, int ack)
|
2015-03-31 16:57:18 +00:00
|
|
|
{
|
|
|
|
int data;
|
|
|
|
int j;
|
|
|
|
|
2020-02-07 16:55:31 +00:00
|
|
|
i2c_gpio_sda_high(bus, delay);
|
2015-03-31 16:57:18 +00:00
|
|
|
data = 0;
|
|
|
|
for (j = 0; j < 8; j++) {
|
|
|
|
data <<= 1;
|
2020-02-07 16:55:31 +00:00
|
|
|
data |= i2c_gpio_read_bit(bus, delay);
|
2015-03-31 16:57:18 +00:00
|
|
|
}
|
2020-02-07 16:55:31 +00:00
|
|
|
i2c_gpio_send_ack(bus, delay, ack);
|
2015-03-31 16:57:18 +00:00
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* send start and the slave chip address */
|
2020-02-07 16:55:31 +00:00
|
|
|
int i2c_send_slave_addr(struct i2c_gpio_bus *bus, int delay, uchar chip)
|
2015-03-31 16:57:18 +00:00
|
|
|
{
|
2020-02-07 16:55:31 +00:00
|
|
|
i2c_gpio_send_start(bus, delay);
|
2015-03-31 16:57:18 +00:00
|
|
|
|
2020-02-07 16:55:31 +00:00
|
|
|
if (i2c_gpio_write_byte(bus, delay, chip)) {
|
|
|
|
i2c_gpio_send_stop(bus, delay);
|
2015-03-31 16:57:18 +00:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int i2c_gpio_write_data(struct i2c_gpio_bus *bus, uchar chip,
|
|
|
|
uchar *buffer, int len,
|
|
|
|
bool end_with_repeated_start)
|
|
|
|
{
|
|
|
|
unsigned int delay = bus->udelay;
|
|
|
|
int failures = 0;
|
|
|
|
|
|
|
|
debug("%s: chip %x buffer %p len %d\n", __func__, chip, buffer, len);
|
|
|
|
|
2020-02-07 16:55:31 +00:00
|
|
|
if (i2c_send_slave_addr(bus, delay, chip << 1)) {
|
2015-03-31 16:57:18 +00:00
|
|
|
debug("i2c_write, no chip responded %02X\n", chip);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (len-- > 0) {
|
2020-02-07 16:55:31 +00:00
|
|
|
if (i2c_gpio_write_byte(bus, delay, *buffer++))
|
2015-03-31 16:57:18 +00:00
|
|
|
failures++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!end_with_repeated_start) {
|
2020-02-07 16:55:31 +00:00
|
|
|
i2c_gpio_send_stop(bus, delay);
|
2015-03-31 16:57:18 +00:00
|
|
|
return failures;
|
|
|
|
}
|
|
|
|
|
2020-02-07 16:55:31 +00:00
|
|
|
if (i2c_send_slave_addr(bus, delay, (chip << 1) | 0x1)) {
|
2015-03-31 16:57:18 +00:00
|
|
|
debug("i2c_write, no chip responded %02X\n", chip);
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return failures;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int i2c_gpio_read_data(struct i2c_gpio_bus *bus, uchar chip,
|
|
|
|
uchar *buffer, int len)
|
|
|
|
{
|
|
|
|
unsigned int delay = bus->udelay;
|
|
|
|
|
|
|
|
debug("%s: chip %x buffer: %p len %d\n", __func__, chip, buffer, len);
|
|
|
|
|
|
|
|
while (len-- > 0)
|
2020-02-07 16:55:31 +00:00
|
|
|
*buffer++ = i2c_gpio_read_byte(bus, delay, len == 0);
|
2015-03-31 16:57:18 +00:00
|
|
|
|
2020-02-07 16:55:31 +00:00
|
|
|
i2c_gpio_send_stop(bus, delay);
|
2015-03-31 16:57:18 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int i2c_gpio_xfer(struct udevice *dev, struct i2c_msg *msg, int nmsgs)
|
|
|
|
{
|
|
|
|
struct i2c_gpio_bus *bus = dev_get_priv(dev);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
for (; nmsgs > 0; nmsgs--, msg++) {
|
|
|
|
bool next_is_read = nmsgs > 1 && (msg[1].flags & I2C_M_RD);
|
|
|
|
|
|
|
|
if (msg->flags & I2C_M_RD) {
|
|
|
|
ret = i2c_gpio_read_data(bus, msg->addr, msg->buf,
|
|
|
|
msg->len);
|
|
|
|
} else {
|
|
|
|
ret = i2c_gpio_write_data(bus, msg->addr, msg->buf,
|
|
|
|
msg->len, next_is_read);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return -EREMOTEIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int i2c_gpio_probe(struct udevice *dev, uint chip, uint chip_flags)
|
|
|
|
{
|
|
|
|
struct i2c_gpio_bus *bus = dev_get_priv(dev);
|
|
|
|
unsigned int delay = bus->udelay;
|
|
|
|
int ret;
|
|
|
|
|
2020-02-07 16:55:31 +00:00
|
|
|
i2c_gpio_send_start(bus, delay);
|
|
|
|
ret = i2c_gpio_write_byte(bus, delay, (chip << 1) | 0);
|
|
|
|
i2c_gpio_send_stop(bus, delay);
|
2015-03-31 16:57:18 +00:00
|
|
|
|
|
|
|
debug("%s: bus: %d (%s) chip: %x flags: %x ret: %d\n",
|
|
|
|
__func__, dev->seq, dev->name, chip, chip_flags, ret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int i2c_gpio_set_bus_speed(struct udevice *dev, unsigned int speed_hz)
|
|
|
|
{
|
|
|
|
struct i2c_gpio_bus *bus = dev_get_priv(dev);
|
|
|
|
|
|
|
|
bus->udelay = 1000000 / (speed_hz << 2);
|
|
|
|
|
2020-02-07 16:55:31 +00:00
|
|
|
i2c_gpio_send_reset(bus, bus->udelay);
|
2015-03-31 16:57:18 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-07 15:57:52 +00:00
|
|
|
static int i2c_gpio_drv_probe(struct udevice *dev)
|
|
|
|
{
|
|
|
|
if (dev_read_bool(dev, "i2c-gpio,deblock")) {
|
|
|
|
/* @200kHz 9 clocks = 44us, 62us is ok */
|
|
|
|
const unsigned int DELAY_ABORT_SEQ = 62;
|
|
|
|
struct i2c_gpio_bus *bus = dev_get_priv(dev);
|
|
|
|
|
|
|
|
return i2c_deblock_gpio_loop(&bus->gpios[PIN_SDA],
|
|
|
|
&bus->gpios[PIN_SCL],
|
|
|
|
16, 5, DELAY_ABORT_SEQ);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-03 23:55:21 +00:00
|
|
|
static int i2c_gpio_of_to_plat(struct udevice *dev)
|
2015-03-31 16:57:18 +00:00
|
|
|
{
|
|
|
|
struct i2c_gpio_bus *bus = dev_get_priv(dev);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = gpio_request_list_by_name(dev, "gpios", bus->gpios,
|
|
|
|
ARRAY_SIZE(bus->gpios), 0);
|
|
|
|
if (ret < 0)
|
|
|
|
goto error;
|
|
|
|
|
2020-09-10 16:25:09 +00:00
|
|
|
bus->udelay = dev_read_u32_default(dev, "i2c-gpio,delay-us",
|
|
|
|
DEFAULT_UDELAY);
|
2015-03-31 16:57:18 +00:00
|
|
|
|
2020-02-07 16:55:31 +00:00
|
|
|
bus->get_sda = i2c_gpio_sda_get;
|
|
|
|
bus->set_sda = i2c_gpio_sda_set;
|
2020-09-10 16:25:09 +00:00
|
|
|
if (dev_read_bool(dev, "i2c-gpio,scl-output-only"))
|
2020-03-16 06:55:06 +00:00
|
|
|
bus->set_scl = i2c_gpio_scl_set_output_only;
|
|
|
|
else
|
|
|
|
bus->set_scl = i2c_gpio_scl_set;
|
2020-02-07 16:55:31 +00:00
|
|
|
|
2015-03-31 16:57:18 +00:00
|
|
|
return 0;
|
|
|
|
error:
|
2017-09-16 05:10:41 +00:00
|
|
|
pr_err("Can't get %s gpios! Error: %d", dev->name, ret);
|
2015-03-31 16:57:18 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct dm_i2c_ops i2c_gpio_ops = {
|
|
|
|
.xfer = i2c_gpio_xfer,
|
|
|
|
.probe_chip = i2c_gpio_probe,
|
|
|
|
.set_bus_speed = i2c_gpio_set_bus_speed,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct udevice_id i2c_gpio_ids[] = {
|
|
|
|
{ .compatible = "i2c-gpio" },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
|
|
|
|
U_BOOT_DRIVER(i2c_gpio) = {
|
|
|
|
.name = "i2c-gpio",
|
|
|
|
.id = UCLASS_I2C,
|
|
|
|
.of_match = i2c_gpio_ids,
|
2020-02-07 15:57:52 +00:00
|
|
|
.probe = i2c_gpio_drv_probe,
|
2020-12-03 23:55:21 +00:00
|
|
|
.of_to_plat = i2c_gpio_of_to_plat,
|
2020-12-03 23:55:17 +00:00
|
|
|
.priv_auto = sizeof(struct i2c_gpio_bus),
|
2015-03-31 16:57:18 +00:00
|
|
|
.ops = &i2c_gpio_ops,
|
|
|
|
};
|