[PATCH 2/5] hw/i2c: Rename i2c_try_create_slave() as i2c_slave_new()

Philippe Mathieu-Daudé posted 5 patches 5 years, 7 months ago
There is a newer version of this series
[PATCH 2/5] hw/i2c: Rename i2c_try_create_slave() as i2c_slave_new()
Posted by Philippe Mathieu-Daudé 5 years, 7 months ago
We use "new" names for functions that allocate and initialize
device objects: pci_new(), isa_new(), usb_new().
Let's call this one i2c_slave_new(). Since we have to update
all the callers, also let it return a I2CSlave object.

Suggested-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
---
 include/hw/i2c/i2c.h | 2 +-
 hw/arm/aspeed.c      | 4 ++--
 hw/i2c/core.c        | 9 ++++-----
 3 files changed, 7 insertions(+), 8 deletions(-)

diff --git a/include/hw/i2c/i2c.h b/include/hw/i2c/i2c.h
index d6e3d85faf..18efc668f1 100644
--- a/include/hw/i2c/i2c.h
+++ b/include/hw/i2c/i2c.h
@@ -79,8 +79,8 @@ int i2c_send_recv(I2CBus *bus, uint8_t *data, bool send);
 int i2c_send(I2CBus *bus, uint8_t data);
 uint8_t i2c_recv(I2CBus *bus);
 
+I2CSlave *i2c_slave_new(const char *name, uint8_t addr);
 DeviceState *i2c_create_slave(I2CBus *bus, const char *name, uint8_t addr);
-DeviceState *i2c_try_create_slave(const char *name, uint8_t addr);
 bool i2c_realize_and_unref(DeviceState *dev, I2CBus *bus, Error **errp);
 
 /* lm832x.c */
diff --git a/hw/arm/aspeed.c b/hw/arm/aspeed.c
index 1285bf82c0..54ca36e0b6 100644
--- a/hw/arm/aspeed.c
+++ b/hw/arm/aspeed.c
@@ -513,7 +513,7 @@ static void witherspoon_bmc_i2c_init(AspeedMachineState *bmc)
     /* Bus 3: TODO bmp280@77 */
     /* Bus 3: TODO max31785@52 */
     /* Bus 3: TODO dps310@76 */
-    dev = i2c_try_create_slave(TYPE_PCA9552, 0x60);
+    dev = DEVICE(i2c_slave_new(TYPE_PCA9552, 0x60));
     qdev_prop_set_string(dev, "description", "pca1");
     i2c_realize_and_unref(dev, aspeed_i2c_get_bus(&soc->i2c, 3),
                           &error_fatal);
@@ -531,7 +531,7 @@ static void witherspoon_bmc_i2c_init(AspeedMachineState *bmc)
 
     smbus_eeprom_init_one(aspeed_i2c_get_bus(&soc->i2c, 11), 0x51,
                           eeprom_buf);
-    dev = i2c_try_create_slave(TYPE_PCA9552, 0x60);
+    dev = DEVICE(i2c_slave_new(TYPE_PCA9552, 0x60));
     qdev_prop_set_string(dev, "description", "pca0");
     i2c_realize_and_unref(dev, aspeed_i2c_get_bus(&soc->i2c, 11),
                           &error_fatal);
diff --git a/hw/i2c/core.c b/hw/i2c/core.c
index acf34a12d6..6eacb4a463 100644
--- a/hw/i2c/core.c
+++ b/hw/i2c/core.c
@@ -267,13 +267,13 @@ const VMStateDescription vmstate_i2c_slave = {
     }
 };
 
-DeviceState *i2c_try_create_slave(const char *name, uint8_t addr)
+I2CSlave *i2c_slave_new(const char *name, uint8_t addr)
 {
     DeviceState *dev;
 
     dev = qdev_new(name);
     qdev_prop_set_uint8(dev, "address", addr);
-    return dev;
+    return I2C_SLAVE(dev);
 }
 
 bool i2c_realize_and_unref(DeviceState *dev, I2CBus *bus, Error **errp)
@@ -283,10 +283,9 @@ bool i2c_realize_and_unref(DeviceState *dev, I2CBus *bus, Error **errp)
 
 DeviceState *i2c_create_slave(I2CBus *bus, const char *name, uint8_t addr)
 {
-    DeviceState *dev;
+    DeviceState *dev = DEVICE(i2c_slave_new(name, addr));
 
-    dev = i2c_try_create_slave(name, addr);
-    i2c_realize_and_unref(dev, bus, &error_fatal);
+    i2c_realize_and_unref(I2C_SLAVE(dev), bus, &error_fatal);
 
     return dev;
 }
