1 // SPDX-License-Identifier: GPL-2.0+
3 * ADT7316 digital temperature sensor driver supporting ADT7316/7/8 ADT7516/7/9
5 * Copyright 2010 Analog Devices Inc.
8 #include <linux/interrupt.h>
9 #include <linux/gpio.h>
10 #include <linux/workqueue.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/sysfs.h>
15 #include <linux/list.h>
16 #include <linux/i2c.h>
17 #include <linux/rtc.h>
18 #include <linux/module.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/events.h>
22 #include <linux/iio/sysfs.h>
26 * ADT7316 registers definition
28 #define ADT7316_INT_STAT1 0x0
29 #define ADT7316_INT_STAT2 0x1
30 #define ADT7316_LSB_IN_TEMP_VDD 0x3
31 #define ADT7316_LSB_IN_TEMP_MASK 0x3
32 #define ADT7316_LSB_VDD_MASK 0xC
33 #define ADT7316_LSB_VDD_OFFSET 2
34 #define ADT7316_LSB_EX_TEMP_AIN 0x4
35 #define ADT7316_LSB_EX_TEMP_MASK 0x3
36 #define ADT7516_LSB_AIN_SHIFT 2
37 #define ADT7316_AD_MSB_DATA_BASE 0x6
38 #define ADT7316_AD_MSB_DATA_REGS 3
39 #define ADT7516_AD_MSB_DATA_REGS 6
40 #define ADT7316_MSB_VDD 0x6
41 #define ADT7316_MSB_IN_TEMP 0x7
42 #define ADT7316_MSB_EX_TEMP 0x8
43 #define ADT7516_MSB_AIN1 0x8
44 #define ADT7516_MSB_AIN2 0x9
45 #define ADT7516_MSB_AIN3 0xA
46 #define ADT7516_MSB_AIN4 0xB
47 #define ADT7316_DA_DATA_BASE 0x10
48 #define ADT7316_DA_10_BIT_LSB_SHIFT 6
49 #define ADT7316_DA_12_BIT_LSB_SHIFT 4
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_DAC_CONFIG 0x1B
63 #define ADT7316_LDAC_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_LDAC 0x8
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_SHIFT 4
131 #define ADT7316_DA_EN_MODE_SINGLE 0x00
132 #define ADT7316_DA_EN_MODE_AB_CD 0x10
133 #define ADT7316_DA_EN_MODE_ABCD 0x20
134 #define ADT7316_DA_EN_MODE_LDAC 0x30
135 #define ADT7316_VREF_BYPASS_DAC_AB 0x40
136 #define ADT7316_VREF_BYPASS_DAC_CD 0x80
139 * ADT7316 LDAC config
141 #define ADT7316_LDAC_EN_DA_MASK 0xF
142 #define ADT7316_DAC_IN_VREF 0x10
143 #define ADT7516_DAC_AB_IN_VREF 0x10
144 #define ADT7516_DAC_CD_IN_VREF 0x20
145 #define ADT7516_DAC_IN_VREF_OFFSET 4
146 #define ADT7516_DAC_IN_VREF_MASK 0x30
151 #define ADT7316_INT_MASK2_VDD 0x10
154 * ADT7316 value masks
156 #define ADT7316_VALUE_MASK 0xfff
157 #define ADT7316_T_VALUE_SIGN 0x400
158 #define ADT7316_T_VALUE_FLOAT_OFFSET 2
159 #define ADT7316_T_VALUE_FLOAT_MASK 0x2
164 #define ID_ADT7316 0x1
165 #define ID_ADT7317 0x2
166 #define ID_ADT7318 0x3
167 #define ID_ADT7516 0x11
168 #define ID_ADT7517 0x12
169 #define ID_ADT7519 0x14
171 #define ID_FAMILY_MASK 0xF0
172 #define ID_ADT73XX 0x0
173 #define ID_ADT75XX 0x10
176 * struct adt7316_chip_info - chip specific information
179 struct adt7316_chip_info {
180 struct adt7316_bus bus;
181 struct gpio_desc *ldac_pin;
182 u16 int_mask; /* 0x2f */
186 u8 dac_config; /* DAC config */
187 u8 ldac_config; /* LDAC config */
188 u8 dac_bits; /* 8, 10, 12 */
193 * Logic interrupt mask for user application to enable
196 #define ADT7316_IN_TEMP_HIGH_INT_MASK 0x1
197 #define ADT7316_IN_TEMP_LOW_INT_MASK 0x2
198 #define ADT7316_EX_TEMP_HIGH_INT_MASK 0x4
199 #define ADT7316_EX_TEMP_LOW_INT_MASK 0x8
200 #define ADT7316_EX_TEMP_FAULT_INT_MASK 0x10
201 #define ADT7516_AIN1_INT_MASK 0x4
202 #define ADT7516_AIN2_INT_MASK 0x20
203 #define ADT7516_AIN3_INT_MASK 0x40
204 #define ADT7516_AIN4_INT_MASK 0x80
205 #define ADT7316_VDD_INT_MASK 0x100
206 #define ADT7316_TEMP_INT_MASK 0x1F
207 #define ADT7516_AIN_INT_MASK 0xE0
208 #define ADT7316_TEMP_AIN_INT_MASK \
209 (ADT7316_TEMP_INT_MASK)
212 * struct adt7316_chip_info - chip specific information
215 struct adt7316_limit_regs {
220 static ssize_t adt7316_show_enabled(struct device *dev,
221 struct device_attribute *attr,
224 struct iio_dev *dev_info = dev_to_iio_dev(dev);
225 struct adt7316_chip_info *chip = iio_priv(dev_info);
227 return sprintf(buf, "%d\n", !!(chip->config1 & ADT7316_EN));
230 static ssize_t _adt7316_store_enabled(struct adt7316_chip_info *chip,
237 config1 = chip->config1 | ADT7316_EN;
239 config1 = chip->config1 & ~ADT7316_EN;
241 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG1, config1);
245 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, 0644,
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, 0644,
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, 0644,
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, 0444, 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);
439 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG2, config2);
443 chip->config2 = config2;
448 static IIO_DEVICE_ATTR(ad_channel, 0644,
449 adt7316_show_ad_channel,
450 adt7316_store_ad_channel,
453 static ssize_t adt7316_show_all_ad_channels(struct device *dev,
454 struct device_attribute *attr,
457 struct iio_dev *dev_info = dev_to_iio_dev(dev);
458 struct adt7316_chip_info *chip = iio_priv(dev_info);
460 if (!(chip->config2 & ADT7316_AD_SINGLE_CH_MODE))
463 if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
464 return sprintf(buf, "0 - VDD\n1 - Internal Temperature\n"
465 "2 - External Temperature or AIN1\n"
466 "3 - AIN2\n4 - AIN3\n5 - AIN4\n");
467 return sprintf(buf, "0 - VDD\n1 - Internal Temperature\n"
468 "2 - External Temperature\n");
471 static IIO_DEVICE_ATTR(all_ad_channels, 0444,
472 adt7316_show_all_ad_channels, NULL, 0);
474 static ssize_t adt7316_show_disable_averaging(struct device *dev,
475 struct device_attribute *attr,
478 struct iio_dev *dev_info = dev_to_iio_dev(dev);
479 struct adt7316_chip_info *chip = iio_priv(dev_info);
481 return sprintf(buf, "%d\n",
482 !!(chip->config2 & ADT7316_DISABLE_AVERAGING));
485 static ssize_t adt7316_store_disable_averaging(struct device *dev,
486 struct device_attribute *attr,
490 struct iio_dev *dev_info = dev_to_iio_dev(dev);
491 struct adt7316_chip_info *chip = iio_priv(dev_info);
495 config2 = chip->config2 & (~ADT7316_DISABLE_AVERAGING);
497 config2 |= ADT7316_DISABLE_AVERAGING;
499 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG2, config2);
503 chip->config2 = config2;
508 static IIO_DEVICE_ATTR(disable_averaging, 0644,
509 adt7316_show_disable_averaging,
510 adt7316_store_disable_averaging,
513 static ssize_t adt7316_show_enable_smbus_timeout(struct device *dev,
514 struct device_attribute *attr,
517 struct iio_dev *dev_info = dev_to_iio_dev(dev);
518 struct adt7316_chip_info *chip = iio_priv(dev_info);
520 return sprintf(buf, "%d\n",
521 !!(chip->config2 & ADT7316_EN_SMBUS_TIMEOUT));
524 static ssize_t adt7316_store_enable_smbus_timeout(struct device *dev,
525 struct device_attribute *attr,
529 struct iio_dev *dev_info = dev_to_iio_dev(dev);
530 struct adt7316_chip_info *chip = iio_priv(dev_info);
534 config2 = chip->config2 & (~ADT7316_EN_SMBUS_TIMEOUT);
536 config2 |= ADT7316_EN_SMBUS_TIMEOUT;
538 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG2, config2);
542 chip->config2 = config2;
547 static IIO_DEVICE_ATTR(enable_smbus_timeout, 0644,
548 adt7316_show_enable_smbus_timeout,
549 adt7316_store_enable_smbus_timeout,
552 static ssize_t adt7316_show_powerdown(struct device *dev,
553 struct device_attribute *attr,
556 struct iio_dev *dev_info = dev_to_iio_dev(dev);
557 struct adt7316_chip_info *chip = iio_priv(dev_info);
559 return sprintf(buf, "%d\n", !!(chip->config1 & ADT7316_PD));
562 static ssize_t adt7316_store_powerdown(struct device *dev,
563 struct device_attribute *attr,
567 struct iio_dev *dev_info = dev_to_iio_dev(dev);
568 struct adt7316_chip_info *chip = iio_priv(dev_info);
572 config1 = chip->config1 & (~ADT7316_PD);
574 config1 |= ADT7316_PD;
576 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG1, config1);
580 chip->config1 = config1;
585 static IIO_DEVICE_ATTR(powerdown, 0644,
586 adt7316_show_powerdown,
587 adt7316_store_powerdown,
590 static ssize_t adt7316_show_fast_ad_clock(struct device *dev,
591 struct device_attribute *attr,
594 struct iio_dev *dev_info = dev_to_iio_dev(dev);
595 struct adt7316_chip_info *chip = iio_priv(dev_info);
597 return sprintf(buf, "%d\n", !!(chip->config3 & ADT7316_ADCLK_22_5));
600 static ssize_t adt7316_store_fast_ad_clock(struct device *dev,
601 struct device_attribute *attr,
605 struct iio_dev *dev_info = dev_to_iio_dev(dev);
606 struct adt7316_chip_info *chip = iio_priv(dev_info);
610 config3 = chip->config3 & (~ADT7316_ADCLK_22_5);
612 config3 |= ADT7316_ADCLK_22_5;
614 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, config3);
618 chip->config3 = config3;
623 static IIO_DEVICE_ATTR(fast_ad_clock, 0644,
624 adt7316_show_fast_ad_clock,
625 adt7316_store_fast_ad_clock,
628 static ssize_t adt7316_show_da_high_resolution(struct device *dev,
629 struct device_attribute *attr,
632 struct iio_dev *dev_info = dev_to_iio_dev(dev);
633 struct adt7316_chip_info *chip = iio_priv(dev_info);
635 if (chip->config3 & ADT7316_DA_HIGH_RESOLUTION) {
636 if (chip->id != ID_ADT7318 && chip->id != ID_ADT7519)
637 return sprintf(buf, "1 (10 bits)\n");
640 return sprintf(buf, "0 (8 bits)\n");
643 static ssize_t adt7316_store_da_high_resolution(struct device *dev,
644 struct device_attribute *attr,
648 struct iio_dev *dev_info = dev_to_iio_dev(dev);
649 struct adt7316_chip_info *chip = iio_priv(dev_info);
653 if (chip->id == ID_ADT7318 || chip->id == ID_ADT7519)
656 config3 = chip->config3 & (~ADT7316_DA_HIGH_RESOLUTION);
658 config3 |= ADT7316_DA_HIGH_RESOLUTION;
660 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, config3);
664 chip->config3 = config3;
669 static IIO_DEVICE_ATTR(da_high_resolution, 0644,
670 adt7316_show_da_high_resolution,
671 adt7316_store_da_high_resolution,
674 static ssize_t adt7316_show_AIN_internal_Vref(struct device *dev,
675 struct device_attribute *attr,
678 struct iio_dev *dev_info = dev_to_iio_dev(dev);
679 struct adt7316_chip_info *chip = iio_priv(dev_info);
681 if ((chip->id & ID_FAMILY_MASK) != ID_ADT75XX)
684 return sprintf(buf, "%d\n",
685 !!(chip->config3 & ADT7516_AIN_IN_VREF));
688 static ssize_t adt7316_store_AIN_internal_Vref(struct device *dev,
689 struct device_attribute *attr,
693 struct iio_dev *dev_info = dev_to_iio_dev(dev);
694 struct adt7316_chip_info *chip = iio_priv(dev_info);
698 if ((chip->id & ID_FAMILY_MASK) != ID_ADT75XX)
702 config3 = chip->config3 & (~ADT7516_AIN_IN_VREF);
704 config3 = chip->config3 | ADT7516_AIN_IN_VREF;
706 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, config3);
710 chip->config3 = config3;
715 static IIO_DEVICE_ATTR(AIN_internal_Vref, 0644,
716 adt7316_show_AIN_internal_Vref,
717 adt7316_store_AIN_internal_Vref,
720 static ssize_t adt7316_show_enable_prop_DACA(struct device *dev,
721 struct device_attribute *attr,
724 struct iio_dev *dev_info = dev_to_iio_dev(dev);
725 struct adt7316_chip_info *chip = iio_priv(dev_info);
727 return sprintf(buf, "%d\n",
728 !!(chip->config3 & ADT7316_EN_IN_TEMP_PROP_DACA));
731 static ssize_t adt7316_store_enable_prop_DACA(struct device *dev,
732 struct device_attribute *attr,
736 struct iio_dev *dev_info = dev_to_iio_dev(dev);
737 struct adt7316_chip_info *chip = iio_priv(dev_info);
741 config3 = chip->config3 & (~ADT7316_EN_IN_TEMP_PROP_DACA);
743 config3 |= ADT7316_EN_IN_TEMP_PROP_DACA;
745 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, config3);
749 chip->config3 = config3;
754 static IIO_DEVICE_ATTR(enable_proportion_DACA, 0644,
755 adt7316_show_enable_prop_DACA,
756 adt7316_store_enable_prop_DACA,
759 static ssize_t adt7316_show_enable_prop_DACB(struct device *dev,
760 struct device_attribute *attr,
763 struct iio_dev *dev_info = dev_to_iio_dev(dev);
764 struct adt7316_chip_info *chip = iio_priv(dev_info);
766 return sprintf(buf, "%d\n",
767 !!(chip->config3 & ADT7316_EN_EX_TEMP_PROP_DACB));
770 static ssize_t adt7316_store_enable_prop_DACB(struct device *dev,
771 struct device_attribute *attr,
775 struct iio_dev *dev_info = dev_to_iio_dev(dev);
776 struct adt7316_chip_info *chip = iio_priv(dev_info);
780 config3 = chip->config3 & (~ADT7316_EN_EX_TEMP_PROP_DACB);
782 config3 |= ADT7316_EN_EX_TEMP_PROP_DACB;
784 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, config3);
788 chip->config3 = config3;
793 static IIO_DEVICE_ATTR(enable_proportion_DACB, 0644,
794 adt7316_show_enable_prop_DACB,
795 adt7316_store_enable_prop_DACB,
798 static ssize_t adt7316_show_DAC_2Vref_ch_mask(struct device *dev,
799 struct device_attribute *attr,
802 struct iio_dev *dev_info = dev_to_iio_dev(dev);
803 struct adt7316_chip_info *chip = iio_priv(dev_info);
805 return sprintf(buf, "0x%x\n",
806 chip->dac_config & ADT7316_DA_2VREF_CH_MASK);
809 static ssize_t adt7316_store_DAC_2Vref_ch_mask(struct device *dev,
810 struct device_attribute *attr,
814 struct iio_dev *dev_info = dev_to_iio_dev(dev);
815 struct adt7316_chip_info *chip = iio_priv(dev_info);
820 ret = kstrtou8(buf, 16, &data);
821 if (ret || data > ADT7316_DA_2VREF_CH_MASK)
824 dac_config = chip->dac_config & (~ADT7316_DA_2VREF_CH_MASK);
827 ret = chip->bus.write(chip->bus.client, ADT7316_DAC_CONFIG, dac_config);
831 chip->dac_config = dac_config;
836 static IIO_DEVICE_ATTR(DAC_2Vref_channels_mask, 0644,
837 adt7316_show_DAC_2Vref_ch_mask,
838 adt7316_store_DAC_2Vref_ch_mask,
841 static ssize_t adt7316_show_DAC_update_mode(struct device *dev,
842 struct device_attribute *attr,
845 struct iio_dev *dev_info = dev_to_iio_dev(dev);
846 struct adt7316_chip_info *chip = iio_priv(dev_info);
848 if (!(chip->config3 & ADT7316_DA_EN_VIA_DAC_LDAC))
849 return sprintf(buf, "manual\n");
851 switch (chip->dac_config & ADT7316_DA_EN_MODE_MASK) {
852 case ADT7316_DA_EN_MODE_SINGLE:
854 "0 - auto at any MSB DAC writing\n");
855 case ADT7316_DA_EN_MODE_AB_CD:
857 "1 - auto at MSB DAC AB and CD writing\n");
858 case ADT7316_DA_EN_MODE_ABCD:
860 "2 - auto at MSB DAC ABCD writing\n");
861 default: /* ADT7316_DA_EN_MODE_LDAC */
862 return sprintf(buf, "3 - manual\n");
866 static ssize_t adt7316_store_DAC_update_mode(struct device *dev,
867 struct device_attribute *attr,
871 struct iio_dev *dev_info = dev_to_iio_dev(dev);
872 struct adt7316_chip_info *chip = iio_priv(dev_info);
877 if (!(chip->config3 & ADT7316_DA_EN_VIA_DAC_LDAC))
880 ret = kstrtou8(buf, 10, &data);
881 if (ret || data > (ADT7316_DA_EN_MODE_MASK >> ADT7316_DA_EN_MODE_SHIFT))
884 dac_config = chip->dac_config & (~ADT7316_DA_EN_MODE_MASK);
885 dac_config |= data << ADT7316_DA_EN_MODE_SHIFT;
887 ret = chip->bus.write(chip->bus.client, ADT7316_DAC_CONFIG, dac_config);
891 chip->dac_config = dac_config;
896 static IIO_DEVICE_ATTR(DAC_update_mode, 0644,
897 adt7316_show_DAC_update_mode,
898 adt7316_store_DAC_update_mode,
901 static ssize_t adt7316_show_all_DAC_update_modes(struct device *dev,
902 struct device_attribute *attr,
905 struct iio_dev *dev_info = dev_to_iio_dev(dev);
906 struct adt7316_chip_info *chip = iio_priv(dev_info);
908 if (chip->config3 & ADT7316_DA_EN_VIA_DAC_LDAC)
909 return sprintf(buf, "0 - auto at any MSB DAC writing\n"
910 "1 - auto at MSB DAC AB and CD writing\n"
911 "2 - auto at MSB DAC ABCD writing\n"
913 return sprintf(buf, "manual\n");
916 static IIO_DEVICE_ATTR(all_DAC_update_modes, 0444,
917 adt7316_show_all_DAC_update_modes, NULL, 0);
919 static ssize_t adt7316_store_update_DAC(struct device *dev,
920 struct device_attribute *attr,
924 struct iio_dev *dev_info = dev_to_iio_dev(dev);
925 struct adt7316_chip_info *chip = iio_priv(dev_info);
930 if (chip->config3 & ADT7316_DA_EN_VIA_DAC_LDAC) {
931 if ((chip->dac_config & ADT7316_DA_EN_MODE_MASK) !=
932 ADT7316_DA_EN_MODE_LDAC)
935 ret = kstrtou8(buf, 16, &data);
936 if (ret || data > ADT7316_LDAC_EN_DA_MASK)
939 ldac_config = chip->ldac_config & (~ADT7316_LDAC_EN_DA_MASK);
942 ret = chip->bus.write(chip->bus.client, ADT7316_LDAC_CONFIG,
947 gpiod_set_value(chip->ldac_pin, 0);
948 gpiod_set_value(chip->ldac_pin, 1);
954 static IIO_DEVICE_ATTR(update_DAC, 0644,
956 adt7316_store_update_DAC,
959 static ssize_t adt7316_show_DA_AB_Vref_bypass(struct device *dev,
960 struct device_attribute *attr,
963 struct iio_dev *dev_info = dev_to_iio_dev(dev);
964 struct adt7316_chip_info *chip = iio_priv(dev_info);
966 return sprintf(buf, "%d\n",
967 !!(chip->dac_config & ADT7316_VREF_BYPASS_DAC_AB));
970 static ssize_t adt7316_store_DA_AB_Vref_bypass(struct device *dev,
971 struct device_attribute *attr,
975 struct iio_dev *dev_info = dev_to_iio_dev(dev);
976 struct adt7316_chip_info *chip = iio_priv(dev_info);
980 dac_config = chip->dac_config & (~ADT7316_VREF_BYPASS_DAC_AB);
982 dac_config |= ADT7316_VREF_BYPASS_DAC_AB;
984 ret = chip->bus.write(chip->bus.client, ADT7316_DAC_CONFIG, dac_config);
988 chip->dac_config = dac_config;
993 static IIO_DEVICE_ATTR(DA_AB_Vref_bypass, 0644,
994 adt7316_show_DA_AB_Vref_bypass,
995 adt7316_store_DA_AB_Vref_bypass,
998 static ssize_t adt7316_show_DA_CD_Vref_bypass(struct device *dev,
999 struct device_attribute *attr,
1002 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1003 struct adt7316_chip_info *chip = iio_priv(dev_info);
1005 return sprintf(buf, "%d\n",
1006 !!(chip->dac_config & ADT7316_VREF_BYPASS_DAC_CD));
1009 static ssize_t adt7316_store_DA_CD_Vref_bypass(struct device *dev,
1010 struct device_attribute *attr,
1014 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1015 struct adt7316_chip_info *chip = iio_priv(dev_info);
1019 dac_config = chip->dac_config & (~ADT7316_VREF_BYPASS_DAC_CD);
1021 dac_config |= ADT7316_VREF_BYPASS_DAC_CD;
1023 ret = chip->bus.write(chip->bus.client, ADT7316_DAC_CONFIG, dac_config);
1027 chip->dac_config = dac_config;
1032 static IIO_DEVICE_ATTR(DA_CD_Vref_bypass, 0644,
1033 adt7316_show_DA_CD_Vref_bypass,
1034 adt7316_store_DA_CD_Vref_bypass,
1037 static ssize_t adt7316_show_DAC_internal_Vref(struct device *dev,
1038 struct device_attribute *attr,
1041 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1042 struct adt7316_chip_info *chip = iio_priv(dev_info);
1044 if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
1045 return sprintf(buf, "0x%x\n",
1046 (chip->ldac_config & ADT7516_DAC_IN_VREF_MASK) >>
1047 ADT7516_DAC_IN_VREF_OFFSET);
1048 return sprintf(buf, "%d\n",
1049 !!(chip->ldac_config & ADT7316_DAC_IN_VREF));
1052 static ssize_t adt7316_store_DAC_internal_Vref(struct device *dev,
1053 struct device_attribute *attr,
1057 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1058 struct adt7316_chip_info *chip = iio_priv(dev_info);
1063 if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX) {
1064 ret = kstrtou8(buf, 16, &data);
1065 if (ret || data > 3)
1068 ldac_config = chip->ldac_config & (~ADT7516_DAC_IN_VREF_MASK);
1070 ldac_config |= ADT7516_DAC_AB_IN_VREF;
1072 ldac_config |= ADT7516_DAC_CD_IN_VREF;
1074 ret = kstrtou8(buf, 16, &data);
1078 ldac_config = chip->ldac_config & (~ADT7316_DAC_IN_VREF);
1080 ldac_config = chip->ldac_config | ADT7316_DAC_IN_VREF;
1083 ret = chip->bus.write(chip->bus.client, ADT7316_LDAC_CONFIG,
1088 chip->ldac_config = ldac_config;
1093 static IIO_DEVICE_ATTR(DAC_internal_Vref, 0644,
1094 adt7316_show_DAC_internal_Vref,
1095 adt7316_store_DAC_internal_Vref,
1098 static ssize_t adt7316_show_ad(struct adt7316_chip_info *chip,
1099 int channel, char *buf)
1106 if ((chip->config2 & ADT7316_AD_SINGLE_CH_MODE) &&
1107 channel != (chip->config2 & ADT7516_AD_SINGLE_CH_MASK))
1111 case ADT7316_AD_SINGLE_CH_IN:
1112 ret = chip->bus.read(chip->bus.client,
1113 ADT7316_LSB_IN_TEMP_VDD, &lsb);
1117 ret = chip->bus.read(chip->bus.client,
1118 ADT7316_AD_MSB_DATA_BASE + channel, &msb);
1122 data = msb << ADT7316_T_VALUE_FLOAT_OFFSET;
1123 data |= lsb & ADT7316_LSB_IN_TEMP_MASK;
1125 case ADT7316_AD_SINGLE_CH_VDD:
1126 ret = chip->bus.read(chip->bus.client,
1127 ADT7316_LSB_IN_TEMP_VDD, &lsb);
1131 ret = chip->bus.read(chip->bus.client,
1133 ADT7316_AD_MSB_DATA_BASE + channel, &msb);
1137 data = msb << ADT7316_T_VALUE_FLOAT_OFFSET;
1138 data |= (lsb & ADT7316_LSB_VDD_MASK) >> ADT7316_LSB_VDD_OFFSET;
1139 return sprintf(buf, "%d\n", data);
1140 default: /* ex_temp and ain */
1141 ret = chip->bus.read(chip->bus.client,
1142 ADT7316_LSB_EX_TEMP_AIN, &lsb);
1146 ret = chip->bus.read(chip->bus.client,
1147 ADT7316_AD_MSB_DATA_BASE + channel, &msb);
1151 data = msb << ADT7316_T_VALUE_FLOAT_OFFSET;
1152 data |= lsb & (ADT7316_LSB_EX_TEMP_MASK <<
1153 (ADT7516_LSB_AIN_SHIFT * (channel -
1154 (ADT7316_MSB_EX_TEMP - ADT7316_AD_MSB_DATA_BASE))));
1156 if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
1157 return sprintf(buf, "%d\n", data);
1162 if (data & ADT7316_T_VALUE_SIGN) {
1163 /* convert supplement to positive value */
1164 data = (ADT7316_T_VALUE_SIGN << 1) - data;
1168 return sprintf(buf, "%c%d.%.2d\n", sign,
1169 (data >> ADT7316_T_VALUE_FLOAT_OFFSET),
1170 (data & ADT7316_T_VALUE_FLOAT_MASK) * 25);
1173 static ssize_t adt7316_show_VDD(struct device *dev,
1174 struct device_attribute *attr,
1177 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1178 struct adt7316_chip_info *chip = iio_priv(dev_info);
1180 return adt7316_show_ad(chip, ADT7316_AD_SINGLE_CH_VDD, buf);
1182 static IIO_DEVICE_ATTR(VDD, 0444, adt7316_show_VDD, NULL, 0);
1184 static ssize_t adt7316_show_in_temp(struct device *dev,
1185 struct device_attribute *attr,
1188 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1189 struct adt7316_chip_info *chip = iio_priv(dev_info);
1191 return adt7316_show_ad(chip, ADT7316_AD_SINGLE_CH_IN, buf);
1194 static IIO_DEVICE_ATTR(in_temp, 0444, adt7316_show_in_temp, NULL, 0);
1196 static ssize_t adt7316_show_ex_temp_AIN1(struct device *dev,
1197 struct device_attribute *attr,
1200 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1201 struct adt7316_chip_info *chip = iio_priv(dev_info);
1203 return adt7316_show_ad(chip, ADT7316_AD_SINGLE_CH_EX, buf);
1206 static IIO_DEVICE_ATTR(ex_temp_AIN1, 0444, adt7316_show_ex_temp_AIN1,
1208 static IIO_DEVICE_ATTR(ex_temp, 0444, adt7316_show_ex_temp_AIN1, NULL, 0);
1210 static ssize_t adt7316_show_AIN2(struct device *dev,
1211 struct device_attribute *attr,
1214 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1215 struct adt7316_chip_info *chip = iio_priv(dev_info);
1217 return adt7316_show_ad(chip, ADT7516_AD_SINGLE_CH_AIN2, buf);
1219 static IIO_DEVICE_ATTR(AIN2, 0444, adt7316_show_AIN2, NULL, 0);
1221 static ssize_t adt7316_show_AIN3(struct device *dev,
1222 struct device_attribute *attr,
1225 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1226 struct adt7316_chip_info *chip = iio_priv(dev_info);
1228 return adt7316_show_ad(chip, ADT7516_AD_SINGLE_CH_AIN3, buf);
1230 static IIO_DEVICE_ATTR(AIN3, 0444, adt7316_show_AIN3, NULL, 0);
1232 static ssize_t adt7316_show_AIN4(struct device *dev,
1233 struct device_attribute *attr,
1236 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1237 struct adt7316_chip_info *chip = iio_priv(dev_info);
1239 return adt7316_show_ad(chip, ADT7516_AD_SINGLE_CH_AIN4, buf);
1241 static IIO_DEVICE_ATTR(AIN4, 0444, adt7316_show_AIN4, NULL, 0);
1243 static ssize_t adt7316_show_temp_offset(struct adt7316_chip_info *chip,
1244 int offset_addr, char *buf)
1250 ret = chip->bus.read(chip->bus.client, offset_addr, &val);
1258 return sprintf(buf, "%d\n", data);
1261 static ssize_t adt7316_store_temp_offset(struct adt7316_chip_info *chip,
1270 ret = kstrtoint(buf, 10, &data);
1271 if (ret || data > 127 || data < -128)
1279 ret = chip->bus.write(chip->bus.client, offset_addr, val);
1286 static ssize_t adt7316_show_in_temp_offset(struct device *dev,
1287 struct device_attribute *attr,
1290 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1291 struct adt7316_chip_info *chip = iio_priv(dev_info);
1293 return adt7316_show_temp_offset(chip, ADT7316_IN_TEMP_OFFSET, buf);
1296 static ssize_t adt7316_store_in_temp_offset(struct device *dev,
1297 struct device_attribute *attr,
1301 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1302 struct adt7316_chip_info *chip = iio_priv(dev_info);
1304 return adt7316_store_temp_offset(chip, ADT7316_IN_TEMP_OFFSET, buf,
1308 static IIO_DEVICE_ATTR(in_temp_offset, 0644,
1309 adt7316_show_in_temp_offset,
1310 adt7316_store_in_temp_offset, 0);
1312 static ssize_t adt7316_show_ex_temp_offset(struct device *dev,
1313 struct device_attribute *attr,
1316 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1317 struct adt7316_chip_info *chip = iio_priv(dev_info);
1319 return adt7316_show_temp_offset(chip, ADT7316_EX_TEMP_OFFSET, buf);
1322 static ssize_t adt7316_store_ex_temp_offset(struct device *dev,
1323 struct device_attribute *attr,
1327 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1328 struct adt7316_chip_info *chip = iio_priv(dev_info);
1330 return adt7316_store_temp_offset(chip, ADT7316_EX_TEMP_OFFSET, buf,
1334 static IIO_DEVICE_ATTR(ex_temp_offset, 0644,
1335 adt7316_show_ex_temp_offset,
1336 adt7316_store_ex_temp_offset, 0);
1338 static ssize_t adt7316_show_in_analog_temp_offset(struct device *dev,
1339 struct device_attribute *attr,
1342 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1343 struct adt7316_chip_info *chip = iio_priv(dev_info);
1345 return adt7316_show_temp_offset(chip,
1346 ADT7316_IN_ANALOG_TEMP_OFFSET, buf);
1349 static ssize_t adt7316_store_in_analog_temp_offset(struct device *dev,
1350 struct device_attribute *attr,
1354 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1355 struct adt7316_chip_info *chip = iio_priv(dev_info);
1357 return adt7316_store_temp_offset(chip,
1358 ADT7316_IN_ANALOG_TEMP_OFFSET, buf, len);
1361 static IIO_DEVICE_ATTR(in_analog_temp_offset, 0644,
1362 adt7316_show_in_analog_temp_offset,
1363 adt7316_store_in_analog_temp_offset, 0);
1365 static ssize_t adt7316_show_ex_analog_temp_offset(struct device *dev,
1366 struct device_attribute *attr,
1369 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1370 struct adt7316_chip_info *chip = iio_priv(dev_info);
1372 return adt7316_show_temp_offset(chip,
1373 ADT7316_EX_ANALOG_TEMP_OFFSET, buf);
1376 static ssize_t adt7316_store_ex_analog_temp_offset(struct device *dev,
1377 struct device_attribute *attr,
1381 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1382 struct adt7316_chip_info *chip = iio_priv(dev_info);
1384 return adt7316_store_temp_offset(chip,
1385 ADT7316_EX_ANALOG_TEMP_OFFSET, buf, len);
1388 static IIO_DEVICE_ATTR(ex_analog_temp_offset, 0644,
1389 adt7316_show_ex_analog_temp_offset,
1390 adt7316_store_ex_analog_temp_offset, 0);
1392 static ssize_t adt7316_show_DAC(struct adt7316_chip_info *chip,
1393 int channel, char *buf)
1396 u8 msb, lsb, offset;
1399 if (channel >= ADT7316_DA_MSB_DATA_REGS ||
1401 (chip->config3 & ADT7316_EN_IN_TEMP_PROP_DACA)) ||
1403 (chip->config3 & ADT7316_EN_EX_TEMP_PROP_DACB)))
1406 offset = chip->dac_bits - 8;
1408 if (chip->dac_bits > 8) {
1409 ret = chip->bus.read(chip->bus.client,
1410 ADT7316_DA_DATA_BASE + channel * 2, &lsb);
1415 ret = chip->bus.read(chip->bus.client,
1416 ADT7316_DA_DATA_BASE + 1 + channel * 2, &msb);
1420 if (chip->dac_bits == 12)
1421 data = lsb >> ADT7316_DA_12_BIT_LSB_SHIFT;
1422 else if (chip->dac_bits == 10)
1423 data = lsb >> ADT7316_DA_10_BIT_LSB_SHIFT;
1424 data |= msb << offset;
1426 return sprintf(buf, "%d\n", data);
1429 static ssize_t adt7316_store_DAC(struct adt7316_chip_info *chip,
1430 int channel, const char *buf, size_t len)
1432 u8 msb, lsb, lsb_reg, offset;
1436 if (channel >= ADT7316_DA_MSB_DATA_REGS ||
1438 (chip->config3 & ADT7316_EN_IN_TEMP_PROP_DACA)) ||
1440 (chip->config3 & ADT7316_EN_EX_TEMP_PROP_DACB)))
1443 offset = chip->dac_bits - 8;
1445 ret = kstrtou16(buf, 10, &data);
1446 if (ret || data >= (1 << chip->dac_bits))
1449 if (chip->dac_bits > 8) {
1450 lsb = data & ((1 << offset) - 1);
1451 if (chip->dac_bits == 12)
1452 lsb_reg = lsb << ADT7316_DA_12_BIT_LSB_SHIFT;
1454 lsb_reg = lsb << ADT7316_DA_10_BIT_LSB_SHIFT;
1455 ret = chip->bus.write(chip->bus.client,
1456 ADT7316_DA_DATA_BASE + channel * 2, lsb_reg);
1461 msb = data >> offset;
1462 ret = chip->bus.write(chip->bus.client,
1463 ADT7316_DA_DATA_BASE + 1 + channel * 2, msb);
1470 static ssize_t adt7316_show_DAC_A(struct device *dev,
1471 struct device_attribute *attr,
1474 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1475 struct adt7316_chip_info *chip = iio_priv(dev_info);
1477 return adt7316_show_DAC(chip, 0, buf);
1480 static ssize_t adt7316_store_DAC_A(struct device *dev,
1481 struct device_attribute *attr,
1485 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1486 struct adt7316_chip_info *chip = iio_priv(dev_info);
1488 return adt7316_store_DAC(chip, 0, buf, len);
1491 static IIO_DEVICE_ATTR(DAC_A, 0644, adt7316_show_DAC_A,
1492 adt7316_store_DAC_A, 0);
1494 static ssize_t adt7316_show_DAC_B(struct device *dev,
1495 struct device_attribute *attr,
1498 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1499 struct adt7316_chip_info *chip = iio_priv(dev_info);
1501 return adt7316_show_DAC(chip, 1, buf);
1504 static ssize_t adt7316_store_DAC_B(struct device *dev,
1505 struct device_attribute *attr,
1509 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1510 struct adt7316_chip_info *chip = iio_priv(dev_info);
1512 return adt7316_store_DAC(chip, 1, buf, len);
1515 static IIO_DEVICE_ATTR(DAC_B, 0644, adt7316_show_DAC_B,
1516 adt7316_store_DAC_B, 0);
1518 static ssize_t adt7316_show_DAC_C(struct device *dev,
1519 struct device_attribute *attr,
1522 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1523 struct adt7316_chip_info *chip = iio_priv(dev_info);
1525 return adt7316_show_DAC(chip, 2, buf);
1528 static ssize_t adt7316_store_DAC_C(struct device *dev,
1529 struct device_attribute *attr,
1533 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1534 struct adt7316_chip_info *chip = iio_priv(dev_info);
1536 return adt7316_store_DAC(chip, 2, buf, len);
1539 static IIO_DEVICE_ATTR(DAC_C, 0644, adt7316_show_DAC_C,
1540 adt7316_store_DAC_C, 0);
1542 static ssize_t adt7316_show_DAC_D(struct device *dev,
1543 struct device_attribute *attr,
1546 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1547 struct adt7316_chip_info *chip = iio_priv(dev_info);
1549 return adt7316_show_DAC(chip, 3, buf);
1552 static ssize_t adt7316_store_DAC_D(struct device *dev,
1553 struct device_attribute *attr,
1557 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1558 struct adt7316_chip_info *chip = iio_priv(dev_info);
1560 return adt7316_store_DAC(chip, 3, buf, len);
1563 static IIO_DEVICE_ATTR(DAC_D, 0644, adt7316_show_DAC_D,
1564 adt7316_store_DAC_D, 0);
1566 static ssize_t adt7316_show_device_id(struct device *dev,
1567 struct device_attribute *attr,
1570 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1571 struct adt7316_chip_info *chip = iio_priv(dev_info);
1575 ret = chip->bus.read(chip->bus.client, ADT7316_DEVICE_ID, &id);
1579 return sprintf(buf, "%d\n", id);
1582 static IIO_DEVICE_ATTR(device_id, 0444, adt7316_show_device_id, NULL, 0);
1584 static ssize_t adt7316_show_manufactorer_id(struct device *dev,
1585 struct device_attribute *attr,
1588 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1589 struct adt7316_chip_info *chip = iio_priv(dev_info);
1593 ret = chip->bus.read(chip->bus.client, ADT7316_MANUFACTURE_ID, &id);
1597 return sprintf(buf, "%d\n", id);
1600 static IIO_DEVICE_ATTR(manufactorer_id, 0444,
1601 adt7316_show_manufactorer_id, NULL, 0);
1603 static ssize_t adt7316_show_device_rev(struct device *dev,
1604 struct device_attribute *attr,
1607 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1608 struct adt7316_chip_info *chip = iio_priv(dev_info);
1612 ret = chip->bus.read(chip->bus.client, ADT7316_DEVICE_REV, &rev);
1616 return sprintf(buf, "%d\n", rev);
1619 static IIO_DEVICE_ATTR(device_rev, 0444, adt7316_show_device_rev, NULL, 0);
1621 static ssize_t adt7316_show_bus_type(struct device *dev,
1622 struct device_attribute *attr,
1625 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1626 struct adt7316_chip_info *chip = iio_priv(dev_info);
1630 ret = chip->bus.read(chip->bus.client, ADT7316_SPI_LOCK_STAT, &stat);
1635 return sprintf(buf, "spi\n");
1637 return sprintf(buf, "i2c\n");
1640 static IIO_DEVICE_ATTR(bus_type, 0444, adt7316_show_bus_type, NULL, 0);
1642 static struct attribute *adt7316_attributes[] = {
1643 &iio_dev_attr_all_modes.dev_attr.attr,
1644 &iio_dev_attr_mode.dev_attr.attr,
1645 &iio_dev_attr_enabled.dev_attr.attr,
1646 &iio_dev_attr_ad_channel.dev_attr.attr,
1647 &iio_dev_attr_all_ad_channels.dev_attr.attr,
1648 &iio_dev_attr_disable_averaging.dev_attr.attr,
1649 &iio_dev_attr_enable_smbus_timeout.dev_attr.attr,
1650 &iio_dev_attr_powerdown.dev_attr.attr,
1651 &iio_dev_attr_fast_ad_clock.dev_attr.attr,
1652 &iio_dev_attr_da_high_resolution.dev_attr.attr,
1653 &iio_dev_attr_enable_proportion_DACA.dev_attr.attr,
1654 &iio_dev_attr_enable_proportion_DACB.dev_attr.attr,
1655 &iio_dev_attr_DAC_2Vref_channels_mask.dev_attr.attr,
1656 &iio_dev_attr_DAC_update_mode.dev_attr.attr,
1657 &iio_dev_attr_all_DAC_update_modes.dev_attr.attr,
1658 &iio_dev_attr_update_DAC.dev_attr.attr,
1659 &iio_dev_attr_DA_AB_Vref_bypass.dev_attr.attr,
1660 &iio_dev_attr_DA_CD_Vref_bypass.dev_attr.attr,
1661 &iio_dev_attr_DAC_internal_Vref.dev_attr.attr,
1662 &iio_dev_attr_VDD.dev_attr.attr,
1663 &iio_dev_attr_in_temp.dev_attr.attr,
1664 &iio_dev_attr_ex_temp.dev_attr.attr,
1665 &iio_dev_attr_in_temp_offset.dev_attr.attr,
1666 &iio_dev_attr_ex_temp_offset.dev_attr.attr,
1667 &iio_dev_attr_in_analog_temp_offset.dev_attr.attr,
1668 &iio_dev_attr_ex_analog_temp_offset.dev_attr.attr,
1669 &iio_dev_attr_DAC_A.dev_attr.attr,
1670 &iio_dev_attr_DAC_B.dev_attr.attr,
1671 &iio_dev_attr_DAC_C.dev_attr.attr,
1672 &iio_dev_attr_DAC_D.dev_attr.attr,
1673 &iio_dev_attr_device_id.dev_attr.attr,
1674 &iio_dev_attr_manufactorer_id.dev_attr.attr,
1675 &iio_dev_attr_device_rev.dev_attr.attr,
1676 &iio_dev_attr_bus_type.dev_attr.attr,
1680 static const struct attribute_group adt7316_attribute_group = {
1681 .attrs = adt7316_attributes,
1684 static struct attribute *adt7516_attributes[] = {
1685 &iio_dev_attr_all_modes.dev_attr.attr,
1686 &iio_dev_attr_mode.dev_attr.attr,
1687 &iio_dev_attr_select_ex_temp.dev_attr.attr,
1688 &iio_dev_attr_enabled.dev_attr.attr,
1689 &iio_dev_attr_ad_channel.dev_attr.attr,
1690 &iio_dev_attr_all_ad_channels.dev_attr.attr,
1691 &iio_dev_attr_disable_averaging.dev_attr.attr,
1692 &iio_dev_attr_enable_smbus_timeout.dev_attr.attr,
1693 &iio_dev_attr_powerdown.dev_attr.attr,
1694 &iio_dev_attr_fast_ad_clock.dev_attr.attr,
1695 &iio_dev_attr_AIN_internal_Vref.dev_attr.attr,
1696 &iio_dev_attr_da_high_resolution.dev_attr.attr,
1697 &iio_dev_attr_enable_proportion_DACA.dev_attr.attr,
1698 &iio_dev_attr_enable_proportion_DACB.dev_attr.attr,
1699 &iio_dev_attr_DAC_2Vref_channels_mask.dev_attr.attr,
1700 &iio_dev_attr_DAC_update_mode.dev_attr.attr,
1701 &iio_dev_attr_all_DAC_update_modes.dev_attr.attr,
1702 &iio_dev_attr_update_DAC.dev_attr.attr,
1703 &iio_dev_attr_DAC_internal_Vref.dev_attr.attr,
1704 &iio_dev_attr_VDD.dev_attr.attr,
1705 &iio_dev_attr_in_temp.dev_attr.attr,
1706 &iio_dev_attr_ex_temp_AIN1.dev_attr.attr,
1707 &iio_dev_attr_AIN2.dev_attr.attr,
1708 &iio_dev_attr_AIN3.dev_attr.attr,
1709 &iio_dev_attr_AIN4.dev_attr.attr,
1710 &iio_dev_attr_in_temp_offset.dev_attr.attr,
1711 &iio_dev_attr_ex_temp_offset.dev_attr.attr,
1712 &iio_dev_attr_in_analog_temp_offset.dev_attr.attr,
1713 &iio_dev_attr_ex_analog_temp_offset.dev_attr.attr,
1714 &iio_dev_attr_DAC_A.dev_attr.attr,
1715 &iio_dev_attr_DAC_B.dev_attr.attr,
1716 &iio_dev_attr_DAC_C.dev_attr.attr,
1717 &iio_dev_attr_DAC_D.dev_attr.attr,
1718 &iio_dev_attr_device_id.dev_attr.attr,
1719 &iio_dev_attr_manufactorer_id.dev_attr.attr,
1720 &iio_dev_attr_device_rev.dev_attr.attr,
1721 &iio_dev_attr_bus_type.dev_attr.attr,
1725 static const struct attribute_group adt7516_attribute_group = {
1726 .attrs = adt7516_attributes,
1729 static irqreturn_t adt7316_event_handler(int irq, void *private)
1731 struct iio_dev *indio_dev = private;
1732 struct adt7316_chip_info *chip = iio_priv(indio_dev);
1737 ret = chip->bus.read(chip->bus.client, ADT7316_INT_STAT1, &stat1);
1739 if ((chip->id & ID_FAMILY_MASK) != ID_ADT75XX)
1742 time = iio_get_time_ns(indio_dev);
1744 iio_push_event(indio_dev,
1745 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
1750 iio_push_event(indio_dev,
1751 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
1753 IIO_EV_DIR_FALLING),
1756 iio_push_event(indio_dev,
1757 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 1,
1762 iio_push_event(indio_dev,
1763 IIO_UNMOD_EVENT_CODE(IIO_TEMP, 1,
1765 IIO_EV_DIR_FALLING),
1768 iio_push_event(indio_dev,
1769 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 1,
1774 iio_push_event(indio_dev,
1775 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 2,
1780 iio_push_event(indio_dev,
1781 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 3,
1786 ret = chip->bus.read(chip->bus.client, ADT7316_INT_STAT2, &stat2);
1788 if (stat2 & ADT7316_INT_MASK2_VDD)
1789 iio_push_event(indio_dev,
1790 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
1794 iio_get_time_ns(indio_dev));
1800 static int adt7316_setup_irq(struct iio_dev *indio_dev)
1802 struct adt7316_chip_info *chip = iio_priv(indio_dev);
1805 irq_type = irqd_get_trigger_type(irq_get_irq_data(chip->bus.irq));
1808 case IRQF_TRIGGER_HIGH:
1809 case IRQF_TRIGGER_RISING:
1811 case IRQF_TRIGGER_LOW:
1812 case IRQF_TRIGGER_FALLING:
1815 dev_info(&indio_dev->dev, "mode %d unsupported, using IRQF_TRIGGER_LOW\n",
1817 irq_type = IRQF_TRIGGER_LOW;
1821 ret = devm_request_threaded_irq(&indio_dev->dev, chip->bus.irq,
1822 NULL, adt7316_event_handler,
1823 irq_type | IRQF_ONESHOT,
1824 indio_dev->name, indio_dev);
1826 dev_err(&indio_dev->dev, "failed to request irq %d\n",
1831 if (irq_type & IRQF_TRIGGER_HIGH)
1832 chip->config1 |= ADT7316_INT_POLARITY;
1838 * Show mask of enabled interrupts in Hex.
1840 static ssize_t adt7316_show_int_mask(struct device *dev,
1841 struct device_attribute *attr,
1844 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1845 struct adt7316_chip_info *chip = iio_priv(dev_info);
1847 return sprintf(buf, "0x%x\n", chip->int_mask);
1851 * Set 1 to the mask in Hex to enabled interrupts.
1853 static ssize_t adt7316_set_int_mask(struct device *dev,
1854 struct device_attribute *attr,
1858 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1859 struct adt7316_chip_info *chip = iio_priv(dev_info);
1864 ret = kstrtou16(buf, 16, &data);
1865 if (ret || data >= ADT7316_VDD_INT_MASK + 1)
1868 if (data & ADT7316_VDD_INT_MASK)
1869 mask = 0; /* enable vdd int */
1871 mask = ADT7316_INT_MASK2_VDD; /* disable vdd int */
1873 ret = chip->bus.write(chip->bus.client, ADT7316_INT_MASK2, mask);
1875 chip->int_mask &= ~ADT7316_VDD_INT_MASK;
1876 chip->int_mask |= data & ADT7316_VDD_INT_MASK;
1879 if (data & ADT7316_TEMP_AIN_INT_MASK) {
1880 if ((chip->id & ID_FAMILY_MASK) == ID_ADT73XX)
1881 /* mask in reg is opposite, set 1 to disable */
1882 mask = (~data) & ADT7316_TEMP_INT_MASK;
1884 /* mask in reg is opposite, set 1 to disable */
1885 mask = (~data) & ADT7316_TEMP_AIN_INT_MASK;
1887 ret = chip->bus.write(chip->bus.client, ADT7316_INT_MASK1, mask);
1889 chip->int_mask = mask;
1894 static inline ssize_t adt7316_show_ad_bound(struct device *dev,
1895 struct device_attribute *attr,
1898 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
1899 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1900 struct adt7316_chip_info *chip = iio_priv(dev_info);
1905 if ((chip->id & ID_FAMILY_MASK) == ID_ADT73XX &&
1906 this_attr->address > ADT7316_EX_TEMP_LOW)
1909 ret = chip->bus.read(chip->bus.client, this_attr->address, &val);
1915 if (!((chip->id & ID_FAMILY_MASK) == ID_ADT75XX &&
1916 (chip->config1 & ADT7516_SEL_AIN1_2_EX_TEMP_MASK) == 0)) {
1921 return sprintf(buf, "%d\n", data);
1924 static inline ssize_t adt7316_set_ad_bound(struct device *dev,
1925 struct device_attribute *attr,
1929 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
1930 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1931 struct adt7316_chip_info *chip = iio_priv(dev_info);
1936 if ((chip->id & ID_FAMILY_MASK) == ID_ADT73XX &&
1937 this_attr->address > ADT7316_EX_TEMP_LOW)
1940 ret = kstrtoint(buf, 10, &data);
1944 if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX &&
1945 (chip->config1 & ADT7516_SEL_AIN1_2_EX_TEMP_MASK) == 0) {
1946 if (data > 255 || data < 0)
1949 if (data > 127 || data < -128)
1958 ret = chip->bus.write(chip->bus.client, this_attr->address, val);
1965 static ssize_t adt7316_show_int_enabled(struct device *dev,
1966 struct device_attribute *attr,
1969 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1970 struct adt7316_chip_info *chip = iio_priv(dev_info);
1972 return sprintf(buf, "%d\n", !!(chip->config1 & ADT7316_INT_EN));
1975 static ssize_t adt7316_set_int_enabled(struct device *dev,
1976 struct device_attribute *attr,
1980 struct iio_dev *dev_info = dev_to_iio_dev(dev);
1981 struct adt7316_chip_info *chip = iio_priv(dev_info);
1985 config1 = chip->config1 & (~ADT7316_INT_EN);
1987 config1 |= ADT7316_INT_EN;
1989 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG1, config1);
1993 chip->config1 = config1;
1998 static IIO_DEVICE_ATTR(int_mask,
2000 adt7316_show_int_mask, adt7316_set_int_mask,
2002 static IIO_DEVICE_ATTR(in_temp_high_value,
2004 adt7316_show_ad_bound, adt7316_set_ad_bound,
2005 ADT7316_IN_TEMP_HIGH);
2006 static IIO_DEVICE_ATTR(in_temp_low_value,
2008 adt7316_show_ad_bound, adt7316_set_ad_bound,
2009 ADT7316_IN_TEMP_LOW);
2010 static IIO_DEVICE_ATTR(ex_temp_high_value,
2012 adt7316_show_ad_bound, adt7316_set_ad_bound,
2013 ADT7316_EX_TEMP_HIGH);
2014 static IIO_DEVICE_ATTR(ex_temp_low_value,
2016 adt7316_show_ad_bound, adt7316_set_ad_bound,
2017 ADT7316_EX_TEMP_LOW);
2019 /* NASTY duplication to be fixed */
2020 static IIO_DEVICE_ATTR(ex_temp_ain1_high_value,
2022 adt7316_show_ad_bound, adt7316_set_ad_bound,
2023 ADT7316_EX_TEMP_HIGH);
2024 static IIO_DEVICE_ATTR(ex_temp_ain1_low_value,
2026 adt7316_show_ad_bound, adt7316_set_ad_bound,
2027 ADT7316_EX_TEMP_LOW);
2028 static IIO_DEVICE_ATTR(ain2_high_value,
2030 adt7316_show_ad_bound, adt7316_set_ad_bound,
2032 static IIO_DEVICE_ATTR(ain2_low_value,
2034 adt7316_show_ad_bound, adt7316_set_ad_bound,
2036 static IIO_DEVICE_ATTR(ain3_high_value,
2038 adt7316_show_ad_bound, adt7316_set_ad_bound,
2040 static IIO_DEVICE_ATTR(ain3_low_value,
2042 adt7316_show_ad_bound, adt7316_set_ad_bound,
2044 static IIO_DEVICE_ATTR(ain4_high_value,
2046 adt7316_show_ad_bound, adt7316_set_ad_bound,
2048 static IIO_DEVICE_ATTR(ain4_low_value,
2050 adt7316_show_ad_bound, adt7316_set_ad_bound,
2052 static IIO_DEVICE_ATTR(int_enabled,
2054 adt7316_show_int_enabled,
2055 adt7316_set_int_enabled, 0);
2057 static struct attribute *adt7316_event_attributes[] = {
2058 &iio_dev_attr_int_mask.dev_attr.attr,
2059 &iio_dev_attr_in_temp_high_value.dev_attr.attr,
2060 &iio_dev_attr_in_temp_low_value.dev_attr.attr,
2061 &iio_dev_attr_ex_temp_high_value.dev_attr.attr,
2062 &iio_dev_attr_ex_temp_low_value.dev_attr.attr,
2063 &iio_dev_attr_int_enabled.dev_attr.attr,
2067 static const struct attribute_group adt7316_event_attribute_group = {
2068 .attrs = adt7316_event_attributes,
2072 static struct attribute *adt7516_event_attributes[] = {
2073 &iio_dev_attr_int_mask.dev_attr.attr,
2074 &iio_dev_attr_in_temp_high_value.dev_attr.attr,
2075 &iio_dev_attr_in_temp_low_value.dev_attr.attr,
2076 &iio_dev_attr_ex_temp_ain1_high_value.dev_attr.attr,
2077 &iio_dev_attr_ex_temp_ain1_low_value.dev_attr.attr,
2078 &iio_dev_attr_ain2_high_value.dev_attr.attr,
2079 &iio_dev_attr_ain2_low_value.dev_attr.attr,
2080 &iio_dev_attr_ain3_high_value.dev_attr.attr,
2081 &iio_dev_attr_ain3_low_value.dev_attr.attr,
2082 &iio_dev_attr_ain4_high_value.dev_attr.attr,
2083 &iio_dev_attr_ain4_low_value.dev_attr.attr,
2084 &iio_dev_attr_int_enabled.dev_attr.attr,
2088 static const struct attribute_group adt7516_event_attribute_group = {
2089 .attrs = adt7516_event_attributes,
2093 #ifdef CONFIG_PM_SLEEP
2094 static int adt7316_disable(struct device *dev)
2096 struct iio_dev *dev_info = dev_get_drvdata(dev);
2097 struct adt7316_chip_info *chip = iio_priv(dev_info);
2099 return _adt7316_store_enabled(chip, 0);
2102 static int adt7316_enable(struct device *dev)
2104 struct iio_dev *dev_info = dev_get_drvdata(dev);
2105 struct adt7316_chip_info *chip = iio_priv(dev_info);
2107 return _adt7316_store_enabled(chip, 1);
2109 EXPORT_SYMBOL_GPL(adt7316_pm_ops);
2110 SIMPLE_DEV_PM_OPS(adt7316_pm_ops, adt7316_disable, adt7316_enable);
2113 static const struct iio_info adt7316_info = {
2114 .attrs = &adt7316_attribute_group,
2115 .event_attrs = &adt7316_event_attribute_group,
2118 static const struct iio_info adt7516_info = {
2119 .attrs = &adt7516_attribute_group,
2120 .event_attrs = &adt7516_event_attribute_group,
2124 * device probe and remove
2126 int adt7316_probe(struct device *dev, struct adt7316_bus *bus,
2129 struct adt7316_chip_info *chip;
2130 struct iio_dev *indio_dev;
2133 indio_dev = devm_iio_device_alloc(dev, sizeof(*chip));
2136 chip = iio_priv(indio_dev);
2137 /* this is only used for device removal purposes */
2138 dev_set_drvdata(dev, indio_dev);
2143 chip->id = ID_ADT7316 + (name[6] - '6');
2144 else if (name[4] == '5')
2145 chip->id = ID_ADT7516 + (name[6] - '6');
2149 if (chip->id == ID_ADT7316 || chip->id == ID_ADT7516)
2150 chip->dac_bits = 12;
2151 else if (chip->id == ID_ADT7317 || chip->id == ID_ADT7517)
2152 chip->dac_bits = 10;
2156 chip->ldac_pin = devm_gpiod_get_optional(dev, "adi,ldac",
2158 if (IS_ERR(chip->ldac_pin)) {
2159 ret = PTR_ERR(chip->ldac_pin);
2160 dev_err(dev, "Failed to request ldac GPIO: %d\n", ret);
2164 if (!chip->ldac_pin) {
2165 chip->config3 |= ADT7316_DA_EN_VIA_DAC_LDAC;
2166 if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
2167 chip->config1 |= ADT7516_SEL_AIN3;
2169 chip->int_mask = ADT7316_TEMP_INT_MASK | ADT7316_VDD_INT_MASK;
2170 if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
2171 chip->int_mask |= ADT7516_AIN_INT_MASK;
2173 indio_dev->dev.parent = dev;
2174 if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
2175 indio_dev->info = &adt7516_info;
2177 indio_dev->info = &adt7316_info;
2178 indio_dev->name = name;
2179 indio_dev->modes = INDIO_DIRECT_MODE;
2181 if (chip->bus.irq > 0) {
2182 ret = adt7316_setup_irq(indio_dev);
2187 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG1, chip->config1);
2191 ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, chip->config3);
2195 ret = devm_iio_device_register(dev, indio_dev);
2199 dev_info(dev, "%s temperature sensor, ADC and DAC registered.\n",
2204 EXPORT_SYMBOL(adt7316_probe);
2206 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
2207 MODULE_DESCRIPTION("Analog Devices ADT7316/7/8 and ADT7516/7/9 digital temperature sensor, ADC and DAC driver");
2208 MODULE_LICENSE("GPL v2");