xref: /openbmc/qemu/tests/qtest/libqos/i2c.c (revision 26ed501b)
1 /*
2  * QTest I2C driver
3  *
4  * Copyright (c) 2012 Andreas Färber
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2 or later.
7  * See the COPYING file in the top-level directory.
8  */
9 #include "qemu/osdep.h"
10 #include "i2c.h"
11 #include "../libqtest.h"
12 
13 void qi2c_send(QI2CDevice *i2cdev, const uint8_t *buf, uint16_t len)
14 {
15     i2cdev->bus->send(i2cdev->bus, i2cdev->addr, buf, len);
16 }
17 
18 void qi2c_recv(QI2CDevice *i2cdev, uint8_t *buf, uint16_t len)
19 {
20     i2cdev->bus->recv(i2cdev->bus, i2cdev->addr, buf, len);
21 }
22 
23 void i2c_read_block(QI2CDevice *i2cdev, uint8_t reg,
24                     uint8_t *buf, uint16_t len)
25 {
26     qi2c_send(i2cdev, &reg, 1);
27     qi2c_recv(i2cdev, buf, len);
28 }
29 
30 void i2c_write_block(QI2CDevice *i2cdev, uint8_t reg,
31                      const uint8_t *buf, uint16_t len)
32 {
33     uint8_t *cmd = g_malloc(len + 1);
34     cmd[0] = reg;
35     memcpy(&cmd[1], buf, len);
36     qi2c_send(i2cdev, cmd, len + 1);
37     g_free(cmd);
38 }
39 
40 uint8_t i2c_get8(QI2CDevice *i2cdev, uint8_t reg)
41 {
42     uint8_t resp[1];
43     i2c_read_block(i2cdev, reg, resp, sizeof(resp));
44     return resp[0];
45 }
46 
47 uint16_t i2c_get16(QI2CDevice *i2cdev, uint8_t reg)
48 {
49     uint8_t resp[2];
50     i2c_read_block(i2cdev, reg, resp, sizeof(resp));
51     return (resp[0] << 8) | resp[1];
52 }
53 
54 void i2c_set8(QI2CDevice *i2cdev, uint8_t reg, uint8_t value)
55 {
56     i2c_write_block(i2cdev, reg, &value, 1);
57 }
58 
59 void i2c_set16(QI2CDevice *i2cdev, uint8_t reg, uint16_t value)
60 {
61     uint8_t data[2];
62 
63     data[0] = value >> 8;
64     data[1] = value & 255;
65     i2c_write_block(i2cdev, reg, data, sizeof(data));
66 }
67 
68 void *i2c_device_create(void *i2c_bus, QGuestAllocator *alloc, void *addr)
69 {
70     QI2CDevice *i2cdev = g_new0(QI2CDevice, 1);
71 
72     i2cdev->bus = i2c_bus;
73     if (addr) {
74         i2cdev->addr = ((QI2CAddress *)addr)->addr;
75     }
76     return &i2cdev->obj;
77 }
78 
79 void add_qi2c_address(QOSGraphEdgeOptions *opts, QI2CAddress *addr)
80 {
81     g_assert(addr);
82 
83     opts->arg = addr;
84     opts->size_arg = sizeof(QI2CAddress);
85 }
86