drivers: iio: adc: ltc2497: LTC2499 support
authorCiprian Regus <ciprian.regus@analog.com>
Fri, 16 Sep 2022 14:09:21 +0000 (17:09 +0300)
committerJonathan Cameron <Jonathan.Cameron@huawei.com>
Wed, 21 Sep 2022 17:42:54 +0000 (18:42 +0100)
The LTC2499 is a 16-channel (eight differential), 24-bit,
ADC with Easy Drive technology and a 2-wire, I2C interface.

Implement support for the LTC2499 ADC by extending the LTC2497
driver. A new chip_info struct is added to differentiate between
chip types and resolutions when reading data from the device.

Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/2499fe.pdf
Signed-off-by: Ciprian Regus <ciprian.regus@analog.com>
Link: https://lore.kernel.org/r/20220916140922.2506248-5-ciprian.regus@analog.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
drivers/iio/adc/ltc2496.c
drivers/iio/adc/ltc2497-core.c
drivers/iio/adc/ltc2497.c
drivers/iio/adc/ltc2497.h

index dfb3bb5..bf89d5a 100644 (file)
@@ -15,6 +15,7 @@
 #include <linux/iio/driver.h>
 #include <linux/module.h>
 #include <linux/mod_devicetable.h>
+#include <linux/property.h>
 
 #include "ltc2497.h"
 
@@ -74,6 +75,7 @@ static int ltc2496_probe(struct spi_device *spi)
        spi_set_drvdata(spi, indio_dev);
        st->spi = spi;
        st->common_ddata.result_and_measure = ltc2496_result_and_measure;
+       st->common_ddata.chip_info = device_get_match_data(dev);
 
        return ltc2497core_probe(dev, indio_dev);
 }
@@ -85,8 +87,12 @@ static void ltc2496_remove(struct spi_device *spi)
        ltc2497core_remove(indio_dev);
 }
 
+static const struct ltc2497_chip_info ltc2496_info = {
+       .resolution = 16,
+};
+
 static const struct of_device_id ltc2496_of_match[] = {
-       { .compatible = "lltc,ltc2496", },
+       { .compatible = "lltc,ltc2496", .data = &ltc2496_info, },
        {},
 };
 MODULE_DEVICE_TABLE(of, ltc2496_of_match);
