2013-01-16 01:57:54 +01:00
|
|
|
/*
|
|
|
|
* QTest I2C driver
|
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Andreas Färber
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*/
|
2016-02-08 19:08:51 +01:00
|
|
|
#include "qemu/osdep.h"
|
2013-05-02 15:56:26 +02:00
|
|
|
#include "libqos/i2c.h"
|
2013-01-16 01:57:54 +01:00
|
|
|
#include "libqtest.h"
|
|
|
|
|
2019-03-18 15:06:50 +01:00
|
|
|
void i2c_send(QI2CDevice *i2cdev, const uint8_t *buf, uint16_t len)
|
2013-01-16 01:57:54 +01:00
|
|
|
{
|
2019-03-18 15:06:50 +01:00
|
|
|
i2cdev->bus->send(i2cdev->bus, i2cdev->addr, buf, len);
|
2013-01-16 01:57:54 +01:00
|
|
|
}
|
|
|
|
|
2019-03-18 15:06:50 +01:00
|
|
|
void i2c_recv(QI2CDevice *i2cdev, uint8_t *buf, uint16_t len)
|
2013-01-16 01:57:54 +01:00
|
|
|
{
|
2019-03-18 15:06:50 +01:00
|
|
|
i2cdev->bus->recv(i2cdev->bus, i2cdev->addr, buf, len);
|
2013-01-16 01:57:54 +01:00
|
|
|
}
|
2019-03-18 15:09:51 +01:00
|
|
|
|
2019-03-18 15:06:50 +01:00
|
|
|
void i2c_read_block(QI2CDevice *i2cdev, uint8_t reg,
|
|
|
|
uint8_t *buf, uint16_t len)
|
2019-03-18 15:09:51 +01:00
|
|
|
{
|
2019-03-18 15:06:50 +01:00
|
|
|
i2c_send(i2cdev, ®, 1);
|
|
|
|
i2c_recv(i2cdev, buf, len);
|
2019-03-18 15:09:51 +01:00
|
|
|
}
|
|
|
|
|
2019-03-18 15:06:50 +01:00
|
|
|
void i2c_write_block(QI2CDevice *i2cdev, uint8_t reg,
|
2019-03-18 15:09:51 +01:00
|
|
|
const uint8_t *buf, uint16_t len)
|
|
|
|
{
|
|
|
|
uint8_t *cmd = g_malloc(len + 1);
|
|
|
|
cmd[0] = reg;
|
|
|
|
memcpy(&cmd[1], buf, len);
|
2019-03-18 15:06:50 +01:00
|
|
|
i2c_send(i2cdev, cmd, len + 1);
|
2019-03-18 15:09:51 +01:00
|
|
|
g_free(cmd);
|
|
|
|
}
|
|
|
|
|
2019-03-18 15:06:50 +01:00
|
|
|
uint8_t i2c_get8(QI2CDevice *i2cdev, uint8_t reg)
|
2019-03-18 15:09:51 +01:00
|
|
|
{
|
|
|
|
uint8_t resp[1];
|
2019-03-18 15:06:50 +01:00
|
|
|
i2c_read_block(i2cdev, reg, resp, sizeof(resp));
|
2019-03-18 15:09:51 +01:00
|
|
|
return resp[0];
|
|
|
|
}
|
|
|
|
|
2019-03-18 15:06:50 +01:00
|
|
|
uint16_t i2c_get16(QI2CDevice *i2cdev, uint8_t reg)
|
2019-03-18 15:09:51 +01:00
|
|
|
{
|
|
|
|
uint8_t resp[2];
|
2019-03-18 15:06:50 +01:00
|
|
|
i2c_read_block(i2cdev, reg, resp, sizeof(resp));
|
2019-03-18 15:09:51 +01:00
|
|
|
return (resp[0] << 8) | resp[1];
|
|
|
|
}
|
|
|
|
|
2019-03-18 15:06:50 +01:00
|
|
|
void i2c_set8(QI2CDevice *i2cdev, uint8_t reg, uint8_t value)
|
2019-03-18 15:09:51 +01:00
|
|
|
{
|
2019-03-18 15:06:50 +01:00
|
|
|
i2c_write_block(i2cdev, reg, &value, 1);
|
2019-03-18 15:09:51 +01:00
|
|
|
}
|
|
|
|
|
2019-03-18 15:06:50 +01:00
|
|
|
void i2c_set16(QI2CDevice *i2cdev, uint8_t reg, uint16_t value)
|
2019-03-18 15:09:51 +01:00
|
|
|
{
|
|
|
|
uint8_t data[2];
|
|
|
|
|
|
|
|
data[0] = value >> 8;
|
|
|
|
data[1] = value & 255;
|
2019-03-18 15:06:50 +01:00
|
|
|
i2c_write_block(i2cdev, reg, data, sizeof(data));
|
2019-03-18 15:09:51 +01:00
|
|
|
}
|
2019-03-18 13:48:23 +01:00
|
|
|
|
|
|
|
void *i2c_device_create(void *i2c_bus, QGuestAllocator *alloc, void *addr)
|
|
|
|
{
|
|
|
|
QI2CDevice *i2cdev = g_new0(QI2CDevice, 1);
|
|
|
|
|
|
|
|
i2cdev->bus = i2c_bus;
|
2019-03-18 15:06:50 +01:00
|
|
|
if (addr) {
|
|
|
|
i2cdev->addr = ((QI2CAddress *)addr)->addr;
|
|
|
|
}
|
2019-03-18 13:48:23 +01:00
|
|
|
return &i2cdev->obj;
|
|
|
|
}
|
2019-03-18 15:06:50 +01:00
|
|
|
|
|
|
|
void add_qi2c_address(QOSGraphEdgeOptions *opts, QI2CAddress *addr)
|
|
|
|
{
|
|
|
|
g_assert(addr);
|
|
|
|
|
|
|
|
opts->arg = addr;
|
|
|
|
opts->size_arg = sizeof(QI2CAddress);
|
|
|
|
}
|