1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * ltc2497.c - Driver for Analog Devices/Linear Technology LTC2497 ADC
4 *
5 * Copyright (C) 2017 Analog Devices Inc.
6 *
7 * Datasheet: http://cds.linear.com/docs/en/datasheet/2497fd.pdf
8 */
9
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/property.h>
15
16 #include <linux/unaligned.h>
17
18 #include "ltc2497.h"
19
20 enum ltc2497_chip_type {
21 TYPE_LTC2497,
22 TYPE_LTC2499,
23 };
24
25 struct ltc2497_driverdata {
26 /* this must be the first member */
27 struct ltc2497core_driverdata common_ddata;
28 struct i2c_client *client;
29 u32 recv_size;
30 /*
31 * DMA (thus cache coherency maintenance) may require the
32 * transfer buffers to live in their own cache lines.
33 */
34 union {
35 __be32 d32;
36 u8 d8[3];
37 } data __aligned(IIO_DMA_MINALIGN);
38 };
39
ltc2497_result_and_measure(struct ltc2497core_driverdata * ddata,u8 address,int * val)40 static int ltc2497_result_and_measure(struct ltc2497core_driverdata *ddata,
41 u8 address, int *val)
42 {
43 struct ltc2497_driverdata *st =
44 container_of(ddata, struct ltc2497_driverdata, common_ddata);
45 int ret;
46
47 if (val) {
48 if (st->recv_size == 3)
49 ret = i2c_master_recv(st->client, (char *)&st->data.d8,
50 st->recv_size);
51 else
52 ret = i2c_master_recv(st->client, (char *)&st->data.d32,
53 st->recv_size);
54 if (ret < 0) {
55 dev_err(&st->client->dev, "i2c_master_recv failed\n");
56 return ret;
57 }
58
59 /*
60 * The data format is 16/24 bit 2s complement, but with an upper sign bit on the
61 * resolution + 1 position, which is set for positive values only. Given this
62 * bit's value, subtracting BIT(resolution + 1) from the ADC's result is
63 * equivalent to a sign extension.
64 */
65 if (st->recv_size == 3) {
66 *val = (get_unaligned_be24(st->data.d8) >> 6)
67 - BIT(ddata->chip_info->resolution + 1);
68 } else {
69 *val = (be32_to_cpu(st->data.d32) >> 6)
70 - BIT(ddata->chip_info->resolution + 1);
71 }
72
73 /*
74 * The part started a new conversion at the end of the above i2c
75 * transfer, so if the address didn't change since the last call
76 * everything is fine and we can return early.
77 * If not (which should only happen when some sort of bulk
78 * conversion is implemented) we have to program the new
79 * address. Note that this probably fails as the conversion that
80 * was triggered above is like not complete yet and the two
81 * operations have to be done in a single transfer.
82 */
83 if (ddata->addr_prev == address)
84 return 0;
85 }
86
87 ret = i2c_smbus_write_byte(st->client,
88 LTC2497_ENABLE | address);
89 if (ret)
90 dev_err(&st->client->dev, "i2c transfer failed: %pe\n",
91 ERR_PTR(ret));
92 return ret;
93 }
94
ltc2497_probe(struct i2c_client * client)95 static int ltc2497_probe(struct i2c_client *client)
96 {
97 const struct ltc2497_chip_info *chip_info;
98 struct iio_dev *indio_dev;
99 struct ltc2497_driverdata *st;
100 struct device *dev = &client->dev;
101 u32 resolution;
102
103 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C |
104 I2C_FUNC_SMBUS_WRITE_BYTE))
105 return -EOPNOTSUPP;
106
107 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
108 if (!indio_dev)
109 return -ENOMEM;
110
111 st = iio_priv(indio_dev);
112 i2c_set_clientdata(client, indio_dev);
113 st->client = client;
114 st->common_ddata.result_and_measure = ltc2497_result_and_measure;
115
116 chip_info = i2c_get_match_data(client);
117 st->common_ddata.chip_info = chip_info;
118
119 resolution = chip_info->resolution;
120 st->recv_size = BITS_TO_BYTES(resolution) + 1;
121
122 return ltc2497core_probe(dev, indio_dev);
123 }
124
ltc2497_remove(struct i2c_client * client)125 static void ltc2497_remove(struct i2c_client *client)
126 {
127 struct iio_dev *indio_dev = i2c_get_clientdata(client);
128
129 ltc2497core_remove(indio_dev);
130 }
131
132 static const struct ltc2497_chip_info ltc2497_info[] = {
133 [TYPE_LTC2497] = {
134 .resolution = 16,
135 .name = NULL,
136 },
137 [TYPE_LTC2499] = {
138 .resolution = 24,
139 .name = "ltc2499",
140 },
141 };
142
143 static const struct i2c_device_id ltc2497_id[] = {
144 { .name = "ltc2497", .driver_data = (kernel_ulong_t)<c2497_info[TYPE_LTC2497] },
145 { .name = "ltc2499", .driver_data = (kernel_ulong_t)<c2497_info[TYPE_LTC2499] },
146 { }
147 };
148 MODULE_DEVICE_TABLE(i2c, ltc2497_id);
149
150 static const struct of_device_id ltc2497_of_match[] = {
151 { .compatible = "lltc,ltc2497", .data = <c2497_info[TYPE_LTC2497] },
152 { .compatible = "lltc,ltc2499", .data = <c2497_info[TYPE_LTC2499] },
153 { }
154 };
155 MODULE_DEVICE_TABLE(of, ltc2497_of_match);
156
157 static struct i2c_driver ltc2497_driver = {
158 .driver = {
159 .name = "ltc2497",
160 .of_match_table = ltc2497_of_match,
161 },
162 .probe = ltc2497_probe,
163 .remove = ltc2497_remove,
164 .id_table = ltc2497_id,
165 };
166 module_i2c_driver(ltc2497_driver);
167
168 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
169 MODULE_DESCRIPTION("Linear Technology LTC2497 ADC driver");
170 MODULE_LICENSE("GPL v2");
171