-- 
2.21.3


Re: [PATCH 2/5] hw/i2c: Rename i2c_try_create_slave() as i2c_slave_new()
Posted by Corey Minyard 5 years, 7 months ago
On Mon, Jun 29, 2020 at 07:38:18PM +0200, Philippe Mathieu-Daudé wrote:
> We use "new" names for functions that allocate and initialize
> device objects: pci_new(), isa_new(), usb_new().
> Let's call this one i2c_slave_new(). Since we have to update
> all the callers, also let it return a I2CSlave object.

Reviewed-by: Corey Minyard <cminyard@mvista.com>


> 
> Suggested-by: Markus Armbruster <armbru@redhat.com>
> Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
> ---
>  include/hw/i2c/i2c.h | 2 +-
>  hw/arm/aspeed.c      | 4 ++--
>  hw/i2c/core.c        | 9 ++++-----
>  3 files changed, 7 insertions(+), 8 deletions(-)
> 
> diff --git a/include/hw/i2c/i2c.h b/include/hw/i2c/i2c.h
> index d6e3d85faf..18efc668f1 100644
> --- a/include/hw/i2c/i2c.h
> +++ b/include/hw/i2c/i2c.h
> @@ -79,8 +79,8 @@ int i2c_send_recv(I2CBus *bus, uint8_t *data, bool send);
>  int i2c_send(I2CBus *bus, uint8_t data);
>  uint8_t i2c_recv(I2CBus *bus);
>  
> +I2CSlave *i2c_slave_new(const char *name, uint8_t addr);
>  DeviceState *i2c_create_slave(I2CBus *bus, const char *name, uint8_t addr);
> -DeviceState *i2c_try_create_slave(const char *name, uint8_t addr);
>  bool i2c_realize_and_unref(DeviceState *dev, I2CBus *bus, Error **errp);
>  
>  /* lm832x.c */
> diff --git a/hw/arm/aspeed.c b/hw/arm/aspeed.c
> index 1285bf82c0..54ca36e0b6 100644
> --- a/hw/arm/aspeed.c
> +++ b/hw/arm/aspeed.c
> @@ -513,7 +513,7 @@ static void witherspoon_bmc_i2c_init(AspeedMachineState *bmc)
>      /* Bus 3: TODO bmp280@77 */
>      /* Bus 3: TODO max31785@52 */
>      /* Bus 3: TODO dps310@76 */
> -    dev = i2c_try_create_slave(TYPE_PCA9552, 0x60);
> +    dev = DEVICE(i2c_slave_new(TYPE_PCA9552, 0x60));
>      qdev_prop_set_string(dev, "description", "pca1");
>      i2c_realize_and_unref(dev, aspeed_i2c_get_bus(&soc->i2c, 3),
>                            &error_fatal);
> @@ -531,7 +531,7 @@ static void witherspoon_bmc_i2c_init(AspeedMachineState *bmc)
>  
>      smbus_eeprom_init_one(aspeed_i2c_get_bus(&soc->i2c, 11), 0x51,
>                            eeprom_buf);
> -    dev = i2c_try_create_slave(TYPE_PCA9552, 0x60);
> +    dev = DEVICE(i2c_slave_new(TYPE_PCA9552, 0x60));
>      qdev_prop_set_string(dev, "description", "pca0");
>      i2c_realize_and_unref(dev, aspeed_i2c_get_bus(&soc->i2c, 11),
>                            &error_fatal);
> diff --git a/hw/i2c/core.c b/hw/i2c/core.c
> index acf34a12d6..6eacb4a463 100644
> --- a/hw/i2c/core.c
> +++ b/hw/i2c/core.c
> @@ -267,13 +267,13 @@ const VMStateDescription vmstate_i2c_slave = {
>      }
>  };
>  
> -DeviceState *i2c_try_create_slave(const char *name, uint8_t addr)
> +I2CSlave *i2c_slave_new(const char *name, uint8_t addr)
>  {
>      DeviceState *dev;
>  
>      dev = qdev_new(name);
>      qdev_prop_set_uint8(dev, "address", addr);
> -    return dev;
> +    return I2C_SLAVE(dev);
>  }
>  
>  bool i2c_realize_and_unref(DeviceState *dev, I2CBus *bus, Error **errp)
> @@ -283,10 +283,9 @@ bool i2c_realize_and_unref(DeviceState *dev, I2CBus *bus, Error **errp)
>  
>  DeviceState *i2c_create_slave(I2CBus *bus, const char *name, uint8_t addr)
>  {
> -    DeviceState *dev;
> +    DeviceState *dev = DEVICE(i2c_slave_new(name, addr));
>  
> -    dev = i2c_try_create_slave(name, addr);
> -    i2c_realize_and_unref(dev, bus, &error_fatal);
> +    i2c_realize_and_unref(I2C_SLAVE(dev), bus, &error_fatal);
>  
>      return dev;
>  }
> -- 
> 2.21.3
> 
> 