index 2a485c8..b275239 100644 (file)
@@ -95,7 +95,7 @@ static int ltc2497core_read_raw(struct iio_dev *indio_dev,
                        return ret;
 
                *val = ret / 1000;
-               *val2 = 17;
+               *val2 = ddata->chip_info->resolution + 1;
 
                return IIO_VAL_FRACTIONAL_LOG2;
 
index 78b93c9..bd6a650 100644 (file)
 #include <linux/iio/driver.h>
 #include <linux/module.h>
 #include <linux/mod_devicetable.h>
+#include <linux/property.h>
+
+#include <asm/unaligned.h>
 
 #include "ltc2497.h"
 
+enum ltc2497_chip_type {
+       TYPE_LTC2497,
+       TYPE_LTC2499,
+};
+
 struct ltc2497_driverdata {
        /* this must be the first member */
        struct ltc2497core_driverdata common_ddata;
        struct i2c_client *client;
+       u32 recv_size;
+       u32 sub_lsb;
        /*
         * DMA (thus cache coherency maintenance) may require the
         * transfer buffers to live in their own cache lines.
         */
-       __be32 buf __aligned(IIO_DMA_MINALIGN);
+       union {
+               __be32 d32;
+               u8 d8[3];
+       } data __aligned(IIO_DMA_MINALIGN);
 };
 
 static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata,
@@ -34,13 +47,30 @@ static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata,
        int ret;
 
        if (val) {
-               ret = i2c_master_recv(st->client, (char *)&st->buf, 3);
+               if (st->recv_size == 3)
+                       ret = i2c_master_recv(st->client, (char *)&st->data.d8,
+                                             st->recv_size);
+               else
+                       ret = i2c_master_recv(st->client, (char *)&st->data.d32,
+                                             st->recv_size);
                if (ret < 0) {
                        dev_err(&st->client->dev, "i2c_master_recv failed\n");
                        return ret;
                }
 
-               *val = (be32_to_cpu(st->buf) >> 14) - (1 << 17);
+               /*
+                * The data format is 16/24 bit 2s complement, but with an upper sign bit on the
+                * resolution + 1 position, which is set for positive values only. Given this
+                * bit's value, subtracting BIT(resolution + 1) from the ADC's result is
+                * equivalent to a sign extension.
+                */
+               if (st->recv_size == 3) {
+                       *val = (get_unaligned_be24(st->data.d8) >> st->sub_lsb)
+                               - BIT(ddata->chip_info->resolution + 1);
+               } else {
+                       *val = (be32_to_cpu(st->data.d32) >> st->sub_lsb)
+                               - BIT(ddata->chip_info->resolution + 1);
+               }
 
                /*
                 * The part started a new conversion at the end of the above i2c
@@ -67,9 +97,11 @@ static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata,
 static int ltc2497_probe(struct i2c_client *client,
                         const struct i2c_device_id *id)
 {
+       const struct ltc2497_chip_info *chip_info;
        struct iio_dev *indio_dev;
        struct ltc2497_driverdata *st;
        struct device *dev = &client->dev;
+       u32 resolution;
 
        if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
                                     I2C_FUNC_SMBUS_WRITE_BYTE))
@@ -84,6 +116,15 @@ static int ltc2497_probe(struct i2c_client *client,
        st->client = client;
        st->common_ddata.result_and_measure = ltc2497_result_and_measure;
 
+       chip_info = device_get_match_data(dev);
+       if (!chip_info)
+               chip_info = (const struct ltc2497_chip_info *)id->driver_data;
+       st->common_ddata.chip_info = chip_info;
+
+       resolution = chip_info->resolution;
+       st->sub_lsb = 31 - (resolution + 1);
+       st->recv_size = BITS_TO_BYTES(resolution) + 1;
+
        return ltc2497core_probe(dev, indio_dev);
 }
 
@@ -96,14 +137,25 @@ static int ltc2497_remove(struct i2c_client *client)
        return 0;
 }
 
+static const struct ltc2497_chip_info ltc2497_info[] = {
+       [TYPE_LTC2497] = {
+               .resolution = 16,
+       },
+       [TYPE_LTC2499] = {
+               .resolution = 24,
+       },
+};
+
 static const struct i2c_device_id ltc2497_id[] = {
-       { "ltc2497", 0 },
+       { "ltc2497", (kernel_ulong_t)&ltc2497_info[TYPE_LTC2497] },
+       { "ltc2499", (kernel_ulong_t)&ltc2497_info[TYPE_LTC2499] },
        { }
 };
 MODULE_DEVICE_TABLE(i2c, ltc2497_id);
 
 static const struct of_device_id ltc2497_of_match[] = {
-       { .compatible = "lltc,ltc2497", },
+       { .compatible = "lltc,ltc2497", .data = &ltc2497_info[TYPE_LTC2497] },
+       { .compatible = "lltc,ltc2499", .data = &ltc2497_info[TYPE_LTC2499] },
        {},
 };
 MODULE_DEVICE_TABLE(of, ltc2497_of_match);
index d0b42dd..71957fc 100644 (file)
@@ -4,9 +4,14 @@
 #define LTC2497_CONFIG_DEFAULT         LTC2497_ENABLE
 #define LTC2497_CONVERSION_TIME_MS     150ULL
 
+struct ltc2497_chip_info {
+       u32 resolution;
+};
+
 struct ltc2497core_driverdata {
        struct regulator *ref;
        ktime_t time_prev;
+       const struct ltc2497_chip_info  *chip_info;
        u8 addr_prev;
        int (*result_and_measure)(struct ltc2497core_driverdata *ddata,
                                  u8 address, int *val);