[PATCH v4 4/7] iio: adc: hx711: introduce hx711_chip_info per-variant structure

Piyush Patle
2026-04-27T10:10:48Z

Add struct hx711_chip_info to hold per-variant static configuration:
device name, IIO channel spec array, IIO info ops pointer, and channel
count. Store a chip_info pointer in hx711_data and populate indio_dev
fields from it at probe time instead of hardcoding them.

Use device_get_match_data() to look up the chip_info pointer from the
of_device_id table. Guard against a NULL return so that the driver
fails cleanly if probed via the platform name match path without
associated match data.

No functional change for existing HX711 users.

Signed-off-by: Piyush Patle <piyushpatle228@gmail.com>
---
Changes in v4:
- New patch. Split out from the v3 refactor patch as requested by
Andy Shevchenko.
- Place hx711_chip_info fields in their final order from the start so
that the HX710B support patch (which adds fixed_gain/fixed_gain_val)
appends new fields without reordering existing ones.
- Add NULL guard on device_get_match_data() return value to handle the
platform alias match path that can bind without match data.
---
drivers/iio/adc/hx711.c | 38 +++++++++++++++++++++++++++++++++-----
1 file changed, 33 insertions(+), 5 deletions(-)

diff --git a/drivers/iio/adc/hx711.c b/drivers/iio/adc/hx711.c
index 32362771cfc8..2a6e9645f54d 100644
--- a/drivers/iio/adc/hx711.c
+++ b/drivers/iio/adc/hx711.c
@@ -76,6 +76,20 @@ static int hx711_get_scale_to_gain(const int *gain_scale, int scale)
return -EINVAL;
}

+/**
+ * struct hx711_chip_info - per-variant static configuration
+ * @name: IIO device name
+ * @channels: channel specification array
+ * @iio_info: IIO info ops for this variant
+ * @num_channels: number of entries in @channels
+ */
+struct hx711_chip_info {
+ const char *name;
+ const struct iio_chan_spec *channels;
+ const struct iio_info *iio_info;
+ unsigned int num_channels;
+};
+
struct hx711_data {
struct device *dev;
struct gpio_desc *gpiod_pd_sck;
@@ -83,6 +97,7 @@ struct hx711_data {
int gain_set; /* gain set on device */
int gain_chan_a; /* gain for channel A */
int gain_scale[HX711_GAIN_MAX];
+ const struct hx711_chip_info *chip_info;
struct mutex lock;
/*
* triggered buffer
@@ -459,10 +474,18 @@ static const struct iio_chan_spec hx711_chan_spec[] = {
IIO_CHAN_SOFT_TIMESTAMP(2),
};

+static const struct hx711_chip_info hx711_chip = {
+ .name = "hx711",
+ .channels = hx711_chan_spec,
+ .iio_info = &hx711_iio_info,
+ .num_channels = ARRAY_SIZE(hx711_chan_spec),
+};
+
static int hx711_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct hx711_data *hx711_data;
+ const struct hx711_chip_info *chip_info;
struct iio_dev *indio_dev;
int ret;
int i;
@@ -476,6 +499,11 @@ static int hx711_probe(struct platform_device *pdev)

mutex_init(&hx711_data->lock);

+ chip_info = device_get_match_data(dev);
+ if (!chip_info)
+ return dev_err_probe(dev, -ENODEV, "missing match data\n");
+ hx711_data->chip_info = chip_info;
+
/*
* PD_SCK stands for power down and serial clock input of HX711
* in the driver it is an output
@@ -537,11 +565,11 @@ static int hx711_probe(struct platform_device *pdev)
hx711_data->data_ready_delay_ns =
1000000000 / hx711_data->clock_frequency;

- indio_dev->name = "hx711";
- indio_dev->info = &hx711_iio_info;
+ indio_dev->name = chip_info->name;
+ indio_dev->info = chip_info->iio_info;
indio_dev->modes = INDIO_DIRECT_MODE;
- indio_dev->channels = hx711_chan_spec;
- indio_dev->num_channels = ARRAY_SIZE(hx711_chan_spec);
+ indio_dev->channels = chip_info->channels;
+ indio_dev->num_channels = chip_info->num_channels;

ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
iio_pollfunc_store_time,
@@ -558,7 +586,7 @@ static int hx711_probe(struct platform_device *pdev)
}

static const struct of_device_id of_hx711_match[] = {
- { .compatible = "avia,hx711", },
+ { .compatible = "avia,hx711", .data = &hx711_chip },
{ }
};

--
2.43.0