Skip to contentSkip to conversation

iio: Add LTC2471/LTC2473 driver

PATCH Superseded Mike Looijmans 4 participants 6 messages Jun 30, 2017 – Jul 2, 2017 IIO

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: Add LTC2471/LTC2473 driver

The LTC2741 and LTC2473 are single voltage ADC chips. The LTC2473 is similar to the LTC2471 but outputs a signed differential value.

Datasheet:
http://cds.linear.com/docs/en/datasheet/24713fb.pdf

---
Note: Moved from "hwmon" subsystem to "iio" as discussed per e-mail, see also: https://patchwork.kernel.org/patch/9816607/
Only tested the LTC2741 in real life.

 drivers/iio/adc/Kconfig   |  10 +++
 drivers/iio/adc/Makefile  |   1 +
 drivers/iio/adc/ltc2471.c | 163 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 174 insertions(+)
 create mode 100644 drivers/iio/adc/ltc2471.c
drivers/iio/adc/Kconfig +10 -0
index 401f47b..fbee44a 100644
@@ -362,6 +362,16 @@ config LPC32XX_ADC
 	  activate only one via device tree selection.  Provides direct access
 	  via sysfs.
 
+config LTC2471
+	tristate "Linear Technology LTC2471 and LTC2473 ADC driver"
+	depends on I2C
+	help
+	  Say yes here to build support for Linear Technology LTC2471 and
+	  LTC2473 ADC.
+
+	  This driver can also be built as a module. If so, the module will
+	  be called ltc2471.
+
 config LTC2485
 	tristate "Linear Technology LTC2485 ADC driver"
 	depends on I2C
drivers/iio/adc/Makefile +1 -0
index 9339bec..afafe85 100644
@@ -34,6 +34,7 @@ obj-$(CONFIG_INA2XX_ADC) += ina2xx-adc.o
 obj-$(CONFIG_LP8788_ADC) += lp8788_adc.o
 obj-$(CONFIG_LPC18XX_ADC) += lpc18xx_adc.o
 obj-$(CONFIG_LPC32XX_ADC) += lpc32xx_adc.o
+obj-$(CONFIG_LTC2471) += ltc2471.o
 obj-$(CONFIG_LTC2485) += ltc2485.o
 obj-$(CONFIG_LTC2497) += ltc2497.o
 obj-$(CONFIG_MAX1027) += max1027.o
drivers/iio/adc/ltc2471.c new file +163 -0

163 lines not on this page · raw message

Signature · 1 line
1.9.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Mike Looijmans

The LTC2741 and LTC2473 are single voltage ADC chips. The LTC2473 is similar to the LTC2471 but outputs a signed differential value.

Datasheet:
http://cds.linear.com/docs/en/datasheet/24713fb.pdf

Looks good, thanks!

Just two tiny nitpicks inline.

drivers/iio/adc/ltc2471.c line 90

Quoted 8 lines
drivers/iio/adc/ltc2471.c new file +8 -0
+static const struct iio_chan_spec ltc2471_channel[] = {
+	{
+		.type = IIO_VOLTAGE,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
+		.differential = 0,
+	},
+};

There should be a newline here.

Quoted 6 lines
drivers/iio/adc/ltc2471.c new file +6 -0
+static const struct iio_chan_spec ltc2473_channel[] = {
+	{
+		.type = IIO_VOLTAGE,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
+					    BIT(IIO_CHAN_INFO_OFFSET),

drivers/iio/adc/ltc2471.c line 121

Quoted 10 lines
drivers/iio/adc/ltc2471.c new file +8 -0
+		.differential = 1,
+	},
+};
[...]
+static int ltc2471_i2c_probe(struct i2c_client *client,
+			     const struct i2c_device_id *id)
+{
[...]
+	data = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);

You don't need this since i2c_get_clientdata() is never used.

Quoted 2 lines
(quoted diff) +2 -0
+	data->client = client;
+
maintainer
#2 lore raw
In reply to Mike Looijmans
Quoted 8 lines · On Fri, 30 Jun 2017 09:21:07 +0200 Mike Looijmans <[email protected]>…

The LTC2741 and LTC2473 are single voltage ADC chips. The LTC2473 is similar to the LTC2471 but outputs a signed differential value.

