* Copyright (c) 2007 CodeSourcery.
* Written by Paul Brook
*
- * This code is licenced under the LGPL.
+ * This code is licensed under the LGPL.
*/
/* TODO: Implement PEC. */
//#define DEBUG_SMBUS 1
#ifdef DEBUG_SMBUS
-#define DPRINTF(fmt, args...) \
-do { printf("smbus(%02x): " fmt , dev->i2c.address, ##args); } while (0)
-#define BADF(fmt, args...) \
-do { fprintf(stderr, "smbus: error: " fmt , ##args); exit(1);} while (0)
+#define DPRINTF(fmt, ...) \
+do { printf("smbus(%02x): " fmt , dev->i2c.address, ## __VA_ARGS__); } while (0)
+#define BADF(fmt, ...) \
+do { fprintf(stderr, "smbus: error: " fmt , ## __VA_ARGS__); exit(1);} while (0)
#else
-#define DPRINTF(fmt, args...) do {} while(0)
-#define BADF(fmt, args...) \
-do { fprintf(stderr, "smbus: error: " fmt , ##args);} while (0)
+#define DPRINTF(fmt, ...) do {} while(0)
+#define BADF(fmt, ...) \
+do { fprintf(stderr, "smbus: error: " fmt , ## __VA_ARGS__);} while (0)
#endif
enum {
static void smbus_do_quick_cmd(SMBusDevice *dev, int recv)
{
+ SMBusDeviceClass *sc = SMBUS_DEVICE_GET_CLASS(dev);
+
DPRINTF("Quick Command %d\n", recv);
- if (dev->quick_cmd)
- dev->quick_cmd(dev, recv);
+ if (sc->quick_cmd) {
+ sc->quick_cmd(dev, recv);
+ }
}
static void smbus_do_write(SMBusDevice *dev)
{
+ SMBusDeviceClass *sc = SMBUS_DEVICE_GET_CLASS(dev);
+
if (dev->data_len == 0) {
smbus_do_quick_cmd(dev, 0);
} else if (dev->data_len == 1) {
DPRINTF("Send Byte\n");
- if (dev->send_byte) {
- dev->send_byte(dev, dev->data_buf[0]);
+ if (sc->send_byte) {
+ sc->send_byte(dev, dev->data_buf[0]);
}
} else {
dev->command = dev->data_buf[0];
DPRINTF("Command %d len %d\n", dev->command, dev->data_len - 1);
- if (dev->write_data) {
- dev->write_data(dev, dev->command, dev->data_buf + 1,
- dev->data_len - 1);
+ if (sc->write_data) {
+ sc->write_data(dev, dev->command, dev->data_buf + 1,
+ dev->data_len - 1);
}
}
}
-void smbus_i2c_event(i2c_slave *s, enum i2c_event event)
+static void smbus_i2c_event(I2CSlave *s, enum i2c_event event)
{
- SMBusDevice *dev = (SMBusDevice *)s;
+ SMBusDevice *dev = SMBUS_DEVICE(s);
+
switch (event) {
case I2C_START_SEND:
switch (dev->mode) {
}
}
-static int smbus_i2c_recv(i2c_slave *s)
+static int smbus_i2c_recv(I2CSlave *s)
{
- SMBusDevice *dev = (SMBusDevice *)s;
+ SMBusDevice *dev = SMBUS_DEVICE(s);
+ SMBusDeviceClass *sc = SMBUS_DEVICE_GET_CLASS(dev);
int ret;
switch (dev->mode) {
case SMBUS_RECV_BYTE:
- if (dev->receive_byte) {
- ret = dev->receive_byte(dev);
+ if (sc->receive_byte) {
+ ret = sc->receive_byte(dev);
} else {
ret = 0;
}
dev->mode = SMBUS_DONE;
break;
case SMBUS_READ_DATA:
- if (dev->read_data) {
- ret = dev->read_data(dev, dev->command, dev->data_len);
+ if (sc->read_data) {
+ ret = sc->read_data(dev, dev->command, dev->data_len);
dev->data_len++;
} else {
ret = 0;
return ret;
}
-static int smbus_i2c_send(i2c_slave *s, uint8_t data)
+static int smbus_i2c_send(I2CSlave *s, uint8_t data)
{
- SMBusDevice *dev = (SMBusDevice *)s;
+ SMBusDevice *dev = SMBUS_DEVICE(s);
+
switch (dev->mode) {
case SMBUS_WRITE_DATA:
DPRINTF("Write data %02x\n", data);
return 0;
}
-SMBusDevice *smbus_device_init(i2c_bus *bus, int address, int size)
+static int smbus_device_init(I2CSlave *i2c)
{
- SMBusDevice *dev;
+ SMBusDevice *dev = SMBUS_DEVICE(i2c);
+ SMBusDeviceClass *sc = SMBUS_DEVICE_GET_CLASS(dev);
- if (size < sizeof(SMBusDevice))
- hw_error("SMBus struct too small");
-
- dev = (SMBusDevice *)i2c_slave_init(bus, address, size);
- dev->i2c.event = smbus_i2c_event;
- dev->i2c.recv = smbus_i2c_recv;
- dev->i2c.send = smbus_i2c_send;
-
- return dev;
+ return sc->init(dev);
}
/* Master device commands. */
-void smbus_quick_command(i2c_bus *bus, int addr, int read)
+void smbus_quick_command(i2c_bus *bus, uint8_t addr, int read)
{
i2c_start_transfer(bus, addr, read);
i2c_end_transfer(bus);
}
-uint8_t smbus_receive_byte(i2c_bus *bus, int addr)
+uint8_t smbus_receive_byte(i2c_bus *bus, uint8_t addr)
{
uint8_t data;
return data;
}
-void smbus_send_byte(i2c_bus *bus, int addr, uint8_t data)
+void smbus_send_byte(i2c_bus *bus, uint8_t addr, uint8_t data)
{
i2c_start_transfer(bus, addr, 0);
i2c_send(bus, data);
i2c_end_transfer(bus);
}
-uint8_t smbus_read_byte(i2c_bus *bus, int addr, uint8_t command)
+uint8_t smbus_read_byte(i2c_bus *bus, uint8_t addr, uint8_t command)
{
uint8_t data;
i2c_start_transfer(bus, addr, 0);
return data;
}
-void smbus_write_byte(i2c_bus *bus, int addr, uint8_t command, uint8_t data)
+void smbus_write_byte(i2c_bus *bus, uint8_t addr, uint8_t command, uint8_t data)
{
i2c_start_transfer(bus, addr, 0);
i2c_send(bus, command);
i2c_end_transfer(bus);
}
-uint16_t smbus_read_word(i2c_bus *bus, int addr, uint8_t command)
+uint16_t smbus_read_word(i2c_bus *bus, uint8_t addr, uint8_t command)
{
uint16_t data;
i2c_start_transfer(bus, addr, 0);
return data;
}
-void smbus_write_word(i2c_bus *bus, int addr, uint8_t command, uint16_t data)
+void smbus_write_word(i2c_bus *bus, uint8_t addr, uint8_t command, uint16_t data)
{
i2c_start_transfer(bus, addr, 0);
i2c_send(bus, command);
i2c_end_transfer(bus);
}
-int smbus_read_block(i2c_bus *bus, int addr, uint8_t command, uint8_t *data)
+int smbus_read_block(i2c_bus *bus, uint8_t addr, uint8_t command, uint8_t *data)
{
int len;
int i;
return len;
}
-void smbus_write_block(i2c_bus *bus, int addr, uint8_t command, uint8_t *data,
+void smbus_write_block(i2c_bus *bus, uint8_t addr, uint8_t command, uint8_t *data,
int len)
{
int i;
i2c_send(bus, data[i]);
i2c_end_transfer(bus);
}
+
+static void smbus_device_class_init(ObjectClass *klass, void *data)
+{
+ I2CSlaveClass *sc = I2C_SLAVE_CLASS(klass);
+
+ sc->init = smbus_device_init;
+ sc->event = smbus_i2c_event;
+ sc->recv = smbus_i2c_recv;
+ sc->send = smbus_i2c_send;
+}
+
+static const TypeInfo smbus_device_type_info = {
+ .name = TYPE_SMBUS_DEVICE,
+ .parent = TYPE_I2C_SLAVE,
+ .instance_size = sizeof(SMBusDevice),
+ .abstract = true,
+ .class_size = sizeof(SMBusDeviceClass),
+ .class_init = smbus_device_class_init,
+};
+
+static void smbus_device_register_types(void)
+{
+ type_register_static(&smbus_device_type_info);
+}
+
+type_init(smbus_device_register_types)