2013-01-16 04:57:54 +04: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 21:08:51 +03:00
|
|
|
#include "qemu/osdep.h"
|
2020-08-04 21:00:40 +03:00
|
|
|
#include "i2c.h"
|
2013-01-16 04:57:54 +04:00
|
|
|
#include "libqtest.h"
|
|
|
|
|
2020-02-20 07:11:05 +03:00
|
|
|
void qi2c_send(QI2CDevice *i2cdev, const uint8_t *buf, uint16_t len)
|
2013-01-16 04:57:54 +04:00
|
|
|
{
|
2019-03-18 17:06:50 +03:00
|
|
|
i2cdev->bus->send(i2cdev->bus, i2cdev->addr, buf, len);
|
2013-01-16 04:57:54 +04:00
|
|
|
}
|
|
|
|
|
2020-02-20 07:11:05 +03:00
|
|
|
void qi2c_recv(QI2CDevice *i2cdev, uint8_t *buf, uint16_t len)
|
2013-01-16 04:57:54 +04:00
|
|
|
{
|
2019-03-18 17:06:50 +03:00
|
|
|
i2cdev->bus->recv(i2cdev->bus, i2cdev->addr, buf, len);
|
2013-01-16 04:57:54 +04:00
|
|
|
}
|
2019-03-18 17:09:51 +03:00
|
|
|
|
2019-03-18 17:06:50 +03:00
|
|
|
void i2c_read_block(QI2CDevice *i2cdev, uint8_t reg,
|
|
|
|
uint8_t *buf, uint16_t len)
|
2019-03-18 17:09:51 +03:00
|
|
|
{
|
2020-02-20 07:11:05 +03:00
|
|
|
qi2c_send(i2cdev, ®, 1);
|
|
|
|
qi2c_recv(i2cdev, buf, len);
|
2019-03-18 17:09:51 +03:00
|
|
|
}
|
|
|
|
|
2019-03-18 17:06:50 +03:00
|
|
|
void i2c_write_block(QI2CDevice *i2cdev, uint8_t reg,
|
2019-03-18 17:09:51 +03:00
|
|
|
const uint8_t *buf, uint16_t len)
|
|
|
|
{
|
|
|
|
uint8_t *cmd = g_malloc(len + 1);
|
|
|
|
cmd[0] = reg;
|
|
|
|
memcpy(&cmd[1], buf, len);
|
2020-02-20 07:11:05 +03:00
|
|
|
qi2c_send(i2cdev, cmd, len + 1);
|
2019-03-18 17:09:51 +03:00
|
|
|
g_free(cmd);
|
|
|
|
}
|
|
|
|
|
2019-03-18 17:06:50 +03:00
|
|
|
uint8_t i2c_get8(QI2CDevice *i2cdev, uint8_t reg)
|
2019-03-18 17:09:51 +03:00
|
|
|
{
|
|
|
|
uint8_t resp[1];
|
2019-03-18 17:06:50 +03:00
|
|
|
i2c_read_block(i2cdev, reg, resp, sizeof(resp));
|
2019-03-18 17:09:51 +03:00
|
|
|
return resp[0];
|
|
|
|
}
|
|
|
|
|
2019-03-18 17:06:50 +03:00
|
|
|
uint16_t i2c_get16(QI2CDevice *i2cdev, uint8_t reg)
|
2019-03-18 17:09:51 +03:00
|
|
|
{
|
|
|
|
uint8_t resp[2];
|
2019-03-18 17:06:50 +03:00
|
|
|
i2c_read_block(i2cdev, reg, resp, sizeof(resp));
|
2019-03-18 17:09:51 +03:00
|
|
|
return (resp[0] << 8) | resp[1];
|
|
|
|
}
|
|
|
|
|
2019-03-18 17:06:50 +03:00
|
|
|
void i2c_set8(QI2CDevice *i2cdev, uint8_t reg, uint8_t value)
|
2019-03-18 17:09:51 +03:00
|
|
|
{
|
2019-03-18 17:06:50 +03:00
|
|
|
i2c_write_block(i2cdev, reg, &value, 1);
|
2019-03-18 17:09:51 +03:00
|
|
|
}
|
|
|
|
|
2019-03-18 17:06:50 +03:00
|
|
|
void i2c_set16(QI2CDevice *i2cdev, uint8_t reg, uint16_t value)
|
2019-03-18 17:09:51 +03:00
|
|
|
{
|
|
|
|
uint8_t data[2];
|
|
|
|
|
|
|
|
data[0] = value >> 8;
|
|
|
|
data[1] = value & 255;
|
2019-03-18 17:06:50 +03:00
|
|
|
i2c_write_block(i2cdev, reg, data, sizeof(data));
|
2019-03-18 17:09:51 +03:00
|
|
|
}
|
2019-03-18 15:48:23 +03: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 17:06:50 +03:00
|
|
|
if (addr) {
|
|
|
|
i2cdev->addr = ((QI2CAddress *)addr)->addr;
|
|
|
|
}
|
2019-03-18 15:48:23 +03:00
|
|
|
return &i2cdev->obj;
|
|
|
|
}
|
2019-03-18 17:06:50 +03:00
|
|
|
|
|
|
|
void add_qi2c_address(QOSGraphEdgeOptions *opts, QI2CAddress *addr)
|
|
|
|
{
|
|
|
|
g_assert(addr);
|
|
|
|
|
|
|
|
opts->arg = addr;
|
|
|
|
opts->size_arg = sizeof(QI2CAddress);
|
|
|
|
}
|