Re: [PATCH 2/5] hw/i2c: Rename i2c_try_create_slave() as i2c_slave_new()
Posted by BALATON Zoltan 5 years, 7 months ago
On Mon, 29 Jun 2020, Philippe Mathieu-Daudé wrote:
> We use "new" names for functions that allocate and initialize
> device objects: pci_new(), isa_new(), usb_new().
> Let's call this one i2c_slave_new(). Since we have to update
> all the callers, also let it return a I2CSlave object.

All the callers now need a cast due to change to I2CSlave * instead of 
what they expect. Does that really worth it? Also this introduces 
inconsistency between i2c_create_slave and i2c_new so not sure about that 
part but I don't really mind either way. Maybe return what most callers 
expect so the calls are simple and don't need an additional cast in most 
of the cases?

Regards,
BALATON Zoltan

> Suggested-by: Markus Armbruster <armbru@redhat.com>
> Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
> ---
> include/hw/i2c/i2c.h | 2 +-
> hw/arm/aspeed.c      | 4 ++--
> hw/i2c/core.c        | 9 ++++-----
> 3 files changed, 7 insertions(+), 8 deletions(-)
>
> diff --git a/include/hw/i2c/i2c.h b/include/hw/i2c/i2c.h
> index d6e3d85faf..18efc668f1 100644
> --- a/include/hw/i2c/i2c.h
> +++ b/include/hw/i2c/i2c.h
> @@ -79,8 +79,8 @@ int i2c_send_recv(I2CBus *bus, uint8_t *data, bool send);
> int i2c_send(I2CBus *bus, uint8_t data);
> uint8_t i2c_recv(I2CBus *bus);
>
> +I2CSlave *i2c_slave_new(const char *name, uint8_t addr);
> DeviceState *i2c_create_slave(I2CBus *bus, const char *name, uint8_t addr);
> -DeviceState *i2c_try_create_slave(const char *name, uint8_t addr);
> bool i2c_realize_and_unref(DeviceState *dev, I2CBus *bus, Error **errp);
>
> /* lm832x.c */
> diff --git a/hw/arm/aspeed.c b/hw/arm/aspeed.c
> index 1285bf82c0..54ca36e0b6 100644
> --- a/hw/arm/aspeed.c
> +++ b/hw/arm/aspeed.c
> @@ -513,7 +513,7 @@ static void witherspoon_bmc_i2c_init(AspeedMachineState *bmc)
>     /* Bus 3: TODO bmp280@77 */
>     /* Bus 3: TODO max31785@52 */
>     /* Bus 3: TODO dps310@76 */
> -    dev = i2c_try_create_slave(TYPE_PCA9552, 0x60);
> +    dev = DEVICE(i2c_slave_new(TYPE_PCA9552, 0x60));
>     qdev_prop_set_string(dev, "description", "pca1");
>     i2c_realize_and_unref(dev, aspeed_i2c_get_bus(&soc->i2c, 3),
>                           &error_fatal);
> @@ -531,7 +531,7 @@ static void witherspoon_bmc_i2c_init(AspeedMachineState *bmc)
>
>     smbus_eeprom_init_one(aspeed_i2c_get_bus(&soc->i2c, 11), 0x51,
>                           eeprom_buf);
> -    dev = i2c_try_create_slave(TYPE_PCA9552, 0x60);
> +    dev = DEVICE(i2c_slave_new(TYPE_PCA9552, 0x60));
>     qdev_prop_set_string(dev, "description", "pca0");
>     i2c_realize_and_unref(dev, aspeed_i2c_get_bus(&soc->i2c, 11),
>                           &error_fatal);
> diff --git a/hw/i2c/core.c b/hw/i2c/core.c
> index acf34a12d6..6eacb4a463 100644
> --- a/hw/i2c/core.c
> +++ b/hw/i2c/core.c
> @@ -267,13 +267,13 @@ const VMStateDescription vmstate_i2c_slave = {
>     }
> };
>
> -DeviceState *i2c_try_create_slave(const char *name, uint8_t addr)
> +I2CSlave *i2c_slave_new(const char *name, uint8_t addr)
> {
>     DeviceState *dev;
>
>     dev = qdev_new(name);
>     qdev_prop_set_uint8(dev, "address", addr);
> -    return dev;
> +    return I2C_SLAVE(dev);
> }
>
> bool i2c_realize_and_unref(DeviceState *dev, I2CBus *bus, Error **errp)
> @@ -283,10 +283,9 @@ bool i2c_realize_and_unref(DeviceState *dev, I2CBus *bus, Error **errp)
>
> DeviceState *i2c_create_slave(I2CBus *bus, const char *name, uint8_t addr)
> {
> -    DeviceState *dev;
> +    DeviceState *dev = DEVICE(i2c_slave_new(name, addr));
>
> -    dev = i2c_try_create_slave(name, addr);
> -    i2c_realize_and_unref(dev, bus, &error_fatal);
> +    i2c_realize_and_unref(I2C_SLAVE(dev), bus, &error_fatal);
>
>     return dev;
> }
>
Re: [PATCH 2/5] hw/i2c: Rename i2c_try_create_slave() as i2c_slave_new()
Posted by Philippe Mathieu-Daudé 5 years, 7 months ago
On 6/29/20 11:37 PM, BALATON Zoltan wrote:
> On Mon, 29 Jun 2020, Philippe Mathieu-Daudé wrote:
>> We use "new" names for functions that allocate and initialize
>> device objects: pci_new(), isa_new(), usb_new().
>> Let's call this one i2c_slave_new(). Since we have to update
>> all the callers, also let it return a I2CSlave object.
> 
> All the callers now need a cast due to change to I2CSlave * instead of
> what they expect. Does that really worth it? Also this introduces
> inconsistency between i2c_create_slave and i2c_new so not sure about
> that part but I don't really mind either way. Maybe return what most
> callers expect so the calls are simple and don't need an additional cast
> in most of the cases?

