iio: magnetometer: yas530: Add volatile registers to "chip_info"
authorJakob Hauser <jahau@rocketmail.com>
Fri, 12 Aug 2022 21:54:15 +0000 (23:54 +0200)
committerJonathan Cameron <Jonathan.Cameron@huawei.com>
Mon, 15 Aug 2022 21:30:03 +0000 (22:30 +0100)
Add volatile registers to the "chip_info" structure to ease the handling of
different YAS variants.

Signed-off-by: Jakob Hauser <jahau@rocketmail.com>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>
Link: https://lore.kernel.org/r/aeba3877933ba9d2c920b459a9037d9186c15a4f.1660337264.git.jahau@rocketmail.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
drivers/iio/magnetometer/yamaha-yas530.c

index 4fe7e8c820c3482e4c9458ea90226fa4656f8dfc..fa317b975f8fcc0558b5dbdbbf971bd42d2034d9 100644 (file)
@@ -102,6 +102,11 @@ enum chip_ids {
        yas533,
 };
 
+static const int yas530_volatile_reg[] = {
+       YAS530_ACTUATE_INIT_COIL,
+       YAS530_MEASURE,
+};
+
 struct yas5xx_calibration {
        /* Linearization calibration x, y1, y2 */
        s32 r[3];
@@ -123,11 +128,15 @@ struct yas5xx;
  * @devid: device ID number
  * @product_name: product name of the YAS variant
  * @version_names: version letters or namings
+ * @volatile_reg: device-specific volatile registers
+ * @volatile_reg_qty: quantity of device-specific volatile registers
  */
 struct yas5xx_chip_info {
        unsigned int devid;
        char *product_name;
        char *version_names[2];
+       const int *volatile_reg;
+       int volatile_reg_qty;
 };
 
 /**
@@ -616,9 +625,26 @@ static const struct iio_info yas5xx_info = {
 
 static bool yas5xx_volatile_reg(struct device *dev, unsigned int reg)
 {
-       return reg == YAS530_ACTUATE_INIT_COIL ||
-               reg == YAS530_MEASURE ||
-               (reg >= YAS5XX_MEASURE_DATA && reg < YAS5XX_MEASURE_DATA + 8);
+       struct iio_dev *indio_dev = dev_get_drvdata(dev);
+       struct yas5xx *yas5xx = iio_priv(indio_dev);
+       const struct yas5xx_chip_info *ci = yas5xx->chip_info;
+       int reg_qty;
+       int i;
+
+       if (reg >= YAS5XX_MEASURE_DATA && reg < YAS5XX_MEASURE_DATA + 8)
+               return true;
+
+       /*
+        * YAS versions share different registers on the same address,
+        * need to differentiate.
+        */
+       reg_qty = ci->volatile_reg_qty;
+       for (i = 0; i < reg_qty; i++) {
+               if (reg == ci->volatile_reg[i])
+                       return true;
+       }
+
+       return false;
 }
 
 /* TODO: enable regmap cache, using mark dirty and sync at runtime resume */
@@ -923,16 +949,22 @@ static const struct yas5xx_chip_info yas5xx_chip_info_tbl[] = {
                .devid = YAS530_DEVICE_ID,
                .product_name = "YAS530 MS-3E",
                .version_names = { "A", "B" },
+               .volatile_reg = yas530_volatile_reg,
+               .volatile_reg_qty = ARRAY_SIZE(yas530_volatile_reg),
        },
        [yas532] = {
                .devid = YAS532_DEVICE_ID,
                .product_name = "YAS532 MS-3R",
                .version_names = { "AB", "AC" },
+               .volatile_reg = yas530_volatile_reg,
+               .volatile_reg_qty = ARRAY_SIZE(yas530_volatile_reg),
        },
        [yas533] = {
                .devid = YAS532_DEVICE_ID,
                .product_name = "YAS533 MS-3F",
                .version_names = { "AB", "AC" },
+               .volatile_reg = yas530_volatile_reg,
+               .volatile_reg_qty = ARRAY_SIZE(yas530_volatile_reg),
        },
 };