10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
14 #include <linux/i2c.h>
16 #include <linux/module.h>
17 #include <linux/stat.h>
28 #define AD7746_REG_STATUS 0
29 #define AD7746_REG_CAP_DATA_HIGH 1
30 #define AD7746_REG_CAP_DATA_MID 2
31 #define AD7746_REG_CAP_DATA_LOW 3
32 #define AD7746_REG_VT_DATA_HIGH 4
33 #define AD7746_REG_VT_DATA_MID 5
34 #define AD7746_REG_VT_DATA_LOW 6
35 #define AD7746_REG_CAP_SETUP 7
36 #define AD7746_REG_VT_SETUP 8
37 #define AD7746_REG_EXC_SETUP 9
38 #define AD7746_REG_CFG 10
39 #define AD7746_REG_CAPDACA 11
40 #define AD7746_REG_CAPDACB 12
41 #define AD7746_REG_CAP_OFFH 13
42 #define AD7746_REG_CAP_OFFL 14
43 #define AD7746_REG_CAP_GAINH 15
44 #define AD7746_REG_CAP_GAINL 16
45 #define AD7746_REG_VOLT_GAINH 17
46 #define AD7746_REG_VOLT_GAINL 18
49 #define AD7746_STATUS_EXCERR (1 << 3)
50 #define AD7746_STATUS_RDY (1 << 2)
51 #define AD7746_STATUS_RDYVT (1 << 1)
52 #define AD7746_STATUS_RDYCAP (1 << 0)
55 #define AD7746_CAPSETUP_CAPEN (1 << 7)
56 #define AD7746_CAPSETUP_CIN2 (1 << 6)
57 #define AD7746_CAPSETUP_CAPDIFF (1 << 5)
58 #define AD7746_CAPSETUP_CACHOP (1 << 0)
61 #define AD7746_VTSETUP_VTEN (1 << 7)
62 #define AD7746_VTSETUP_VTMD_INT_TEMP (0 << 5)
63 #define AD7746_VTSETUP_VTMD_EXT_TEMP (1 << 5)
64 #define AD7746_VTSETUP_VTMD_VDD_MON (2 << 5)
65 #define AD7746_VTSETUP_VTMD_EXT_VIN (3 << 5)
66 #define AD7746_VTSETUP_EXTREF (1 << 4)
67 #define AD7746_VTSETUP_VTSHORT (1 << 1)
68 #define AD7746_VTSETUP_VTCHOP (1 << 0)
71 #define AD7746_EXCSETUP_CLKCTRL (1 << 7)
72 #define AD7746_EXCSETUP_EXCON (1 << 6)
73 #define AD7746_EXCSETUP_EXCB (1 << 5)
74 #define AD7746_EXCSETUP_NEXCB (1 << 4)
75 #define AD7746_EXCSETUP_EXCA (1 << 3)
76 #define AD7746_EXCSETUP_NEXCA (1 << 2)
77 #define AD7746_EXCSETUP_EXCLVL(x) (((x) & 0x3) << 0)
80 #define AD7746_CONF_VTFS(x) ((x) << 6)
81 #define AD7746_CONF_CAPFS(x) ((x) << 3)
82 #define AD7746_CONF_MODE_IDLE (0 << 0)
83 #define AD7746_CONF_MODE_CONT_CONV (1 << 0)
84 #define AD7746_CONF_MODE_SINGLE_CONV (2 << 0)
85 #define AD7746_CONF_MODE_PWRDN (3 << 0)
86 #define AD7746_CONF_MODE_OFFS_CAL (5 << 0)
87 #define AD7746_CONF_MODE_GAIN_CAL (6 << 0)
90 #define AD7746_CAPDAC_DACEN (1 << 7)
91 #define AD7746_CAPDAC_DACP(x) ((x) & 0x7F)
135 .extend_name =
"supply",
211 static const unsigned char ad7746_vt_filter_rate_table[][2] = {
212 {50, 20 + 1}, {31, 32 + 1}, {16, 62 + 1}, {8, 122 + 1},
215 static const unsigned char ad7746_cap_filter_rate_table[][2] = {
216 {91, 11 + 1}, {84, 12 + 1}, {50, 20 + 1}, {26, 38 + 1},
217 {16, 62 + 1}, {13, 77 + 1}, {11, 92 + 1}, {9, 110 + 1},
220 static int ad7746_select_channel(
struct iio_dev *indio_dev,
227 switch (chan->
type) {
231 delay = ad7746_cap_filter_rate_table[(chip->
config >> 3) &
253 delay = ad7746_cap_filter_rate_table[(chip->
config >> 6) &
289 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
316 }
while ((ret == regval) && timeout--);
328 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
329 int ret = ad7746_select_channel(indio_dev,
334 return ad7746_start_calib(dev, attr, buf, len,
343 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
344 int ret = ad7746_select_channel(indio_dev,
349 return ad7746_start_calib(dev, attr, buf, len,
364 static ssize_t ad7746_show_cap_filter_rate_setup(
struct device *dev,
368 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
371 return sprintf(buf,
"%d\n", ad7746_cap_filter_rate_table[
372 (chip->
config >> 3) & 0x7][0]);
375 static ssize_t ad7746_store_cap_filter_rate_setup(
struct device *dev,
380 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
389 for (i = 0; i <
ARRAY_SIZE(ad7746_cap_filter_rate_table); i++)
390 if (data >= ad7746_cap_filter_rate_table[i][0])
393 if (i >=
ARRAY_SIZE(ad7746_cap_filter_rate_table))
394 i =
ARRAY_SIZE(ad7746_cap_filter_rate_table) - 1;
404 static ssize_t ad7746_show_vt_filter_rate_setup(
struct device *dev,
408 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
411 return sprintf(buf,
"%d\n", ad7746_vt_filter_rate_table[
412 (chip->
config >> 6) & 0x3][0]);
415 static ssize_t ad7746_store_vt_filter_rate_setup(
struct device *dev,
420 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
429 for (i = 0; i <
ARRAY_SIZE(ad7746_vt_filter_rate_table); i++)
430 if (data >= ad7746_vt_filter_rate_table[i][0])
433 if (i >=
ARRAY_SIZE(ad7746_vt_filter_rate_table))
434 i =
ARRAY_SIZE(ad7746_vt_filter_rate_table) - 1;
446 ad7746_store_cap_filter_rate_setup, 0);
450 ad7746_store_vt_filter_rate_setup, 0);
452 static IIO_CONST_ATTR(in_voltage_sampling_frequency_available,
"50 31 16 8");
453 static IIO_CONST_ATTR(in_capacitance_sampling_frequency_available,
454 "91 84 50 26 16 13 11 9");
456 static struct attribute *ad7746_attributes[] = {
457 &iio_dev_attr_in_capacitance_sampling_frequency.dev_attr.attr,
458 &iio_dev_attr_in_voltage_sampling_frequency.dev_attr.attr,
459 &iio_dev_attr_in_capacitance0_calibbias_calibration.dev_attr.attr,
460 &iio_dev_attr_in_capacitance0_calibscale_calibration.dev_attr.attr,
461 &iio_dev_attr_in_capacitance1_calibscale_calibration.dev_attr.attr,
462 &iio_dev_attr_in_capacitance1_calibbias_calibration.dev_attr.attr,
463 &iio_dev_attr_in_voltage0_calibscale_calibration.dev_attr.attr,
464 &iio_const_attr_in_voltage_sampling_frequency_available.dev_attr.attr,
465 &iio_const_attr_in_capacitance_sampling_frequency_available.
471 .attrs = ad7746_attributes,
474 static int ad7746_write_raw(
struct iio_dev *indio_dev,
492 val = (val2 * 1024) / 15625;
494 switch (chan->
type) {
513 if ((val < 0) | (val > 0xFFFF)) {
525 if ((val < 0) | (val > 43008000)) {
564 static int ad7746_read_raw(
struct iio_dev *indio_dev,
583 ret = ad7746_select_channel(indio_dev, chan);
598 chan->
address >> 8, 3, &data.d8[1]);
603 *val = (
be32_to_cpu(data.d32) & 0xFFFFFF) - 0x800000;
605 switch (chan->
type) {
610 *val = (*val * 125) / 256;
623 switch (chan->
type) {
640 *val2 = (15625 *
swab16(ret)) / 1024;
660 switch (chan->
type) {
686 static const struct iio_info ad7746_info = {
687 .attrs = &ad7746_attribute_group,
688 .read_raw = &ad7746_read_raw,
689 .write_raw = &ad7746_write_raw,
704 unsigned char regval = 0;
707 if (indio_dev ==
NULL) {
711 chip = iio_priv(indio_dev);
713 i2c_set_clientdata(client, indio_dev);
719 indio_dev->
name =
id->name;
720 indio_dev->
dev.parent = &client->
dev;
721 indio_dev->
info = &ad7746_info;
722 indio_dev->
channels = ad7746_channels;
723 if (id->driver_data == 7746)
747 dev_warn(&client->
dev,
"No platform data? using default\n");
773 struct iio_dev *indio_dev = i2c_get_clientdata(client);
792 .name = KBUILD_MODNAME,
794 .probe = ad7746_probe,
796 .id_table = ad7746_id,