You are right that the code guidance is not clear regarding what
qdev_foo_new() should return.

Working on another object (LEDs) Richard suggested me to return
the full type, so I understood it was the recommended default:
https://www.mail-archive.com/qemu-devel@nongnu.org/msg714729.html

> 
> Regards,
> BALATON Zoltan
> 
>> Suggested-by: Markus Armbruster <armbru@redhat.com>
>> Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
>> ---
>> include/hw/i2c/i2c.h | 2 +-
>> hw/arm/aspeed.c      | 4 ++--
>> hw/i2c/core.c        | 9 ++++-----
>> 3 files changed, 7 insertions(+), 8 deletions(-)
>>
>> diff --git a/include/hw/i2c/i2c.h b/include/hw/i2c/i2c.h
>> index d6e3d85faf..18efc668f1 100644
>> --- a/include/hw/i2c/i2c.h
>> +++ b/include/hw/i2c/i2c.h
>> @@ -79,8 +79,8 @@ int i2c_send_recv(I2CBus *bus, uint8_t *data, bool
>> send);
>> int i2c_send(I2CBus *bus, uint8_t data);
>> uint8_t i2c_recv(I2CBus *bus);
>>
>> +I2CSlave *i2c_slave_new(const char *name, uint8_t addr);
>> DeviceState *i2c_create_slave(I2CBus *bus, const char *name, uint8_t
>> addr);
>> -DeviceState *i2c_try_create_slave(const char *name, uint8_t addr);
>> bool i2c_realize_and_unref(DeviceState *dev, I2CBus *bus, Error **errp);
>>
>> /* lm832x.c */
>> diff --git a/hw/arm/aspeed.c b/hw/arm/aspeed.c
>> index 1285bf82c0..54ca36e0b6 100644
>> --- a/hw/arm/aspeed.c
>> +++ b/hw/arm/aspeed.c
>> @@ -513,7 +513,7 @@ static void
>> witherspoon_bmc_i2c_init(AspeedMachineState *bmc)
>>     /* Bus 3: TODO bmp280@77 */
>>     /* Bus 3: TODO max31785@52 */
>>     /* Bus 3: TODO dps310@76 */
>> -    dev = i2c_try_create_slave(TYPE_PCA9552, 0x60);
>> +    dev = DEVICE(i2c_slave_new(TYPE_PCA9552, 0x60));
>>     qdev_prop_set_string(dev, "description", "pca1");
>>     i2c_realize_and_unref(dev, aspeed_i2c_get_bus(&soc->i2c, 3),
>>                           &error_fatal);
>> @@ -531,7 +531,7 @@ static void
>> witherspoon_bmc_i2c_init(AspeedMachineState *bmc)
>>
>>     smbus_eeprom_init_one(aspeed_i2c_get_bus(&soc->i2c, 11), 0x51,
>>                           eeprom_buf);
>> -    dev = i2c_try_create_slave(TYPE_PCA9552, 0x60);
>> +    dev = DEVICE(i2c_slave_new(TYPE_PCA9552, 0x60));
>>     qdev_prop_set_string(dev, "description", "pca0");
>>     i2c_realize_and_unref(dev, aspeed_i2c_get_bus(&soc->i2c, 11),
>>                           &error_fatal);
>> diff --git a/hw/i2c/core.c b/hw/i2c/core.c
>> index acf34a12d6..6eacb4a463 100644
>> --- a/hw/i2c/core.c
>> +++ b/hw/i2c/core.c
>> @@ -267,13 +267,13 @@ const VMStateDescription vmstate_i2c_slave = {
>>     }
>> };
>>
>> -DeviceState *i2c_try_create_slave(const char *name, uint8_t addr)
>> +I2CSlave *i2c_slave_new(const char *name, uint8_t addr)
>> {
>>     DeviceState *dev;
>>
>>     dev = qdev_new(name);
>>     qdev_prop_set_uint8(dev, "address", addr);
>> -    return dev;
>> +    return I2C_SLAVE(dev);
>> }
>>
>> bool i2c_realize_and_unref(DeviceState *dev, I2CBus *bus, Error **errp)
>> @@ -283,10 +283,9 @@ bool i2c_realize_and_unref(DeviceState *dev,
>> I2CBus *bus, Error **errp)
>>
>> DeviceState *i2c_create_slave(I2CBus *bus, const char *name, uint8_t
>> addr)
>> {
>> -    DeviceState *dev;
>> +    DeviceState *dev = DEVICE(i2c_slave_new(name, addr));
>>
>> -    dev = i2c_try_create_slave(name, addr);
>> -    i2c_realize_and_unref(dev, bus, &error_fatal);
>> +    i2c_realize_and_unref(I2C_SLAVE(dev), bus, &error_fatal);
>>
>>     return dev;
>> }
>>

