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>
27 * ADT7316 registers definition
29 #define ADT7316_INT_STAT1 0x0
30 #define ADT7316_INT_STAT2 0x1
31 #define ADT7316_LSB_IN_TEMP_VDD 0x3
32 #define ADT7316_LSB_IN_TEMP_MASK 0x3
33 #define ADT7316_LSB_VDD_MASK 0xC
34 #define ADT7316_LSB_VDD_OFFSET 2
35 #define ADT7316_LSB_EX_TEMP_AIN 0x4
36 #define ADT7316_LSB_EX_TEMP_MASK 0x3
37 #define ADT7516_LSB_AIN_SHIFT 2
38 #define ADT7316_AD_MSB_DATA_BASE 0x6
39 #define ADT7316_AD_MSB_DATA_REGS 3
40 #define ADT7516_AD_MSB_DATA_REGS 6
41 #define ADT7316_MSB_VDD 0x6
42 #define ADT7316_MSB_IN_TEMP 0x7
43 #define ADT7316_MSB_EX_TEMP 0x8
44 #define ADT7516_MSB_AIN1 0x8
45 #define ADT7516_MSB_AIN2 0x9
46 #define ADT7516_MSB_AIN3 0xA
47 #define ADT7516_MSB_AIN4 0xB
48 #define ADT7316_DA_DATA_BASE 0x10
49 #define ADT7316_DA_MSB_DATA_REGS 4
50 #define ADT7316_LSB_DAC_A 0x10
51 #define ADT7316_MSB_DAC_A 0x11
52 #define ADT7316_LSB_DAC_B 0x12
53 #define ADT7316_MSB_DAC_B 0x13
54 #define ADT7316_LSB_DAC_C 0x14
55 #define ADT7316_MSB_DAC_C 0x15
56 #define ADT7316_LSB_DAC_D 0x16
57 #define ADT7316_MSB_DAC_D 0x17
58 #define ADT7316_CONFIG1 0x18
59 #define ADT7316_CONFIG2 0x19
60 #define ADT7316_CONFIG3 0x1A
61 #define ADT7316_LDAC_CONFIG 0x1B
62 #define ADT7316_DAC_CONFIG 0x1C
63 #define ADT7316_INT_MASK1 0x1D
64 #define ADT7316_INT_MASK2 0x1E
65 #define ADT7316_IN_TEMP_OFFSET 0x1F
66 #define ADT7316_EX_TEMP_OFFSET 0x20
67 #define ADT7316_IN_ANALOG_TEMP_OFFSET 0x21
68 #define ADT7316_EX_ANALOG_TEMP_OFFSET 0x22
69 #define ADT7316_VDD_HIGH 0x23
70 #define ADT7316_VDD_LOW 0x24
71 #define ADT7316_IN_TEMP_HIGH 0x25
72 #define ADT7316_IN_TEMP_LOW 0x26
73 #define ADT7316_EX_TEMP_HIGH 0x27
74 #define ADT7316_EX_TEMP_LOW 0x28
75 #define ADT7516_AIN2_HIGH 0x2B
76 #define ADT7516_AIN2_LOW 0x2C
77 #define ADT7516_AIN3_HIGH 0x2D
78 #define ADT7516_AIN3_LOW 0x2E
79 #define ADT7516_AIN4_HIGH 0x2F
80 #define ADT7516_AIN4_LOW 0x30
81 #define ADT7316_DEVICE_ID 0x4D
82 #define ADT7316_MANUFACTURE_ID 0x4E
83 #define ADT7316_DEVICE_REV 0x4F
84 #define ADT7316_SPI_LOCK_STAT 0x7F
89 #define ADT7316_EN 0x1
90 #define ADT7516_SEL_EX_TEMP 0x4
91 #define ADT7516_SEL_AIN1_2_EX_TEMP_MASK 0x6
92 #define ADT7516_SEL_AIN3 0x8
93 #define ADT7316_INT_EN 0x20
94 #define ADT7316_INT_POLARITY 0x40
95 #define ADT7316_PD 0x80
100 #define ADT7316_AD_SINGLE_CH_MASK 0x3
101 #define ADT7516_AD_SINGLE_CH_MASK 0x7
102 #define ADT7316_AD_SINGLE_CH_VDD 0
103 #define ADT7316_AD_SINGLE_CH_IN 1
104 #define ADT7316_AD_SINGLE_CH_EX 2
105 #define ADT7516_AD_SINGLE_CH_AIN1 2
106 #define ADT7516_AD_SINGLE_CH_AIN2 3
107 #define ADT7516_AD_SINGLE_CH_AIN3 4
108 #define ADT7516_AD_SINGLE_CH_AIN4 5
109 #define ADT7316_AD_SINGLE_CH_MODE 0x10
110 #define ADT7316_DISABLE_AVERAGING 0x20
111 #define ADT7316_EN_SMBUS_TIMEOUT 0x40
112 #define ADT7316_RESET 0x80
117 #define ADT7316_ADCLK_22_5 0x1
118 #define ADT7316_DA_HIGH_RESOLUTION 0x2
119 #define ADT7316_DA_EN_VIA_DAC_LDCA 0x4
120 #define ADT7516_AIN_IN_VREF 0x10
121 #define ADT7316_EN_IN_TEMP_PROP_DACA 0x20
122 #define ADT7316_EN_EX_TEMP_PROP_DACB 0x40
127 #define ADT7316_DA_2VREF_CH_MASK 0xF
128 #define ADT7316_DA_EN_MODE_MASK 0x30
129 #define ADT7316_DA_EN_MODE_SINGLE 0x00
130 #define ADT7316_DA_EN_MODE_AB_CD 0x10
131 #define ADT7316_DA_EN_MODE_ABCD 0x20
132 #define ADT7316_DA_EN_MODE_LDAC 0x30
133 #define ADT7316_VREF_BYPASS_DAC_AB 0x40
134 #define ADT7316_VREF_BYPASS_DAC_CD 0x80
137 * ADT7316 LDAC config
139 #define ADT7316_LDAC_EN_DA_MASK 0xF
140 #define ADT7316_DAC_IN_VREF 0x10
141 #define ADT7516_DAC_AB_IN_VREF 0x10
142 #define ADT7516_DAC_CD_IN_VREF 0x20
143 #define ADT7516_DAC_IN_VREF_OFFSET 4
144 #define ADT7516_DAC_IN_VREF_MASK 0x30
149 #define ADT7316_INT_MASK2_VDD 0x10
152 * ADT7316 value masks
154 #define ADT7316_VALUE_MASK 0xfff
155 #define ADT7316_T_VALUE_SIGN 0x400
156 #define ADT7316_T_VALUE_FLOAT_OFFSET 2
157 #define ADT7316_T_VALUE_FLOAT_MASK 0x2
162 #define ID_ADT7316 0x1
163 #define ID_ADT7317 0x2
164 #define ID_ADT7318 0x3
165 #define ID_ADT7516 0x11
166 #define ID_ADT7517 0x12
167 #define ID_ADT7519 0x14
169 #define ID_FAMILY_MASK 0xF0
170 #define ID_ADT73XX 0x0
171 #define ID_ADT75XX 0x10
174 * struct adt7316_chip_info - chip specifc information
177 struct adt7316_chip_info
{
178 struct adt7316_bus bus
;
180 u16 int_mask
; /* 0x2f */
184 u8 dac_config
; /* DAC config */
185 u8 ldac_config
; /* LDAC config */
186 u8 dac_bits
; /* 8, 10, 12 */
191 * Logic interrupt mask for user application to enable
194 #define ADT7316_IN_TEMP_HIGH_INT_MASK 0x1
195 #define ADT7316_IN_TEMP_LOW_INT_MASK 0x2
196 #define ADT7316_EX_TEMP_HIGH_INT_MASK 0x4
197 #define ADT7316_EX_TEMP_LOW_INT_MASK 0x8
198 #define ADT7316_EX_TEMP_FAULT_INT_MASK 0x10
199 #define ADT7516_AIN1_INT_MASK 0x4
200 #define ADT7516_AIN2_INT_MASK 0x20
201 #define ADT7516_AIN3_INT_MASK 0x40
202 #define ADT7516_AIN4_INT_MASK 0x80
203 #define ADT7316_VDD_INT_MASK 0x100
204 #define ADT7316_TEMP_INT_MASK 0x1F
205 #define ADT7516_AIN_INT_MASK 0xE0
206 #define ADT7316_TEMP_AIN_INT_MASK \
207 (ADT7316_TEMP_INT_MASK)
210 * struct adt7316_chip_info - chip specifc information
213 struct adt7316_limit_regs
{
218 static ssize_t
adt7316_show_enabled(struct device
*dev
,
219 struct device_attribute
*attr
,
222 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
223 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
225 return sprintf(buf
, "%d\n", !!(chip
->config1
& ADT7316_EN
));
228 static ssize_t
_adt7316_store_enabled(struct adt7316_chip_info
*chip
,
235 config1
= chip
->config1
| ADT7316_EN
;
237 config1
= chip
->config1
& ~ADT7316_EN
;
239 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG1
, config1
);
243 chip
->config1
= config1
;
249 static ssize_t
adt7316_store_enabled(struct device
*dev
,
250 struct device_attribute
*attr
,
254 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
255 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
258 if (!memcmp(buf
, "1", 1))
263 if (_adt7316_store_enabled(chip
, enable
) < 0)
269 static IIO_DEVICE_ATTR(enabled
, S_IRUGO
| S_IWUSR
,
270 adt7316_show_enabled
,
271 adt7316_store_enabled
,
274 static ssize_t
adt7316_show_select_ex_temp(struct device
*dev
,
275 struct device_attribute
*attr
,
278 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
279 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
281 if ((chip
->id
& ID_FAMILY_MASK
) != ID_ADT75XX
)
284 return sprintf(buf
, "%d\n", !!(chip
->config1
& ADT7516_SEL_EX_TEMP
));
287 static ssize_t
adt7316_store_select_ex_temp(struct device
*dev
,
288 struct device_attribute
*attr
,
292 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
293 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
297 if ((chip
->id
& ID_FAMILY_MASK
) != ID_ADT75XX
)
300 config1
= chip
->config1
& (~ADT7516_SEL_EX_TEMP
);
301 if (!memcmp(buf
, "1", 1))
302 config1
|= ADT7516_SEL_EX_TEMP
;
304 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG1
, config1
);
308 chip
->config1
= config1
;
313 static IIO_DEVICE_ATTR(select_ex_temp
, S_IRUGO
| S_IWUSR
,
314 adt7316_show_select_ex_temp
,
315 adt7316_store_select_ex_temp
,
318 static ssize_t
adt7316_show_mode(struct device
*dev
,
319 struct device_attribute
*attr
,
322 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
323 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
325 if (chip
->config2
& ADT7316_AD_SINGLE_CH_MODE
)
326 return sprintf(buf
, "single_channel\n");
328 return sprintf(buf
, "round_robin\n");
331 static ssize_t
adt7316_store_mode(struct device
*dev
,
332 struct device_attribute
*attr
,
336 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
337 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
341 config2
= chip
->config2
& (~ADT7316_AD_SINGLE_CH_MODE
);
342 if (!memcmp(buf
, "single_channel", 14))
343 config2
|= ADT7316_AD_SINGLE_CH_MODE
;
345 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG2
, config2
);
349 chip
->config2
= config2
;
354 static IIO_DEVICE_ATTR(mode
, S_IRUGO
| S_IWUSR
,
359 static ssize_t
adt7316_show_all_modes(struct device
*dev
,
360 struct device_attribute
*attr
,
363 return sprintf(buf
, "single_channel\nround_robin\n");
366 static IIO_DEVICE_ATTR(all_modes
, S_IRUGO
, adt7316_show_all_modes
, NULL
, 0);
368 static ssize_t
adt7316_show_ad_channel(struct device
*dev
,
369 struct device_attribute
*attr
,
372 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
373 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
375 if (!(chip
->config2
& ADT7316_AD_SINGLE_CH_MODE
))
378 switch (chip
->config2
& ADT7516_AD_SINGLE_CH_MASK
) {
379 case ADT7316_AD_SINGLE_CH_VDD
:
380 return sprintf(buf
, "0 - VDD\n");
381 case ADT7316_AD_SINGLE_CH_IN
:
382 return sprintf(buf
, "1 - Internal Temperature\n");
383 case ADT7316_AD_SINGLE_CH_EX
:
384 if (((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
) &&
385 (chip
->config1
& ADT7516_SEL_AIN1_2_EX_TEMP_MASK
) == 0)
386 return sprintf(buf
, "2 - AIN1\n");
388 return sprintf(buf
, "2 - External Temperature\n");
389 case ADT7516_AD_SINGLE_CH_AIN2
:
390 if ((chip
->config1
& ADT7516_SEL_AIN1_2_EX_TEMP_MASK
) == 0)
391 return sprintf(buf
, "3 - AIN2\n");
393 return sprintf(buf
, "N/A\n");
394 case ADT7516_AD_SINGLE_CH_AIN3
:
395 if (chip
->config1
& ADT7516_SEL_AIN3
)
396 return sprintf(buf
, "4 - AIN3\n");
398 return sprintf(buf
, "N/A\n");
399 case ADT7516_AD_SINGLE_CH_AIN4
:
400 return sprintf(buf
, "5 - AIN4\n");
402 return sprintf(buf
, "N/A\n");
406 static ssize_t
adt7316_store_ad_channel(struct device
*dev
,
407 struct device_attribute
*attr
,
411 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
412 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
414 unsigned long data
= 0;
417 if (!(chip
->config2
& ADT7316_AD_SINGLE_CH_MODE
))
420 ret
= strict_strtoul(buf
, 10, &data
);
424 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
) {
428 config2
= chip
->config2
& (~ADT7516_AD_SINGLE_CH_MASK
);
433 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
, S_IRUGO
| S_IWUSR
,
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_get_drvdata(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");
468 return sprintf(buf
, "0 - VDD\n1 - Internal Temperature\n"
469 "2 - External Temperature\n");
472 static IIO_DEVICE_ATTR(all_ad_channels
, S_IRUGO
,
473 adt7316_show_all_ad_channels
, NULL
, 0);
475 static ssize_t
adt7316_show_disable_averaging(struct device
*dev
,
476 struct device_attribute
*attr
,
479 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
480 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
482 return sprintf(buf
, "%d\n",
483 !!(chip
->config2
& ADT7316_DISABLE_AVERAGING
));
486 static ssize_t
adt7316_store_disable_averaging(struct device
*dev
,
487 struct device_attribute
*attr
,
491 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
492 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
496 config2
= chip
->config2
& (~ADT7316_DISABLE_AVERAGING
);
497 if (!memcmp(buf
, "1", 1))
498 config2
|= ADT7316_DISABLE_AVERAGING
;
500 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG2
, config2
);
504 chip
->config2
= config2
;
509 static IIO_DEVICE_ATTR(disable_averaging
, S_IRUGO
| S_IWUSR
,
510 adt7316_show_disable_averaging
,
511 adt7316_store_disable_averaging
,
514 static ssize_t
adt7316_show_enable_smbus_timeout(struct device
*dev
,
515 struct device_attribute
*attr
,
518 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
519 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
521 return sprintf(buf
, "%d\n",
522 !!(chip
->config2
& ADT7316_EN_SMBUS_TIMEOUT
));
525 static ssize_t
adt7316_store_enable_smbus_timeout(struct device
*dev
,
526 struct device_attribute
*attr
,
530 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
531 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
535 config2
= chip
->config2
& (~ADT7316_EN_SMBUS_TIMEOUT
);
536 if (!memcmp(buf
, "1", 1))
537 config2
|= ADT7316_EN_SMBUS_TIMEOUT
;
539 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG2
, config2
);
543 chip
->config2
= config2
;
548 static IIO_DEVICE_ATTR(enable_smbus_timeout
, S_IRUGO
| S_IWUSR
,
549 adt7316_show_enable_smbus_timeout
,
550 adt7316_store_enable_smbus_timeout
,
554 static ssize_t
adt7316_store_reset(struct device
*dev
,
555 struct device_attribute
*attr
,
559 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
560 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
564 config2
= chip
->config2
| ADT7316_RESET
;
566 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG2
, config2
);
573 static IIO_DEVICE_ATTR(reset
, S_IWUSR
,
578 static ssize_t
adt7316_show_powerdown(struct device
*dev
,
579 struct device_attribute
*attr
,
582 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
583 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
585 return sprintf(buf
, "%d\n", !!(chip
->config1
& ADT7316_PD
));
588 static ssize_t
adt7316_store_powerdown(struct device
*dev
,
589 struct device_attribute
*attr
,
593 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
594 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
598 config1
= chip
->config1
& (~ADT7316_PD
);
599 if (!memcmp(buf
, "1", 1))
600 config1
|= ADT7316_PD
;
602 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG1
, config1
);
606 chip
->config1
= config1
;
611 static IIO_DEVICE_ATTR(powerdown
, S_IRUGO
| S_IWUSR
,
612 adt7316_show_powerdown
,
613 adt7316_store_powerdown
,
616 static ssize_t
adt7316_show_fast_ad_clock(struct device
*dev
,
617 struct device_attribute
*attr
,
620 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
621 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
623 return sprintf(buf
, "%d\n", !!(chip
->config3
& ADT7316_ADCLK_22_5
));
626 static ssize_t
adt7316_store_fast_ad_clock(struct device
*dev
,
627 struct device_attribute
*attr
,
631 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
632 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
636 config3
= chip
->config3
& (~ADT7316_ADCLK_22_5
);
637 if (!memcmp(buf
, "1", 1))
638 config3
|= ADT7316_ADCLK_22_5
;
640 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG3
, config3
);
644 chip
->config3
= config3
;
649 static IIO_DEVICE_ATTR(fast_ad_clock
, S_IRUGO
| S_IWUSR
,
650 adt7316_show_fast_ad_clock
,
651 adt7316_store_fast_ad_clock
,
654 static ssize_t
adt7316_show_da_high_resolution(struct device
*dev
,
655 struct device_attribute
*attr
,
658 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
659 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
661 if (chip
->config3
& ADT7316_DA_HIGH_RESOLUTION
) {
662 if (chip
->id
== ID_ADT7316
|| chip
->id
== ID_ADT7516
)
663 return sprintf(buf
, "1 (12 bits)\n");
664 else if (chip
->id
== ID_ADT7317
|| chip
->id
== ID_ADT7517
)
665 return sprintf(buf
, "1 (10 bits)\n");
668 return sprintf(buf
, "0 (8 bits)\n");
671 static ssize_t
adt7316_store_da_high_resolution(struct device
*dev
,
672 struct device_attribute
*attr
,
676 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
677 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
683 if (!memcmp(buf
, "1", 1)) {
684 config3
= chip
->config3
| ADT7316_DA_HIGH_RESOLUTION
;
685 if (chip
->id
== ID_ADT7316
|| chip
->id
== ID_ADT7516
)
687 else if (chip
->id
== ID_ADT7317
|| chip
->id
== ID_ADT7517
)
690 config3
= chip
->config3
& (~ADT7316_DA_HIGH_RESOLUTION
);
692 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG3
, config3
);
696 chip
->config3
= config3
;
701 static IIO_DEVICE_ATTR(da_high_resolution
, S_IRUGO
| S_IWUSR
,
702 adt7316_show_da_high_resolution
,
703 adt7316_store_da_high_resolution
,
706 static ssize_t
adt7316_show_AIN_internal_Vref(struct device
*dev
,
707 struct device_attribute
*attr
,
710 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
711 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
713 if ((chip
->id
& ID_FAMILY_MASK
) != ID_ADT75XX
)
716 return sprintf(buf
, "%d\n",
717 !!(chip
->config3
& ADT7516_AIN_IN_VREF
));
720 static ssize_t
adt7316_store_AIN_internal_Vref(struct device
*dev
,
721 struct device_attribute
*attr
,
725 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
726 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
730 if ((chip
->id
& ID_FAMILY_MASK
) != ID_ADT75XX
)
733 if (memcmp(buf
, "1", 1))
734 config3
= chip
->config3
& (~ADT7516_AIN_IN_VREF
);
736 config3
= chip
->config3
| ADT7516_AIN_IN_VREF
;
738 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG3
, config3
);
742 chip
->config3
= config3
;
747 static IIO_DEVICE_ATTR(AIN_internal_Vref
, S_IRUGO
| S_IWUSR
,
748 adt7316_show_AIN_internal_Vref
,
749 adt7316_store_AIN_internal_Vref
,
753 static ssize_t
adt7316_show_enable_prop_DACA(struct device
*dev
,
754 struct device_attribute
*attr
,
757 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
758 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
760 return sprintf(buf
, "%d\n",
761 !!(chip
->config3
& ADT7316_EN_IN_TEMP_PROP_DACA
));
764 static ssize_t
adt7316_store_enable_prop_DACA(struct device
*dev
,
765 struct device_attribute
*attr
,
769 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
770 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
774 config3
= chip
->config3
& (~ADT7316_EN_IN_TEMP_PROP_DACA
);
775 if (!memcmp(buf
, "1", 1))
776 config3
|= ADT7316_EN_IN_TEMP_PROP_DACA
;
778 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG3
, config3
);
782 chip
->config3
= config3
;
787 static IIO_DEVICE_ATTR(enable_proportion_DACA
, S_IRUGO
| S_IWUSR
,
788 adt7316_show_enable_prop_DACA
,
789 adt7316_store_enable_prop_DACA
,
792 static ssize_t
adt7316_show_enable_prop_DACB(struct device
*dev
,
793 struct device_attribute
*attr
,
796 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
797 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
799 return sprintf(buf
, "%d\n",
800 !!(chip
->config3
& ADT7316_EN_EX_TEMP_PROP_DACB
));
803 static ssize_t
adt7316_store_enable_prop_DACB(struct device
*dev
,
804 struct device_attribute
*attr
,
808 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
809 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
813 config3
= chip
->config3
& (~ADT7316_EN_EX_TEMP_PROP_DACB
);
814 if (!memcmp(buf
, "1", 1))
815 config3
|= ADT7316_EN_EX_TEMP_PROP_DACB
;
817 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG3
, config3
);
821 chip
->config3
= config3
;
826 static IIO_DEVICE_ATTR(enable_proportion_DACB
, S_IRUGO
| S_IWUSR
,
827 adt7316_show_enable_prop_DACB
,
828 adt7316_store_enable_prop_DACB
,
831 static ssize_t
adt7316_show_DAC_2Vref_ch_mask(struct device
*dev
,
832 struct device_attribute
*attr
,
835 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
836 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
838 return sprintf(buf
, "0x%x\n",
839 chip
->dac_config
& ADT7316_DA_2VREF_CH_MASK
);
842 static ssize_t
adt7316_store_DAC_2Vref_ch_mask(struct device
*dev
,
843 struct device_attribute
*attr
,
847 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
848 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
850 unsigned long data
= 0;
853 ret
= strict_strtoul(buf
, 16, &data
);
854 if (ret
|| data
> ADT7316_DA_2VREF_CH_MASK
)
857 dac_config
= chip
->dac_config
& (~ADT7316_DA_2VREF_CH_MASK
);
860 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_DAC_CONFIG
, dac_config
);
864 chip
->dac_config
= dac_config
;
869 static IIO_DEVICE_ATTR(DAC_2Vref_channels_mask
, S_IRUGO
| S_IWUSR
,
870 adt7316_show_DAC_2Vref_ch_mask
,
871 adt7316_store_DAC_2Vref_ch_mask
,
874 static ssize_t
adt7316_show_DAC_update_mode(struct device
*dev
,
875 struct device_attribute
*attr
,
878 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
879 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
881 if (!(chip
->config3
& ADT7316_DA_EN_VIA_DAC_LDCA
))
882 return sprintf(buf
, "manual\n");
884 switch (chip
->dac_config
& ADT7316_DA_EN_MODE_MASK
) {
885 case ADT7316_DA_EN_MODE_SINGLE
:
886 return sprintf(buf
, "0 - auto at any MSB DAC writing\n");
887 case ADT7316_DA_EN_MODE_AB_CD
:
888 return sprintf(buf
, "1 - auto at MSB DAC AB and CD writing\n");
889 case ADT7316_DA_EN_MODE_ABCD
:
890 return sprintf(buf
, "2 - auto at MSB DAC ABCD writing\n");
891 default: /* ADT7316_DA_EN_MODE_LDAC */
892 return sprintf(buf
, "3 - manual\n");
897 static ssize_t
adt7316_store_DAC_update_mode(struct device
*dev
,
898 struct device_attribute
*attr
,
902 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
903 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
908 if (!(chip
->config3
& ADT7316_DA_EN_VIA_DAC_LDCA
))
911 ret
= strict_strtoul(buf
, 10, &data
);
912 if (ret
|| data
> ADT7316_DA_EN_MODE_MASK
)
915 dac_config
= chip
->dac_config
& (~ADT7316_DA_EN_MODE_MASK
);
918 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_DAC_CONFIG
, dac_config
);
922 chip
->dac_config
= dac_config
;
927 static IIO_DEVICE_ATTR(DAC_update_mode
, S_IRUGO
| S_IWUSR
,
928 adt7316_show_DAC_update_mode
,
929 adt7316_store_DAC_update_mode
,
932 static ssize_t
adt7316_show_all_DAC_update_modes(struct device
*dev
,
933 struct device_attribute
*attr
,
936 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
937 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
939 if (chip
->config3
& ADT7316_DA_EN_VIA_DAC_LDCA
)
940 return sprintf(buf
, "0 - auto at any MSB DAC writing\n"
941 "1 - auto at MSB DAC AB and CD writing\n"
942 "2 - auto at MSB DAC ABCD writing\n"
945 return sprintf(buf
, "manual\n");
948 static IIO_DEVICE_ATTR(all_DAC_update_modes
, S_IRUGO
,
949 adt7316_show_all_DAC_update_modes
, NULL
, 0);
952 static ssize_t
adt7316_store_update_DAC(struct device
*dev
,
953 struct device_attribute
*attr
,
957 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
958 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
963 if (chip
->config3
& ADT7316_DA_EN_VIA_DAC_LDCA
) {
964 if ((chip
->dac_config
& ADT7316_DA_EN_MODE_MASK
) !=
965 ADT7316_DA_EN_MODE_LDAC
)
968 ret
= strict_strtoul(buf
, 16, &data
);
969 if (ret
|| data
> ADT7316_LDAC_EN_DA_MASK
)
972 ldac_config
= chip
->ldac_config
& (~ADT7316_LDAC_EN_DA_MASK
);
975 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_LDAC_CONFIG
,
980 gpio_set_value(chip
->ldac_pin
, 0);
981 gpio_set_value(chip
->ldac_pin
, 1);
987 static IIO_DEVICE_ATTR(update_DAC
, S_IRUGO
| S_IWUSR
,
989 adt7316_store_update_DAC
,
992 static ssize_t
adt7316_show_DA_AB_Vref_bypass(struct device
*dev
,
993 struct device_attribute
*attr
,
996 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
997 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
999 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
)
1002 return sprintf(buf
, "%d\n",
1003 !!(chip
->dac_config
& ADT7316_VREF_BYPASS_DAC_AB
));
1006 static ssize_t
adt7316_store_DA_AB_Vref_bypass(struct device
*dev
,
1007 struct device_attribute
*attr
,
1011 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1012 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1016 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
)
1019 dac_config
= chip
->dac_config
& (~ADT7316_VREF_BYPASS_DAC_AB
);
1020 if (!memcmp(buf
, "1", 1))
1021 dac_config
|= ADT7316_VREF_BYPASS_DAC_AB
;
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_AB_Vref_bypass
, S_IRUGO
| S_IWUSR
,
1033 adt7316_show_DA_AB_Vref_bypass
,
1034 adt7316_store_DA_AB_Vref_bypass
,
1037 static ssize_t
adt7316_show_DA_CD_Vref_bypass(struct device
*dev
,
1038 struct device_attribute
*attr
,
1041 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1042 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1044 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
)
1047 return sprintf(buf
, "%d\n",
1048 !!(chip
->dac_config
& ADT7316_VREF_BYPASS_DAC_CD
));
1051 static ssize_t
adt7316_store_DA_CD_Vref_bypass(struct device
*dev
,
1052 struct device_attribute
*attr
,
1056 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1057 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1061 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
)
1064 dac_config
= chip
->dac_config
& (~ADT7316_VREF_BYPASS_DAC_CD
);
1065 if (!memcmp(buf
, "1", 1))
1066 dac_config
|= ADT7316_VREF_BYPASS_DAC_CD
;
1068 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_DAC_CONFIG
, dac_config
);
1072 chip
->dac_config
= dac_config
;
1077 static IIO_DEVICE_ATTR(DA_CD_Vref_bypass
, S_IRUGO
| S_IWUSR
,
1078 adt7316_show_DA_CD_Vref_bypass
,
1079 adt7316_store_DA_CD_Vref_bypass
,
1082 static ssize_t
adt7316_show_DAC_internal_Vref(struct device
*dev
,
1083 struct device_attribute
*attr
,
1086 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1087 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1089 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
)
1090 return sprintf(buf
, "0x%x\n",
1091 (chip
->dac_config
& ADT7516_DAC_IN_VREF_MASK
) >>
1092 ADT7516_DAC_IN_VREF_OFFSET
);
1094 return sprintf(buf
, "%d\n",
1095 !!(chip
->dac_config
& ADT7316_DAC_IN_VREF
));
1098 static ssize_t
adt7316_store_DAC_internal_Vref(struct device
*dev
,
1099 struct device_attribute
*attr
,
1103 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1104 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1109 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
) {
1110 ret
= strict_strtoul(buf
, 16, &data
);
1111 if (ret
|| data
> 3)
1114 ldac_config
= chip
->ldac_config
& (~ADT7516_DAC_IN_VREF_MASK
);
1116 ldac_config
|= ADT7516_DAC_AB_IN_VREF
;
1117 else if (data
& 0x2)
1118 ldac_config
|= ADT7516_DAC_CD_IN_VREF
;
1120 ret
= strict_strtoul(buf
, 16, &data
);
1124 ldac_config
= chip
->ldac_config
& (~ADT7316_DAC_IN_VREF
);
1126 ldac_config
= chip
->ldac_config
| ADT7316_DAC_IN_VREF
;
1129 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_LDAC_CONFIG
, ldac_config
);
1133 chip
->ldac_config
= ldac_config
;
1138 static IIO_DEVICE_ATTR(DAC_internal_Vref
, S_IRUGO
| S_IWUSR
,
1139 adt7316_show_DAC_internal_Vref
,
1140 adt7316_store_DAC_internal_Vref
,
1143 static ssize_t
adt7316_show_ad(struct adt7316_chip_info
*chip
,
1144 int channel
, char *buf
)
1151 if ((chip
->config2
& ADT7316_AD_SINGLE_CH_MODE
) &&
1152 channel
!= (chip
->config2
& ADT7516_AD_SINGLE_CH_MASK
))
1156 case ADT7316_AD_SINGLE_CH_IN
:
1157 ret
= chip
->bus
.read(chip
->bus
.client
,
1158 ADT7316_LSB_IN_TEMP_VDD
, &lsb
);
1162 ret
= chip
->bus
.read(chip
->bus
.client
,
1163 ADT7316_AD_MSB_DATA_BASE
+ channel
, &msb
);
1167 data
= msb
<< ADT7316_T_VALUE_FLOAT_OFFSET
;
1168 data
|= lsb
& ADT7316_LSB_IN_TEMP_MASK
;
1170 case ADT7316_AD_SINGLE_CH_VDD
:
1171 ret
= chip
->bus
.read(chip
->bus
.client
,
1172 ADT7316_LSB_IN_TEMP_VDD
, &lsb
);
1176 ret
= chip
->bus
.read(chip
->bus
.client
,
1178 ADT7316_AD_MSB_DATA_BASE
+ channel
, &msb
);
1182 data
= msb
<< ADT7316_T_VALUE_FLOAT_OFFSET
;
1183 data
|= (lsb
& ADT7316_LSB_VDD_MASK
) >> ADT7316_LSB_VDD_OFFSET
;
1184 return sprintf(buf
, "%d\n", data
);
1185 default: /* ex_temp and ain */
1186 ret
= chip
->bus
.read(chip
->bus
.client
,
1187 ADT7316_LSB_EX_TEMP_AIN
, &lsb
);
1191 ret
= chip
->bus
.read(chip
->bus
.client
,
1192 ADT7316_AD_MSB_DATA_BASE
+ channel
, &msb
);
1196 data
= msb
<< ADT7316_T_VALUE_FLOAT_OFFSET
;
1197 data
|= lsb
& (ADT7316_LSB_EX_TEMP_MASK
<<
1198 (ADT7516_LSB_AIN_SHIFT
* (channel
-
1199 (ADT7316_MSB_EX_TEMP
- ADT7316_AD_MSB_DATA_BASE
))));
1201 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
)
1202 return sprintf(buf
, "%d\n", data
);
1207 if (data
& ADT7316_T_VALUE_SIGN
) {
1208 /* convert supplement to positive value */
1209 data
= (ADT7316_T_VALUE_SIGN
<< 1) - data
;
1213 return sprintf(buf
, "%c%d.%.2d\n", sign
,
1214 (data
>> ADT7316_T_VALUE_FLOAT_OFFSET
),
1215 (data
& ADT7316_T_VALUE_FLOAT_MASK
) * 25);
1218 static ssize_t
adt7316_show_VDD(struct device
*dev
,
1219 struct device_attribute
*attr
,
1222 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1223 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1225 return adt7316_show_ad(chip
, ADT7316_AD_SINGLE_CH_VDD
, buf
);
1227 static IIO_DEVICE_ATTR(VDD
, S_IRUGO
, adt7316_show_VDD
, NULL
, 0);
1229 static ssize_t
adt7316_show_in_temp(struct device
*dev
,
1230 struct device_attribute
*attr
,
1233 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1234 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1236 return adt7316_show_ad(chip
, ADT7316_AD_SINGLE_CH_IN
, buf
);
1239 static IIO_DEVICE_ATTR(in_temp
, S_IRUGO
, adt7316_show_in_temp
, NULL
, 0);
1241 static ssize_t
adt7316_show_ex_temp_AIN1(struct device
*dev
,
1242 struct device_attribute
*attr
,
1245 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1246 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1248 return adt7316_show_ad(chip
, ADT7316_AD_SINGLE_CH_EX
, buf
);
1251 static IIO_DEVICE_ATTR(ex_temp_AIN1
, S_IRUGO
, adt7316_show_ex_temp_AIN1
, NULL
, 0);
1252 static IIO_DEVICE_ATTR(ex_temp
, S_IRUGO
, adt7316_show_ex_temp_AIN1
, NULL
, 0);
1254 static ssize_t
adt7316_show_AIN2(struct device
*dev
,
1255 struct device_attribute
*attr
,
1258 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1259 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1261 return adt7316_show_ad(chip
, ADT7516_AD_SINGLE_CH_AIN2
, buf
);
1263 static IIO_DEVICE_ATTR(AIN2
, S_IRUGO
, adt7316_show_AIN2
, NULL
, 0);
1265 static ssize_t
adt7316_show_AIN3(struct device
*dev
,
1266 struct device_attribute
*attr
,
1269 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1270 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1272 return adt7316_show_ad(chip
, ADT7516_AD_SINGLE_CH_AIN3
, buf
);
1274 static IIO_DEVICE_ATTR(AIN3
, S_IRUGO
, adt7316_show_AIN3
, NULL
, 0);
1276 static ssize_t
adt7316_show_AIN4(struct device
*dev
,
1277 struct device_attribute
*attr
,
1280 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1281 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1283 return adt7316_show_ad(chip
, ADT7516_AD_SINGLE_CH_AIN4
, buf
);
1285 static IIO_DEVICE_ATTR(AIN4
, S_IRUGO
, adt7316_show_AIN4
, NULL
, 0);
1287 static ssize_t
adt7316_show_temp_offset(struct adt7316_chip_info
*chip
,
1288 int offset_addr
, char *buf
)
1294 ret
= chip
->bus
.read(chip
->bus
.client
, offset_addr
, &val
);
1302 return sprintf(buf
, "%d\n", data
);
1305 static ssize_t
adt7316_store_temp_offset(struct adt7316_chip_info
*chip
,
1306 int offset_addr
, const char *buf
, size_t len
)
1312 ret
= strict_strtol(buf
, 10, &data
);
1313 if (ret
|| data
> 127 || data
< -128)
1321 ret
= chip
->bus
.write(chip
->bus
.client
, offset_addr
, val
);
1328 static ssize_t
adt7316_show_in_temp_offset(struct device
*dev
,
1329 struct device_attribute
*attr
,
1332 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1333 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1335 return adt7316_show_temp_offset(chip
, ADT7316_IN_TEMP_OFFSET
, buf
);
1338 static ssize_t
adt7316_store_in_temp_offset(struct device
*dev
,
1339 struct device_attribute
*attr
,
1343 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1344 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1346 return adt7316_store_temp_offset(chip
, ADT7316_IN_TEMP_OFFSET
, buf
, len
);
1349 static IIO_DEVICE_ATTR(in_temp_offset
, S_IRUGO
| S_IWUSR
,
1350 adt7316_show_in_temp_offset
,
1351 adt7316_store_in_temp_offset
, 0);
1353 static ssize_t
adt7316_show_ex_temp_offset(struct device
*dev
,
1354 struct device_attribute
*attr
,
1357 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1358 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1360 return adt7316_show_temp_offset(chip
, ADT7316_EX_TEMP_OFFSET
, buf
);
1363 static ssize_t
adt7316_store_ex_temp_offset(struct device
*dev
,
1364 struct device_attribute
*attr
,
1368 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1369 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1371 return adt7316_store_temp_offset(chip
, ADT7316_EX_TEMP_OFFSET
, buf
, len
);
1374 static IIO_DEVICE_ATTR(ex_temp_offset
, S_IRUGO
| S_IWUSR
,
1375 adt7316_show_ex_temp_offset
,
1376 adt7316_store_ex_temp_offset
, 0);
1378 static ssize_t
adt7316_show_in_analog_temp_offset(struct device
*dev
,
1379 struct device_attribute
*attr
,
1382 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1383 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1385 return adt7316_show_temp_offset(chip
,
1386 ADT7316_IN_ANALOG_TEMP_OFFSET
, buf
);
1389 static ssize_t
adt7316_store_in_analog_temp_offset(struct device
*dev
,
1390 struct device_attribute
*attr
,
1394 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1395 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1397 return adt7316_store_temp_offset(chip
,
1398 ADT7316_IN_ANALOG_TEMP_OFFSET
, buf
, len
);
1401 static IIO_DEVICE_ATTR(in_analog_temp_offset
, S_IRUGO
| S_IWUSR
,
1402 adt7316_show_in_analog_temp_offset
,
1403 adt7316_store_in_analog_temp_offset
, 0);
1405 static ssize_t
adt7316_show_ex_analog_temp_offset(struct device
*dev
,
1406 struct device_attribute
*attr
,
1409 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1410 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1412 return adt7316_show_temp_offset(chip
,
1413 ADT7316_EX_ANALOG_TEMP_OFFSET
, buf
);
1416 static ssize_t
adt7316_store_ex_analog_temp_offset(struct device
*dev
,
1417 struct device_attribute
*attr
,
1421 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1422 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1424 return adt7316_store_temp_offset(chip
,
1425 ADT7316_EX_ANALOG_TEMP_OFFSET
, buf
, len
);
1428 static IIO_DEVICE_ATTR(ex_analog_temp_offset
, S_IRUGO
| S_IWUSR
,
1429 adt7316_show_ex_analog_temp_offset
,
1430 adt7316_store_ex_analog_temp_offset
, 0);
1432 static ssize_t
adt7316_show_DAC(struct adt7316_chip_info
*chip
,
1433 int channel
, char *buf
)
1436 u8 msb
, lsb
, offset
;
1439 if (channel
>= ADT7316_DA_MSB_DATA_REGS
||
1441 (chip
->config3
& ADT7316_EN_IN_TEMP_PROP_DACA
)) ||
1443 (chip
->config3
& ADT7316_EN_EX_TEMP_PROP_DACB
)))
1446 offset
= chip
->dac_bits
- 8;
1448 if (chip
->dac_bits
> 8) {
1449 ret
= chip
->bus
.read(chip
->bus
.client
,
1450 ADT7316_DA_DATA_BASE
+ channel
* 2, &lsb
);
1455 ret
= chip
->bus
.read(chip
->bus
.client
,
1456 ADT7316_DA_DATA_BASE
+ 1 + channel
* 2, &msb
);
1460 data
= (msb
<< offset
) + (lsb
& ((1 << offset
) - 1));
1462 return sprintf(buf
, "%d\n", data
);
1465 static ssize_t
adt7316_store_DAC(struct adt7316_chip_info
*chip
,
1466 int channel
, const char *buf
, size_t len
)
1468 u8 msb
, lsb
, offset
;
1472 if (channel
>= ADT7316_DA_MSB_DATA_REGS
||
1474 (chip
->config3
& ADT7316_EN_IN_TEMP_PROP_DACA
)) ||
1476 (chip
->config3
& ADT7316_EN_EX_TEMP_PROP_DACB
)))
1479 offset
= chip
->dac_bits
- 8;
1481 ret
= strict_strtoul(buf
, 10, &data
);
1482 if (ret
|| data
>= (1 << chip
->dac_bits
))
1485 if (chip
->dac_bits
> 8) {
1486 lsb
= data
& (1 << offset
);
1487 ret
= chip
->bus
.write(chip
->bus
.client
,
1488 ADT7316_DA_DATA_BASE
+ channel
* 2, lsb
);
1493 msb
= data
>> offset
;
1494 ret
= chip
->bus
.write(chip
->bus
.client
,
1495 ADT7316_DA_DATA_BASE
+ 1 + channel
* 2, msb
);
1502 static ssize_t
adt7316_show_DAC_A(struct device
*dev
,
1503 struct device_attribute
*attr
,
1506 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1507 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1509 return adt7316_show_DAC(chip
, 0, buf
);
1512 static ssize_t
adt7316_store_DAC_A(struct device
*dev
,
1513 struct device_attribute
*attr
,
1517 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1518 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1520 return adt7316_store_DAC(chip
, 0, buf
, len
);
1523 static IIO_DEVICE_ATTR(DAC_A
, S_IRUGO
| S_IWUSR
, adt7316_show_DAC_A
,
1524 adt7316_store_DAC_A
, 0);
1526 static ssize_t
adt7316_show_DAC_B(struct device
*dev
,
1527 struct device_attribute
*attr
,
1530 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1531 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1533 return adt7316_show_DAC(chip
, 1, buf
);
1536 static ssize_t
adt7316_store_DAC_B(struct device
*dev
,
1537 struct device_attribute
*attr
,
1541 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1542 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1544 return adt7316_store_DAC(chip
, 1, buf
, len
);
1547 static IIO_DEVICE_ATTR(DAC_B
, S_IRUGO
| S_IWUSR
, adt7316_show_DAC_B
,
1548 adt7316_store_DAC_B
, 0);
1550 static ssize_t
adt7316_show_DAC_C(struct device
*dev
,
1551 struct device_attribute
*attr
,
1554 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1555 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1557 return adt7316_show_DAC(chip
, 2, buf
);
1560 static ssize_t
adt7316_store_DAC_C(struct device
*dev
,
1561 struct device_attribute
*attr
,
1565 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1566 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1568 return adt7316_store_DAC(chip
, 2, buf
, len
);
1571 static IIO_DEVICE_ATTR(DAC_C
, S_IRUGO
| S_IWUSR
, adt7316_show_DAC_C
,
1572 adt7316_store_DAC_C
, 0);
1574 static ssize_t
adt7316_show_DAC_D(struct device
*dev
,
1575 struct device_attribute
*attr
,
1578 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1579 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1581 return adt7316_show_DAC(chip
, 3, buf
);
1584 static ssize_t
adt7316_store_DAC_D(struct device
*dev
,
1585 struct device_attribute
*attr
,
1589 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1590 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1592 return adt7316_store_DAC(chip
, 3, buf
, len
);
1595 static IIO_DEVICE_ATTR(DAC_D
, S_IRUGO
| S_IWUSR
, adt7316_show_DAC_D
,
1596 adt7316_store_DAC_D
, 0);
1598 static ssize_t
adt7316_show_device_id(struct device
*dev
,
1599 struct device_attribute
*attr
,
1602 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1603 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1607 ret
= chip
->bus
.read(chip
->bus
.client
, ADT7316_DEVICE_ID
, &id
);
1611 return sprintf(buf
, "%d\n", id
);
1614 static IIO_DEVICE_ATTR(device_id
, S_IRUGO
, adt7316_show_device_id
, NULL
, 0);
1616 static ssize_t
adt7316_show_manufactorer_id(struct device
*dev
,
1617 struct device_attribute
*attr
,
1620 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1621 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1625 ret
= chip
->bus
.read(chip
->bus
.client
, ADT7316_MANUFACTURE_ID
, &id
);
1629 return sprintf(buf
, "%d\n", id
);
1632 static IIO_DEVICE_ATTR(manufactorer_id
, S_IRUGO
,
1633 adt7316_show_manufactorer_id
, NULL
, 0);
1635 static ssize_t
adt7316_show_device_rev(struct device
*dev
,
1636 struct device_attribute
*attr
,
1639 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1640 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1644 ret
= chip
->bus
.read(chip
->bus
.client
, ADT7316_DEVICE_REV
, &rev
);
1648 return sprintf(buf
, "%d\n", rev
);
1651 static IIO_DEVICE_ATTR(device_rev
, S_IRUGO
, adt7316_show_device_rev
, NULL
, 0);
1653 static ssize_t
adt7316_show_bus_type(struct device
*dev
,
1654 struct device_attribute
*attr
,
1657 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1658 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1662 ret
= chip
->bus
.read(chip
->bus
.client
, ADT7316_SPI_LOCK_STAT
, &stat
);
1667 return sprintf(buf
, "spi\n");
1669 return sprintf(buf
, "i2c\n");
1672 static IIO_DEVICE_ATTR(bus_type
, S_IRUGO
, adt7316_show_bus_type
, NULL
, 0);
1674 static struct attribute
*adt7316_attributes
[] = {
1675 &iio_dev_attr_all_modes
.dev_attr
.attr
,
1676 &iio_dev_attr_mode
.dev_attr
.attr
,
1677 &iio_dev_attr_reset
.dev_attr
.attr
,
1678 &iio_dev_attr_enabled
.dev_attr
.attr
,
1679 &iio_dev_attr_ad_channel
.dev_attr
.attr
,
1680 &iio_dev_attr_all_ad_channels
.dev_attr
.attr
,
1681 &iio_dev_attr_disable_averaging
.dev_attr
.attr
,
1682 &iio_dev_attr_enable_smbus_timeout
.dev_attr
.attr
,
1683 &iio_dev_attr_powerdown
.dev_attr
.attr
,
1684 &iio_dev_attr_fast_ad_clock
.dev_attr
.attr
,
1685 &iio_dev_attr_da_high_resolution
.dev_attr
.attr
,
1686 &iio_dev_attr_enable_proportion_DACA
.dev_attr
.attr
,
1687 &iio_dev_attr_enable_proportion_DACB
.dev_attr
.attr
,
1688 &iio_dev_attr_DAC_2Vref_channels_mask
.dev_attr
.attr
,
1689 &iio_dev_attr_DAC_update_mode
.dev_attr
.attr
,
1690 &iio_dev_attr_all_DAC_update_modes
.dev_attr
.attr
,
1691 &iio_dev_attr_update_DAC
.dev_attr
.attr
,
1692 &iio_dev_attr_DA_AB_Vref_bypass
.dev_attr
.attr
,
1693 &iio_dev_attr_DA_CD_Vref_bypass
.dev_attr
.attr
,
1694 &iio_dev_attr_DAC_internal_Vref
.dev_attr
.attr
,
1695 &iio_dev_attr_VDD
.dev_attr
.attr
,
1696 &iio_dev_attr_in_temp
.dev_attr
.attr
,
1697 &iio_dev_attr_ex_temp
.dev_attr
.attr
,
1698 &iio_dev_attr_in_temp_offset
.dev_attr
.attr
,
1699 &iio_dev_attr_ex_temp_offset
.dev_attr
.attr
,
1700 &iio_dev_attr_in_analog_temp_offset
.dev_attr
.attr
,
1701 &iio_dev_attr_ex_analog_temp_offset
.dev_attr
.attr
,
1702 &iio_dev_attr_DAC_A
.dev_attr
.attr
,
1703 &iio_dev_attr_DAC_B
.dev_attr
.attr
,
1704 &iio_dev_attr_DAC_C
.dev_attr
.attr
,
1705 &iio_dev_attr_DAC_D
.dev_attr
.attr
,
1706 &iio_dev_attr_device_id
.dev_attr
.attr
,
1707 &iio_dev_attr_manufactorer_id
.dev_attr
.attr
,
1708 &iio_dev_attr_device_rev
.dev_attr
.attr
,
1709 &iio_dev_attr_bus_type
.dev_attr
.attr
,
1713 static const struct attribute_group adt7316_attribute_group
= {
1714 .attrs
= adt7316_attributes
,
1717 static struct attribute
*adt7516_attributes
[] = {
1718 &iio_dev_attr_all_modes
.dev_attr
.attr
,
1719 &iio_dev_attr_mode
.dev_attr
.attr
,
1720 &iio_dev_attr_select_ex_temp
.dev_attr
.attr
,
1721 &iio_dev_attr_reset
.dev_attr
.attr
,
1722 &iio_dev_attr_enabled
.dev_attr
.attr
,
1723 &iio_dev_attr_ad_channel
.dev_attr
.attr
,
1724 &iio_dev_attr_all_ad_channels
.dev_attr
.attr
,
1725 &iio_dev_attr_disable_averaging
.dev_attr
.attr
,
1726 &iio_dev_attr_enable_smbus_timeout
.dev_attr
.attr
,
1727 &iio_dev_attr_powerdown
.dev_attr
.attr
,
1728 &iio_dev_attr_fast_ad_clock
.dev_attr
.attr
,
1729 &iio_dev_attr_AIN_internal_Vref
.dev_attr
.attr
,
1730 &iio_dev_attr_da_high_resolution
.dev_attr
.attr
,
1731 &iio_dev_attr_enable_proportion_DACA
.dev_attr
.attr
,
1732 &iio_dev_attr_enable_proportion_DACB
.dev_attr
.attr
,
1733 &iio_dev_attr_DAC_2Vref_channels_mask
.dev_attr
.attr
,
1734 &iio_dev_attr_DAC_update_mode
.dev_attr
.attr
,
1735 &iio_dev_attr_all_DAC_update_modes
.dev_attr
.attr
,
1736 &iio_dev_attr_update_DAC
.dev_attr
.attr
,
1737 &iio_dev_attr_DA_AB_Vref_bypass
.dev_attr
.attr
,
1738 &iio_dev_attr_DA_CD_Vref_bypass
.dev_attr
.attr
,
1739 &iio_dev_attr_DAC_internal_Vref
.dev_attr
.attr
,
1740 &iio_dev_attr_VDD
.dev_attr
.attr
,
1741 &iio_dev_attr_in_temp
.dev_attr
.attr
,
1742 &iio_dev_attr_ex_temp_AIN1
.dev_attr
.attr
,
1743 &iio_dev_attr_AIN2
.dev_attr
.attr
,
1744 &iio_dev_attr_AIN3
.dev_attr
.attr
,
1745 &iio_dev_attr_AIN4
.dev_attr
.attr
,
1746 &iio_dev_attr_in_temp_offset
.dev_attr
.attr
,
1747 &iio_dev_attr_ex_temp_offset
.dev_attr
.attr
,
1748 &iio_dev_attr_in_analog_temp_offset
.dev_attr
.attr
,
1749 &iio_dev_attr_ex_analog_temp_offset
.dev_attr
.attr
,
1750 &iio_dev_attr_DAC_A
.dev_attr
.attr
,
1751 &iio_dev_attr_DAC_B
.dev_attr
.attr
,
1752 &iio_dev_attr_DAC_C
.dev_attr
.attr
,
1753 &iio_dev_attr_DAC_D
.dev_attr
.attr
,
1754 &iio_dev_attr_device_id
.dev_attr
.attr
,
1755 &iio_dev_attr_manufactorer_id
.dev_attr
.attr
,
1756 &iio_dev_attr_device_rev
.dev_attr
.attr
,
1757 &iio_dev_attr_bus_type
.dev_attr
.attr
,
1761 static const struct attribute_group adt7516_attribute_group
= {
1762 .attrs
= adt7516_attributes
,
1765 static irqreturn_t
adt7316_event_handler(int irq
, void *private)
1767 struct iio_dev
*indio_dev
= private;
1768 struct adt7316_chip_info
*chip
= iio_priv(indio_dev
);
1773 ret
= chip
->bus
.read(chip
->bus
.client
, ADT7316_INT_STAT1
, &stat1
);
1775 if ((chip
->id
& ID_FAMILY_MASK
) != ID_ADT75XX
)
1778 time
= iio_get_time_ns();
1779 if (stat1
& (1 << 0))
1780 iio_push_event(indio_dev
,
1781 IIO_UNMOD_EVENT_CODE(IIO_TEMP
, 0,
1785 if (stat1
& (1 << 1))
1786 iio_push_event(indio_dev
,
1787 IIO_UNMOD_EVENT_CODE(IIO_TEMP
, 0,
1789 IIO_EV_DIR_FALLING
),
1791 if (stat1
& (1 << 2))
1792 iio_push_event(indio_dev
,
1793 IIO_UNMOD_EVENT_CODE(IIO_TEMP
, 1,
1797 if (stat1
& (1 << 3))
1798 iio_push_event(indio_dev
,
1799 IIO_UNMOD_EVENT_CODE(IIO_TEMP
, 1,
1801 IIO_EV_DIR_FALLING
),
1803 if (stat1
& (1 << 5))
1804 iio_push_event(indio_dev
,
1805 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE
, 1,
1809 if (stat1
& (1 << 6))
1810 iio_push_event(indio_dev
,
1811 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE
, 2,
1815 if (stat1
& (1 << 7))
1816 iio_push_event(indio_dev
,
1817 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE
, 3,
1822 ret
= chip
->bus
.read(chip
->bus
.client
, ADT7316_INT_STAT2
, &stat2
);
1824 if (stat2
& ADT7316_INT_MASK2_VDD
)
1825 iio_push_event(indio_dev
,
1826 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE
,
1837 * Show mask of enabled interrupts in Hex.
1839 static ssize_t
adt7316_show_int_mask(struct device
*dev
,
1840 struct device_attribute
*attr
,
1843 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1844 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1846 return sprintf(buf
, "0x%x\n", chip
->int_mask
);
1850 * Set 1 to the mask in Hex to enabled interrupts.
1852 static ssize_t
adt7316_set_int_mask(struct device
*dev
,
1853 struct device_attribute
*attr
,
1857 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1858 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1863 ret
= strict_strtoul(buf
, 16, &data
);
1864 if (ret
|| data
>= ADT7316_VDD_INT_MASK
+ 1)
1867 if (data
& ADT7316_VDD_INT_MASK
)
1868 mask
= 0; /* enable vdd int */
1870 mask
= ADT7316_INT_MASK2_VDD
; /* disable vdd int */
1872 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_INT_MASK2
, mask
);
1874 chip
->int_mask
&= ~ADT7316_VDD_INT_MASK
;
1875 chip
->int_mask
|= data
& ADT7316_VDD_INT_MASK
;
1878 if (data
& ADT7316_TEMP_AIN_INT_MASK
) {
1879 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT73XX
)
1880 /* mask in reg is opposite, set 1 to disable */
1881 mask
= (~data
) & ADT7316_TEMP_INT_MASK
;
1883 /* mask in reg is opposite, set 1 to disable */
1884 mask
= (~data
) & ADT7316_TEMP_AIN_INT_MASK
;
1886 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_INT_MASK1
, mask
);
1888 chip
->int_mask
= mask
;
1892 static inline ssize_t
adt7316_show_ad_bound(struct device
*dev
,
1893 struct device_attribute
*attr
,
1896 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
1897 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1898 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1903 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT73XX
&&
1904 this_attr
->address
> ADT7316_EX_TEMP_LOW
)
1907 ret
= chip
->bus
.read(chip
->bus
.client
, this_attr
->address
, &val
);
1913 if (!((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
&&
1914 (chip
->config1
& ADT7516_SEL_AIN1_2_EX_TEMP_MASK
) == 0)) {
1919 return sprintf(buf
, "%d\n", data
);
1922 static inline ssize_t
adt7316_set_ad_bound(struct device
*dev
,
1923 struct device_attribute
*attr
,
1927 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
1928 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1929 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1934 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT73XX
&&
1935 this_attr
->address
> ADT7316_EX_TEMP_LOW
)
1938 ret
= strict_strtol(buf
, 10, &data
);
1942 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
&&
1943 (chip
->config1
& ADT7516_SEL_AIN1_2_EX_TEMP_MASK
) == 0) {
1944 if (data
> 255 || data
< 0)
1947 if (data
> 127 || data
< -128)
1956 ret
= chip
->bus
.write(chip
->bus
.client
, this_attr
->address
, val
);
1963 static ssize_t
adt7316_show_int_enabled(struct device
*dev
,
1964 struct device_attribute
*attr
,
1967 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1968 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1970 return sprintf(buf
, "%d\n", !!(chip
->config1
& ADT7316_INT_EN
));
1973 static ssize_t
adt7316_set_int_enabled(struct device
*dev
,
1974 struct device_attribute
*attr
,
1978 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
1979 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1983 config1
= chip
->config1
& (~ADT7316_INT_EN
);
1984 if (!memcmp(buf
, "1", 1))
1985 config1
|= ADT7316_INT_EN
;
1987 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG1
, config1
);
1991 chip
->config1
= config1
;
1996 static IIO_DEVICE_ATTR(int_mask
,
1998 adt7316_show_int_mask
, adt7316_set_int_mask
,
2000 static IIO_DEVICE_ATTR(in_temp_high_value
,
2002 adt7316_show_ad_bound
, adt7316_set_ad_bound
,
2003 ADT7316_IN_TEMP_HIGH
);
2004 static IIO_DEVICE_ATTR(in_temp_low_value
,
2006 adt7316_show_ad_bound
, adt7316_set_ad_bound
,
2007 ADT7316_IN_TEMP_LOW
);
2008 static IIO_DEVICE_ATTR(ex_temp_high_value
,
2010 adt7316_show_ad_bound
, adt7316_set_ad_bound
,
2011 ADT7316_EX_TEMP_HIGH
);
2012 static IIO_DEVICE_ATTR(ex_temp_low_value
,
2014 adt7316_show_ad_bound
, adt7316_set_ad_bound
,
2015 ADT7316_EX_TEMP_LOW
);
2017 /* NASTY duplication to be fixed */
2018 static IIO_DEVICE_ATTR(ex_temp_ain1_high_value
,
2020 adt7316_show_ad_bound
, adt7316_set_ad_bound
,
2021 ADT7316_EX_TEMP_HIGH
);
2022 static IIO_DEVICE_ATTR(ex_temp_ain1_low_value
,
2024 adt7316_show_ad_bound
, adt7316_set_ad_bound
,
2025 ADT7316_EX_TEMP_LOW
);
2026 static IIO_DEVICE_ATTR(ain2_high_value
,
2028 adt7316_show_ad_bound
, adt7316_set_ad_bound
,
2030 static IIO_DEVICE_ATTR(ain2_low_value
,
2032 adt7316_show_ad_bound
, adt7316_set_ad_bound
,
2034 static IIO_DEVICE_ATTR(ain3_high_value
,
2036 adt7316_show_ad_bound
, adt7316_set_ad_bound
,
2038 static IIO_DEVICE_ATTR(ain3_low_value
,
2040 adt7316_show_ad_bound
, adt7316_set_ad_bound
,
2042 static IIO_DEVICE_ATTR(ain4_high_value
,
2044 adt7316_show_ad_bound
, adt7316_set_ad_bound
,
2046 static IIO_DEVICE_ATTR(ain4_low_value
,
2048 adt7316_show_ad_bound
, adt7316_set_ad_bound
,
2050 static IIO_DEVICE_ATTR(int_enabled
,
2052 adt7316_show_int_enabled
,
2053 adt7316_set_int_enabled
, 0);
2055 static struct attribute
*adt7316_event_attributes
[] = {
2056 &iio_dev_attr_int_mask
.dev_attr
.attr
,
2057 &iio_dev_attr_in_temp_high_value
.dev_attr
.attr
,
2058 &iio_dev_attr_in_temp_low_value
.dev_attr
.attr
,
2059 &iio_dev_attr_ex_temp_high_value
.dev_attr
.attr
,
2060 &iio_dev_attr_ex_temp_low_value
.dev_attr
.attr
,
2061 &iio_dev_attr_int_enabled
.dev_attr
.attr
,
2065 static struct attribute_group adt7316_event_attribute_group
= {
2066 .attrs
= adt7316_event_attributes
,
2070 static struct attribute
*adt7516_event_attributes
[] = {
2071 &iio_dev_attr_int_mask
.dev_attr
.attr
,
2072 &iio_dev_attr_in_temp_high_value
.dev_attr
.attr
,
2073 &iio_dev_attr_in_temp_low_value
.dev_attr
.attr
,
2074 &iio_dev_attr_ex_temp_ain1_high_value
.dev_attr
.attr
,
2075 &iio_dev_attr_ex_temp_ain1_low_value
.dev_attr
.attr
,
2076 &iio_dev_attr_ain2_high_value
.dev_attr
.attr
,
2077 &iio_dev_attr_ain2_low_value
.dev_attr
.attr
,
2078 &iio_dev_attr_ain3_high_value
.dev_attr
.attr
,
2079 &iio_dev_attr_ain3_low_value
.dev_attr
.attr
,
2080 &iio_dev_attr_ain4_high_value
.dev_attr
.attr
,
2081 &iio_dev_attr_ain4_low_value
.dev_attr
.attr
,
2082 &iio_dev_attr_int_enabled
.dev_attr
.attr
,
2086 static struct attribute_group adt7516_event_attribute_group
= {
2087 .attrs
= adt7516_event_attributes
,
2092 int adt7316_disable(struct device
*dev
)
2094 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
2095 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
2097 return _adt7316_store_enabled(chip
, 0);
2099 EXPORT_SYMBOL(adt7316_disable
);
2101 int adt7316_enable(struct device
*dev
)
2103 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
2104 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
2106 return _adt7316_store_enabled(chip
, 1);
2108 EXPORT_SYMBOL(adt7316_enable
);
2111 static const struct iio_info adt7316_info
= {
2112 .attrs
= &adt7316_attribute_group
,
2113 .event_attrs
= &adt7316_event_attribute_group
,
2114 .driver_module
= THIS_MODULE
,
2117 static const struct iio_info adt7516_info
= {
2118 .attrs
= &adt7516_attribute_group
,
2119 .event_attrs
= &adt7516_event_attribute_group
,
2120 .driver_module
= THIS_MODULE
,
2124 * device probe and remove
2126 int __devinit
adt7316_probe(struct device
*dev
, struct adt7316_bus
*bus
,
2129 struct adt7316_chip_info
*chip
;
2130 struct iio_dev
*indio_dev
;
2131 unsigned short *adt7316_platform_data
= dev
->platform_data
;
2134 indio_dev
= iio_allocate_device(sizeof(*chip
));
2135 if (indio_dev
== NULL
) {
2139 chip
= iio_priv(indio_dev
);
2140 /* this is only used for device removal purposes */
2141 dev_set_drvdata(dev
, indio_dev
);
2146 chip
->id
= ID_ADT7316
+ (name
[6] - '6');
2147 else if (name
[4] == '5')
2148 chip
->id
= ID_ADT7516
+ (name
[6] - '6');
2152 chip
->ldac_pin
= adt7316_platform_data
[1];
2153 if (chip
->ldac_pin
) {
2154 chip
->config3
|= ADT7316_DA_EN_VIA_DAC_LDCA
;
2155 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
)
2156 chip
->config1
|= ADT7516_SEL_AIN3
;
2158 chip
->int_mask
= ADT7316_TEMP_INT_MASK
| ADT7316_VDD_INT_MASK
;
2159 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
)
2160 chip
->int_mask
|= ADT7516_AIN_INT_MASK
;
2162 indio_dev
->dev
.parent
= dev
;
2163 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
)
2164 indio_dev
->info
= &adt7516_info
;
2166 indio_dev
->info
= &adt7316_info
;
2167 indio_dev
->name
= name
;
2168 indio_dev
->modes
= INDIO_DIRECT_MODE
;
2170 if (chip
->bus
.irq
> 0) {
2171 if (adt7316_platform_data
[0])
2172 chip
->bus
.irq_flags
= adt7316_platform_data
[0];
2174 ret
= request_threaded_irq(chip
->bus
.irq
,
2176 &adt7316_event_handler
,
2177 chip
->bus
.irq_flags
| IRQF_ONESHOT
,
2181 goto error_free_dev
;
2183 if (chip
->bus
.irq_flags
& IRQF_TRIGGER_HIGH
)
2184 chip
->config1
|= ADT7316_INT_POLARITY
;
2187 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG1
, chip
->config1
);
2190 goto error_unreg_irq
;
2193 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG3
, chip
->config3
);
2196 goto error_unreg_irq
;
2199 ret
= iio_device_register(indio_dev
);
2201 goto error_unreg_irq
;
2203 dev_info(dev
, "%s temperature sensor, ADC and DAC registered.\n",
2209 free_irq(chip
->bus
.irq
, indio_dev
);
2211 iio_free_device(indio_dev
);
2215 EXPORT_SYMBOL(adt7316_probe
);
2217 int __devexit
adt7316_remove(struct device
*dev
)
2219 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
2220 struct adt7316_chip_info
*chip
= iio_priv(indio_dev
);
2222 iio_device_unregister(indio_dev
);
2224 free_irq(chip
->bus
.irq
, indio_dev
);
2225 iio_free_device(indio_dev
);
2229 EXPORT_SYMBOL(adt7316_remove
);
2231 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
2232 MODULE_DESCRIPTION("Analog Devices ADT7316/7/8 and ADT7516/7/9 digital"
2233 " temperature sensor, ADC and DAC driver");
2234 MODULE_LICENSE("GPL v2");