1 // SPDX-License-Identifier: GPL-2.0-only
3 * ltc2497.c - Driver for Analog Devices/Linear Technology LTC2497 ADC
5 * Copyright (C) 2017 Analog Devices Inc.
7 * Datasheet: http://cds.linear.com/docs/en/datasheet/2497fd.pdf
10 #include <linux/i2c.h>
11 #include <linux/iio/iio.h>
12 #include <linux/iio/driver.h>
13 #include <linux/module.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/property.h>
17 #include <asm/unaligned.h>
21 enum ltc2497_chip_type {
26 struct ltc2497_driverdata {
27 /* this must be the first member */
28 struct ltc2497core_driverdata common_ddata;
29 struct i2c_client *client;
33 * DMA (thus cache coherency maintenance) may require the
34 * transfer buffers to live in their own cache lines.
39 } data __aligned(IIO_DMA_MINALIGN);
42 static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata,
45 struct ltc2497_driverdata *st =
46 container_of(ddata, struct ltc2497_driverdata, common_ddata);
50 if (st->recv_size == 3)
51 ret = i2c_master_recv(st->client, (char *)&st->data.d8,
54 ret = i2c_master_recv(st->client, (char *)&st->data.d32,
57 dev_err(&st->client->dev, "i2c_master_recv failed\n");
62 * The data format is 16/24 bit 2s complement, but with an upper sign bit on the
63 * resolution + 1 position, which is set for positive values only. Given this
64 * bit's value, subtracting BIT(resolution + 1) from the ADC's result is
65 * equivalent to a sign extension.
67 if (st->recv_size == 3) {
68 *val = (get_unaligned_be24(st->data.d8) >> st->sub_lsb)
69 - BIT(ddata->chip_info->resolution + 1);
71 *val = (be32_to_cpu(st->data.d32) >> st->sub_lsb)
72 - BIT(ddata->chip_info->resolution + 1);
76 * The part started a new conversion at the end of the above i2c
77 * transfer, so if the address didn't change since the last call
78 * everything is fine and we can return early.
79 * If not (which should only happen when some sort of bulk
80 * conversion is implemented) we have to program the new
81 * address. Note that this probably fails as the conversion that
82 * was triggered above is like not complete yet and the two
83 * operations have to be done in a single transfer.
85 if (ddata->addr_prev == address)
89 ret = i2c_smbus_write_byte(st->client,
90 LTC2497_ENABLE | address);
92 dev_err(&st->client->dev, "i2c transfer failed: %pe\n",
97 static int ltc2497_probe(struct i2c_client *client,
98 const struct i2c_device_id *id)
100 const struct ltc2497_chip_info *chip_info;
101 struct iio_dev *indio_dev;
102 struct ltc2497_driverdata *st;
103 struct device *dev = &client->dev;
106 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
107 I2C_FUNC_SMBUS_WRITE_BYTE))
110 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
114 st = iio_priv(indio_dev);
115 i2c_set_clientdata(client, indio_dev);
117 st->common_ddata.result_and_measure = ltc2497_result_and_measure;
119 chip_info = device_get_match_data(dev);
121 chip_info = (const struct ltc2497_chip_info *)id->driver_data;
122 st->common_ddata.chip_info = chip_info;
124 resolution = chip_info->resolution;
125 st->sub_lsb = 31 - (resolution + 1);
126 st->recv_size = BITS_TO_BYTES(resolution) + 1;
128 return ltc2497core_probe(dev, indio_dev);
131 static void ltc2497_remove(struct i2c_client *client)
133 struct iio_dev *indio_dev = i2c_get_clientdata(client);
135 ltc2497core_remove(indio_dev);
138 static const struct ltc2497_chip_info ltc2497_info[] = {
149 static const struct i2c_device_id ltc2497_id[] = {
150 { "ltc2497", (kernel_ulong_t)<c2497_info[TYPE_LTC2497] },
151 { "ltc2499", (kernel_ulong_t)<c2497_info[TYPE_LTC2499] },
154 MODULE_DEVICE_TABLE(i2c, ltc2497_id);
156 static const struct of_device_id ltc2497_of_match[] = {
157 { .compatible = "lltc,ltc2497", .data = <c2497_info[TYPE_LTC2497] },
158 { .compatible = "lltc,ltc2499", .data = <c2497_info[TYPE_LTC2499] },
161 MODULE_DEVICE_TABLE(of, ltc2497_of_match);
163 static struct i2c_driver ltc2497_driver = {
166 .of_match_table = ltc2497_of_match,
168 .probe = ltc2497_probe,
169 .remove = ltc2497_remove,
170 .id_table = ltc2497_id,
172 module_i2c_driver(ltc2497_driver);
175 MODULE_DESCRIPTION("Linear Technology LTC2497 ADC driver");
176 MODULE_LICENSE("GPL v2");