All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Daniel P. Berrangé" <berrange@redhat.com>
To: Mark Cave-Ayland <mark.caveayland@nutanix.com>
Cc: alistair@alistair23.me, peter.maydell@linaro.org,
	kfting@nuvoton.com, wuhaotsh@google.com, pbonzini@redhat.com,
	qemu-arm@nongnu.org, qemu-devel@nongnu.org
Subject: Re: [PATCH 2/2] hw/adc/npcm7xx_adc.c: convert NPCM7XX_ADC object props to class props
Date: Wed, 24 Jun 2026 18:04:08 +0100	[thread overview]
Message-ID: <ajwOCBoTqZWJ2lqD@redhat.com> (raw)
In-Reply-To: <20260624153322.1470576-3-mark.caveayland@nutanix.com>

On Wed, Jun 24, 2026 at 04:32:30PM +0100, Mark Cave-Ayland wrote:
> Signed-off-by: Mark Cave-Ayland <mark.caveayland@nutanix.com>
> ---
>  hw/adc/npcm7xx_adc.c | 75 +++++++++++++++++++++++++++++++++++++++-----
>  1 file changed, 68 insertions(+), 7 deletions(-)
> 
> diff --git a/hw/adc/npcm7xx_adc.c b/hw/adc/npcm7xx_adc.c
> index 3584c27c75..accaaf2620 100644
> --- a/hw/adc/npcm7xx_adc.c
> +++ b/hw/adc/npcm7xx_adc.c
> @@ -20,6 +20,8 @@
>  #include "hw/core/qdev-properties.h"
>  #include "hw/core/registerfields.h"
>  #include "migration/vmstate.h"
> +#include "qapi/error.h"
> +#include "qapi/visitor.h"
>  #include "qemu/log.h"
>  #include "qemu/module.h"
>  #include "qemu/timer.h"
> @@ -229,7 +231,6 @@ static void npcm7xx_adc_init(Object *obj)
>  {
>      NPCM7xxADCState *s = NPCM7XX_ADC(obj);
>      SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
> -    int i;
>  
>      sysbus_init_irq(sbd, &s->irq);
>  
> @@ -240,15 +241,63 @@ static void npcm7xx_adc_init(Object *obj)
>      sysbus_init_mmio(sbd, &s->iomem);
>      s->clock = qdev_init_clock_in(DEVICE(s), "clock", NULL, NULL, 0);
>  
> -    for (i = 0; i < NPCM7XX_ADC_NUM_INPUTS; ++i) {
> -        object_property_add_uint32_ptr(obj, "adci[*]",
> -                &s->adci[i], OBJ_PROP_FLAG_READWRITE);
> -    }
> -    object_property_add_uint32_ptr(obj, "vref",
> -            &s->vref, OBJ_PROP_FLAG_WRITE);
>      npcm7xx_adc_calibrate(s);
>  }
>  
> +static void npcm7xx_adc_get_input(Object *obj, Visitor *v,
> +                                  const char *name, void *opaque,
> +                                  Error **errp)
> +{
> +    NPCM7xxADCState *s = NPCM7XX_ADC(obj);
> +    uint32_t idx, val;
> +    int res;
> +
> +    res = sscanf(name, "adci[%u]", &idx);
> +    if (res == EOF || res != 1) {
> +        error_setg(errp, "unable to parse index from %s", name);
> +        return;
> +    }
> +
> +    val = s->adci[idx];
> +    visit_type_uint32(v, name, &val, errp);
> +}
> +
> +static void npcm7xx_adc_set_input(Object *obj, Visitor *v,
> +                                  const char *name, void *opaque,
> +                                  Error **errp)
> +{
> +    NPCM7xxADCState *s = NPCM7XX_ADC(obj);
> +    uint32_t idx, val;
> +    int res;
> +
> +    res = sscanf(name, "adci[%u]", &idx);
> +    if (res == EOF || res != 1) {
> +        error_setg(errp, "unable to parse index from %s", name);
> +        return;
> +    }
> +
> +    val = s->adci[idx];
> +    if (!visit_type_uint32(v, name, &val, errp)) {
> +        return;
> +    }
> +
> +    s->adci[idx] = val;
> +}
> +
> +static void npcm7xx_adc_set_vref(Object *obj, Visitor *v,
> +                                 const char *name, void *opaque,
> +                                 Error **errp)
> +{
> +    NPCM7xxADCState *s = NPCM7XX_ADC(obj);
> +    uint32_t vref = s->vref;
> +
> +    if (!visit_type_uint32(v, name, &vref, errp)) {
> +        return;
> +    }
> +
> +    s->vref = vref;
> +}
> +
>  static const VMStateDescription vmstate_npcm7xx_adc = {
>      .name = "npcm7xx-adc",
>      .version_id = 0,
> @@ -275,6 +324,7 @@ static void npcm7xx_adc_class_init(ObjectClass *klass, const void *data)
>  {
>      ResettableClass *rc = RESETTABLE_CLASS(klass);
>      DeviceClass *dc = DEVICE_CLASS(klass);
> +    int i;
>  
>      dc->desc = "NPCM7xx ADC Module";
>      dc->vmsd = &vmstate_npcm7xx_adc;
> @@ -282,6 +332,17 @@ static void npcm7xx_adc_class_init(ObjectClass *klass, const void *data)
>      rc->phases.hold = npcm7xx_adc_hold_reset;
>  
>      device_class_set_props(dc, npcm7xx_timer_properties);
> +
> +    for (i = 0; i < NPCM7XX_ADC_NUM_INPUTS; ++i) {
> +        object_class_property_add(klass, "adci[*]", "uint32",
> +                                  npcm7xx_adc_get_input,
> +                                  npcm7xx_adc_set_input,
> +                                  NULL, NULL);
> +    }

IMHO the logic around "[*]" expansion is mostly useful for child
properties where you don't know ahead of time how many relationships
might already exist.

In this case though, we know exactly how many props we want and we
know we want to start from 0. 

How about we do this explicitly:

   g_autofree char *adciprop = g_strdup_printf("adci[%u]", i);
   object_class_property_add(klass, adciprop, "uint32",
                             npcm7xx_adc_get_input,
                             npcm7xx_adc_set_input,
                             NULL, NULL);

which avoids the O(N^2) performance of handling '[*]' and
thus makes the prior patch redundant.

I kind of wish we didn't have '[' in the property name but
we're probably stuck with that for back compat.

> +    object_class_property_add(klass, "vref", "uint32",
> +                              NULL,
> +                              npcm7xx_adc_set_vref,
> +                              NULL, NULL);
>  }
>  
>  static const TypeInfo npcm7xx_adc_info = {
> -- 
> 2.43.0
> 

With regards,
Daniel
-- 
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|



      reply	other threads:[~2026-06-24 17:05 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-24 15:32 [PATCH 0/2] hw/adc: convert object props to class props Mark Cave-Ayland
2026-06-24 15:32 ` [PATCH 1/2] qom: add support for array class properties Mark Cave-Ayland
2026-06-24 15:32 ` [PATCH 2/2] hw/adc/npcm7xx_adc.c: convert NPCM7XX_ADC object props to class props Mark Cave-Ayland
2026-06-24 17:04   ` Daniel P. Berrangé [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ajwOCBoTqZWJ2lqD@redhat.com \
    --to=berrange@redhat.com \
    --cc=alistair@alistair23.me \
    --cc=kfting@nuvoton.com \
    --cc=mark.caveayland@nutanix.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-arm@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=wuhaotsh@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.