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>
18 struct ltc2497_driverdata {
19 /* this must be the first member */
20 struct ltc2497core_driverdata common_ddata;
21 struct i2c_client *client;
23 * DMA (thus cache coherency maintenance) requires the
24 * transfer buffers to live in their own cache lines.
26 __be32 buf ____cacheline_aligned;
29 static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata,
32 struct ltc2497_driverdata *st =
33 container_of(ddata, struct ltc2497_driverdata, common_ddata);
37 ret = i2c_master_recv(st->client, (char *)&st->buf, 3);
39 dev_err(&st->client->dev, "i2c_master_recv failed\n");
43 *val = (be32_to_cpu(st->buf) >> 14) - (1 << 17);
46 * The part started a new conversion at the end of the above i2c
47 * transfer, so if the address didn't change since the last call
48 * everything is fine and we can return early.
49 * If not (which should only happen when some sort of bulk
50 * conversion is implemented) we have to program the new
51 * address. Note that this probably fails as the conversion that
52 * was triggered above is like not complete yet and the two
53 * operations have to be done in a single transfer.
55 if (ddata->addr_prev == address)
59 ret = i2c_smbus_write_byte(st->client,
60 LTC2497_ENABLE | address);
62 dev_err(&st->client->dev, "i2c transfer failed: %pe\n",
67 static int ltc2497_probe(struct i2c_client *client,
68 const struct i2c_device_id *id)
70 struct iio_dev *indio_dev;
71 struct ltc2497_driverdata *st;
72 struct device *dev = &client->dev;
74 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
75 I2C_FUNC_SMBUS_WRITE_BYTE))
78 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
82 st = iio_priv(indio_dev);
83 i2c_set_clientdata(client, indio_dev);
85 st->common_ddata.result_and_measure = ltc2497_result_and_measure;
87 return ltc2497core_probe(dev, indio_dev);
90 static int ltc2497_remove(struct i2c_client *client)
92 struct iio_dev *indio_dev = i2c_get_clientdata(client);
94 ltc2497core_remove(indio_dev);
99 static const struct i2c_device_id ltc2497_id[] = {
103 MODULE_DEVICE_TABLE(i2c, ltc2497_id);
105 static const struct of_device_id ltc2497_of_match[] = {
106 { .compatible = "lltc,ltc2497", },
109 MODULE_DEVICE_TABLE(of, ltc2497_of_match);
111 static struct i2c_driver ltc2497_driver = {
114 .of_match_table = ltc2497_of_match,
116 .probe = ltc2497_probe,
117 .remove = ltc2497_remove,
118 .id_table = ltc2497_id,
120 module_i2c_driver(ltc2497_driver);
122 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
123 MODULE_DESCRIPTION("Linear Technology LTC2497 ADC driver");
124 MODULE_LICENSE("GPL v2");