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/delay.h>
11 #include <linux/i2c.h>
12 #include <linux/iio/iio.h>
13 #include <linux/iio/driver.h>
14 #include <linux/iio/sysfs.h>
15 #include <linux/module.h>
17 #include <linux/regulator/consumer.h>
19 #define LTC2497_ENABLE 0xA0
20 #define LTC2497_SGL BIT(4)
21 #define LTC2497_DIFF 0
22 #define LTC2497_SIGN BIT(3)
23 #define LTC2497_CONFIG_DEFAULT LTC2497_ENABLE
24 #define LTC2497_CONVERSION_TIME_MS 150ULL
27 struct i2c_client
*client
;
28 struct regulator
*ref
;
32 * DMA (thus cache coherency maintenance) requires the
33 * transfer buffers to live in their own cache lines.
35 __be32 buf ____cacheline_aligned
;
38 static int ltc2497_wait_conv(struct ltc2497_st
*st
)
42 time_elapsed
= ktime_ms_delta(ktime_get(), st
->time_prev
);
44 if (time_elapsed
< LTC2497_CONVERSION_TIME_MS
) {
45 /* delay if conversion time not passed
46 * since last read or write
48 if (msleep_interruptible(
49 LTC2497_CONVERSION_TIME_MS
- time_elapsed
))
55 if (time_elapsed
- LTC2497_CONVERSION_TIME_MS
<= 0) {
56 /* We're in automatic mode -
57 * so the last reading is stil not outdated
65 static int ltc2497_read(struct ltc2497_st
*st
, u8 address
, int *val
)
67 struct i2c_client
*client
= st
->client
;
70 ret
= ltc2497_wait_conv(st
);
74 if (ret
|| st
->addr_prev
!= address
) {
75 ret
= i2c_smbus_write_byte(st
->client
,
76 LTC2497_ENABLE
| address
);
79 st
->addr_prev
= address
;
80 if (msleep_interruptible(LTC2497_CONVERSION_TIME_MS
))
83 ret
= i2c_master_recv(client
, (char *)&st
->buf
, 3);
85 dev_err(&client
->dev
, "i2c_master_recv failed\n");
88 st
->time_prev
= ktime_get();
90 /* convert and shift the result,
91 * and finally convert from offset binary to signed integer
93 *val
= (be32_to_cpu(st
->buf
) >> 14) - (1 << 17);
98 static int ltc2497_read_raw(struct iio_dev
*indio_dev
,
99 struct iio_chan_spec
const *chan
,
100 int *val
, int *val2
, long mask
)
102 struct ltc2497_st
*st
= iio_priv(indio_dev
);
106 case IIO_CHAN_INFO_RAW
:
107 mutex_lock(&indio_dev
->mlock
);
108 ret
= ltc2497_read(st
, chan
->address
, val
);
109 mutex_unlock(&indio_dev
->mlock
);
115 case IIO_CHAN_INFO_SCALE
:
116 ret
= regulator_get_voltage(st
->ref
);
123 return IIO_VAL_FRACTIONAL_LOG2
;
130 #define LTC2497_CHAN(_chan, _addr, _ds_name) { \
131 .type = IIO_VOLTAGE, \
133 .channel = (_chan), \
134 .address = (_addr | (_chan / 2) | ((_chan & 1) ? LTC2497_SIGN : 0)), \
135 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
136 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
137 .datasheet_name = (_ds_name), \
140 #define LTC2497_CHAN_DIFF(_chan, _addr) { \
141 .type = IIO_VOLTAGE, \
143 .channel = (_chan) * 2 + ((_addr) & LTC2497_SIGN ? 1 : 0), \
144 .channel2 = (_chan) * 2 + ((_addr) & LTC2497_SIGN ? 0 : 1),\
145 .address = (_addr | _chan), \
146 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
147 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
151 static const struct iio_chan_spec ltc2497_channel
[] = {
152 LTC2497_CHAN(0, LTC2497_SGL
, "CH0"),
153 LTC2497_CHAN(1, LTC2497_SGL
, "CH1"),
154 LTC2497_CHAN(2, LTC2497_SGL
, "CH2"),
155 LTC2497_CHAN(3, LTC2497_SGL
, "CH3"),
156 LTC2497_CHAN(4, LTC2497_SGL
, "CH4"),
157 LTC2497_CHAN(5, LTC2497_SGL
, "CH5"),
158 LTC2497_CHAN(6, LTC2497_SGL
, "CH6"),
159 LTC2497_CHAN(7, LTC2497_SGL
, "CH7"),
160 LTC2497_CHAN(8, LTC2497_SGL
, "CH8"),
161 LTC2497_CHAN(9, LTC2497_SGL
, "CH9"),
162 LTC2497_CHAN(10, LTC2497_SGL
, "CH10"),
163 LTC2497_CHAN(11, LTC2497_SGL
, "CH11"),
164 LTC2497_CHAN(12, LTC2497_SGL
, "CH12"),
165 LTC2497_CHAN(13, LTC2497_SGL
, "CH13"),
166 LTC2497_CHAN(14, LTC2497_SGL
, "CH14"),
167 LTC2497_CHAN(15, LTC2497_SGL
, "CH15"),
168 LTC2497_CHAN_DIFF(0, LTC2497_DIFF
),
169 LTC2497_CHAN_DIFF(1, LTC2497_DIFF
),
170 LTC2497_CHAN_DIFF(2, LTC2497_DIFF
),
171 LTC2497_CHAN_DIFF(3, LTC2497_DIFF
),
172 LTC2497_CHAN_DIFF(4, LTC2497_DIFF
),
173 LTC2497_CHAN_DIFF(5, LTC2497_DIFF
),
174 LTC2497_CHAN_DIFF(6, LTC2497_DIFF
),
175 LTC2497_CHAN_DIFF(7, LTC2497_DIFF
),
176 LTC2497_CHAN_DIFF(0, LTC2497_DIFF
| LTC2497_SIGN
),
177 LTC2497_CHAN_DIFF(1, LTC2497_DIFF
| LTC2497_SIGN
),
178 LTC2497_CHAN_DIFF(2, LTC2497_DIFF
| LTC2497_SIGN
),
179 LTC2497_CHAN_DIFF(3, LTC2497_DIFF
| LTC2497_SIGN
),
180 LTC2497_CHAN_DIFF(4, LTC2497_DIFF
| LTC2497_SIGN
),
181 LTC2497_CHAN_DIFF(5, LTC2497_DIFF
| LTC2497_SIGN
),
182 LTC2497_CHAN_DIFF(6, LTC2497_DIFF
| LTC2497_SIGN
),
183 LTC2497_CHAN_DIFF(7, LTC2497_DIFF
| LTC2497_SIGN
),
186 static const struct iio_info ltc2497_info
= {
187 .read_raw
= ltc2497_read_raw
,
190 static int ltc2497_probe(struct i2c_client
*client
,
191 const struct i2c_device_id
*id
)
193 struct iio_dev
*indio_dev
;
194 struct ltc2497_st
*st
;
195 struct iio_map
*plat_data
;
198 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
|
199 I2C_FUNC_SMBUS_WRITE_BYTE
))
202 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*st
));
206 st
= iio_priv(indio_dev
);
207 i2c_set_clientdata(client
, indio_dev
);
210 indio_dev
->dev
.parent
= &client
->dev
;
211 indio_dev
->name
= id
->name
;
212 indio_dev
->info
= <c2497_info
;
213 indio_dev
->modes
= INDIO_DIRECT_MODE
;
214 indio_dev
->channels
= ltc2497_channel
;
215 indio_dev
->num_channels
= ARRAY_SIZE(ltc2497_channel
);
217 st
->ref
= devm_regulator_get(&client
->dev
, "vref");
219 return PTR_ERR(st
->ref
);
221 ret
= regulator_enable(st
->ref
);
225 if (client
->dev
.platform_data
) {
226 plat_data
= ((struct iio_map
*)client
->dev
.platform_data
);
227 ret
= iio_map_array_register(indio_dev
, plat_data
);
229 dev_err(&indio_dev
->dev
, "iio map err: %d\n", ret
);
230 goto err_regulator_disable
;
234 ret
= i2c_smbus_write_byte(st
->client
, LTC2497_CONFIG_DEFAULT
);
236 goto err_array_unregister
;
238 st
->addr_prev
= LTC2497_CONFIG_DEFAULT
;
239 st
->time_prev
= ktime_get();
241 ret
= iio_device_register(indio_dev
);
243 goto err_array_unregister
;
247 err_array_unregister
:
248 iio_map_array_unregister(indio_dev
);
250 err_regulator_disable
:
251 regulator_disable(st
->ref
);
256 static int ltc2497_remove(struct i2c_client
*client
)
258 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
259 struct ltc2497_st
*st
= iio_priv(indio_dev
);
261 iio_map_array_unregister(indio_dev
);
262 iio_device_unregister(indio_dev
);
263 regulator_disable(st
->ref
);
268 static const struct i2c_device_id ltc2497_id
[] = {
272 MODULE_DEVICE_TABLE(i2c
, ltc2497_id
);
274 static const struct of_device_id ltc2497_of_match
[] = {
275 { .compatible
= "lltc,ltc2497", },
278 MODULE_DEVICE_TABLE(of
, ltc2497_of_match
);
280 static struct i2c_driver ltc2497_driver
= {
283 .of_match_table
= of_match_ptr(ltc2497_of_match
),
285 .probe
= ltc2497_probe
,
286 .remove
= ltc2497_remove
,
287 .id_table
= ltc2497_id
,
289 module_i2c_driver(ltc2497_driver
);
291 MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
292 MODULE_DESCRIPTION("Linear Technology LTC2497 ADC driver");
293 MODULE_LICENSE("GPL v2");