2 * ADT7316 digital temperature sensor driver supporting ADT7316/7/8 ADT7516/7/9
5 * Copyright 2010 Analog Devices Inc.
7 * Licensed under the GPL-2 or later.
10 #include <linux/interrupt.h>
11 #include <linux/gpio.h>
12 #include <linux/workqueue.h>
13 #include <linux/device.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/sysfs.h>
17 #include <linux/list.h>
18 #include <linux/i2c.h>
19 #include <linux/rtc.h>
20 #include <linux/module.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/events.h>
24 #include <linux/iio/sysfs.h>
28 * ADT7316 registers definition
30 #define ADT7316_INT_STAT1 0x0
31 #define ADT7316_INT_STAT2 0x1
32 #define ADT7316_LSB_IN_TEMP_VDD 0x3
33 #define ADT7316_LSB_IN_TEMP_MASK 0x3
34 #define ADT7316_LSB_VDD_MASK 0xC
35 #define ADT7316_LSB_VDD_OFFSET 2
36 #define ADT7316_LSB_EX_TEMP_AIN 0x4
37 #define ADT7316_LSB_EX_TEMP_MASK 0x3
38 #define ADT7516_LSB_AIN_SHIFT 2
39 #define ADT7316_AD_MSB_DATA_BASE 0x6
40 #define ADT7316_AD_MSB_DATA_REGS 3
41 #define ADT7516_AD_MSB_DATA_REGS 6
42 #define ADT7316_MSB_VDD 0x6
43 #define ADT7316_MSB_IN_TEMP 0x7
44 #define ADT7316_MSB_EX_TEMP 0x8
45 #define ADT7516_MSB_AIN1 0x8
46 #define ADT7516_MSB_AIN2 0x9
47 #define ADT7516_MSB_AIN3 0xA
48 #define ADT7516_MSB_AIN4 0xB
49 #define ADT7316_DA_DATA_BASE 0x10
50 #define ADT7316_DA_MSB_DATA_REGS 4
51 #define ADT7316_LSB_DAC_A 0x10
52 #define ADT7316_MSB_DAC_A 0x11
53 #define ADT7316_LSB_DAC_B 0x12
54 #define ADT7316_MSB_DAC_B 0x13
55 #define ADT7316_LSB_DAC_C 0x14
56 #define ADT7316_MSB_DAC_C 0x15
57 #define ADT7316_LSB_DAC_D 0x16
58 #define ADT7316_MSB_DAC_D 0x17
59 #define ADT7316_CONFIG1 0x18
60 #define ADT7316_CONFIG2 0x19
61 #define ADT7316_CONFIG3 0x1A
62 #define ADT7316_LDAC_CONFIG 0x1B
63 #define ADT7316_DAC_CONFIG 0x1C
64 #define ADT7316_INT_MASK1 0x1D
65 #define ADT7316_INT_MASK2 0x1E
66 #define ADT7316_IN_TEMP_OFFSET 0x1F
67 #define ADT7316_EX_TEMP_OFFSET 0x20
68 #define ADT7316_IN_ANALOG_TEMP_OFFSET 0x21
69 #define ADT7316_EX_ANALOG_TEMP_OFFSET 0x22
70 #define ADT7316_VDD_HIGH 0x23
71 #define ADT7316_VDD_LOW 0x24
72 #define ADT7316_IN_TEMP_HIGH 0x25
73 #define ADT7316_IN_TEMP_LOW 0x26
74 #define ADT7316_EX_TEMP_HIGH 0x27
75 #define ADT7316_EX_TEMP_LOW 0x28
76 #define ADT7516_AIN2_HIGH 0x2B
77 #define ADT7516_AIN2_LOW 0x2C
78 #define ADT7516_AIN3_HIGH 0x2D
79 #define ADT7516_AIN3_LOW 0x2E
80 #define ADT7516_AIN4_HIGH 0x2F
81 #define ADT7516_AIN4_LOW 0x30
82 #define ADT7316_DEVICE_ID 0x4D
83 #define ADT7316_MANUFACTURE_ID 0x4E
84 #define ADT7316_DEVICE_REV 0x4F
85 #define ADT7316_SPI_LOCK_STAT 0x7F
90 #define ADT7316_EN 0x1
91 #define ADT7516_SEL_EX_TEMP 0x4
92 #define ADT7516_SEL_AIN1_2_EX_TEMP_MASK 0x6
93 #define ADT7516_SEL_AIN3 0x8
94 #define ADT7316_INT_EN 0x20
95 #define ADT7316_INT_POLARITY 0x40
96 #define ADT7316_PD 0x80
101 #define ADT7316_AD_SINGLE_CH_MASK 0x3
102 #define ADT7516_AD_SINGLE_CH_MASK 0x7
103 #define ADT7316_AD_SINGLE_CH_VDD 0
104 #define ADT7316_AD_SINGLE_CH_IN 1
105 #define ADT7316_AD_SINGLE_CH_EX 2
106 #define ADT7516_AD_SINGLE_CH_AIN1 2
107 #define ADT7516_AD_SINGLE_CH_AIN2 3
108 #define ADT7516_AD_SINGLE_CH_AIN3 4
109 #define ADT7516_AD_SINGLE_CH_AIN4 5
110 #define ADT7316_AD_SINGLE_CH_MODE 0x10
111 #define ADT7316_DISABLE_AVERAGING 0x20
112 #define ADT7316_EN_SMBUS_TIMEOUT 0x40
113 #define ADT7316_RESET 0x80
118 #define ADT7316_ADCLK_22_5 0x1
119 #define ADT7316_DA_HIGH_RESOLUTION 0x2
120 #define ADT7316_DA_EN_VIA_DAC_LDCA 0x4
121 #define ADT7516_AIN_IN_VREF 0x10
122 #define ADT7316_EN_IN_TEMP_PROP_DACA 0x20
123 #define ADT7316_EN_EX_TEMP_PROP_DACB 0x40
128 #define ADT7316_DA_2VREF_CH_MASK 0xF
129 #define ADT7316_DA_EN_MODE_MASK 0x30
130 #define ADT7316_DA_EN_MODE_SINGLE 0x00
131 #define ADT7316_DA_EN_MODE_AB_CD 0x10
132 #define ADT7316_DA_EN_MODE_ABCD 0x20
133 #define ADT7316_DA_EN_MODE_LDAC 0x30
134 #define ADT7316_VREF_BYPASS_DAC_AB 0x40
135 #define ADT7316_VREF_BYPASS_DAC_CD 0x80
138 * ADT7316 LDAC config
140 #define ADT7316_LDAC_EN_DA_MASK 0xF
141 #define ADT7316_DAC_IN_VREF 0x10
142 #define ADT7516_DAC_AB_IN_VREF 0x10
143 #define ADT7516_DAC_CD_IN_VREF 0x20
144 #define ADT7516_DAC_IN_VREF_OFFSET 4
145 #define ADT7516_DAC_IN_VREF_MASK 0x30
150 #define ADT7316_INT_MASK2_VDD 0x10
153 * ADT7316 value masks
155 #define ADT7316_VALUE_MASK 0xfff
156 #define ADT7316_T_VALUE_SIGN 0x400
157 #define ADT7316_T_VALUE_FLOAT_OFFSET 2
158 #define ADT7316_T_VALUE_FLOAT_MASK 0x2
163 #define ID_ADT7316 0x1
164 #define ID_ADT7317 0x2
165 #define ID_ADT7318 0x3
166 #define ID_ADT7516 0x11
167 #define ID_ADT7517 0x12
168 #define ID_ADT7519 0x14
170 #define ID_FAMILY_MASK 0xF0
171 #define ID_ADT73XX 0x0
172 #define ID_ADT75XX 0x10
175 * struct adt7316_chip_info - chip specifc information
178 struct adt7316_chip_info {
179 struct adt7316_bus bus;
181 u16 int_mask; /* 0x2f */
185 u8 dac_config; /* DAC config */
186 u8 ldac_config; /* LDAC config */
187 u8 dac_bits; /* 8, 10, 12 */
192 * Logic interrupt mask for user application to enable
195 #define ADT7316_IN_TEMP_HIGH_INT_MASK 0x1
196 #define ADT7316_IN_TEMP_LOW_INT_MASK 0x2
197 #define ADT7316_EX_TEMP_HIGH_INT_MASK 0x4
198 #define ADT7316_EX_TEMP_LOW_INT_MASK 0x8
199 #define ADT7316_EX_TEMP_FAULT_INT_MASK 0x10
200 #define ADT7516_AIN1_INT_MASK 0x4
201 #define ADT7516_AIN2_INT_MASK 0x20
202 #define ADT7516_AIN3_INT_MASK 0x40
203 #define ADT7516_AIN4_INT_MASK 0x80
204 #define ADT7316_VDD_INT_MASK 0x100
205 #define ADT7316_TEMP_INT_MASK 0x1F
206 #define ADT7516_AIN_INT_MASK 0xE0
207 #define ADT7316_TEMP_AIN_INT_MASK \
208 (ADT7316_TEMP_INT_MASK)
211 * struct adt7316_chip_info - chip specifc information
214 struct adt7316_limit_regs {
219 static ssize_t adt7316_show_enabled(struct device *dev,
220 struct device_attribute *attr,
223 struct iio_dev *dev_info = dev_to_iio_dev(dev);
224 struct adt7316_chip_info *chip = iio_priv(dev_info);
226 return sprintf(buf, "%d\n", !!(chip->config1 & ADT7316_EN));
229 static ssize_t _adt7316_store_enabled(struct adt7316_chip_info *chip,
236 config1 = chip->config1 | ADT7316_EN;
238 config1 = chip->config1 & ~ADT7316_EN;
240 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG1, config1);
244 chip->config1 = config1;
250 static ssize_t adt7316_store_enabled(struct device *dev,
251 struct device_attribute *attr,
255 struct iio_dev *dev_info = dev_to_iio_dev(dev);
256 struct adt7316_chip_info *chip = iio_priv(dev_info);
264 if (_adt7316_store_enabled(chip, enable) < 0)
270 static IIO_DEVICE_ATTR(enabled, S_IRUGO | S_IWUSR,
271 adt7316_show_enabled,
272 adt7316_store_enabled,
275 static ssize_t adt7316_show_select_ex_temp(struct device *dev,
276 struct device_attribute *attr,
279 struct iio_dev *dev_info = dev_to_iio_dev(dev);
280 struct adt7316_chip_info *chip = iio_priv(dev_info);
282 if ((chip->id & ID_FAMILY_MASK) != ID_ADT75XX)
285 return sprintf(buf, "%d\n", !!(chip->config1 & ADT7516_SEL_EX_TEMP));
288 static ssize_t adt7316_store_select_ex_temp(struct device *dev,
289 struct device_attribute *attr,
293 struct iio_dev *dev_info = dev_to_iio_dev(dev);
294 struct adt7316_chip_info *chip = iio_priv(dev_info);
298 if ((chip->id & ID_FAMILY_MASK) != ID_ADT75XX)
301 config1 = chip->config1 & (~ADT7516_SEL_EX_TEMP);
303 config1 |= ADT7516_SEL_EX_TEMP;
305 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG1, config1);
309 chip->config1 = config1;
314 static IIO_DEVICE_ATTR(select_ex_temp, S_IRUGO | S_IWUSR,
315 adt7316_show_select_ex_temp,
316 adt7316_store_select_ex_temp,
319 static ssize_t adt7316_show_mode(struct device *dev,
320 struct device_attribute *attr,
323 struct iio_dev *dev_info = dev_to_iio_dev(dev);
324 struct adt7316_chip_info *chip = iio_priv(dev_info);
326 if (chip->config2 & ADT7316_AD_SINGLE_CH_MODE)
327 return sprintf(buf, "single_channel\n");
329 return sprintf(buf, "round_robin\n");
332 static ssize_t adt7316_store_mode(struct device *dev,
333 struct device_attribute *attr,
337 struct iio_dev *dev_info = dev_to_iio_dev(dev);
338 struct adt7316_chip_info *chip = iio_priv(dev_info);
342 config2 = chip->config2 & (~ADT7316_AD_SINGLE_CH_MODE);
343 if (!memcmp(buf, "single_channel", 14))
344 config2 |= ADT7316_AD_SINGLE_CH_MODE;
346 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG2, config2);
350 chip->config2 = config2;
355 static IIO_DEVICE_ATTR(mode, S_IRUGO | S_IWUSR,
360 static ssize_t adt7316_show_all_modes(struct device *dev,
361 struct device_attribute *attr,
364 return sprintf(buf, "single_channel\nround_robin\n");
367 static IIO_DEVICE_ATTR(all_modes, S_IRUGO, adt7316_show_all_modes, NULL, 0);
369 static ssize_t adt7316_show_ad_channel(struct device *dev,
370 struct device_attribute *attr,
373 struct iio_dev *dev_info = dev_to_iio_dev(dev);
374 struct adt7316_chip_info *chip = iio_priv(dev_info);
376 if (!(chip->config2 & ADT7316_AD_SINGLE_CH_MODE))
379 switch (chip->config2 & ADT7516_AD_SINGLE_CH_MASK) {
380 case ADT7316_AD_SINGLE_CH_VDD:
381 return sprintf(buf, "0 - VDD\n");
382 case ADT7316_AD_SINGLE_CH_IN:
383 return sprintf(buf, "1 - Internal Temperature\n");
384 case ADT7316_AD_SINGLE_CH_EX:
385 if (((chip->id & ID_FAMILY_MASK) == ID_ADT75XX) &&
386 (chip->config1 & ADT7516_SEL_AIN1_2_EX_TEMP_MASK) == 0)
387 return sprintf(buf, "2 - AIN1\n");
389 return sprintf(buf, "2 - External Temperature\n");
390 case ADT7516_AD_SINGLE_CH_AIN2:
391 if ((chip->config1 & ADT7516_SEL_AIN1_2_EX_TEMP_MASK) == 0)
392 return sprintf(buf, "3 - AIN2\n");
394 return sprintf(buf, "N/A\n");
395 case ADT7516_AD_SINGLE_CH_AIN3:
396 if (chip->config1 & ADT7516_SEL_AIN3)
397 return sprintf(buf, "4 - AIN3\n");
399 return sprintf(buf, "N/A\n");
400 case ADT7516_AD_SINGLE_CH_AIN4:
401 return sprintf(buf, "5 - AIN4\n");
403 return sprintf(buf, "N/A\n");
407 static ssize_t adt7316_store_ad_channel(struct device *dev,
408 struct device_attribute *attr,
412 struct iio_dev *dev_info = dev_to_iio_dev(dev);
413 struct adt7316_chip_info *chip = iio_priv(dev_info);
418 if (!(chip->config2 & ADT7316_AD_SINGLE_CH_MODE))
421 ret = kstrtou8(buf, 10, &data);
425 if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX) {
429 config2 = chip->config2 & (~ADT7516_AD_SINGLE_CH_MASK);
434 config2 = chip->config2 & (~ADT7316_AD_SINGLE_CH_MASK);
440 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG2, config2);
444 chip->config2 = config2;
449 static IIO_DEVICE_ATTR(ad_channel, S_IRUGO | S_IWUSR,
450 adt7316_show_ad_channel,
451 adt7316_store_ad_channel,
454 static ssize_t adt7316_show_all_ad_channels(struct device *dev,
455 struct device_attribute *attr,
458 struct iio_dev *dev_info = dev_to_iio_dev(dev);
459 struct adt7316_chip_info *chip = iio_priv(dev_info);
461 if (!(chip->config2 & ADT7316_AD_SINGLE_CH_MODE))
464 if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
465 return sprintf(buf, "0 - VDD\n1 - Internal Temperature\n"
466 "2 - External Temperature or AIN1\n"
467 "3 - AIN2\n4 - AIN3\n5 - AIN4\n");
469 return sprintf(buf, "0 - VDD\n1 - Internal Temperature\n"
470 "2 - External Temperature\n");
473 static IIO_DEVICE_ATTR(all_ad_channels, S_IRUGO,
474 adt7316_show_all_ad_channels, NULL, 0);
476 static ssize_t adt7316_show_disable_averaging(struct device *dev,
477 struct device_attribute *attr,
480 struct iio_dev *dev_info = dev_to_iio_dev(dev);
481 struct adt7316_chip_info *chip = iio_priv(dev_info);
483 return sprintf(buf, "%d\n",
484 !!(chip->config2 & ADT7316_DISABLE_AVERAGING));
487 static ssize_t adt7316_store_disable_averaging(struct device *dev,
488 struct device_attribute *attr,
492 struct iio_dev *dev_info = dev_to_iio_dev(dev);
493 struct adt7316_chip_info *chip = iio_priv(dev_info);
497 config2 = chip->config2 & (~ADT7316_DISABLE_AVERAGING);
499 config2 |= ADT7316_DISABLE_AVERAGING;
501 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG2, config2);
505 chip->config2 = config2;
510 static IIO_DEVICE_ATTR(disable_averaging, S_IRUGO | S_IWUSR,
511 adt7316_show_disable_averaging,
512 adt7316_store_disable_averaging,
515 static ssize_t adt7316_show_enable_smbus_timeout(struct device *dev,
516 struct device_attribute *attr,
519 struct iio_dev *dev_info = dev_to_iio_dev(dev);
520 struct adt7316_chip_info *chip = iio_priv(dev_info);
522 return sprintf(buf, "%d\n",
523 !!(chip->config2 & ADT7316_EN_SMBUS_TIMEOUT));
526 static ssize_t adt7316_store_enable_smbus_timeout(struct device *dev,
527 struct device_attribute *attr,
531 struct iio_dev *dev_info = dev_to_iio_dev(dev);
532 struct adt7316_chip_info *chip = iio_priv(dev_info);
536 config2 = chip->config2 & (~ADT7316_EN_SMBUS_TIMEOUT);
538 config2 |= ADT7316_EN_SMBUS_TIMEOUT;
540 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG2, config2);
544 chip->config2 = config2;
549 static IIO_DEVICE_ATTR(enable_smbus_timeout, S_IRUGO | S_IWUSR,
550 adt7316_show_enable_smbus_timeout,
551 adt7316_store_enable_smbus_timeout,
554 static ssize_t adt7316_show_powerdown(struct device *dev,
555 struct device_attribute *attr,
558 struct iio_dev *dev_info = dev_to_iio_dev(dev);
559 struct adt7316_chip_info *chip = iio_priv(dev_info);
561 return sprintf(buf, "%d\n", !!(chip->config1 & ADT7316_PD));
564 static ssize_t adt7316_store_powerdown(struct device *dev,
565 struct device_attribute *attr,
569 struct iio_dev *dev_info = dev_to_iio_dev(dev);
570 struct adt7316_chip_info *chip = iio_priv(dev_info);
574 config1 = chip->config1 & (~ADT7316_PD);
576 config1 |= ADT7316_PD;
578 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG1, config1);
582 chip->config1 = config1;
587 static IIO_DEVICE_ATTR(powerdown, S_IRUGO | S_IWUSR,
588 adt7316_show_powerdown,
589 adt7316_store_powerdown,
592 static ssize_t adt7316_show_fast_ad_clock(struct device *dev,
593 struct device_attribute *attr,
596 struct iio_dev *dev_info = dev_to_iio_dev(dev);
597 struct adt7316_chip_info *chip = iio_priv(dev_info);
599 return sprintf(buf, "%d\n", !!(chip->config3 & ADT7316_ADCLK_22_5));
602 static ssize_t adt7316_store_fast_ad_clock(struct device *dev,
603 struct device_attribute *attr,
607 struct iio_dev *dev_info = dev_to_iio_dev(dev);
608 struct adt7316_chip_info *chip = iio_priv(dev_info);
612 config3 = chip->config3 & (~ADT7316_ADCLK_22_5);
614 config3 |= ADT7316_ADCLK_22_5;
616 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, config3);
620 chip->config3 = config3;
625 static IIO_DEVICE_ATTR(fast_ad_clock, S_IRUGO | S_IWUSR,
626 adt7316_show_fast_ad_clock,
627 adt7316_store_fast_ad_clock,
630 static ssize_t adt7316_show_da_high_resolution(struct device *dev,
631 struct device_attribute *attr,
634 struct iio_dev *dev_info = dev_to_iio_dev(dev);
635 struct adt7316_chip_info *chip = iio_priv(dev_info);
637 if (chip->config3 & ADT7316_DA_HIGH_RESOLUTION) {
638 if (chip->id == ID_ADT7316 || chip->id == ID_ADT7516)
639 return sprintf(buf, "1 (12 bits)\n");
640 else if (chip->id == ID_ADT7317 || chip->id == ID_ADT7517)
641 return sprintf(buf, "1 (10 bits)\n");
644 return sprintf(buf, "0 (8 bits)\n");
647 static ssize_t adt7316_store_da_high_resolution(struct device *dev,
648 struct device_attribute *attr,
652 struct iio_dev *dev_info = dev_to_iio_dev(dev);
653 struct adt7316_chip_info *chip = iio_priv(dev_info);
660 config3 = chip->config3 | ADT7316_DA_HIGH_RESOLUTION;
661 if (chip->id == ID_ADT7316 || chip->id == ID_ADT7516)
663 else if (chip->id == ID_ADT7317 || chip->id == ID_ADT7517)
666 config3 = chip->config3 & (~ADT7316_DA_HIGH_RESOLUTION);
668 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, config3);
672 chip->config3 = config3;
677 static IIO_DEVICE_ATTR(da_high_resolution, S_IRUGO | S_IWUSR,
678 adt7316_show_da_high_resolution,
679 adt7316_store_da_high_resolution,
682 static ssize_t adt7316_show_AIN_internal_Vref(struct device *dev,
683 struct device_attribute *attr,
686 struct iio_dev *dev_info = dev_to_iio_dev(dev);
687 struct adt7316_chip_info *chip = iio_priv(dev_info);
689 if ((chip->id & ID_FAMILY_MASK) != ID_ADT75XX)
692 return sprintf(buf, "%d\n",
693 !!(chip->config3 & ADT7516_AIN_IN_VREF));
696 static ssize_t adt7316_store_AIN_internal_Vref(struct device *dev,
697 struct device_attribute *attr,
701 struct iio_dev *dev_info = dev_to_iio_dev(dev);
702 struct adt7316_chip_info *chip = iio_priv(dev_info);
706 if ((chip->id & ID_FAMILY_MASK) != ID_ADT75XX)
710 config3 = chip->config3 & (~ADT7516_AIN_IN_VREF);
712 config3 = chip->config3 | ADT7516_AIN_IN_VREF;
714 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, config3);
718 chip->config3 = config3;
723 static IIO_DEVICE_ATTR(AIN_internal_Vref, S_IRUGO | S_IWUSR,
724 adt7316_show_AIN_internal_Vref,
725 adt7316_store_AIN_internal_Vref,
729 static ssize_t adt7316_show_enable_prop_DACA(struct device *dev,
730 struct device_attribute *attr,
733 struct iio_dev *dev_info = dev_to_iio_dev(dev);
734 struct adt7316_chip_info *chip = iio_priv(dev_info);
736 return sprintf(buf, "%d\n",
737 !!(chip->config3 & ADT7316_EN_IN_TEMP_PROP_DACA));
740 static ssize_t adt7316_store_enable_prop_DACA(struct device *dev,
741 struct device_attribute *attr,
745 struct iio_dev *dev_info = dev_to_iio_dev(dev);
746 struct adt7316_chip_info *chip = iio_priv(dev_info);
750 config3 = chip->config3 & (~ADT7316_EN_IN_TEMP_PROP_DACA);
752 config3 |= ADT7316_EN_IN_TEMP_PROP_DACA;
754 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, config3);
758 chip->config3 = config3;
763 static IIO_DEVICE_ATTR(enable_proportion_DACA, S_IRUGO | S_IWUSR,
764 adt7316_show_enable_prop_DACA,
765 adt7316_store_enable_prop_DACA,
768 static ssize_t adt7316_show_enable_prop_DACB(struct device *dev,
769 struct device_attribute *attr,
772 struct iio_dev *dev_info = dev_to_iio_dev(dev);
773 struct adt7316_chip_info *chip = iio_priv(dev_info);
775 return sprintf(buf, "%d\n",
776 !!(chip->config3 & ADT7316_EN_EX_TEMP_PROP_DACB));
779 static ssize_t adt7316_store_enable_prop_DACB(struct device *dev,
780 struct device_attribute *attr,
784 struct iio_dev *dev_info = dev_to_iio_dev(dev);
785 struct adt7316_chip_info *chip = iio_priv(dev_info);
789 config3 = chip->config3 & (~ADT7316_EN_EX_TEMP_PROP_DACB);
791 config3 |= ADT7316_EN_EX_TEMP_PROP_DACB;
793 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, config3);
797 chip->config3 = config3;
802 static IIO_DEVICE_ATTR(enable_proportion_DACB, S_IRUGO | S_IWUSR,
803 adt7316_show_enable_prop_DACB,
804 adt7316_store_enable_prop_DACB,
807 static ssize_t adt7316_show_DAC_2Vref_ch_mask(struct device *dev,
808 struct device_attribute *attr,
811 struct iio_dev *dev_info = dev_to_iio_dev(dev);
812 struct adt7316_chip_info *chip = iio_priv(dev_info);
814 return sprintf(buf, "0x%x\n",
815 chip->dac_config & ADT7316_DA_2VREF_CH_MASK);
818 static ssize_t adt7316_store_DAC_2Vref_ch_mask(struct device *dev,
819 struct device_attribute *attr,
823 struct iio_dev *dev_info = dev_to_iio_dev(dev);
824 struct adt7316_chip_info *chip = iio_priv(dev_info);
829 ret = kstrtou8(buf, 16, &data);
830 if (ret || data > ADT7316_DA_2VREF_CH_MASK)
833 dac_config = chip->dac_config & (~ADT7316_DA_2VREF_CH_MASK);
836 ret = chip->bus.write(chip->bus.client, ADT7316_DAC_CONFIG, dac_config);
840 chip->dac_config = dac_config;
845 static IIO_DEVICE_ATTR(DAC_2Vref_channels_mask, S_IRUGO | S_IWUSR,
846 adt7316_show_DAC_2Vref_ch_mask,
847 adt7316_store_DAC_2Vref_ch_mask,
850 static ssize_t adt7316_show_DAC_update_mode(struct device *dev,
851 struct device_attribute *attr,
854 struct iio_dev *dev_info = dev_to_iio_dev(dev);
855 struct adt7316_chip_info *chip = iio_priv(dev_info);
857 if (!(chip->config3 & ADT7316_DA_EN_VIA_DAC_LDCA))
858 return sprintf(buf, "manual\n");
860 switch (chip->dac_config & ADT7316_DA_EN_MODE_MASK) {
861 case ADT7316_DA_EN_MODE_SINGLE:
862 return sprintf(buf, "0 - auto at any MSB DAC writing\n");
863 case ADT7316_DA_EN_MODE_AB_CD:
864 return sprintf(buf, "1 - auto at MSB DAC AB and CD writing\n");
865 case ADT7316_DA_EN_MODE_ABCD:
866 return sprintf(buf, "2 - auto at MSB DAC ABCD writing\n");
867 default: /* ADT7316_DA_EN_MODE_LDAC */
868 return sprintf(buf, "3 - manual\n");
873 static ssize_t adt7316_store_DAC_update_mode(struct device *dev,
874 struct device_attribute *attr,
878 struct iio_dev *dev_info = dev_to_iio_dev(dev);
879 struct adt7316_chip_info *chip = iio_priv(dev_info);
884 if (!(chip->config3 & ADT7316_DA_EN_VIA_DAC_LDCA))
887 ret = kstrtou8(buf, 10, &data);
888 if (ret || data > ADT7316_DA_EN_MODE_MASK)
891 dac_config = chip->dac_config & (~ADT7316_DA_EN_MODE_MASK);
894 ret = chip->bus.write(chip->bus.client, ADT7316_DAC_CONFIG, dac_config);
898 chip->dac_config = dac_config;
903 static IIO_DEVICE_ATTR(DAC_update_mode, S_IRUGO | S_IWUSR,
904 adt7316_show_DAC_update_mode,
905 adt7316_store_DAC_update_mode,
908 static ssize_t adt7316_show_all_DAC_update_modes(struct device *dev,
909 struct device_attribute *attr,
912 struct iio_dev *dev_info = dev_to_iio_dev(dev);
913 struct adt7316_chip_info *chip = iio_priv(dev_info);
915 if (chip->config3 & ADT7316_DA_EN_VIA_DAC_LDCA)
916 return sprintf(buf, "0 - auto at any MSB DAC writing\n"
917 "1 - auto at MSB DAC AB and CD writing\n"
918 "2 - auto at MSB DAC ABCD writing\n"
921 return sprintf(buf, "manual\n");
924 static IIO_DEVICE_ATTR(all_DAC_update_modes, S_IRUGO,
925 adt7316_show_all_DAC_update_modes, NULL, 0);
928 static ssize_t adt7316_store_update_DAC(struct device *dev,
929 struct device_attribute *attr,
933 struct iio_dev *dev_info = dev_to_iio_dev(dev);
934 struct adt7316_chip_info *chip = iio_priv(dev_info);
939 if (chip->config3 & ADT7316_DA_EN_VIA_DAC_LDCA) {
940 if ((chip->dac_config & ADT7316_DA_EN_MODE_MASK) !=
941 ADT7316_DA_EN_MODE_LDAC)
944 ret = kstrtou8(buf, 16, &data);
945 if (ret || data > ADT7316_LDAC_EN_DA_MASK)
948 ldac_config = chip->ldac_config & (~ADT7316_LDAC_EN_DA_MASK);
951 ret = chip->bus.write(chip->bus.client, ADT7316_LDAC_CONFIG,
956 gpio_set_value(chip->ldac_pin, 0);
957 gpio_set_value(chip->ldac_pin, 1);
963 static IIO_DEVICE_ATTR(update_DAC, S_IRUGO | S_IWUSR,
965 adt7316_store_update_DAC,
968 static ssize_t adt7316_show_DA_AB_Vref_bypass(struct device *dev,
969 struct device_attribute *attr,
972 struct iio_dev *dev_info = dev_to_iio_dev(dev);
973 struct adt7316_chip_info *chip = iio_priv(dev_info);
975 if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
978 return sprintf(buf, "%d\n",
979 !!(chip->dac_config & ADT7316_VREF_BYPASS_DAC_AB));
982 static ssize_t adt7316_store_DA_AB_Vref_bypass(struct device *dev,
983 struct device_attribute *attr,
987 struct iio_dev *dev_info = dev_to_iio_dev(dev);
988 struct adt7316_chip_info *chip = iio_priv(dev_info);
992 if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
995 dac_config = chip->dac_config & (~ADT7316_VREF_BYPASS_DAC_AB);
997 dac_config |= ADT7316_VREF_BYPASS_DAC_AB;
999 ret = chip->bus.write(chip->bus.client, ADT7316_DAC_CONFIG, dac_config);
1003 chip->dac_config = dac_config;
1008 static IIO_DEVICE_ATTR(DA_AB_Vref_bypass, S_IRUGO | S_IWUSR,
1009 adt7316_show_DA_AB_Vref_bypass,
1010 adt7316_store_DA_AB_Vref_bypass,
1013 static ssize_t adt7316_show_DA_CD_Vref_bypass(struct device *dev,
1014 struct device_attribute *attr,
1017 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1018 struct adt7316_chip_info *chip = iio_priv(dev_info);
1020 if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
1023 return sprintf(buf, "%d\n",
1024 !!(chip->dac_config & ADT7316_VREF_BYPASS_DAC_CD));
1027 static ssize_t adt7316_store_DA_CD_Vref_bypass(struct device *dev,
1028 struct device_attribute *attr,
1032 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1033 struct adt7316_chip_info *chip = iio_priv(dev_info);
1037 if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
1040 dac_config = chip->dac_config & (~ADT7316_VREF_BYPASS_DAC_CD);
1042 dac_config |= ADT7316_VREF_BYPASS_DAC_CD;
1044 ret = chip->bus.write(chip->bus.client, ADT7316_DAC_CONFIG, dac_config);
1048 chip->dac_config = dac_config;
1053 static IIO_DEVICE_ATTR(DA_CD_Vref_bypass, S_IRUGO | S_IWUSR,
1054 adt7316_show_DA_CD_Vref_bypass,
1055 adt7316_store_DA_CD_Vref_bypass,
1058 static ssize_t adt7316_show_DAC_internal_Vref(struct device *dev,
1059 struct device_attribute *attr,
1062 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1063 struct adt7316_chip_info *chip = iio_priv(dev_info);
1065 if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
1066 return sprintf(buf, "0x%x\n",
1067 (chip->dac_config & ADT7516_DAC_IN_VREF_MASK) >>
1068 ADT7516_DAC_IN_VREF_OFFSET);
1070 return sprintf(buf, "%d\n",
1071 !!(chip->dac_config & ADT7316_DAC_IN_VREF));
1074 static ssize_t adt7316_store_DAC_internal_Vref(struct device *dev,
1075 struct device_attribute *attr,
1079 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1080 struct adt7316_chip_info *chip = iio_priv(dev_info);
1085 if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX) {
1086 ret = kstrtou8(buf, 16, &data);
1087 if (ret || data > 3)
1090 ldac_config = chip->ldac_config & (~ADT7516_DAC_IN_VREF_MASK);
1092 ldac_config |= ADT7516_DAC_AB_IN_VREF;
1093 else if (data & 0x2)
1094 ldac_config |= ADT7516_DAC_CD_IN_VREF;
1096 ret = kstrtou8(buf, 16, &data);
1100 ldac_config = chip->ldac_config & (~ADT7316_DAC_IN_VREF);
1102 ldac_config = chip->ldac_config | ADT7316_DAC_IN_VREF;
1105 ret = chip->bus.write(chip->bus.client, ADT7316_LDAC_CONFIG, ldac_config);
1109 chip->ldac_config = ldac_config;
1114 static IIO_DEVICE_ATTR(DAC_internal_Vref, S_IRUGO | S_IWUSR,
1115 adt7316_show_DAC_internal_Vref,
1116 adt7316_store_DAC_internal_Vref,
1119 static ssize_t adt7316_show_ad(struct adt7316_chip_info *chip,
1120 int channel, char *buf)
1127 if ((chip->config2 & ADT7316_AD_SINGLE_CH_MODE) &&
1128 channel != (chip->config2 & ADT7516_AD_SINGLE_CH_MASK))
1132 case ADT7316_AD_SINGLE_CH_IN:
1133 ret = chip->bus.read(chip->bus.client,
1134 ADT7316_LSB_IN_TEMP_VDD, &lsb);
1138 ret = chip->bus.read(chip->bus.client,
1139 ADT7316_AD_MSB_DATA_BASE + channel, &msb);
1143 data = msb << ADT7316_T_VALUE_FLOAT_OFFSET;
1144 data |= lsb & ADT7316_LSB_IN_TEMP_MASK;
1146 case ADT7316_AD_SINGLE_CH_VDD:
1147 ret = chip->bus.read(chip->bus.client,
1148 ADT7316_LSB_IN_TEMP_VDD, &lsb);
1152 ret = chip->bus.read(chip->bus.client,
1154 ADT7316_AD_MSB_DATA_BASE + channel, &msb);
1158 data = msb << ADT7316_T_VALUE_FLOAT_OFFSET;
1159 data |= (lsb & ADT7316_LSB_VDD_MASK) >> ADT7316_LSB_VDD_OFFSET;
1160 return sprintf(buf, "%d\n", data);
1161 default: /* ex_temp and ain */
1162 ret = chip->bus.read(chip->bus.client,
1163 ADT7316_LSB_EX_TEMP_AIN, &lsb);
1167 ret = chip->bus.read(chip->bus.client,
1168 ADT7316_AD_MSB_DATA_BASE + channel, &msb);
1172 data = msb << ADT7316_T_VALUE_FLOAT_OFFSET;
1173 data |= lsb & (ADT7316_LSB_EX_TEMP_MASK <<
1174 (ADT7516_LSB_AIN_SHIFT * (channel -
1175 (ADT7316_MSB_EX_TEMP - ADT7316_AD_MSB_DATA_BASE))));
1177 if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
1178 return sprintf(buf, "%d\n", data);
1183 if (data & ADT7316_T_VALUE_SIGN) {
1184 /* convert supplement to positive value */
1185 data = (ADT7316_T_VALUE_SIGN << 1) - data;
1189 return sprintf(buf, "%c%d.%.2d\n", sign,
1190 (data >> ADT7316_T_VALUE_FLOAT_OFFSET),
1191 (data & ADT7316_T_VALUE_FLOAT_MASK) * 25);
1194 static ssize_t adt7316_show_VDD(struct device *dev,
1195 struct device_attribute *attr,
1198 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1199 struct adt7316_chip_info *chip = iio_priv(dev_info);
1201 return adt7316_show_ad(chip, ADT7316_AD_SINGLE_CH_VDD, buf);
1203 static IIO_DEVICE_ATTR(VDD, S_IRUGO, adt7316_show_VDD, NULL, 0);
1205 static ssize_t adt7316_show_in_temp(struct device *dev,
1206 struct device_attribute *attr,
1209 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1210 struct adt7316_chip_info *chip = iio_priv(dev_info);
1212 return adt7316_show_ad(chip, ADT7316_AD_SINGLE_CH_IN, buf);
1215 static IIO_DEVICE_ATTR(in_temp, S_IRUGO, adt7316_show_in_temp, NULL, 0);
1217 static ssize_t adt7316_show_ex_temp_AIN1(struct device *dev,
1218 struct device_attribute *attr,
1221 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1222 struct adt7316_chip_info *chip = iio_priv(dev_info);
1224 return adt7316_show_ad(chip, ADT7316_AD_SINGLE_CH_EX, buf);
1227 static IIO_DEVICE_ATTR(ex_temp_AIN1, S_IRUGO, adt7316_show_ex_temp_AIN1, NULL, 0);
1228 static IIO_DEVICE_ATTR(ex_temp, S_IRUGO, adt7316_show_ex_temp_AIN1, NULL, 0);
1230 static ssize_t adt7316_show_AIN2(struct device *dev,
1231 struct device_attribute *attr,
1234 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1235 struct adt7316_chip_info *chip = iio_priv(dev_info);
1237 return adt7316_show_ad(chip, ADT7516_AD_SINGLE_CH_AIN2, buf);
1239 static IIO_DEVICE_ATTR(AIN2, S_IRUGO, adt7316_show_AIN2, NULL, 0);
1241 static ssize_t adt7316_show_AIN3(struct device *dev,
1242 struct device_attribute *attr,
1245 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1246 struct adt7316_chip_info *chip = iio_priv(dev_info);
1248 return adt7316_show_ad(chip, ADT7516_AD_SINGLE_CH_AIN3, buf);
1250 static IIO_DEVICE_ATTR(AIN3, S_IRUGO, adt7316_show_AIN3, NULL, 0);
1252 static ssize_t adt7316_show_AIN4(struct device *dev,
1253 struct device_attribute *attr,
1256 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1257 struct adt7316_chip_info *chip = iio_priv(dev_info);
1259 return adt7316_show_ad(chip, ADT7516_AD_SINGLE_CH_AIN4, buf);
1261 static IIO_DEVICE_ATTR(AIN4, S_IRUGO, adt7316_show_AIN4, NULL, 0);
1263 static ssize_t adt7316_show_temp_offset(struct adt7316_chip_info *chip,
1264 int offset_addr, char *buf)
1270 ret = chip->bus.read(chip->bus.client, offset_addr, &val);
1278 return sprintf(buf, "%d\n", data);
1281 static ssize_t adt7316_store_temp_offset(struct adt7316_chip_info *chip,
1282 int offset_addr, const char *buf, size_t len)
1288 ret = kstrtoint(buf, 10, &data);
1289 if (ret || data > 127 || data < -128)
1297 ret = chip->bus.write(chip->bus.client, offset_addr, val);
1304 static ssize_t adt7316_show_in_temp_offset(struct device *dev,
1305 struct device_attribute *attr,
1308 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1309 struct adt7316_chip_info *chip = iio_priv(dev_info);
1311 return adt7316_show_temp_offset(chip, ADT7316_IN_TEMP_OFFSET, buf);
1314 static ssize_t adt7316_store_in_temp_offset(struct device *dev,
1315 struct device_attribute *attr,
1319 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1320 struct adt7316_chip_info *chip = iio_priv(dev_info);
1322 return adt7316_store_temp_offset(chip, ADT7316_IN_TEMP_OFFSET, buf, len);
1325 static IIO_DEVICE_ATTR(in_temp_offset, S_IRUGO | S_IWUSR,
1326 adt7316_show_in_temp_offset,
1327 adt7316_store_in_temp_offset, 0);
1329 static ssize_t adt7316_show_ex_temp_offset(struct device *dev,
1330 struct device_attribute *attr,
1333 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1334 struct adt7316_chip_info *chip = iio_priv(dev_info);
1336 return adt7316_show_temp_offset(chip, ADT7316_EX_TEMP_OFFSET, buf);
1339 static ssize_t adt7316_store_ex_temp_offset(struct device *dev,
1340 struct device_attribute *attr,
1344 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1345 struct adt7316_chip_info *chip = iio_priv(dev_info);
1347 return adt7316_store_temp_offset(chip, ADT7316_EX_TEMP_OFFSET, buf, len);
1350 static IIO_DEVICE_ATTR(ex_temp_offset, S_IRUGO | S_IWUSR,
1351 adt7316_show_ex_temp_offset,
1352 adt7316_store_ex_temp_offset, 0);
1354 static ssize_t adt7316_show_in_analog_temp_offset(struct device *dev,
1355 struct device_attribute *attr,
1358 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1359 struct adt7316_chip_info *chip = iio_priv(dev_info);
1361 return adt7316_show_temp_offset(chip,
1362 ADT7316_IN_ANALOG_TEMP_OFFSET, buf);
1365 static ssize_t adt7316_store_in_analog_temp_offset(struct device *dev,
1366 struct device_attribute *attr,
1370 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1371 struct adt7316_chip_info *chip = iio_priv(dev_info);
1373 return adt7316_store_temp_offset(chip,
1374 ADT7316_IN_ANALOG_TEMP_OFFSET, buf, len);
1377 static IIO_DEVICE_ATTR(in_analog_temp_offset, S_IRUGO | S_IWUSR,
1378 adt7316_show_in_analog_temp_offset,
1379 adt7316_store_in_analog_temp_offset, 0);
1381 static ssize_t adt7316_show_ex_analog_temp_offset(struct device *dev,
1382 struct device_attribute *attr,
1385 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1386 struct adt7316_chip_info *chip = iio_priv(dev_info);
1388 return adt7316_show_temp_offset(chip,
1389 ADT7316_EX_ANALOG_TEMP_OFFSET, buf);
1392 static ssize_t adt7316_store_ex_analog_temp_offset(struct device *dev,
1393 struct device_attribute *attr,
1397 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1398 struct adt7316_chip_info *chip = iio_priv(dev_info);
1400 return adt7316_store_temp_offset(chip,
1401 ADT7316_EX_ANALOG_TEMP_OFFSET, buf, len);
1404 static IIO_DEVICE_ATTR(ex_analog_temp_offset, S_IRUGO | S_IWUSR,
1405 adt7316_show_ex_analog_temp_offset,
1406 adt7316_store_ex_analog_temp_offset, 0);
1408 static ssize_t adt7316_show_DAC(struct adt7316_chip_info *chip,
1409 int channel, char *buf)
1412 u8 msb, lsb, offset;
1415 if (channel >= ADT7316_DA_MSB_DATA_REGS ||
1417 (chip->config3 & ADT7316_EN_IN_TEMP_PROP_DACA)) ||
1419 (chip->config3 & ADT7316_EN_EX_TEMP_PROP_DACB)))
1422 offset = chip->dac_bits - 8;
1424 if (chip->dac_bits > 8) {
1425 ret = chip->bus.read(chip->bus.client,
1426 ADT7316_DA_DATA_BASE + channel * 2, &lsb);
1431 ret = chip->bus.read(chip->bus.client,
1432 ADT7316_DA_DATA_BASE + 1 + channel * 2, &msb);
1436 data = (msb << offset) + (lsb & ((1 << offset) - 1));
1438 return sprintf(buf, "%d\n", data);
1441 static ssize_t adt7316_store_DAC(struct adt7316_chip_info *chip,
1442 int channel, const char *buf, size_t len)
1444 u8 msb, lsb, offset;
1448 if (channel >= ADT7316_DA_MSB_DATA_REGS ||
1450 (chip->config3 & ADT7316_EN_IN_TEMP_PROP_DACA)) ||
1452 (chip->config3 & ADT7316_EN_EX_TEMP_PROP_DACB)))
1455 offset = chip->dac_bits - 8;
1457 ret = kstrtou16(buf, 10, &data);
1458 if (ret || data >= (1 << chip->dac_bits))
1461 if (chip->dac_bits > 8) {
1462 lsb = data & (1 << offset);
1463 ret = chip->bus.write(chip->bus.client,
1464 ADT7316_DA_DATA_BASE + channel * 2, lsb);
1469 msb = data >> offset;
1470 ret = chip->bus.write(chip->bus.client,
1471 ADT7316_DA_DATA_BASE + 1 + channel * 2, msb);
1478 static ssize_t adt7316_show_DAC_A(struct device *dev,
1479 struct device_attribute *attr,
1482 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1483 struct adt7316_chip_info *chip = iio_priv(dev_info);
1485 return adt7316_show_DAC(chip, 0, buf);
1488 static ssize_t adt7316_store_DAC_A(struct device *dev,
1489 struct device_attribute *attr,
1493 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1494 struct adt7316_chip_info *chip = iio_priv(dev_info);
1496 return adt7316_store_DAC(chip, 0, buf, len);
1499 static IIO_DEVICE_ATTR(DAC_A, S_IRUGO | S_IWUSR, adt7316_show_DAC_A,
1500 adt7316_store_DAC_A, 0);
1502 static ssize_t adt7316_show_DAC_B(struct device *dev,
1503 struct device_attribute *attr,
1506 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1507 struct adt7316_chip_info *chip = iio_priv(dev_info);
1509 return adt7316_show_DAC(chip, 1, buf);
1512 static ssize_t adt7316_store_DAC_B(struct device *dev,
1513 struct device_attribute *attr,
1517 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1518 struct adt7316_chip_info *chip = iio_priv(dev_info);
1520 return adt7316_store_DAC(chip, 1, buf, len);
1523 static IIO_DEVICE_ATTR(DAC_B, S_IRUGO | S_IWUSR, adt7316_show_DAC_B,
1524 adt7316_store_DAC_B, 0);
1526 static ssize_t adt7316_show_DAC_C(struct device *dev,
1527 struct device_attribute *attr,
1530 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1531 struct adt7316_chip_info *chip = iio_priv(dev_info);
1533 return adt7316_show_DAC(chip, 2, buf);
1536 static ssize_t adt7316_store_DAC_C(struct device *dev,
1537 struct device_attribute *attr,
1541 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1542 struct adt7316_chip_info *chip = iio_priv(dev_info);
1544 return adt7316_store_DAC(chip, 2, buf, len);
1547 static IIO_DEVICE_ATTR(DAC_C, S_IRUGO | S_IWUSR, adt7316_show_DAC_C,
1548 adt7316_store_DAC_C, 0);
1550 static ssize_t adt7316_show_DAC_D(struct device *dev,
1551 struct device_attribute *attr,
1554 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1555 struct adt7316_chip_info *chip = iio_priv(dev_info);
1557 return adt7316_show_DAC(chip, 3, buf);
1560 static ssize_t adt7316_store_DAC_D(struct device *dev,
1561 struct device_attribute *attr,
1565 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1566 struct adt7316_chip_info *chip = iio_priv(dev_info);
1568 return adt7316_store_DAC(chip, 3, buf, len);
1571 static IIO_DEVICE_ATTR(DAC_D, S_IRUGO | S_IWUSR, adt7316_show_DAC_D,
1572 adt7316_store_DAC_D, 0);
1574 static ssize_t adt7316_show_device_id(struct device *dev,
1575 struct device_attribute *attr,
1578 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1579 struct adt7316_chip_info *chip = iio_priv(dev_info);
1583 ret = chip->bus.read(chip->bus.client, ADT7316_DEVICE_ID, &id);
1587 return sprintf(buf, "%d\n", id);
1590 static IIO_DEVICE_ATTR(device_id, S_IRUGO, adt7316_show_device_id, NULL, 0);
1592 static ssize_t adt7316_show_manufactorer_id(struct device *dev,
1593 struct device_attribute *attr,
1596 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1597 struct adt7316_chip_info *chip = iio_priv(dev_info);
1601 ret = chip->bus.read(chip->bus.client, ADT7316_MANUFACTURE_ID, &id);
1605 return sprintf(buf, "%d\n", id);
1608 static IIO_DEVICE_ATTR(manufactorer_id, S_IRUGO,
1609 adt7316_show_manufactorer_id, NULL, 0);
1611 static ssize_t adt7316_show_device_rev(struct device *dev,
1612 struct device_attribute *attr,
1615 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1616 struct adt7316_chip_info *chip = iio_priv(dev_info);
1620 ret = chip->bus.read(chip->bus.client, ADT7316_DEVICE_REV, &rev);
1624 return sprintf(buf, "%d\n", rev);
1627 static IIO_DEVICE_ATTR(device_rev, S_IRUGO, adt7316_show_device_rev, NULL, 0);
1629 static ssize_t adt7316_show_bus_type(struct device *dev,
1630 struct device_attribute *attr,
1633 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1634 struct adt7316_chip_info *chip = iio_priv(dev_info);
1638 ret = chip->bus.read(chip->bus.client, ADT7316_SPI_LOCK_STAT, &stat);
1643 return sprintf(buf, "spi\n");
1645 return sprintf(buf, "i2c\n");
1648 static IIO_DEVICE_ATTR(bus_type, S_IRUGO, adt7316_show_bus_type, NULL, 0);
1650 static struct attribute *adt7316_attributes[] = {
1651 &iio_dev_attr_all_modes.dev_attr.attr,
1652 &iio_dev_attr_mode.dev_attr.attr,
1653 &iio_dev_attr_enabled.dev_attr.attr,
1654 &iio_dev_attr_ad_channel.dev_attr.attr,
1655 &iio_dev_attr_all_ad_channels.dev_attr.attr,
1656 &iio_dev_attr_disable_averaging.dev_attr.attr,
1657 &iio_dev_attr_enable_smbus_timeout.dev_attr.attr,
1658 &iio_dev_attr_powerdown.dev_attr.attr,
1659 &iio_dev_attr_fast_ad_clock.dev_attr.attr,
1660 &iio_dev_attr_da_high_resolution.dev_attr.attr,
1661 &iio_dev_attr_enable_proportion_DACA.dev_attr.attr,
1662 &iio_dev_attr_enable_proportion_DACB.dev_attr.attr,
1663 &iio_dev_attr_DAC_2Vref_channels_mask.dev_attr.attr,
1664 &iio_dev_attr_DAC_update_mode.dev_attr.attr,
1665 &iio_dev_attr_all_DAC_update_modes.dev_attr.attr,
1666 &iio_dev_attr_update_DAC.dev_attr.attr,
1667 &iio_dev_attr_DA_AB_Vref_bypass.dev_attr.attr,
1668 &iio_dev_attr_DA_CD_Vref_bypass.dev_attr.attr,
1669 &iio_dev_attr_DAC_internal_Vref.dev_attr.attr,
1670 &iio_dev_attr_VDD.dev_attr.attr,
1671 &iio_dev_attr_in_temp.dev_attr.attr,
1672 &iio_dev_attr_ex_temp.dev_attr.attr,
1673 &iio_dev_attr_in_temp_offset.dev_attr.attr,
1674 &iio_dev_attr_ex_temp_offset.dev_attr.attr,
1675 &iio_dev_attr_in_analog_temp_offset.dev_attr.attr,
1676 &iio_dev_attr_ex_analog_temp_offset.dev_attr.attr,
1677 &iio_dev_attr_DAC_A.dev_attr.attr,
1678 &iio_dev_attr_DAC_B.dev_attr.attr,
1679 &iio_dev_attr_DAC_C.dev_attr.attr,
1680 &iio_dev_attr_DAC_D.dev_attr.attr,
1681 &iio_dev_attr_device_id.dev_attr.attr,
1682 &iio_dev_attr_manufactorer_id.dev_attr.attr,
1683 &iio_dev_attr_device_rev.dev_attr.attr,
1684 &iio_dev_attr_bus_type.dev_attr.attr,
1688 static const struct attribute_group adt7316_attribute_group = {
1689 .attrs = adt7316_attributes,
1692 static struct attribute *adt7516_attributes[] = {
1693 &iio_dev_attr_all_modes.dev_attr.attr,
1694 &iio_dev_attr_mode.dev_attr.attr,
1695 &iio_dev_attr_select_ex_temp.dev_attr.attr,
1696 &iio_dev_attr_enabled.dev_attr.attr,
1697 &iio_dev_attr_ad_channel.dev_attr.attr,
1698 &iio_dev_attr_all_ad_channels.dev_attr.attr,
1699 &iio_dev_attr_disable_averaging.dev_attr.attr,
1700 &iio_dev_attr_enable_smbus_timeout.dev_attr.attr,
1701 &iio_dev_attr_powerdown.dev_attr.attr,
1702 &iio_dev_attr_fast_ad_clock.dev_attr.attr,
1703 &iio_dev_attr_AIN_internal_Vref.dev_attr.attr,
1704 &iio_dev_attr_da_high_resolution.dev_attr.attr,
1705 &iio_dev_attr_enable_proportion_DACA.dev_attr.attr,
1706 &iio_dev_attr_enable_proportion_DACB.dev_attr.attr,
1707 &iio_dev_attr_DAC_2Vref_channels_mask.dev_attr.attr,
1708 &iio_dev_attr_DAC_update_mode.dev_attr.attr,
1709 &iio_dev_attr_all_DAC_update_modes.dev_attr.attr,
1710 &iio_dev_attr_update_DAC.dev_attr.attr,
1711 &iio_dev_attr_DA_AB_Vref_bypass.dev_attr.attr,
1712 &iio_dev_attr_DA_CD_Vref_bypass.dev_attr.attr,
1713 &iio_dev_attr_DAC_internal_Vref.dev_attr.attr,
1714 &iio_dev_attr_VDD.dev_attr.attr,
1715 &iio_dev_attr_in_temp.dev_attr.attr,
1716 &iio_dev_attr_ex_temp_AIN1.dev_attr.attr,
1717 &iio_dev_attr_AIN2.dev_attr.attr,
1718 &iio_dev_attr_AIN3.dev_attr.attr,
1719 &iio_dev_attr_AIN4.dev_attr.attr,
1720 &iio_dev_attr_in_temp_offset.dev_attr.attr,
1721 &iio_dev_attr_ex_temp_offset.dev_attr.attr,
1722 &iio_dev_attr_in_analog_temp_offset.dev_attr.attr,
1723 &iio_dev_attr_ex_analog_temp_offset.dev_attr.attr,
1724 &iio_dev_attr_DAC_A.dev_attr.attr,
1725 &iio_dev_attr_DAC_B.dev_attr.attr,
1726 &iio_dev_attr_DAC_C.dev_attr.attr,
1727 &iio_dev_attr_DAC_D.dev_attr.attr,
1728 &iio_dev_attr_device_id.dev_attr.attr,
1729 &iio_dev_attr_manufactorer_id.dev_attr.attr,
1730 &iio_dev_attr_device_rev.dev_attr.attr,
1731 &iio_dev_attr_bus_type.dev_attr.attr,
1735 static const struct attribute_group adt7516_attribute_group = {
1736 .attrs = adt7516_attributes,
1739 static irqreturn_t adt7316_event_handler(int irq, void *private)
1741 struct iio_dev *indio_dev = private;
1742 struct adt7316_chip_info *chip = iio_priv(indio_dev);
1747 ret = chip->bus.read(chip->bus.client, ADT7316_INT_STAT1, &stat1);
1749 if ((chip->id & ID_FAMILY_MASK) != ID_ADT75XX)
1752 time = iio_get_time_ns();
1753 if (stat1 & (1 << 0))
1754 iio_push_event(indio_dev,
1755 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
1759 if (stat1 & (1 << 1))
1760 iio_push_event(indio_dev,
1761 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
1763 IIO_EV_DIR_FALLING),
1765 if (stat1 & (1 << 2))
1766 iio_push_event(indio_dev,
1767 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 1,
1771 if (stat1 & (1 << 3))
1772 iio_push_event(indio_dev,
1773 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 1,
1775 IIO_EV_DIR_FALLING),
1777 if (stat1 & (1 << 5))
1778 iio_push_event(indio_dev,
1779 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 1,
1783 if (stat1 & (1 << 6))
1784 iio_push_event(indio_dev,
1785 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 2,
1789 if (stat1 & (1 << 7))
1790 iio_push_event(indio_dev,
1791 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 3,
1796 ret = chip->bus.read(chip->bus.client, ADT7316_INT_STAT2, &stat2);
1798 if (stat2 & ADT7316_INT_MASK2_VDD)
1799 iio_push_event(indio_dev,
1800 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
1811 * Show mask of enabled interrupts in Hex.
1813 static ssize_t adt7316_show_int_mask(struct device *dev,
1814 struct device_attribute *attr,
1817 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1818 struct adt7316_chip_info *chip = iio_priv(dev_info);
1820 return sprintf(buf, "0x%x\n", chip->int_mask);
1824 * Set 1 to the mask in Hex to enabled interrupts.
1826 static ssize_t adt7316_set_int_mask(struct device *dev,
1827 struct device_attribute *attr,
1831 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1832 struct adt7316_chip_info *chip = iio_priv(dev_info);
1837 ret = kstrtou16(buf, 16, &data);
1838 if (ret || data >= ADT7316_VDD_INT_MASK + 1)
1841 if (data & ADT7316_VDD_INT_MASK)
1842 mask = 0; /* enable vdd int */
1844 mask = ADT7316_INT_MASK2_VDD; /* disable vdd int */
1846 ret = chip->bus.write(chip->bus.client, ADT7316_INT_MASK2, mask);
1848 chip->int_mask &= ~ADT7316_VDD_INT_MASK;
1849 chip->int_mask |= data & ADT7316_VDD_INT_MASK;
1852 if (data & ADT7316_TEMP_AIN_INT_MASK) {
1853 if ((chip->id & ID_FAMILY_MASK) == ID_ADT73XX)
1854 /* mask in reg is opposite, set 1 to disable */
1855 mask = (~data) & ADT7316_TEMP_INT_MASK;
1857 /* mask in reg is opposite, set 1 to disable */
1858 mask = (~data) & ADT7316_TEMP_AIN_INT_MASK;
1860 ret = chip->bus.write(chip->bus.client, ADT7316_INT_MASK1, mask);
1862 chip->int_mask = mask;
1866 static inline ssize_t adt7316_show_ad_bound(struct device *dev,
1867 struct device_attribute *attr,
1870 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
1871 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1872 struct adt7316_chip_info *chip = iio_priv(dev_info);
1877 if ((chip->id & ID_FAMILY_MASK) == ID_ADT73XX &&
1878 this_attr->address > ADT7316_EX_TEMP_LOW)
1881 ret = chip->bus.read(chip->bus.client, this_attr->address, &val);
1887 if (!((chip->id & ID_FAMILY_MASK) == ID_ADT75XX &&
1888 (chip->config1 & ADT7516_SEL_AIN1_2_EX_TEMP_MASK) == 0)) {
1893 return sprintf(buf, "%d\n", data);
1896 static inline ssize_t adt7316_set_ad_bound(struct device *dev,
1897 struct device_attribute *attr,
1901 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
1902 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1903 struct adt7316_chip_info *chip = iio_priv(dev_info);
1908 if ((chip->id & ID_FAMILY_MASK) == ID_ADT73XX &&
1909 this_attr->address > ADT7316_EX_TEMP_LOW)
1912 ret = kstrtoint(buf, 10, &data);
1916 if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX &&
1917 (chip->config1 & ADT7516_SEL_AIN1_2_EX_TEMP_MASK) == 0) {
1918 if (data > 255 || data < 0)
1921 if (data > 127 || data < -128)
1930 ret = chip->bus.write(chip->bus.client, this_attr->address, val);
1937 static ssize_t adt7316_show_int_enabled(struct device *dev,
1938 struct device_attribute *attr,
1941 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1942 struct adt7316_chip_info *chip = iio_priv(dev_info);
1944 return sprintf(buf, "%d\n", !!(chip->config1 & ADT7316_INT_EN));
1947 static ssize_t adt7316_set_int_enabled(struct device *dev,
1948 struct device_attribute *attr,
1952 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1953 struct adt7316_chip_info *chip = iio_priv(dev_info);
1957 config1 = chip->config1 & (~ADT7316_INT_EN);
1959 config1 |= ADT7316_INT_EN;
1961 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG1, config1);
1965 chip->config1 = config1;
1970 static IIO_DEVICE_ATTR(int_mask,
1972 adt7316_show_int_mask, adt7316_set_int_mask,
1974 static IIO_DEVICE_ATTR(in_temp_high_value,
1976 adt7316_show_ad_bound, adt7316_set_ad_bound,
1977 ADT7316_IN_TEMP_HIGH);
1978 static IIO_DEVICE_ATTR(in_temp_low_value,
1980 adt7316_show_ad_bound, adt7316_set_ad_bound,
1981 ADT7316_IN_TEMP_LOW);
1982 static IIO_DEVICE_ATTR(ex_temp_high_value,
1984 adt7316_show_ad_bound, adt7316_set_ad_bound,
1985 ADT7316_EX_TEMP_HIGH);
1986 static IIO_DEVICE_ATTR(ex_temp_low_value,
1988 adt7316_show_ad_bound, adt7316_set_ad_bound,
1989 ADT7316_EX_TEMP_LOW);
1991 /* NASTY duplication to be fixed */
1992 static IIO_DEVICE_ATTR(ex_temp_ain1_high_value,
1994 adt7316_show_ad_bound, adt7316_set_ad_bound,
1995 ADT7316_EX_TEMP_HIGH);
1996 static IIO_DEVICE_ATTR(ex_temp_ain1_low_value,
1998 adt7316_show_ad_bound, adt7316_set_ad_bound,
1999 ADT7316_EX_TEMP_LOW);
2000 static IIO_DEVICE_ATTR(ain2_high_value,
2002 adt7316_show_ad_bound, adt7316_set_ad_bound,
2004 static IIO_DEVICE_ATTR(ain2_low_value,
2006 adt7316_show_ad_bound, adt7316_set_ad_bound,
2008 static IIO_DEVICE_ATTR(ain3_high_value,
2010 adt7316_show_ad_bound, adt7316_set_ad_bound,
2012 static IIO_DEVICE_ATTR(ain3_low_value,
2014 adt7316_show_ad_bound, adt7316_set_ad_bound,
2016 static IIO_DEVICE_ATTR(ain4_high_value,
2018 adt7316_show_ad_bound, adt7316_set_ad_bound,
2020 static IIO_DEVICE_ATTR(ain4_low_value,
2022 adt7316_show_ad_bound, adt7316_set_ad_bound,
2024 static IIO_DEVICE_ATTR(int_enabled,
2026 adt7316_show_int_enabled,
2027 adt7316_set_int_enabled, 0);
2029 static struct attribute *adt7316_event_attributes[] = {
2030 &iio_dev_attr_int_mask.dev_attr.attr,
2031 &iio_dev_attr_in_temp_high_value.dev_attr.attr,
2032 &iio_dev_attr_in_temp_low_value.dev_attr.attr,
2033 &iio_dev_attr_ex_temp_high_value.dev_attr.attr,
2034 &iio_dev_attr_ex_temp_low_value.dev_attr.attr,
2035 &iio_dev_attr_int_enabled.dev_attr.attr,
2039 static struct attribute_group adt7316_event_attribute_group = {
2040 .attrs = adt7316_event_attributes,
2044 static struct attribute *adt7516_event_attributes[] = {
2045 &iio_dev_attr_int_mask.dev_attr.attr,
2046 &iio_dev_attr_in_temp_high_value.dev_attr.attr,
2047 &iio_dev_attr_in_temp_low_value.dev_attr.attr,
2048 &iio_dev_attr_ex_temp_ain1_high_value.dev_attr.attr,
2049 &iio_dev_attr_ex_temp_ain1_low_value.dev_attr.attr,
2050 &iio_dev_attr_ain2_high_value.dev_attr.attr,
2051 &iio_dev_attr_ain2_low_value.dev_attr.attr,
2052 &iio_dev_attr_ain3_high_value.dev_attr.attr,
2053 &iio_dev_attr_ain3_low_value.dev_attr.attr,
2054 &iio_dev_attr_ain4_high_value.dev_attr.attr,
2055 &iio_dev_attr_ain4_low_value.dev_attr.attr,
2056 &iio_dev_attr_int_enabled.dev_attr.attr,
2060 static struct attribute_group adt7516_event_attribute_group = {
2061 .attrs = adt7516_event_attributes,
2065 #ifdef CONFIG_PM_SLEEP
2066 static int adt7316_disable(struct device *dev)
2068 struct iio_dev *dev_info = dev_get_drvdata(dev);
2069 struct adt7316_chip_info *chip = iio_priv(dev_info);
2071 return _adt7316_store_enabled(chip, 0);
2074 static int adt7316_enable(struct device *dev)
2076 struct iio_dev *dev_info = dev_get_drvdata(dev);
2077 struct adt7316_chip_info *chip = iio_priv(dev_info);
2079 return _adt7316_store_enabled(chip, 1);
2082 SIMPLE_DEV_PM_OPS(adt7316_pm_ops, adt7316_disable, adt7316_enable);
2083 EXPORT_SYMBOL_GPL(adt7316_pm_ops);
2086 static const struct iio_info adt7316_info = {
2087 .attrs = &adt7316_attribute_group,
2088 .event_attrs = &adt7316_event_attribute_group,
2089 .driver_module = THIS_MODULE,
2092 static const struct iio_info adt7516_info = {
2093 .attrs = &adt7516_attribute_group,
2094 .event_attrs = &adt7516_event_attribute_group,
2095 .driver_module = THIS_MODULE,
2099 * device probe and remove
2101 int adt7316_probe(struct device *dev, struct adt7316_bus *bus,
2104 struct adt7316_chip_info *chip;
2105 struct iio_dev *indio_dev;
2106 unsigned short *adt7316_platform_data = dev->platform_data;
2109 indio_dev = devm_iio_device_alloc(dev, sizeof(*chip));
2112 chip = iio_priv(indio_dev);
2113 /* this is only used for device removal purposes */
2114 dev_set_drvdata(dev, indio_dev);
2119 chip->id = ID_ADT7316 + (name[6] - '6');
2120 else if (name[4] == '5')
2121 chip->id = ID_ADT7516 + (name[6] - '6');
2125 chip->ldac_pin = adt7316_platform_data[1];
2126 if (chip->ldac_pin) {
2127 chip->config3 |= ADT7316_DA_EN_VIA_DAC_LDCA;
2128 if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
2129 chip->config1 |= ADT7516_SEL_AIN3;
2131 chip->int_mask = ADT7316_TEMP_INT_MASK | ADT7316_VDD_INT_MASK;
2132 if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
2133 chip->int_mask |= ADT7516_AIN_INT_MASK;
2135 indio_dev->dev.parent = dev;
2136 if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
2137 indio_dev->info = &adt7516_info;
2139 indio_dev->info = &adt7316_info;
2140 indio_dev->name = name;
2141 indio_dev->modes = INDIO_DIRECT_MODE;
2143 if (chip->bus.irq > 0) {
2144 if (adt7316_platform_data[0])
2145 chip->bus.irq_flags = adt7316_platform_data[0];
2147 ret = devm_request_threaded_irq(dev, chip->bus.irq,
2149 &adt7316_event_handler,
2150 chip->bus.irq_flags |
2157 if (chip->bus.irq_flags & IRQF_TRIGGER_HIGH)
2158 chip->config1 |= ADT7316_INT_POLARITY;
2161 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG1, chip->config1);
2165 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, chip->config3);
2169 ret = iio_device_register(indio_dev);
2173 dev_info(dev, "%s temperature sensor, ADC and DAC registered.\n",
2178 EXPORT_SYMBOL(adt7316_probe);
2180 int adt7316_remove(struct device *dev)
2182 struct iio_dev *indio_dev = dev_get_drvdata(dev);
2184 iio_device_unregister(indio_dev);
2188 EXPORT_SYMBOL(adt7316_remove);
2190 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
2191 MODULE_DESCRIPTION("Analog Devices ADT7316/7/8 and ADT7516/7/9 digital"
2192 " temperature sensor, ADC and DAC driver");
2193 MODULE_LICENSE("GPL v2");