Datasheet:
http://cds.linear.com/docs/en/datasheet/24713fb.pdf

---

Looks good to me. I'll clean up Lars' points whilst applying.

Applied to the togreg branch of iio.git and pushed out as testing for the autobuilders to play with it.

Thanks,

Jonathan

Quoted 212 lines

Note: Moved from "hwmon" subsystem to "iio" as discussed per e-mail, see also: https://patchwork.kernel.org/patch/9816607/
Only tested the LTC2741 in real life.

 drivers/iio/adc/Kconfig   |  10 +++
 drivers/iio/adc/Makefile  |   1 +
 drivers/iio/adc/ltc2471.c | 163 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 174 insertions(+)
 create mode 100644 drivers/iio/adc/ltc2471.c
drivers/iio/adc/Kconfig +10 -0
index 401f47b..fbee44a 100644
@@ -362,6 +362,16 @@ config LPC32XX_ADC
 	  activate only one via device tree selection.  Provides direct access
 	  via sysfs.
 
+config LTC2471
+	tristate "Linear Technology LTC2471 and LTC2473 ADC driver"
+	depends on I2C
+	help
+	  Say yes here to build support for Linear Technology LTC2471 and
+	  LTC2473 ADC.
+
+	  This driver can also be built as a module. If so, the module will
+	  be called ltc2471.
+
 config LTC2485
 	tristate "Linear Technology LTC2485 ADC driver"
 	depends on I2C
drivers/iio/adc/Makefile +1 -0
index 9339bec..afafe85 100644
@@ -34,6 +34,7 @@ obj-$(CONFIG_INA2XX_ADC) += ina2xx-adc.o
 obj-$(CONFIG_LP8788_ADC) += lp8788_adc.o
 obj-$(CONFIG_LPC18XX_ADC) += lpc18xx_adc.o
 obj-$(CONFIG_LPC32XX_ADC) += lpc32xx_adc.o
+obj-$(CONFIG_LTC2471) += ltc2471.o
 obj-$(CONFIG_LTC2485) += ltc2485.o
 obj-$(CONFIG_LTC2497) += ltc2497.o
 obj-$(CONFIG_MAX1027) += max1027.o
