iio: accel: adxl345: Convert enum->pointer for data in match data table
authorBiju Das <biju.das.jz@bp.renesas.com>
Sun, 3 Sep 2023 09:00:50 +0000 (10:00 +0100)
committerJonathan Cameron <jonathan.cameron@huawei.com>
Tue, 12 Sep 2023 09:42:04 +0000 (10:42 +0100)
Convert enum->pointer for data in match data table, so that
device_get_match_data() can do match against OF/ACPI/I2C tables, once i2c
bus type match support added to it.

Add struct adxl345_chip_info and replace enum->adxl345_chip_info in the
match table and simplify adxl345_probe().

Signed-off-by: Biju Das <biju.das.jz@bp.renesas.com>
Link: https://lore.kernel.org/r/20230903090051.39274-2-biju.das.jz@bp.renesas.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
drivers/iio/accel/adxl345.h
drivers/iio/accel/adxl345_core.c
drivers/iio/accel/adxl345_i2c.c
drivers/iio/accel/adxl345_spi.c

index d7e67cb08538324fcca18b0ce85b7fac4e08d298..826e0575ae7959c2f6bc373199ce3b1a8759ac64 100644 (file)
@@ -13,6 +13,11 @@ enum adxl345_device_type {
        ADXL375 = 2,
 };
 
+struct adxl345_chip_info {
+       const char *name;
+       unsigned int type;
+};
+
 int adxl345_core_probe(struct device *dev, struct regmap *regmap);
 
 #endif /* _ADXL345_H_ */
index 1919e0089c1156937fff00810463839c3e72dd44..28f77b5de47dc020932ef5e0e388d385a59f2d2a 100644 (file)
@@ -61,9 +61,9 @@ static const int adxl345_uscale = 38300;
 static const int adxl375_uscale = 480000;
 
 struct adxl345_data {
+       const struct adxl345_chip_info *info;
        struct regmap *regmap;
        u8 data_range;
-       enum adxl345_device_type type;
 };
 
 #define ADXL345_CHANNEL(index, axis) {                                 \
@@ -110,7 +110,7 @@ static int adxl345_read_raw(struct iio_dev *indio_dev,
                return IIO_VAL_INT;
        case IIO_CHAN_INFO_SCALE:
                *val = 0;
-               switch (data->type) {
+               switch (data->info->type) {
                case ADXL345:
                        *val2 = adxl345_uscale;
                        break;
@@ -222,25 +222,11 @@ static void adxl345_powerdown(void *regmap)
 
 int adxl345_core_probe(struct device *dev, struct regmap *regmap)
 {
-       enum adxl345_device_type type;
        struct adxl345_data *data;
        struct iio_dev *indio_dev;
-       const char *name;
        u32 regval;
        int ret;
 
-       type = (uintptr_t)device_get_match_data(dev);
-       switch (type) {
-       case ADXL345:
-               name = "adxl345";
-               break;
-       case ADXL375:
-               name = "adxl375";
-               break;
-       default:
-               return -EINVAL;
-       }
-
        ret = regmap_read(regmap, ADXL345_REG_DEVID, &regval);
        if (ret < 0)
                return dev_err_probe(dev, ret, "Error reading device ID\n");
@@ -255,16 +241,18 @@ int adxl345_core_probe(struct device *dev, struct regmap *regmap)
 
        data = iio_priv(indio_dev);
        data->regmap = regmap;
-       data->type = type;
        /* Enable full-resolution mode */
        data->data_range = ADXL345_DATA_FORMAT_FULL_RES;
+       data->info = device_get_match_data(dev);
+       if (!data->info)
+               return -ENODEV;
 
        ret = regmap_write(data->regmap, ADXL345_REG_DATA_FORMAT,
                           data->data_range);
        if (ret < 0)
                return dev_err_probe(dev, ret, "Failed to set data range\n");
 
-       indio_dev->name = name;
+       indio_dev->name = data->info->name;
        indio_dev->info = &adxl345_info;
        indio_dev->modes = INDIO_DIRECT_MODE;
        indio_dev->channels = adxl345_channels;
index e47d12f19602c28b8076a0e75f10d6ea6a51563f..8cb6254297f78526bdea730b47300eb28f121260 100644 (file)
@@ -30,22 +30,32 @@ static int adxl345_i2c_probe(struct i2c_client *client)
        return adxl345_core_probe(&client->dev, regmap);
 }
 
+static const struct adxl345_chip_info adxl345_i2c_info = {
+       .name = "adxl345",
+       .type = ADXL345,
+};
+
+static const struct adxl345_chip_info adxl375_i2c_info = {
+       .name = "adxl375",
+       .type = ADXL375,
+};
+
 static const struct i2c_device_id adxl345_i2c_id[] = {
-       { "adxl345", ADXL345 },
-       { "adxl375", ADXL375 },
+       { "adxl345", (kernel_ulong_t)&adxl345_i2c_info },
+       { "adxl375", (kernel_ulong_t)&adxl375_i2c_info },
        { }
 };
 MODULE_DEVICE_TABLE(i2c, adxl345_i2c_id);
 
 static const struct of_device_id adxl345_of_match[] = {
-       { .compatible = "adi,adxl345", .data = (const void *)ADXL345 },
-       { .compatible = "adi,adxl375", .data = (const void *)ADXL375 },
+       { .compatible = "adi,adxl345", .data = &adxl345_i2c_info },
+       { .compatible = "adi,adxl375", .data = &adxl375_i2c_info },
        { }
 };
 MODULE_DEVICE_TABLE(of, adxl345_of_match);
 
 static const struct acpi_device_id adxl345_acpi_match[] = {
-       { "ADS0345", ADXL345 },
+       { "ADS0345", (kernel_ulong_t)&adxl345_i2c_info },
        { }
 };
 MODULE_DEVICE_TABLE(acpi, adxl345_acpi_match);
index aaade5808657ae9baeb286289b33550abeb216e9..ede9b949615859cb03520b2d8eb17c678bf566ad 100644 (file)
@@ -36,22 +36,32 @@ static int adxl345_spi_probe(struct spi_device *spi)
        return adxl345_core_probe(&spi->dev, regmap);
 }
 
+static const struct adxl345_chip_info adxl345_spi_info = {
+       .name = "adxl345",
+       .type = ADXL345,
+};
+
+static const struct adxl345_chip_info adxl375_spi_info = {
+       .name = "adxl375",
+       .type = ADXL375,
+};
+
 static const struct spi_device_id adxl345_spi_id[] = {
-       { "adxl345", ADXL345 },
-       { "adxl375", ADXL375 },
+       { "adxl345", (kernel_ulong_t)&adxl345_spi_info },
+       { "adxl375", (kernel_ulong_t)&adxl375_spi_info },
        { }
 };
 MODULE_DEVICE_TABLE(spi, adxl345_spi_id);
 
 static const struct of_device_id adxl345_of_match[] = {
-       { .compatible = "adi,adxl345", .data = (const void *)ADXL345 },
-       { .compatible = "adi,adxl375", .data = (const void *)ADXL375 },
+       { .compatible = "adi,adxl345", .data = &adxl345_spi_info },
+       { .compatible = "adi,adxl375", .data = &adxl375_spi_info },
        { }
 };
 MODULE_DEVICE_TABLE(of, adxl345_of_match);
 
 static const struct acpi_device_id adxl345_acpi_match[] = {
-       { "ADS0345", ADXL345 },
+       { "ADS0345", (kernel_ulong_t)&adxl345_spi_info },
        { }
 };
 MODULE_DEVICE_TABLE(acpi, adxl345_acpi_match);