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 BIT(3)
50 #define AD7746_STATUS_RDY BIT(2)
51 #define AD7746_STATUS_RDYVT BIT(1)
52 #define AD7746_STATUS_RDYCAP BIT(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 BIT(7)
72 #define AD7746_EXCSETUP_EXCON BIT(6)
73 #define AD7746_EXCSETUP_EXCB BIT(5)
74 #define AD7746_EXCSETUP_NEXCB BIT(4)
75 #define AD7746_EXCSETUP_EXCA BIT(3)
76 #define AD7746_EXCSETUP_NEXCA BIT(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 specific 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
112 } data ____cacheline_aligned;
126 static const struct iio_chan_spec ad7746_channels[] = {
131 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
132 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
133 .address = AD7746_REG_VT_DATA_HIGH << 8 |
134 AD7746_VTSETUP_VTMD_EXT_VIN,
140 .extend_name = "supply",
141 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW),
142 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),
143 .address = AD7746_REG_VT_DATA_HIGH << 8 |
144 AD7746_VTSETUP_VTMD_VDD_MON,
150 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
151 .address = AD7746_REG_VT_DATA_HIGH << 8 |
152 AD7746_VTSETUP_VTMD_INT_TEMP,
158 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
159 .address = AD7746_REG_VT_DATA_HIGH << 8 |
160 AD7746_VTSETUP_VTMD_EXT_TEMP,
163 .type = IIO_CAPACITANCE,
166 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
167 BIT(IIO_CHAN_INFO_CALIBSCALE) | BIT(IIO_CHAN_INFO_OFFSET),
168 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBBIAS) |
169 BIT(IIO_CHAN_INFO_SCALE),
170 .address = AD7746_REG_CAP_DATA_HIGH << 8,
173 .type = IIO_CAPACITANCE,
178 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
179 BIT(IIO_CHAN_INFO_CALIBSCALE) | BIT(IIO_CHAN_INFO_OFFSET),
180 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBBIAS) |
181 BIT(IIO_CHAN_INFO_SCALE),
182 .address = AD7746_REG_CAP_DATA_HIGH << 8 |
183 AD7746_CAPSETUP_CAPDIFF
186 .type = IIO_CAPACITANCE,
189 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
190 BIT(IIO_CHAN_INFO_CALIBSCALE) | BIT(IIO_CHAN_INFO_OFFSET),
191 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBBIAS) |
192 BIT(IIO_CHAN_INFO_SCALE),
193 .address = AD7746_REG_CAP_DATA_HIGH << 8 |
194 AD7746_CAPSETUP_CIN2,
197 .type = IIO_CAPACITANCE,
202 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
203 BIT(IIO_CHAN_INFO_CALIBSCALE) | BIT(IIO_CHAN_INFO_OFFSET),
204 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_CALIBBIAS) |
205 BIT(IIO_CHAN_INFO_SCALE),
206 .address = AD7746_REG_CAP_DATA_HIGH << 8 |
207 AD7746_CAPSETUP_CAPDIFF | AD7746_CAPSETUP_CIN2,
211 /* Values are Update Rate (Hz), Conversion Time (ms) + 1*/
212 static const unsigned char ad7746_vt_filter_rate_table[][2] = {
213 {50, 20 + 1}, {31, 32 + 1}, {16, 62 + 1}, {8, 122 + 1},
216 static const unsigned char ad7746_cap_filter_rate_table[][2] = {
217 {91, 11 + 1}, {84, 12 + 1}, {50, 20 + 1}, {26, 38 + 1},
218 {16, 62 + 1}, {13, 77 + 1}, {11, 92 + 1}, {9, 110 + 1},
221 static int ad7746_select_channel(struct iio_dev *indio_dev,
222 struct iio_chan_spec const *chan)
224 struct ad7746_chip_info *chip = iio_priv(indio_dev);
226 u8 vt_setup, cap_setup;
228 switch (chan->type) {
229 case IIO_CAPACITANCE:
230 cap_setup = (chan->address & 0xFF) | AD7746_CAPSETUP_CAPEN;
231 vt_setup = chip->vt_setup & ~AD7746_VTSETUP_VTEN;
232 delay = ad7746_cap_filter_rate_table[(chip->config >> 3) &
235 if (chip->capdac_set != chan->channel) {
236 ret = i2c_smbus_write_byte_data(chip->client,
238 chip->capdac[chan->channel][0]);
241 ret = i2c_smbus_write_byte_data(chip->client,
243 chip->capdac[chan->channel][1]);
247 chip->capdac_set = chan->channel;
252 vt_setup = (chan->address & 0xFF) | AD7746_VTSETUP_VTEN;
253 cap_setup = chip->cap_setup & ~AD7746_CAPSETUP_CAPEN;
254 delay = ad7746_cap_filter_rate_table[(chip->config >> 6) &
261 if (chip->cap_setup != cap_setup) {
262 ret = i2c_smbus_write_byte_data(chip->client,
263 AD7746_REG_CAP_SETUP,
268 chip->cap_setup = cap_setup;
271 if (chip->vt_setup != vt_setup) {
272 ret = i2c_smbus_write_byte_data(chip->client,
278 chip->vt_setup = vt_setup;
284 static inline ssize_t ad7746_start_calib(struct device *dev,
285 struct device_attribute *attr,
290 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
291 struct ad7746_chip_info *chip = iio_priv(indio_dev);
293 int ret, timeout = 10;
295 ret = strtobool(buf, &doit);
302 mutex_lock(&indio_dev->mlock);
303 regval |= chip->config;
304 ret = i2c_smbus_write_byte_data(chip->client, AD7746_REG_CFG, regval);
306 mutex_unlock(&indio_dev->mlock);
312 ret = i2c_smbus_read_byte_data(chip->client, AD7746_REG_CFG);
314 mutex_unlock(&indio_dev->mlock);
317 } while ((ret == regval) && timeout--);
319 mutex_unlock(&indio_dev->mlock);
324 static ssize_t ad7746_start_offset_calib(struct device *dev,
325 struct device_attribute *attr,
329 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
330 int ret = ad7746_select_channel(indio_dev,
331 &ad7746_channels[to_iio_dev_attr(attr)->address]);
335 return ad7746_start_calib(dev, attr, buf, len,
336 AD7746_CONF_MODE_OFFS_CAL);
339 static ssize_t ad7746_start_gain_calib(struct device *dev,
340 struct device_attribute *attr,
344 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
345 int ret = ad7746_select_channel(indio_dev,
346 &ad7746_channels[to_iio_dev_attr(attr)->address]);
350 return ad7746_start_calib(dev, attr, buf, len,
351 AD7746_CONF_MODE_GAIN_CAL);
354 static IIO_DEVICE_ATTR(in_capacitance0_calibbias_calibration,
355 S_IWUSR, NULL, ad7746_start_offset_calib, CIN1);
356 static IIO_DEVICE_ATTR(in_capacitance1_calibbias_calibration,
357 S_IWUSR, NULL, ad7746_start_offset_calib, CIN2);
358 static IIO_DEVICE_ATTR(in_capacitance0_calibscale_calibration,
359 S_IWUSR, NULL, ad7746_start_gain_calib, CIN1);
360 static IIO_DEVICE_ATTR(in_capacitance1_calibscale_calibration,
361 S_IWUSR, NULL, ad7746_start_gain_calib, CIN2);
362 static IIO_DEVICE_ATTR(in_voltage0_calibscale_calibration,
363 S_IWUSR, NULL, ad7746_start_gain_calib, VIN);
365 static ssize_t ad7746_show_cap_filter_rate_setup(struct device *dev,
366 struct device_attribute *attr,
369 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
370 struct ad7746_chip_info *chip = iio_priv(indio_dev);
372 return sprintf(buf, "%d\n", ad7746_cap_filter_rate_table[
373 (chip->config >> 3) & 0x7][0]);
376 static ssize_t ad7746_store_cap_filter_rate_setup(struct device *dev,
377 struct device_attribute *attr,
381 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
382 struct ad7746_chip_info *chip = iio_priv(indio_dev);
386 ret = kstrtou8(buf, 10, &data);
390 for (i = 0; i < ARRAY_SIZE(ad7746_cap_filter_rate_table); i++)
391 if (data >= ad7746_cap_filter_rate_table[i][0])
394 if (i >= ARRAY_SIZE(ad7746_cap_filter_rate_table))
395 i = ARRAY_SIZE(ad7746_cap_filter_rate_table) - 1;
397 mutex_lock(&indio_dev->mlock);
398 chip->config &= ~AD7746_CONF_CAPFS(0x7);
399 chip->config |= AD7746_CONF_CAPFS(i);
400 mutex_unlock(&indio_dev->mlock);
405 static ssize_t ad7746_show_vt_filter_rate_setup(struct device *dev,
406 struct device_attribute *attr,
409 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
410 struct ad7746_chip_info *chip = iio_priv(indio_dev);
412 return sprintf(buf, "%d\n", ad7746_vt_filter_rate_table[
413 (chip->config >> 6) & 0x3][0]);
416 static ssize_t ad7746_store_vt_filter_rate_setup(struct device *dev,
417 struct device_attribute *attr,
421 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
422 struct ad7746_chip_info *chip = iio_priv(indio_dev);
426 ret = kstrtou8(buf, 10, &data);
430 for (i = 0; i < ARRAY_SIZE(ad7746_vt_filter_rate_table); i++)
431 if (data >= ad7746_vt_filter_rate_table[i][0])
434 if (i >= ARRAY_SIZE(ad7746_vt_filter_rate_table))
435 i = ARRAY_SIZE(ad7746_vt_filter_rate_table) - 1;
437 mutex_lock(&indio_dev->mlock);
438 chip->config &= ~AD7746_CONF_VTFS(0x3);
439 chip->config |= AD7746_CONF_VTFS(i);
440 mutex_unlock(&indio_dev->mlock);
445 static IIO_DEVICE_ATTR(in_capacitance_sampling_frequency,
446 S_IRUGO | S_IWUSR, ad7746_show_cap_filter_rate_setup,
447 ad7746_store_cap_filter_rate_setup, 0);
449 static IIO_DEVICE_ATTR(in_voltage_sampling_frequency,
450 S_IRUGO | S_IWUSR, ad7746_show_vt_filter_rate_setup,
451 ad7746_store_vt_filter_rate_setup, 0);
453 static IIO_CONST_ATTR(in_voltage_sampling_frequency_available, "50 31 16 8");
454 static IIO_CONST_ATTR(in_capacitance_sampling_frequency_available,
455 "91 84 50 26 16 13 11 9");
457 static struct attribute *ad7746_attributes[] = {
458 &iio_dev_attr_in_capacitance_sampling_frequency.dev_attr.attr,
459 &iio_dev_attr_in_voltage_sampling_frequency.dev_attr.attr,
460 &iio_dev_attr_in_capacitance0_calibbias_calibration.dev_attr.attr,
461 &iio_dev_attr_in_capacitance0_calibscale_calibration.dev_attr.attr,
462 &iio_dev_attr_in_capacitance1_calibscale_calibration.dev_attr.attr,
463 &iio_dev_attr_in_capacitance1_calibbias_calibration.dev_attr.attr,
464 &iio_dev_attr_in_voltage0_calibscale_calibration.dev_attr.attr,
465 &iio_const_attr_in_voltage_sampling_frequency_available.dev_attr.attr,
466 &iio_const_attr_in_capacitance_sampling_frequency_available.
471 static const struct attribute_group ad7746_attribute_group = {
472 .attrs = ad7746_attributes,
475 static int ad7746_write_raw(struct iio_dev *indio_dev,
476 struct iio_chan_spec const *chan,
481 struct ad7746_chip_info *chip = iio_priv(indio_dev);
484 mutex_lock(&indio_dev->mlock);
487 case IIO_CHAN_INFO_CALIBSCALE:
493 val = (val2 * 1024) / 15625;
495 switch (chan->type) {
496 case IIO_CAPACITANCE:
497 reg = AD7746_REG_CAP_GAINH;
500 reg = AD7746_REG_VOLT_GAINH;
507 ret = i2c_smbus_write_word_data(chip->client, reg, swab16(val));
513 case IIO_CHAN_INFO_CALIBBIAS:
514 if ((val < 0) | (val > 0xFFFF)) {
518 ret = i2c_smbus_write_word_data(chip->client,
519 AD7746_REG_CAP_OFFH, swab16(val));
525 case IIO_CHAN_INFO_OFFSET:
526 if ((val < 0) | (val > 43008000)) { /* 21pF */
531 /* CAPDAC Scale = 21pF_typ / 127
532 * CIN Scale = 8.192pF / 2^24
533 * Offset Scale = CAPDAC Scale / CIN Scale = 338646
538 chip->capdac[chan->channel][chan->differential] = (val > 0 ?
539 AD7746_CAPDAC_DACP(val) | AD7746_CAPDAC_DACEN : 0);
541 ret = i2c_smbus_write_byte_data(chip->client,
543 chip->capdac[chan->channel][0]);
546 ret = i2c_smbus_write_byte_data(chip->client,
548 chip->capdac[chan->channel][1]);
552 chip->capdac_set = chan->channel;
561 mutex_unlock(&indio_dev->mlock);
565 static int ad7746_read_raw(struct iio_dev *indio_dev,
566 struct iio_chan_spec const *chan,
570 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, &chip->data.d8[1]);
599 *val = (be32_to_cpu(chip->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:
661 ret = IIO_VAL_INT_PLUS_NANO;
667 ret = IIO_VAL_FRACTIONAL_LOG2;
679 mutex_unlock(&indio_dev->mlock);
683 static const struct iio_info ad7746_info = {
684 .attrs = &ad7746_attribute_group,
685 .read_raw = &ad7746_read_raw,
686 .write_raw = &ad7746_write_raw,
687 .driver_module = THIS_MODULE,
691 * device probe and remove
694 static int ad7746_probe(struct i2c_client *client,
695 const struct i2c_device_id *id)
697 struct ad7746_platform_data *pdata = client->dev.platform_data;
698 struct ad7746_chip_info *chip;
699 struct iio_dev *indio_dev;
701 unsigned char regval = 0;
703 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
706 chip = iio_priv(indio_dev);
707 /* this is only used for device removal purposes */
708 i2c_set_clientdata(client, indio_dev);
710 chip->client = client;
711 chip->capdac_set = -1;
713 /* Establish that the iio_dev is a child of the i2c device */
714 indio_dev->name = id->name;
715 indio_dev->dev.parent = &client->dev;
716 indio_dev->info = &ad7746_info;
717 indio_dev->channels = ad7746_channels;
718 if (id->driver_data == 7746)
719 indio_dev->num_channels = ARRAY_SIZE(ad7746_channels);
721 indio_dev->num_channels = ARRAY_SIZE(ad7746_channels) - 2;
722 indio_dev->num_channels = ARRAY_SIZE(ad7746_channels);
723 indio_dev->modes = INDIO_DIRECT_MODE;
726 if (pdata->exca_en) {
727 if (pdata->exca_inv_en)
728 regval |= AD7746_EXCSETUP_NEXCA;
730 regval |= AD7746_EXCSETUP_EXCA;
733 if (pdata->excb_en) {
734 if (pdata->excb_inv_en)
735 regval |= AD7746_EXCSETUP_NEXCB;
737 regval |= AD7746_EXCSETUP_EXCB;
740 regval |= AD7746_EXCSETUP_EXCLVL(pdata->exclvl);
742 dev_warn(&client->dev, "No platform data? using default\n");
743 regval = AD7746_EXCSETUP_EXCA | AD7746_EXCSETUP_EXCB |
744 AD7746_EXCSETUP_EXCLVL(3);
747 ret = i2c_smbus_write_byte_data(chip->client,
748 AD7746_REG_EXC_SETUP, regval);
752 ret = iio_device_register(indio_dev);
759 static int ad7746_remove(struct i2c_client *client)
761 struct iio_dev *indio_dev = i2c_get_clientdata(client);
763 iio_device_unregister(indio_dev);
768 static const struct i2c_device_id ad7746_id[] = {
775 MODULE_DEVICE_TABLE(i2c, ad7746_id);
777 static struct i2c_driver ad7746_driver = {
779 .name = KBUILD_MODNAME,
781 .probe = ad7746_probe,
782 .remove = ad7746_remove,
783 .id_table = ad7746_id,
785 module_i2c_driver(ad7746_driver);
787 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
788 MODULE_DESCRIPTION("Analog Devices AD7746/5/7 capacitive sensor driver");
789 MODULE_LICENSE("GPL v2");