drivers/iio/adc/ltc2471.c new file +163 -0
new file mode 100644
index 0000000..f26bdad
@@ -0,0 +1,163 @@
+/*
+ * Driver for Linear Technology LTC2471 and LTC2473 voltage monitors
+ * The LTC2473 is identical to the 2471, but reports a differential signal.
+ *
+ * Copyright (C) 2017 Topic Embedded Products
+ * Author: Mike Looijmans <[email protected]>
+ *
+ * License: GPLv2
+ */
+
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+
+enum chips {
+	ltc2471,
+	ltc2473
+};
+
+struct ltc2471_data {
+	struct i2c_client *client;
+};
+
+/* Reference voltage is 1.25V */
+#define LTC2471_VREF 1250
+
+/* Read two bytes from the I2C bus to obtain the ADC result */
+static int ltc2471_get_value(struct i2c_client *client)
+{
+	int ret;
+	__be16 buf;
+
+	ret = i2c_master_recv(client, (char *)&buf, 2);
+	if (ret < 0)
+		return ret;
+	if (ret != 2)
+		return -EIO;
+
+	/* MSB first */
+	return be16_to_cpu(buf);
+}
+
+static int ltc2471_read_raw(struct iio_dev *indio_dev,
+			    struct iio_chan_spec const *chan,
+			    int *val, int *val2, long info)
+{
+	struct ltc2471_data *data = iio_priv(indio_dev);
+	int ret;
+
+	switch (info) {
+	case IIO_CHAN_INFO_RAW:
+		ret = ltc2471_get_value(data->client);
+		if (ret < 0)
+			return ret;
+		*val = ret;
+		return IIO_VAL_INT;
+
+	case IIO_CHAN_INFO_SCALE:
+		if (chan->differential)
+			/* Output ranges from -VREF to +VREF */
+			*val = 2 * LTC2471_VREF;
+		else
+			/* Output ranges from 0 to VREF */
+			*val = LTC2471_VREF;
+		*val2 = 16;	/* 16 data bits */
+		return IIO_VAL_FRACTIONAL_LOG2;
+
+	case IIO_CHAN_INFO_OFFSET:
+		if (chan->differential)
+			*val = -LTC2471_VREF;
+		else
+			*val = 0;
+		return IIO_VAL_INT;
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static const struct iio_chan_spec ltc2471_channel[] = {
+	{
+		.type = IIO_VOLTAGE,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
+		.differential = 0,
+	},
+};
+static const struct iio_chan_spec ltc2473_channel[] = {
+	{
+		.type = IIO_VOLTAGE,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
+					    BIT(IIO_CHAN_INFO_OFFSET),
+		.differential = 1,
+	},
+};
+
+static const struct iio_info ltc2471_info = {
+	.read_raw = ltc2471_read_raw,
+	.driver_module = THIS_MODULE,
+};
+
+static int ltc2471_i2c_probe(struct i2c_client *client,
+			     const struct i2c_device_id *id)
+{
+	struct iio_dev *indio_dev;
+	struct ltc2471_data *data;
+	int ret;
+
+	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
+		return -EOPNOTSUPP;
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	data = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);
+	data->client = client;
+
+	indio_dev->dev.parent = &client->dev;
+	indio_dev->name = id->name;
+	indio_dev->info = &ltc2471_info;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	if (id->driver_data == ltc2473)
+		indio_dev->channels = ltc2473_channel;
+	else
+		indio_dev->channels = ltc2471_channel;
+	indio_dev->num_channels = 1;
+
+	/* Trigger once to start conversion and check if chip is there */
+	ret = ltc2471_get_value(client);
+	if (ret < 0) {
+		dev_err(&client->dev, "Cannot read from device.\n");
+		return ret;
+	}
+
+	return devm_iio_device_register(&client->dev, indio_dev);
+}
+
+static const struct i2c_device_id ltc2471_i2c_id[] = {
+	{ "ltc2471", ltc2471 },
+	{ "ltc2473", ltc2473 },
+	{}
+};
+MODULE_DEVICE_TABLE(i2c, ltc2471_i2c_id);
+
+static struct i2c_driver ltc2471_i2c_driver = {
+	.driver = {
+		.name = "ltc2471",
+	},
+	.probe    = ltc2471_i2c_probe,
+	.id_table = ltc2471_i2c_id,
+};
+
+module_i2c_driver(ltc2471_i2c_driver);
+
+MODULE_DESCRIPTION("LTC2471/LTC2473 Sensor Driver");
+MODULE_AUTHOR("Topic Embedded Products");
+MODULE_LICENSE("GPL v2");
#3 lore raw
In reply to Mike Looijmans
Quoted 3 lines

The LTC2741 and LTC2473 are single voltage ADC chips. The LTC2473 is similar to the LTC2471 but outputs a signed differential value.

comments below

Quoted 62 lines

Datasheet:
http://cds.linear.com/docs/en/datasheet/24713fb.pdf

---
Note: Moved from "hwmon" subsystem to "iio" as discussed per e-mail, see also: https://patchwork.kernel.org/patch/9816607/
Only tested the LTC2741 in real life.

 drivers/iio/adc/Kconfig   |  10 +++
 drivers/iio/adc/Makefile  |   1 +
 drivers/iio/adc/ltc2471.c | 163 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 174 insertions(+)
 create mode 100644 drivers/iio/adc/ltc2471.c
drivers/iio/adc/Kconfig +10 -0
index 401f47b..fbee44a 100644
@@ -362,6 +362,16 @@ config LPC32XX_ADC
 	  activate only one via device tree selection.  Provides direct access
 	  via sysfs.
 
+config LTC2471
+	tristate "Linear Technology LTC2471 and LTC2473 ADC driver"
+	depends on I2C
+	help
+	  Say yes here to build support for Linear Technology LTC2471 and
+	  LTC2473 ADC.
+
+	  This driver can also be built as a module. If so, the module will
+	  be called ltc2471.
+
 config LTC2485
 	tristate "Linear Technology LTC2485 ADC driver"
 	depends on I2C
drivers/iio/adc/Makefile +1 -0
index 9339bec..afafe85 100644
@@ -34,6 +34,7 @@ obj-$(CONFIG_INA2XX_ADC) += ina2xx-adc.o
 obj-$(CONFIG_LP8788_ADC) += lp8788_adc.o
 obj-$(CONFIG_LPC18XX_ADC) += lpc18xx_adc.o
 obj-$(CONFIG_LPC32XX_ADC) += lpc32xx_adc.o
+obj-$(CONFIG_LTC2471) += ltc2471.o
 obj-$(CONFIG_LTC2485) += ltc2485.o
 obj-$(CONFIG_LTC2497) += ltc2497.o
 obj-$(CONFIG_MAX1027) += max1027.o
drivers/iio/adc/ltc2471.c new file +8 -0
new file mode 100644
index 0000000..f26bdad
@@ -0,0 +1,163 @@
+/*
+ * Driver for Linear Technology LTC2471 and LTC2473 voltage monitors
+ * The LTC2473 is identical to the 2471, but reports a differential signal.
+ *
+ * Copyright (C) 2017 Topic Embedded Products
+ * Author: Mike Looijmans <[email protected]>
+ *
+ * License: GPLv2

drivers/iio/adc/ltc2471.c line 18

Quoted 10 lines
drivers/iio/adc/ltc2471.c new file +10 -0
+ */
+
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+
+enum chips {

ltc2471_ prefix please

drivers/iio/adc/ltc2471.c line 20

Quoted 2 lines
drivers/iio/adc/ltc2471.c new file +2 -0
+	ltc2471,
+	ltc2473

end last line with a comma, so additions can be done with a one-line patch

Quoted 6 lines
drivers/iio/adc/ltc2471.c new file +6 -0
+};
+
+struct ltc2471_data {
+	struct i2c_client *client;
+};
+

drivers/iio/adc/ltc2471.c line 36

Quoted 10 lines
drivers/iio/adc/ltc2471.c new file +10 -0
+/* Reference voltage is 1.25V */
+#define LTC2471_VREF 1250
+
+/* Read two bytes from the I2C bus to obtain the ADC result */
+static int ltc2471_get_value(struct i2c_client *client)
+{
+	int ret;
+	__be16 buf;
+
+	ret = i2c_master_recv(client, (char *)&buf, 2);

sizeof(buf) probably

drivers/iio/adc/ltc2471.c line 39

Quoted 3 lines
drivers/iio/adc/ltc2471.c new file +3 -0
+	if (ret < 0)
+		return ret;
+	if (ret != 2)

sizeof(buf) probably

Quoted 26 lines
drivers/iio/adc/ltc2471.c new file +26 -0
+		return -EIO;
+
+	/* MSB first */
+	return be16_to_cpu(buf);
+}
+
+static int ltc2471_read_raw(struct iio_dev *indio_dev,
+			    struct iio_chan_spec const *chan,
+			    int *val, int *val2, long info)
+{
+	struct ltc2471_data *data = iio_priv(indio_dev);
+	int ret;
+
+	switch (info) {
+	case IIO_CHAN_INFO_RAW:
+		ret = ltc2471_get_value(data->client);
+		if (ret < 0)
+			return ret;
+		*val = ret;
+		return IIO_VAL_INT;
+
+	case IIO_CHAN_INFO_SCALE:
+		if (chan->differential)
+			/* Output ranges from -VREF to +VREF */
+			*val = 2 * LTC2471_VREF;
+		else

drivers/iio/adc/ltc2471.c line 75

Quoted 10 lines
drivers/iio/adc/ltc2471.c new file +10 -0
+			/* Output ranges from 0 to VREF */
+			*val = LTC2471_VREF;
+		*val2 = 16;	/* 16 data bits */
+		return IIO_VAL_FRACTIONAL_LOG2;
+
+	case IIO_CHAN_INFO_OFFSET:
+		if (chan->differential)
+			*val = -LTC2471_VREF;
+		else
+			*val = 0;

the non-differential (single ended) chip has no offset channel anyway according to iio_chan_spec...

drivers/iio/adc/ltc2471.c line 88

Quoted 13 lines
drivers/iio/adc/ltc2471.c new file +13 -0
+		return IIO_VAL_INT;
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static const struct iio_chan_spec ltc2471_channel[] = {
+	{
+		.type = IIO_VOLTAGE,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
+		.differential = 0,

.differential = 0 not needed

Quoted 63 lines
drivers/iio/adc/ltc2471.c new file +63 -0
+	},
+};
+static const struct iio_chan_spec ltc2473_channel[] = {
+	{
+		.type = IIO_VOLTAGE,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
+					    BIT(IIO_CHAN_INFO_OFFSET),
+		.differential = 1,
+	},
+};
+
+static const struct iio_info ltc2471_info = {
+	.read_raw = ltc2471_read_raw,
+	.driver_module = THIS_MODULE,
+};
+
+static int ltc2471_i2c_probe(struct i2c_client *client,
+			     const struct i2c_device_id *id)
+{
+	struct iio_dev *indio_dev;
+	struct ltc2471_data *data;
+	int ret;
+
+	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
+		return -EOPNOTSUPP;
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	data = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);
+	data->client = client;
+
+	indio_dev->dev.parent = &client->dev;
+	indio_dev->name = id->name;
+	indio_dev->info = &ltc2471_info;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	if (id->driver_data == ltc2473)
+		indio_dev->channels = ltc2473_channel;
+	else
+		indio_dev->channels = ltc2471_channel;
+	indio_dev->num_channels = 1;
+
+	/* Trigger once to start conversion and check if chip is there */
+	ret = ltc2471_get_value(client);
+	if (ret < 0) {
+		dev_err(&client->dev, "Cannot read from device.\n");
+		return ret;
+	}
+
+	return devm_iio_device_register(&client->dev, indio_dev);
+}
+
+static const struct i2c_device_id ltc2471_i2c_id[] = {
+	{ "ltc2471", ltc2471 },
+	{ "ltc2473", ltc2473 },
+	{}
+};
+MODULE_DEVICE_TABLE(i2c, ltc2471_i2c_id);
+
+static struct i2c_driver ltc2471_i2c_driver = {

drivers/iio/adc/ltc2471.c line 161

Quoted 10 lines
drivers/iio/adc/ltc2471.c new file +10 -0
+	.driver = {
+		.name = "ltc2471",
+	},
+	.probe    = ltc2471_i2c_probe,
+	.id_table = ltc2471_i2c_id,
+};
+
+module_i2c_driver(ltc2471_i2c_driver);
+
+MODULE_DESCRIPTION("LTC2471/LTC2473 Sensor Driver");

maybe "ADC driver"

Quoted 3 lines
drivers/iio/adc/ltc2471.c new file +2 -0
+MODULE_AUTHOR("Topic Embedded Products");
+MODULE_LICENSE("GPL v2");
Signature · 2 lines
Peter Meerwald-Stadler
Mobile: +43 664 24 44 418
maintainer
#4 lore raw
Quoted 9 lines · On Sun, 2 Jul 2017 10:29:35 +0200 (CEST) Peter Meerwald-Stadler…
Quoted 2 lines

The LTC2741 and LTC2473 are single voltage ADC chips. The LTC2473 is similar to the LTC2471 but outputs a signed differential value.

comments below

Quoted 4 lines

Datasheet:
http://cds.linear.com/docs/en/datasheet/24713fb.pdf

Enough here that I've dropped the patch for now and look forward to a v2 incorporating both Peter and Lars' comments.

Jonathan

Quoted 237 lines
Quoted 58 lines

---
Note: Moved from "hwmon" subsystem to "iio" as discussed per e-mail, see also: https://patchwork.kernel.org/patch/9816607/
Only tested the LTC2741 in real life.

 drivers/iio/adc/Kconfig   |  10 +++
 drivers/iio/adc/Makefile  |   1 +
 drivers/iio/adc/ltc2471.c | 163 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 174 insertions(+)
 create mode 100644 drivers/iio/adc/ltc2471.c
drivers/iio/adc/Kconfig +10 -0
index 401f47b..fbee44a 100644
@@ -362,6 +362,16 @@ config LPC32XX_ADC
 	  activate only one via device tree selection.  Provides direct access
 	  via sysfs.
 
+config LTC2471
+	tristate "Linear Technology LTC2471 and LTC2473 ADC driver"
+	depends on I2C
+	help
+	  Say yes here to build support for Linear Technology LTC2471 and
+	  LTC2473 ADC.
+
+	  This driver can also be built as a module. If so, the module will
+	  be called ltc2471.
+
 config LTC2485
 	tristate "Linear Technology LTC2485 ADC driver"
 	depends on I2C
drivers/iio/adc/Makefile +1 -0
index 9339bec..afafe85 100644
@@ -34,6 +34,7 @@ obj-$(CONFIG_INA2XX_ADC) += ina2xx-adc.o
 obj-$(CONFIG_LP8788_ADC) += lp8788_adc.o
 obj-$(CONFIG_LPC18XX_ADC) += lpc18xx_adc.o
 obj-$(CONFIG_LPC32XX_ADC) += lpc32xx_adc.o
+obj-$(CONFIG_LTC2471) += ltc2471.o
 obj-$(CONFIG_LTC2485) += ltc2485.o
 obj-$(CONFIG_LTC2497) += ltc2497.o
 obj-$(CONFIG_MAX1027) += max1027.o
drivers/iio/adc/ltc2471.c new file +8 -0
new file mode 100644
index 0000000..f26bdad
@@ -0,0 +1,163 @@
+/*
+ * Driver for Linear Technology LTC2471 and LTC2473 voltage monitors
+ * The LTC2473 is identical to the 2471, but reports a differential signal.
+ *
+ * Copyright (C) 2017 Topic Embedded Products
+ * Author: Mike Looijmans <[email protected]>
+ *
+ * License: GPLv2

drivers/iio/adc/ltc2471.c line 18

Quoted 10 lines
drivers/iio/adc/ltc2471.c new file +10 -0
+ */
+
+#include <linux/err.h>
+#include <linux/i2c.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+
+enum chips {  

ltc2471_ prefix please

drivers/iio/adc/ltc2471.c line 20

Quoted 2 lines
drivers/iio/adc/ltc2471.c new file +2 -0
+	ltc2471,
+	ltc2473  

end last line with a comma, so additions can be done with a one-line patch

Quoted 6 lines
drivers/iio/adc/ltc2471.c new file +6 -0
+};
+
+struct ltc2471_data {
+	struct i2c_client *client;
+};
+

drivers/iio/adc/ltc2471.c line 36

Quoted 10 lines
drivers/iio/adc/ltc2471.c new file +10 -0
+/* Reference voltage is 1.25V */
+#define LTC2471_VREF 1250
+
+/* Read two bytes from the I2C bus to obtain the ADC result */
+static int ltc2471_get_value(struct i2c_client *client)
+{
+	int ret;
+	__be16 buf;
+
+	ret = i2c_master_recv(client, (char *)&buf, 2);  

sizeof(buf) probably

drivers/iio/adc/ltc2471.c line 39

Quoted 3 lines
drivers/iio/adc/ltc2471.c new file +3 -0
+	if (ret < 0)
+		return ret;
+	if (ret != 2)  

sizeof(buf) probably

Quoted 26 lines
drivers/iio/adc/ltc2471.c new file +26 -0
+		return -EIO;
+
+	/* MSB first */
+	return be16_to_cpu(buf);
+}
+
+static int ltc2471_read_raw(struct iio_dev *indio_dev,
+			    struct iio_chan_spec const *chan,
+			    int *val, int *val2, long info)
+{
+	struct ltc2471_data *data = iio_priv(indio_dev);
+	int ret;
+
+	switch (info) {
+	case IIO_CHAN_INFO_RAW:
+		ret = ltc2471_get_value(data->client);
+		if (ret < 0)
+			return ret;
+		*val = ret;
+		return IIO_VAL_INT;
+
+	case IIO_CHAN_INFO_SCALE:
+		if (chan->differential)
+			/* Output ranges from -VREF to +VREF */
+			*val = 2 * LTC2471_VREF;
+		else

drivers/iio/adc/ltc2471.c line 75

Quoted 10 lines
drivers/iio/adc/ltc2471.c new file +10 -0
+			/* Output ranges from 0 to VREF */
+			*val = LTC2471_VREF;
+		*val2 = 16;	/* 16 data bits */
+		return IIO_VAL_FRACTIONAL_LOG2;
+
+	case IIO_CHAN_INFO_OFFSET:
+		if (chan->differential)
+			*val = -LTC2471_VREF;
+		else
+			*val = 0;  

the non-differential (single ended) chip has no offset channel anyway according to iio_chan_spec...

drivers/iio/adc/ltc2471.c line 88

Quoted 13 lines
drivers/iio/adc/ltc2471.c new file +13 -0
+		return IIO_VAL_INT;
+
+	default:
+		return -EINVAL;
+	}
+}
+
+static const struct iio_chan_spec ltc2471_channel[] = {
+	{
+		.type = IIO_VOLTAGE,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
+		.differential = 0,  

.differential = 0 not needed

Quoted 63 lines
drivers/iio/adc/ltc2471.c new file +63 -0
+	},
+};
+static const struct iio_chan_spec ltc2473_channel[] = {
+	{
+		.type = IIO_VOLTAGE,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
+		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |
+					    BIT(IIO_CHAN_INFO_OFFSET),
+		.differential = 1,
+	},
+};
+
+static const struct iio_info ltc2471_info = {
+	.read_raw = ltc2471_read_raw,
+	.driver_module = THIS_MODULE,
+};
+
+static int ltc2471_i2c_probe(struct i2c_client *client,
+			     const struct i2c_device_id *id)
+{
+	struct iio_dev *indio_dev;
+	struct ltc2471_data *data;
+	int ret;
+
+	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
+		return -EOPNOTSUPP;
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	data = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);
+	data->client = client;
+
+	indio_dev->dev.parent = &client->dev;
+	indio_dev->name = id->name;
+	indio_dev->info = &ltc2471_info;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	if (id->driver_data == ltc2473)
+		indio_dev->channels = ltc2473_channel;
+	else
+		indio_dev->channels = ltc2471_channel;
+	indio_dev->num_channels = 1;
+
+	/* Trigger once to start conversion and check if chip is there */
+	ret = ltc2471_get_value(client);
+	if (ret < 0) {
+		dev_err(&client->dev, "Cannot read from device.\n");
+		return ret;
+	}
+
+	return devm_iio_device_register(&client->dev, indio_dev);
+}
+
+static const struct i2c_device_id ltc2471_i2c_id[] = {
+	{ "ltc2471", ltc2471 },
+	{ "ltc2473", ltc2473 },
+	{}
+};
+MODULE_DEVICE_TABLE(i2c, ltc2471_i2c_id);
+
+static struct i2c_driver ltc2471_i2c_driver = {

drivers/iio/adc/ltc2471.c line 161

Quoted 10 lines
drivers/iio/adc/ltc2471.c new file +10 -0
+	.driver = {
+		.name = "ltc2471",
+	},
+	.probe    = ltc2471_i2c_probe,
+	.id_table = ltc2471_i2c_id,
+};
+
+module_i2c_driver(ltc2471_i2c_driver);
+
+MODULE_DESCRIPTION("LTC2471/LTC2473 Sensor Driver");  

maybe "ADC driver"

Quoted 3 lines
drivers/iio/adc/ltc2471.c new file +2 -0
+MODULE_AUTHOR("Topic Embedded Products");
+MODULE_LICENSE("GPL v2");
#5 lore raw
Quoted 6 lines · Jonathan Cameron
Quoted 3 lines · On Sun, 2 Jul 2017 10:29:35 +0200 (CEST) Peter Meerwald-Stadler…
Quoted 2 lines

The LTC2741 and LTC2473 are single voltage ADC chips. The LTC2473 is similar to the LTC2471 but outputs a signed differential value.

Quoted 9 lines
Quoted 6 lines

comments below

Quoted 4 lines

Datasheet:

   http://cds.linear.com/docs/en/datasheet/24713fb.pdf

Enough here that I've dropped the patch for now and look forward to a v2 incorporating both Peter and Lars' comments.

I'll create and send a v2 probably tomorrow.

Kind regards,

Mike Looijmans
System Expert

TOPIC Products
Materiaalweg 4, NL-5681 RJ Best
Postbus 440, NL-5680 AK Best
Telefoon: +31 (0) 499 33 69 79
E-mail: [email protected]
Website: www.topicproducts.com

Please consider the environment before printing this e-mail