2 * AD7746 capacitive sensor driver supporting AD7745, AD7746 and AD7747
4 * Copyright 2011 Analog Devices Inc.
6 * Licensed under the GPL-2.
9 #include <linux/interrupt.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 #include <linux/i2c.h>
15 #include <linux/delay.h>
16 #include <linux/module.h>
17 #include <linux/stat.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/sysfs.h>
25 * AD7746 Register Definition
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
48 /* Status Register Bit Designations (AD7746_REG_STATUS) */
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)
54 /* Capacitive Channel Setup Register Bit Designations (AD7746_REG_CAP_SETUP) */
55 #define AD7746_CAPSETUP_CAPEN (1 << 7)
56 #define AD7746_CAPSETUP_CIN2 (1 << 6) /* AD7746 only */
57 #define AD7746_CAPSETUP_CAPDIFF (1 << 5)
58 #define AD7746_CAPSETUP_CACHOP (1 << 0)
60 /* Voltage/Temperature Setup Register Bit Designations (AD7746_REG_VT_SETUP) */
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)
70 /* Excitation Setup Register Bit Designations (AD7746_REG_EXC_SETUP) */
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)
79 /* Config Register Bit Designations (AD7746_REG_CFG) */
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)
89 /* CAPDAC Register Bit Designations (AD7746_REG_CAPDACx) */
90 #define AD7746_CAPDAC_DACEN (1 << 7)
91 #define AD7746_CAPDAC_DACP(x) ((x) & 0x7F)
94 * struct ad7746_chip_info - chip specifc information
97 struct ad7746_chip_info {
98 struct i2c_client *client;
100 * Capacitive channel digital filter setup;
101 * conversion time/update rate setup per channel
121 static const struct iio_chan_spec ad7746_channels[] = {
126 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
127 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
128 .address = AD7746_REG_VT_DATA_HIGH << 8 |
129 AD7746_VTSETUP_VTMD_EXT_VIN,
135 .extend_name = "supply",
136 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
137 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
138 .address = AD7746_REG_VT_DATA_HIGH << 8 |
139 AD7746_VTSETUP_VTMD_VDD_MON,
145 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
146 .address = AD7746_REG_VT_DATA_HIGH << 8 |
147 AD7746_VTSETUP_VTMD_INT_TEMP,
153 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
154 .address = AD7746_REG_VT_DATA_HIGH << 8 |
155 AD7746_VTSETUP_VTMD_EXT_TEMP,
158 .type = IIO_CAPACITANCE,
161 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
162 BIT(IIO_CHAN_INFO_CALIBSCALE) | BIT(IIO_CHAN_INFO_OFFSET),
163 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBBIAS) |
164 BIT(IIO_CHAN_INFO_SCALE),
165 .address = AD7746_REG_CAP_DATA_HIGH << 8,
168 .type = IIO_CAPACITANCE,
173 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
174 BIT(IIO_CHAN_INFO_CALIBSCALE) | BIT(IIO_CHAN_INFO_OFFSET),
175 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBBIAS) |
176 BIT(IIO_CHAN_INFO_SCALE),
177 .address = AD7746_REG_CAP_DATA_HIGH << 8 |
178 AD7746_CAPSETUP_CAPDIFF
181 .type = IIO_CAPACITANCE,
184 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
185 BIT(IIO_CHAN_INFO_CALIBSCALE) | BIT(IIO_CHAN_INFO_OFFSET),
186 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBBIAS) |
187 BIT(IIO_CHAN_INFO_SCALE),
188 .address = AD7746_REG_CAP_DATA_HIGH << 8 |
189 AD7746_CAPSETUP_CIN2,
192 .type = IIO_CAPACITANCE,
197 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
198 BIT(IIO_CHAN_INFO_CALIBSCALE) | BIT(IIO_CHAN_INFO_OFFSET),
199 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBBIAS) |
200 BIT(IIO_CHAN_INFO_SCALE),
201 .address = AD7746_REG_CAP_DATA_HIGH << 8 |
202 AD7746_CAPSETUP_CAPDIFF | AD7746_CAPSETUP_CIN2,
206 /* Values are Update Rate (Hz), Conversion Time (ms) + 1*/
207 static const unsigned char ad7746_vt_filter_rate_table[][2] = {
208 {50, 20 + 1}, {31, 32 + 1}, {16, 62 + 1}, {8, 122 + 1},
211 static const unsigned char ad7746_cap_filter_rate_table[][2] = {
212 {91, 11 + 1}, {84, 12 + 1}, {50, 20 + 1}, {26, 38 + 1},
213 {16, 62 + 1}, {13, 77 + 1}, {11, 92 + 1}, {9, 110 + 1},
216 static int ad7746_select_channel(struct iio_dev *indio_dev,
217 struct iio_chan_spec const *chan)
219 struct ad7746_chip_info *chip = iio_priv(indio_dev);
221 u8 vt_setup, cap_setup;
223 switch (chan->type) {
224 case IIO_CAPACITANCE:
225 cap_setup = (chan->address & 0xFF) | AD7746_CAPSETUP_CAPEN;
226 vt_setup = chip->vt_setup & ~AD7746_VTSETUP_VTEN;
227 delay = ad7746_cap_filter_rate_table[(chip->config >> 3) &
230 if (chip->capdac_set != chan->channel) {
231 ret = i2c_smbus_write_byte_data(chip->client,
233 chip->capdac[chan->channel][0]);
236 ret = i2c_smbus_write_byte_data(chip->client,
238 chip->capdac[chan->channel][1]);
242 chip->capdac_set = chan->channel;
247 vt_setup = (chan->address & 0xFF) | AD7746_VTSETUP_VTEN;
248 cap_setup = chip->cap_setup & ~AD7746_CAPSETUP_CAPEN;
249 delay = ad7746_cap_filter_rate_table[(chip->config >> 6) &
256 if (chip->cap_setup != cap_setup) {
257 ret = i2c_smbus_write_byte_data(chip->client,
258 AD7746_REG_CAP_SETUP,
263 chip->cap_setup = cap_setup;
266 if (chip->vt_setup != vt_setup) {
267 ret = i2c_smbus_write_byte_data(chip->client,
273 chip->vt_setup = vt_setup;
279 static inline ssize_t ad7746_start_calib(struct device *dev,
280 struct device_attribute *attr,
285 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
286 struct ad7746_chip_info *chip = iio_priv(indio_dev);
288 int ret, timeout = 10;
290 ret = strtobool(buf, &doit);
297 mutex_lock(&indio_dev->mlock);
298 regval |= chip->config;
299 ret = i2c_smbus_write_byte_data(chip->client, AD7746_REG_CFG, regval);
301 mutex_unlock(&indio_dev->mlock);
307 ret = i2c_smbus_read_byte_data(chip->client, AD7746_REG_CFG);
309 mutex_unlock(&indio_dev->mlock);
312 } while ((ret == regval) && timeout--);
314 mutex_unlock(&indio_dev->mlock);
319 static ssize_t ad7746_start_offset_calib(struct device *dev,
320 struct device_attribute *attr,
324 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
325 int ret = ad7746_select_channel(indio_dev,
326 &ad7746_channels[to_iio_dev_attr(attr)->address]);
330 return ad7746_start_calib(dev, attr, buf, len,
331 AD7746_CONF_MODE_OFFS_CAL);
334 static ssize_t ad7746_start_gain_calib(struct device *dev,
335 struct device_attribute *attr,
339 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
340 int ret = ad7746_select_channel(indio_dev,
341 &ad7746_channels[to_iio_dev_attr(attr)->address]);
345 return ad7746_start_calib(dev, attr, buf, len,
346 AD7746_CONF_MODE_GAIN_CAL);
349 static IIO_DEVICE_ATTR(in_capacitance0_calibbias_calibration,
350 S_IWUSR, NULL, ad7746_start_offset_calib, CIN1);
351 static IIO_DEVICE_ATTR(in_capacitance1_calibbias_calibration,
352 S_IWUSR, NULL, ad7746_start_offset_calib, CIN2);
353 static IIO_DEVICE_ATTR(in_capacitance0_calibscale_calibration,
354 S_IWUSR, NULL, ad7746_start_gain_calib, CIN1);
355 static IIO_DEVICE_ATTR(in_capacitance1_calibscale_calibration,
356 S_IWUSR, NULL, ad7746_start_gain_calib, CIN2);
357 static IIO_DEVICE_ATTR(in_voltage0_calibscale_calibration,
358 S_IWUSR, NULL, ad7746_start_gain_calib, VIN);
360 static ssize_t ad7746_show_cap_filter_rate_setup(struct device *dev,
361 struct device_attribute *attr,
364 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
365 struct ad7746_chip_info *chip = iio_priv(indio_dev);
367 return sprintf(buf, "%d\n", ad7746_cap_filter_rate_table[
368 (chip->config >> 3) & 0x7][0]);
371 static ssize_t ad7746_store_cap_filter_rate_setup(struct device *dev,
372 struct device_attribute *attr,
376 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
377 struct ad7746_chip_info *chip = iio_priv(indio_dev);
381 ret = kstrtou8(buf, 10, &data);
385 for (i = 0; i < ARRAY_SIZE(ad7746_cap_filter_rate_table); i++)
386 if (data >= ad7746_cap_filter_rate_table[i][0])
389 if (i >= ARRAY_SIZE(ad7746_cap_filter_rate_table))
390 i = ARRAY_SIZE(ad7746_cap_filter_rate_table) - 1;
392 mutex_lock(&indio_dev->mlock);
393 chip->config &= ~AD7746_CONF_CAPFS(0x7);
394 chip->config |= AD7746_CONF_CAPFS(i);
395 mutex_unlock(&indio_dev->mlock);
400 static ssize_t ad7746_show_vt_filter_rate_setup(struct device *dev,
401 struct device_attribute *attr,
404 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
405 struct ad7746_chip_info *chip = iio_priv(indio_dev);
407 return sprintf(buf, "%d\n", ad7746_vt_filter_rate_table[
408 (chip->config >> 6) & 0x3][0]);
411 static ssize_t ad7746_store_vt_filter_rate_setup(struct device *dev,
412 struct device_attribute *attr,
416 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
417 struct ad7746_chip_info *chip = iio_priv(indio_dev);
421 ret = kstrtou8(buf, 10, &data);
425 for (i = 0; i < ARRAY_SIZE(ad7746_vt_filter_rate_table); i++)
426 if (data >= ad7746_vt_filter_rate_table[i][0])
429 if (i >= ARRAY_SIZE(ad7746_vt_filter_rate_table))
430 i = ARRAY_SIZE(ad7746_vt_filter_rate_table) - 1;
432 mutex_lock(&indio_dev->mlock);
433 chip->config &= ~AD7746_CONF_VTFS(0x3);
434 chip->config |= AD7746_CONF_VTFS(i);
435 mutex_unlock(&indio_dev->mlock);
440 static IIO_DEVICE_ATTR(in_capacitance_sampling_frequency,
441 S_IRUGO | S_IWUSR, ad7746_show_cap_filter_rate_setup,
442 ad7746_store_cap_filter_rate_setup, 0);
444 static IIO_DEVICE_ATTR(in_voltage_sampling_frequency,
445 S_IRUGO | S_IWUSR, ad7746_show_vt_filter_rate_setup,
446 ad7746_store_vt_filter_rate_setup, 0);
448 static IIO_CONST_ATTR(in_voltage_sampling_frequency_available, "50 31 16 8");
449 static IIO_CONST_ATTR(in_capacitance_sampling_frequency_available,
450 "91 84 50 26 16 13 11 9");
452 static struct attribute *ad7746_attributes[] = {
453 &iio_dev_attr_in_capacitance_sampling_frequency.dev_attr.attr,
454 &iio_dev_attr_in_voltage_sampling_frequency.dev_attr.attr,
455 &iio_dev_attr_in_capacitance0_calibbias_calibration.dev_attr.attr,
456 &iio_dev_attr_in_capacitance0_calibscale_calibration.dev_attr.attr,
457 &iio_dev_attr_in_capacitance1_calibscale_calibration.dev_attr.attr,
458 &iio_dev_attr_in_capacitance1_calibbias_calibration.dev_attr.attr,
459 &iio_dev_attr_in_voltage0_calibscale_calibration.dev_attr.attr,
460 &iio_const_attr_in_voltage_sampling_frequency_available.dev_attr.attr,
461 &iio_const_attr_in_capacitance_sampling_frequency_available.
466 static const struct attribute_group ad7746_attribute_group = {
467 .attrs = ad7746_attributes,
470 static int ad7746_write_raw(struct iio_dev *indio_dev,
471 struct iio_chan_spec const *chan,
476 struct ad7746_chip_info *chip = iio_priv(indio_dev);
479 mutex_lock(&indio_dev->mlock);
482 case IIO_CHAN_INFO_CALIBSCALE:
488 val = (val2 * 1024) / 15625;
490 switch (chan->type) {
491 case IIO_CAPACITANCE:
492 reg = AD7746_REG_CAP_GAINH;
495 reg = AD7746_REG_VOLT_GAINH;
502 ret = i2c_smbus_write_word_data(chip->client, reg, swab16(val));
508 case IIO_CHAN_INFO_CALIBBIAS:
509 if ((val < 0) | (val > 0xFFFF)) {
513 ret = i2c_smbus_write_word_data(chip->client,
514 AD7746_REG_CAP_OFFH, swab16(val));
520 case IIO_CHAN_INFO_OFFSET:
521 if ((val < 0) | (val > 43008000)) { /* 21pF */
526 /* CAPDAC Scale = 21pF_typ / 127
527 * CIN Scale = 8.192pF / 2^24
528 * Offset Scale = CAPDAC Scale / CIN Scale = 338646
533 chip->capdac[chan->channel][chan->differential] = (val > 0 ?
534 AD7746_CAPDAC_DACP(val) | AD7746_CAPDAC_DACEN : 0);
536 ret = i2c_smbus_write_byte_data(chip->client,
538 chip->capdac[chan->channel][0]);
541 ret = i2c_smbus_write_byte_data(chip->client,
543 chip->capdac[chan->channel][1]);
547 chip->capdac_set = chan->channel;
556 mutex_unlock(&indio_dev->mlock);
560 static int ad7746_read_raw(struct iio_dev *indio_dev,
561 struct iio_chan_spec const *chan,
565 struct ad7746_chip_info *chip = iio_priv(indio_dev);
574 mutex_lock(&indio_dev->mlock);
577 case IIO_CHAN_INFO_RAW:
578 case IIO_CHAN_INFO_PROCESSED:
579 ret = ad7746_select_channel(indio_dev, chan);
584 regval = chip->config | AD7746_CONF_MODE_SINGLE_CONV;
585 ret = i2c_smbus_write_byte_data(chip->client, AD7746_REG_CFG,
591 /* Now read the actual register */
593 ret = i2c_smbus_read_i2c_block_data(chip->client,
594 chan->address >> 8, 3, &data.d8[1]);
599 *val = (be32_to_cpu(data.d32) & 0xFFFFFF) - 0x800000;
601 switch (chan->type) {
603 /* temperature in milli degrees Celsius
604 * T = ((*val / 2048) - 4096) * 1000
606 *val = (*val * 125) / 256;
609 if (chan->channel == 1) /* supply_raw*/
618 case IIO_CHAN_INFO_CALIBSCALE:
619 switch (chan->type) {
620 case IIO_CAPACITANCE:
621 reg = AD7746_REG_CAP_GAINH;
624 reg = AD7746_REG_VOLT_GAINH;
631 ret = i2c_smbus_read_word_data(chip->client, reg);
634 /* 1 + gain_val / 2^16 */
636 *val2 = (15625 * swab16(ret)) / 1024;
638 ret = IIO_VAL_INT_PLUS_MICRO;
640 case IIO_CHAN_INFO_CALIBBIAS:
641 ret = i2c_smbus_read_word_data(chip->client,
642 AD7746_REG_CAP_OFFH);
649 case IIO_CHAN_INFO_OFFSET:
650 *val = AD7746_CAPDAC_DACP(chip->capdac[chan->channel]
651 [chan->differential]) * 338646;
655 case IIO_CHAN_INFO_SCALE:
656 switch (chan->type) {
657 case IIO_CAPACITANCE:
672 ret = IIO_VAL_INT_PLUS_NANO;
678 mutex_unlock(&indio_dev->mlock);
682 static const struct iio_info ad7746_info = {
683 .attrs = &ad7746_attribute_group,
684 .read_raw = &ad7746_read_raw,
685 .write_raw = &ad7746_write_raw,
686 .driver_module = THIS_MODULE,
690 * device probe and remove
693 static int ad7746_probe(struct i2c_client *client,
694 const struct i2c_device_id *id)
696 struct ad7746_platform_data *pdata = client->dev.platform_data;
697 struct ad7746_chip_info *chip;
698 struct iio_dev *indio_dev;
700 unsigned char regval = 0;
702 indio_dev = iio_device_alloc(sizeof(*chip));
703 if (indio_dev == NULL) {
707 chip = iio_priv(indio_dev);
708 /* this is only used for device removal purposes */
709 i2c_set_clientdata(client, indio_dev);
711 chip->client = client;
712 chip->capdac_set = -1;
714 /* Establish that the iio_dev is a child of the i2c device */
715 indio_dev->name = id->name;
716 indio_dev->dev.parent = &client->dev;
717 indio_dev->info = &ad7746_info;
718 indio_dev->channels = ad7746_channels;
719 if (id->driver_data == 7746)
720 indio_dev->num_channels = ARRAY_SIZE(ad7746_channels);
722 indio_dev->num_channels = ARRAY_SIZE(ad7746_channels) - 2;
723 indio_dev->num_channels = ARRAY_SIZE(ad7746_channels);
724 indio_dev->modes = INDIO_DIRECT_MODE;
727 if (pdata->exca_en) {
728 if (pdata->exca_inv_en)
729 regval |= AD7746_EXCSETUP_NEXCA;
731 regval |= AD7746_EXCSETUP_EXCA;
734 if (pdata->excb_en) {
735 if (pdata->excb_inv_en)
736 regval |= AD7746_EXCSETUP_NEXCB;
738 regval |= AD7746_EXCSETUP_EXCB;
741 regval |= AD7746_EXCSETUP_EXCLVL(pdata->exclvl);
743 dev_warn(&client->dev, "No platform data? using default\n");
744 regval = AD7746_EXCSETUP_EXCA | AD7746_EXCSETUP_EXCB |
745 AD7746_EXCSETUP_EXCLVL(3);
748 ret = i2c_smbus_write_byte_data(chip->client,
749 AD7746_REG_EXC_SETUP, regval);
753 ret = iio_device_register(indio_dev);
757 dev_info(&client->dev, "%s capacitive sensor registered\n", id->name);
762 iio_device_free(indio_dev);
767 static int ad7746_remove(struct i2c_client *client)
769 struct iio_dev *indio_dev = i2c_get_clientdata(client);
771 iio_device_unregister(indio_dev);
772 iio_device_free(indio_dev);
777 static const struct i2c_device_id ad7746_id[] = {
784 MODULE_DEVICE_TABLE(i2c, ad7746_id);
786 static struct i2c_driver ad7746_driver = {
788 .name = KBUILD_MODNAME,
790 .probe = ad7746_probe,
791 .remove = ad7746_remove,
792 .id_table = ad7746_id,
794 module_i2c_driver(ad7746_driver);
796 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
797 MODULE_DESCRIPTION("Analog Devices AD7746/5/7 capacitive sensor driver");
798 MODULE_LICENSE("GPL v2");