4 * Copyright (c) 2012 Andreas Färber
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.
9 #include "qemu/osdep.h"
10 #include "libqos/i2c.h"
13 void i2c_send(QI2CDevice
*i2cdev
, const uint8_t *buf
, uint16_t len
)
15 i2cdev
->bus
->send(i2cdev
->bus
, i2cdev
->addr
, buf
, len
);
18 void i2c_recv(QI2CDevice
*i2cdev
, uint8_t *buf
, uint16_t len
)
20 i2cdev
->bus
->recv(i2cdev
->bus
, i2cdev
->addr
, buf
, len
);
23 void i2c_read_block(QI2CDevice
*i2cdev
, uint8_t reg
,
24 uint8_t *buf
, uint16_t len
)
26 i2c_send(i2cdev
, ®
, 1);
27 i2c_recv(i2cdev
, buf
, len
);
30 void i2c_write_block(QI2CDevice
*i2cdev
, uint8_t reg
,
31 const uint8_t *buf
, uint16_t len
)
33 uint8_t *cmd
= g_malloc(len
+ 1);
35 memcpy(&cmd
[1], buf
, len
);
36 i2c_send(i2cdev
, cmd
, len
+ 1);
40 uint8_t i2c_get8(QI2CDevice
*i2cdev
, uint8_t reg
)
43 i2c_read_block(i2cdev
, reg
, resp
, sizeof(resp
));
47 uint16_t i2c_get16(QI2CDevice
*i2cdev
, uint8_t reg
)
50 i2c_read_block(i2cdev
, reg
, resp
, sizeof(resp
));
51 return (resp
[0] << 8) | resp
[1];
54 void i2c_set8(QI2CDevice
*i2cdev
, uint8_t reg
, uint8_t value
)
56 i2c_write_block(i2cdev
, reg
, &value
, 1);
59 void i2c_set16(QI2CDevice
*i2cdev
, uint8_t reg
, uint16_t value
)
64 data
[1] = value
& 255;
65 i2c_write_block(i2cdev
, reg
, data
, sizeof(data
));
68 void *i2c_device_create(void *i2c_bus
, QGuestAllocator
*alloc
, void *addr
)
70 QI2CDevice
*i2cdev
= g_new0(QI2CDevice
, 1);
72 i2cdev
->bus
= i2c_bus
;
74 i2cdev
->addr
= ((QI2CAddress
*)addr
)->addr
;
79 void add_qi2c_address(QOSGraphEdgeOptions
*opts
, QI2CAddress
*addr
)
84 opts
->size_arg
= sizeof(QI2CAddress
);