10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
14 #include <linux/i2c.h>
15 #include <linux/module.h>
28 #define AD7152_REG_STATUS 0
29 #define AD7152_REG_CH1_DATA_HIGH 1
30 #define AD7152_REG_CH2_DATA_HIGH 3
31 #define AD7152_REG_CH1_OFFS_HIGH 5
32 #define AD7152_REG_CH2_OFFS_HIGH 7
33 #define AD7152_REG_CH1_GAIN_HIGH 9
34 #define AD7152_REG_CH1_SETUP 11
35 #define AD7152_REG_CH2_GAIN_HIGH 12
36 #define AD7152_REG_CH2_SETUP 14
37 #define AD7152_REG_CFG 15
38 #define AD7152_REG_RESEVERD 16
39 #define AD7152_REG_CAPDAC_POS 17
40 #define AD7152_REG_CAPDAC_NEG 18
41 #define AD7152_REG_CFG2 26
44 #define AD7152_STATUS_RDY1 (1 << 0)
45 #define AD7152_STATUS_RDY2 (1 << 1)
46 #define AD7152_STATUS_C1C2 (1 << 2)
47 #define AD7152_STATUS_PWDN (1 << 7)
50 #define AD7152_SETUP_CAPDIFF (1 << 5)
51 #define AD7152_SETUP_RANGE_2pF (0 << 6)
52 #define AD7152_SETUP_RANGE_0_5pF (1 << 6)
53 #define AD7152_SETUP_RANGE_1pF (2 << 6)
54 #define AD7152_SETUP_RANGE_4pF (3 << 6)
55 #define AD7152_SETUP_RANGE(x) ((x) << 6)
58 #define AD7152_CONF_CH2EN (1 << 3)
59 #define AD7152_CONF_CH1EN (1 << 4)
60 #define AD7152_CONF_MODE_IDLE (0 << 0)
61 #define AD7152_CONF_MODE_CONT_CONV (1 << 0)
62 #define AD7152_CONF_MODE_SINGLE_CONV (2 << 0)
63 #define AD7152_CONF_MODE_OFFS_CAL (5 << 0)
64 #define AD7152_CONF_MODE_GAIN_CAL (6 << 0)
67 #define AD7152_CAPDAC_DACEN (1 << 7)
68 #define AD7152_CAPDAC_DACP(x) ((x) & 0x1F)
71 #define AD7152_CFG2_OSR(x) (((x) & 0x3) << 4)
100 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
132 }
while ((ret == regval) && timeout--);
142 return ad7152_start_calib(dev, attr, buf, len,
150 return ad7152_start_calib(dev, attr, buf, len,
164 static const unsigned char ad7152_filter_rate_table[][2] = {
165 {200, 5 + 1}, {50, 20 + 1}, {20, 50 + 1}, {17, 60 + 1},
168 static ssize_t ad7152_show_filter_rate_setup(
struct device *dev,
172 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
179 static ssize_t ad7152_store_filter_rate_setup(
struct device *dev,
184 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
193 for (i = 0; i <
ARRAY_SIZE(ad7152_filter_rate_table); i++)
194 if (data >= ad7152_filter_rate_table[i][0])
197 if (i >=
ARRAY_SIZE(ad7152_filter_rate_table))
215 ad7152_show_filter_rate_setup,
216 ad7152_store_filter_rate_setup);
221 "0.000061050 0.000030525 0.000015263 0.000007631");
223 static struct attribute *ad7152_attributes[] = {
224 &iio_dev_attr_sampling_frequency.dev_attr.attr,
225 &iio_dev_attr_in_capacitance0_calibbias_calibration.dev_attr.attr,
226 &iio_dev_attr_in_capacitance1_calibbias_calibration.dev_attr.attr,
227 &iio_dev_attr_in_capacitance0_calibscale_calibration.dev_attr.attr,
228 &iio_dev_attr_in_capacitance1_calibscale_calibration.dev_attr.attr,
229 &iio_const_attr_in_capacitance_scale_available.dev_attr.attr,
230 &iio_const_attr_sampling_frequency_available.dev_attr.attr,
235 .attrs = ad7152_attributes,
238 static const u8 ad7152_addresses[][4] = {
246 static const int ad7152_scale_table[] = {
247 30525, 7631, 15263, 61050
250 static int ad7152_write_raw(
struct iio_dev *indio_dev,
268 val = (val2 * 1024) / 15625;
280 if ((val < 0) | (val > 0xFFFF)) {
297 for (i = 0; i <
ARRAY_SIZE(ad7152_scale_table); i++)
298 if (val2 == ad7152_scale_table[i])
320 static int ad7152_read_raw(
struct iio_dev *indio_dev,
378 ad7152_addresses[chan->
channel][AD7152_GAIN]);
383 *val2 = (15625 *
swab16(ret)) / 1024;
389 ad7152_addresses[chan->
channel][AD7152_OFFS]);
402 *val2 = ad7152_scale_table[ret >> 6];
414 static int ad7152_write_raw_get_fmt(
struct iio_dev *indio_dev,
426 static const struct iio_info ad7152_info = {
427 .attrs = &ad7152_attribute_group,
428 .read_raw = &ad7152_read_raw,
429 .write_raw = &ad7152_write_raw,
430 .write_raw_get_fmt = &ad7152_write_raw_get_fmt,
485 if (indio_dev ==
NULL) {
489 chip = iio_priv(indio_dev);
491 i2c_set_clientdata(client, indio_dev);
496 indio_dev->
name =
id->name;
497 indio_dev->
dev.parent = &client->
dev;
498 indio_dev->
info = &ad7152_info;
499 indio_dev->
channels = ad7152_channels;
500 if (id->driver_data == 0)
511 dev_err(&client->
dev,
"%s capacitive sensor registered\n", id->
name);
523 struct iio_dev *indio_dev = i2c_get_clientdata(client);
541 .name = KBUILD_MODNAME,
543 .probe = ad7152_probe,
545 .id_table = ad7152_id,