Re: [PATCH 2/5] hw/i2c: Rename i2c_try_create_slave() as i2c_slave_new()
Posted by Markus Armbruster 5 years, 7 months ago
BALATON Zoltan <balaton@eik.bme.hu> writes:

> On Mon, 29 Jun 2020, Philippe Mathieu-Daudé wrote:
>> We use "new" names for functions that allocate and initialize
>> device objects: pci_new(), isa_new(), usb_new().
>> Let's call this one i2c_slave_new(). Since we have to update
>> all the callers, also let it return a I2CSlave object.
>
> All the callers now need a cast due to change to I2CSlave * instead of

Actually, all but one; I'll mark it inline.

> what they expect. Does that really worth it? Also this introduces
> inconsistency between i2c_create_slave and i2c_new so not sure about

For what it's worth, this inconsistency is healed in PATCH 4.

> that part but I don't really mind either way. Maybe return what most
> callers expect so the calls are simple and don't need an additional
> cast in most of the cases?

I'd prefer consistency with similar FOO_new() functions for abstract
devices plugging into a FOOBus: pci_new(), isa_new(), usb_new().

> Regards,
> BALATON Zoltan
>
>> Suggested-by: Markus Armbruster <armbru@redhat.com>
>> Signed-off-by: Philippe Mathieu-Daudé <f4bug@amsat.org>
>> ---
>> include/hw/i2c/i2c.h | 2 +-
>> hw/arm/aspeed.c      | 4 ++--
>> hw/i2c/core.c        | 9 ++++-----
>> 3 files changed, 7 insertions(+), 8 deletions(-)
>>
>> diff --git a/include/hw/i2c/i2c.h b/include/hw/i2c/i2c.h
>> index d6e3d85faf..18efc668f1 100644
>> --- a/include/hw/i2c/i2c.h
>> +++ b/include/hw/i2c/i2c.h
>> @@ -79,8 +79,8 @@ int i2c_send_recv(I2CBus *bus, uint8_t *data, bool send);
>> int i2c_send(I2CBus *bus, uint8_t data);
>> uint8_t i2c_recv(I2CBus *bus);
>>
>> +I2CSlave *i2c_slave_new(const char *name, uint8_t addr);
>> DeviceState *i2c_create_slave(I2CBus *bus, const char *name, uint8_t addr);
>> -DeviceState *i2c_try_create_slave(const char *name, uint8_t addr);
>> bool i2c_realize_and_unref(DeviceState *dev, I2CBus *bus, Error **errp);
>>
>> /* lm832x.c */
>> diff --git a/hw/arm/aspeed.c b/hw/arm/aspeed.c
>> index 1285bf82c0..54ca36e0b6 100644
>> --- a/hw/arm/aspeed.c
>> +++ b/hw/arm/aspeed.c
>> @@ -513,7 +513,7 @@ static void witherspoon_bmc_i2c_init(AspeedMachineState *bmc)
>>     /* Bus 3: TODO bmp280@77 */
>>     /* Bus 3: TODO max31785@52 */
>>     /* Bus 3: TODO dps310@76 */
>> -    dev = i2c_try_create_slave(TYPE_PCA9552, 0x60);
>> +    dev = DEVICE(i2c_slave_new(TYPE_PCA9552, 0x60));
>>     qdev_prop_set_string(dev, "description", "pca1");
>>     i2c_realize_and_unref(dev, aspeed_i2c_get_bus(&soc->i2c, 3),
>>                           &error_fatal);
>> @@ -531,7 +531,7 @@ static void witherspoon_bmc_i2c_init(AspeedMachineState *bmc)
>>
>>     smbus_eeprom_init_one(aspeed_i2c_get_bus(&soc->i2c, 11), 0x51,
>>                           eeprom_buf);
>> -    dev = i2c_try_create_slave(TYPE_PCA9552, 0x60);
>> +    dev = DEVICE(i2c_slave_new(TYPE_PCA9552, 0x60));
>>     qdev_prop_set_string(dev, "description", "pca0");
>>     i2c_realize_and_unref(dev, aspeed_i2c_get_bus(&soc->i2c, 11),
>>                           &error_fatal);
>> diff --git a/hw/i2c/core.c b/hw/i2c/core.c
>> index acf34a12d6..6eacb4a463 100644
>> --- a/hw/i2c/core.c
>> +++ b/hw/i2c/core.c
>> @@ -267,13 +267,13 @@ const VMStateDescription vmstate_i2c_slave = {
>>     }
>> };
>>
>> -DeviceState *i2c_try_create_slave(const char *name, uint8_t addr)
>> +I2CSlave *i2c_slave_new(const char *name, uint8_t addr)
>> {
>>     DeviceState *dev;
>>
>>     dev = qdev_new(name);
>>     qdev_prop_set_uint8(dev, "address", addr);
>> -    return dev;
>> +    return I2C_SLAVE(dev);
>> }
>>
>> bool i2c_realize_and_unref(DeviceState *dev, I2CBus *bus, Error **errp)
>> @@ -283,10 +283,9 @@ bool i2c_realize_and_unref(DeviceState *dev, I2CBus *bus, Error **errp)
>>
>> DeviceState *i2c_create_slave(I2CBus *bus, const char *name, uint8_t addr)
>> {
>> -    DeviceState *dev;
>> +    DeviceState *dev = DEVICE(i2c_slave_new(name, addr));
>>
>> -    dev = i2c_try_create_slave(name, addr);
>> -    i2c_realize_and_unref(dev, bus, &error_fatal);
>> +    i2c_realize_and_unref(I2C_SLAVE(dev), bus, &error_fatal);
>>
>>     return dev;
>> }

Fewer casts:

   DeviceState *i2c_create_slave(I2CBus *bus, const char *name, uint8_t addr)
   {
        I2CSlave *dev = i2c_slave_new(name, addr );

        i2c_realize_and_unref(dev, bus, &error_fatal);
        return DEVICE(dev);
   }

It's all the same after PATCH 4.  You decide whether to polish the
intermediate state.

Reviewed-by: Markus Armbruster <armbru@redhat.com>