2 * ADT7316 digital temperature sensor driver supporting ADT7316/7/8 ADT7516/7/9
5 * Copyright 2010 Analog Devices Inc.
7 * Licensed under the GPL-2 or later.
10 #include <linux/interrupt.h>
11 #include <linux/gpio.h>
12 #include <linux/workqueue.h>
13 #include <linux/device.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/sysfs.h>
17 #include <linux/list.h>
18 #include <linux/i2c.h>
19 #include <linux/rtc.h>
20 #include <linux/module.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/events.h>
24 #include <linux/iio/sysfs.h>
28 * ADT7316 registers definition
30 #define ADT7316_INT_STAT1 0x0
31 #define ADT7316_INT_STAT2 0x1
32 #define ADT7316_LSB_IN_TEMP_VDD 0x3
33 #define ADT7316_LSB_IN_TEMP_MASK 0x3
34 #define ADT7316_LSB_VDD_MASK 0xC
35 #define ADT7316_LSB_VDD_OFFSET 2
36 #define ADT7316_LSB_EX_TEMP_AIN 0x4
37 #define ADT7316_LSB_EX_TEMP_MASK 0x3
38 #define ADT7516_LSB_AIN_SHIFT 2
39 #define ADT7316_AD_MSB_DATA_BASE 0x6
40 #define ADT7316_AD_MSB_DATA_REGS 3
41 #define ADT7516_AD_MSB_DATA_REGS 6
42 #define ADT7316_MSB_VDD 0x6
43 #define ADT7316_MSB_IN_TEMP 0x7
44 #define ADT7316_MSB_EX_TEMP 0x8
45 #define ADT7516_MSB_AIN1 0x8
46 #define ADT7516_MSB_AIN2 0x9
47 #define ADT7516_MSB_AIN3 0xA
48 #define ADT7516_MSB_AIN4 0xB
49 #define ADT7316_DA_DATA_BASE 0x10
50 #define ADT7316_DA_MSB_DATA_REGS 4
51 #define ADT7316_LSB_DAC_A 0x10
52 #define ADT7316_MSB_DAC_A 0x11
53 #define ADT7316_LSB_DAC_B 0x12
54 #define ADT7316_MSB_DAC_B 0x13
55 #define ADT7316_LSB_DAC_C 0x14
56 #define ADT7316_MSB_DAC_C 0x15
57 #define ADT7316_LSB_DAC_D 0x16
58 #define ADT7316_MSB_DAC_D 0x17
59 #define ADT7316_CONFIG1 0x18
60 #define ADT7316_CONFIG2 0x19
61 #define ADT7316_CONFIG3 0x1A
62 #define ADT7316_LDAC_CONFIG 0x1B
63 #define ADT7316_DAC_CONFIG 0x1C
64 #define ADT7316_INT_MASK1 0x1D
65 #define ADT7316_INT_MASK2 0x1E
66 #define ADT7316_IN_TEMP_OFFSET 0x1F
67 #define ADT7316_EX_TEMP_OFFSET 0x20
68 #define ADT7316_IN_ANALOG_TEMP_OFFSET 0x21
69 #define ADT7316_EX_ANALOG_TEMP_OFFSET 0x22
70 #define ADT7316_VDD_HIGH 0x23
71 #define ADT7316_VDD_LOW 0x24
72 #define ADT7316_IN_TEMP_HIGH 0x25
73 #define ADT7316_IN_TEMP_LOW 0x26
74 #define ADT7316_EX_TEMP_HIGH 0x27
75 #define ADT7316_EX_TEMP_LOW 0x28
76 #define ADT7516_AIN2_HIGH 0x2B
77 #define ADT7516_AIN2_LOW 0x2C
78 #define ADT7516_AIN3_HIGH 0x2D
79 #define ADT7516_AIN3_LOW 0x2E
80 #define ADT7516_AIN4_HIGH 0x2F
81 #define ADT7516_AIN4_LOW 0x30
82 #define ADT7316_DEVICE_ID 0x4D
83 #define ADT7316_MANUFACTURE_ID 0x4E
84 #define ADT7316_DEVICE_REV 0x4F
85 #define ADT7316_SPI_LOCK_STAT 0x7F
90 #define ADT7316_EN 0x1
91 #define ADT7516_SEL_EX_TEMP 0x4
92 #define ADT7516_SEL_AIN1_2_EX_TEMP_MASK 0x6
93 #define ADT7516_SEL_AIN3 0x8
94 #define ADT7316_INT_EN 0x20
95 #define ADT7316_INT_POLARITY 0x40
96 #define ADT7316_PD 0x80
101 #define ADT7316_AD_SINGLE_CH_MASK 0x3
102 #define ADT7516_AD_SINGLE_CH_MASK 0x7
103 #define ADT7316_AD_SINGLE_CH_VDD 0
104 #define ADT7316_AD_SINGLE_CH_IN 1
105 #define ADT7316_AD_SINGLE_CH_EX 2
106 #define ADT7516_AD_SINGLE_CH_AIN1 2
107 #define ADT7516_AD_SINGLE_CH_AIN2 3
108 #define ADT7516_AD_SINGLE_CH_AIN3 4
109 #define ADT7516_AD_SINGLE_CH_AIN4 5
110 #define ADT7316_AD_SINGLE_CH_MODE 0x10
111 #define ADT7316_DISABLE_AVERAGING 0x20
112 #define ADT7316_EN_SMBUS_TIMEOUT 0x40
113 #define ADT7316_RESET 0x80
118 #define ADT7316_ADCLK_22_5 0x1
119 #define ADT7316_DA_HIGH_RESOLUTION 0x2
120 #define ADT7316_DA_EN_VIA_DAC_LDCA 0x4
121 #define ADT7516_AIN_IN_VREF 0x10
122 #define ADT7316_EN_IN_TEMP_PROP_DACA 0x20
123 #define ADT7316_EN_EX_TEMP_PROP_DACB 0x40
128 #define ADT7316_DA_2VREF_CH_MASK 0xF
129 #define ADT7316_DA_EN_MODE_MASK 0x30
130 #define ADT7316_DA_EN_MODE_SINGLE 0x00
131 #define ADT7316_DA_EN_MODE_AB_CD 0x10
132 #define ADT7316_DA_EN_MODE_ABCD 0x20
133 #define ADT7316_DA_EN_MODE_LDAC 0x30
134 #define ADT7316_VREF_BYPASS_DAC_AB 0x40
135 #define ADT7316_VREF_BYPASS_DAC_CD 0x80
138 * ADT7316 LDAC config
140 #define ADT7316_LDAC_EN_DA_MASK 0xF
141 #define ADT7316_DAC_IN_VREF 0x10
142 #define ADT7516_DAC_AB_IN_VREF 0x10
143 #define ADT7516_DAC_CD_IN_VREF 0x20
144 #define ADT7516_DAC_IN_VREF_OFFSET 4
145 #define ADT7516_DAC_IN_VREF_MASK 0x30
150 #define ADT7316_INT_MASK2_VDD 0x10
153 * ADT7316 value masks
155 #define ADT7316_VALUE_MASK 0xfff
156 #define ADT7316_T_VALUE_SIGN 0x400
157 #define ADT7316_T_VALUE_FLOAT_OFFSET 2
158 #define ADT7316_T_VALUE_FLOAT_MASK 0x2
163 #define ID_ADT7316 0x1
164 #define ID_ADT7317 0x2
165 #define ID_ADT7318 0x3
166 #define ID_ADT7516 0x11
167 #define ID_ADT7517 0x12
168 #define ID_ADT7519 0x14
170 #define ID_FAMILY_MASK 0xF0
171 #define ID_ADT73XX 0x0
172 #define ID_ADT75XX 0x10
175 * struct adt7316_chip_info - chip specifc information
178 struct adt7316_chip_info
{
179 struct adt7316_bus bus
;
181 u16 int_mask
; /* 0x2f */
185 u8 dac_config
; /* DAC config */
186 u8 ldac_config
; /* LDAC config */
187 u8 dac_bits
; /* 8, 10, 12 */
192 * Logic interrupt mask for user application to enable
195 #define ADT7316_IN_TEMP_HIGH_INT_MASK 0x1
196 #define ADT7316_IN_TEMP_LOW_INT_MASK 0x2
197 #define ADT7316_EX_TEMP_HIGH_INT_MASK 0x4
198 #define ADT7316_EX_TEMP_LOW_INT_MASK 0x8
199 #define ADT7316_EX_TEMP_FAULT_INT_MASK 0x10
200 #define ADT7516_AIN1_INT_MASK 0x4
201 #define ADT7516_AIN2_INT_MASK 0x20
202 #define ADT7516_AIN3_INT_MASK 0x40
203 #define ADT7516_AIN4_INT_MASK 0x80
204 #define ADT7316_VDD_INT_MASK 0x100
205 #define ADT7316_TEMP_INT_MASK 0x1F
206 #define ADT7516_AIN_INT_MASK 0xE0
207 #define ADT7316_TEMP_AIN_INT_MASK \
208 (ADT7316_TEMP_INT_MASK)
211 * struct adt7316_chip_info - chip specifc information
214 struct adt7316_limit_regs
{
219 static ssize_t
adt7316_show_enabled(struct device
*dev
,
220 struct device_attribute
*attr
,
223 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
224 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
226 return sprintf(buf
, "%d\n", !!(chip
->config1
& ADT7316_EN
));
229 static ssize_t
_adt7316_store_enabled(struct adt7316_chip_info
*chip
,
236 config1
= chip
->config1
| ADT7316_EN
;
238 config1
= chip
->config1
& ~ADT7316_EN
;
240 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG1
, config1
);
244 chip
->config1
= config1
;
250 static ssize_t
adt7316_store_enabled(struct device
*dev
,
251 struct device_attribute
*attr
,
255 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
256 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
259 if (!memcmp(buf
, "1", 1))
264 if (_adt7316_store_enabled(chip
, enable
) < 0)
270 static IIO_DEVICE_ATTR(enabled
, S_IRUGO
| S_IWUSR
,
271 adt7316_show_enabled
,
272 adt7316_store_enabled
,
275 static ssize_t
adt7316_show_select_ex_temp(struct device
*dev
,
276 struct device_attribute
*attr
,
279 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
280 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
282 if ((chip
->id
& ID_FAMILY_MASK
) != ID_ADT75XX
)
285 return sprintf(buf
, "%d\n", !!(chip
->config1
& ADT7516_SEL_EX_TEMP
));
288 static ssize_t
adt7316_store_select_ex_temp(struct device
*dev
,
289 struct device_attribute
*attr
,
293 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
294 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
298 if ((chip
->id
& ID_FAMILY_MASK
) != ID_ADT75XX
)
301 config1
= chip
->config1
& (~ADT7516_SEL_EX_TEMP
);
302 if (!memcmp(buf
, "1", 1))
303 config1
|= ADT7516_SEL_EX_TEMP
;
305 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG1
, config1
);
309 chip
->config1
= config1
;
314 static IIO_DEVICE_ATTR(select_ex_temp
, S_IRUGO
| S_IWUSR
,
315 adt7316_show_select_ex_temp
,
316 adt7316_store_select_ex_temp
,
319 static ssize_t
adt7316_show_mode(struct device
*dev
,
320 struct device_attribute
*attr
,
323 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
324 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
326 if (chip
->config2
& ADT7316_AD_SINGLE_CH_MODE
)
327 return sprintf(buf
, "single_channel\n");
329 return sprintf(buf
, "round_robin\n");
332 static ssize_t
adt7316_store_mode(struct device
*dev
,
333 struct device_attribute
*attr
,
337 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
338 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
342 config2
= chip
->config2
& (~ADT7316_AD_SINGLE_CH_MODE
);
343 if (!memcmp(buf
, "single_channel", 14))
344 config2
|= ADT7316_AD_SINGLE_CH_MODE
;
346 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG2
, config2
);
350 chip
->config2
= config2
;
355 static IIO_DEVICE_ATTR(mode
, S_IRUGO
| S_IWUSR
,
360 static ssize_t
adt7316_show_all_modes(struct device
*dev
,
361 struct device_attribute
*attr
,
364 return sprintf(buf
, "single_channel\nround_robin\n");
367 static IIO_DEVICE_ATTR(all_modes
, S_IRUGO
, adt7316_show_all_modes
, NULL
, 0);
369 static ssize_t
adt7316_show_ad_channel(struct device
*dev
,
370 struct device_attribute
*attr
,
373 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
374 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
376 if (!(chip
->config2
& ADT7316_AD_SINGLE_CH_MODE
))
379 switch (chip
->config2
& ADT7516_AD_SINGLE_CH_MASK
) {
380 case ADT7316_AD_SINGLE_CH_VDD
:
381 return sprintf(buf
, "0 - VDD\n");
382 case ADT7316_AD_SINGLE_CH_IN
:
383 return sprintf(buf
, "1 - Internal Temperature\n");
384 case ADT7316_AD_SINGLE_CH_EX
:
385 if (((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
) &&
386 (chip
->config1
& ADT7516_SEL_AIN1_2_EX_TEMP_MASK
) == 0)
387 return sprintf(buf
, "2 - AIN1\n");
389 return sprintf(buf
, "2 - External Temperature\n");
390 case ADT7516_AD_SINGLE_CH_AIN2
:
391 if ((chip
->config1
& ADT7516_SEL_AIN1_2_EX_TEMP_MASK
) == 0)
392 return sprintf(buf
, "3 - AIN2\n");
394 return sprintf(buf
, "N/A\n");
395 case ADT7516_AD_SINGLE_CH_AIN3
:
396 if (chip
->config1
& ADT7516_SEL_AIN3
)
397 return sprintf(buf
, "4 - AIN3\n");
399 return sprintf(buf
, "N/A\n");
400 case ADT7516_AD_SINGLE_CH_AIN4
:
401 return sprintf(buf
, "5 - AIN4\n");
403 return sprintf(buf
, "N/A\n");
407 static ssize_t
adt7316_store_ad_channel(struct device
*dev
,
408 struct device_attribute
*attr
,
412 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
413 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
415 unsigned long data
= 0;
418 if (!(chip
->config2
& ADT7316_AD_SINGLE_CH_MODE
))
421 ret
= strict_strtoul(buf
, 10, &data
);
425 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
) {
429 config2
= chip
->config2
& (~ADT7516_AD_SINGLE_CH_MASK
);
434 config2
= chip
->config2
& (~ADT7316_AD_SINGLE_CH_MASK
);
440 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG2
, config2
);
444 chip
->config2
= config2
;
449 static IIO_DEVICE_ATTR(ad_channel
, S_IRUGO
| S_IWUSR
,
450 adt7316_show_ad_channel
,
451 adt7316_store_ad_channel
,
454 static ssize_t
adt7316_show_all_ad_channels(struct device
*dev
,
455 struct device_attribute
*attr
,
458 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
459 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
461 if (!(chip
->config2
& ADT7316_AD_SINGLE_CH_MODE
))
464 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
)
465 return sprintf(buf
, "0 - VDD\n1 - Internal Temperature\n"
466 "2 - External Temperature or AIN1\n"
467 "3 - AIN2\n4 - AIN3\n5 - AIN4\n");
469 return sprintf(buf
, "0 - VDD\n1 - Internal Temperature\n"
470 "2 - External Temperature\n");
473 static IIO_DEVICE_ATTR(all_ad_channels
, S_IRUGO
,
474 adt7316_show_all_ad_channels
, NULL
, 0);
476 static ssize_t
adt7316_show_disable_averaging(struct device
*dev
,
477 struct device_attribute
*attr
,
480 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
481 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
483 return sprintf(buf
, "%d\n",
484 !!(chip
->config2
& ADT7316_DISABLE_AVERAGING
));
487 static ssize_t
adt7316_store_disable_averaging(struct device
*dev
,
488 struct device_attribute
*attr
,
492 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
493 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
497 config2
= chip
->config2
& (~ADT7316_DISABLE_AVERAGING
);
498 if (!memcmp(buf
, "1", 1))
499 config2
|= ADT7316_DISABLE_AVERAGING
;
501 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG2
, config2
);
505 chip
->config2
= config2
;
510 static IIO_DEVICE_ATTR(disable_averaging
, S_IRUGO
| S_IWUSR
,
511 adt7316_show_disable_averaging
,
512 adt7316_store_disable_averaging
,
515 static ssize_t
adt7316_show_enable_smbus_timeout(struct device
*dev
,
516 struct device_attribute
*attr
,
519 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
520 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
522 return sprintf(buf
, "%d\n",
523 !!(chip
->config2
& ADT7316_EN_SMBUS_TIMEOUT
));
526 static ssize_t
adt7316_store_enable_smbus_timeout(struct device
*dev
,
527 struct device_attribute
*attr
,
531 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
532 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
536 config2
= chip
->config2
& (~ADT7316_EN_SMBUS_TIMEOUT
);
537 if (!memcmp(buf
, "1", 1))
538 config2
|= ADT7316_EN_SMBUS_TIMEOUT
;
540 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG2
, config2
);
544 chip
->config2
= config2
;
549 static IIO_DEVICE_ATTR(enable_smbus_timeout
, S_IRUGO
| S_IWUSR
,
550 adt7316_show_enable_smbus_timeout
,
551 adt7316_store_enable_smbus_timeout
,
555 static ssize_t
adt7316_store_reset(struct device
*dev
,
556 struct device_attribute
*attr
,
560 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
561 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
565 config2
= chip
->config2
| ADT7316_RESET
;
567 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG2
, config2
);
574 static IIO_DEVICE_ATTR(reset
, S_IWUSR
,
579 static ssize_t
adt7316_show_powerdown(struct device
*dev
,
580 struct device_attribute
*attr
,
583 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
584 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
586 return sprintf(buf
, "%d\n", !!(chip
->config1
& ADT7316_PD
));
589 static ssize_t
adt7316_store_powerdown(struct device
*dev
,
590 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
);
599 config1
= chip
->config1
& (~ADT7316_PD
);
600 if (!memcmp(buf
, "1", 1))
601 config1
|= ADT7316_PD
;
603 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG1
, config1
);
607 chip
->config1
= config1
;
612 static IIO_DEVICE_ATTR(powerdown
, S_IRUGO
| S_IWUSR
,
613 adt7316_show_powerdown
,
614 adt7316_store_powerdown
,
617 static ssize_t
adt7316_show_fast_ad_clock(struct device
*dev
,
618 struct device_attribute
*attr
,
621 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
622 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
624 return sprintf(buf
, "%d\n", !!(chip
->config3
& ADT7316_ADCLK_22_5
));
627 static ssize_t
adt7316_store_fast_ad_clock(struct device
*dev
,
628 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
);
637 config3
= chip
->config3
& (~ADT7316_ADCLK_22_5
);
638 if (!memcmp(buf
, "1", 1))
639 config3
|= ADT7316_ADCLK_22_5
;
641 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG3
, config3
);
645 chip
->config3
= config3
;
650 static IIO_DEVICE_ATTR(fast_ad_clock
, S_IRUGO
| S_IWUSR
,
651 adt7316_show_fast_ad_clock
,
652 adt7316_store_fast_ad_clock
,
655 static ssize_t
adt7316_show_da_high_resolution(struct device
*dev
,
656 struct device_attribute
*attr
,
659 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
660 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
662 if (chip
->config3
& ADT7316_DA_HIGH_RESOLUTION
) {
663 if (chip
->id
== ID_ADT7316
|| chip
->id
== ID_ADT7516
)
664 return sprintf(buf
, "1 (12 bits)\n");
665 else if (chip
->id
== ID_ADT7317
|| chip
->id
== ID_ADT7517
)
666 return sprintf(buf
, "1 (10 bits)\n");
669 return sprintf(buf
, "0 (8 bits)\n");
672 static ssize_t
adt7316_store_da_high_resolution(struct device
*dev
,
673 struct device_attribute
*attr
,
677 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
678 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
684 if (!memcmp(buf
, "1", 1)) {
685 config3
= chip
->config3
| ADT7316_DA_HIGH_RESOLUTION
;
686 if (chip
->id
== ID_ADT7316
|| chip
->id
== ID_ADT7516
)
688 else if (chip
->id
== ID_ADT7317
|| chip
->id
== ID_ADT7517
)
691 config3
= chip
->config3
& (~ADT7316_DA_HIGH_RESOLUTION
);
693 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG3
, config3
);
697 chip
->config3
= config3
;
702 static IIO_DEVICE_ATTR(da_high_resolution
, S_IRUGO
| S_IWUSR
,
703 adt7316_show_da_high_resolution
,
704 adt7316_store_da_high_resolution
,
707 static ssize_t
adt7316_show_AIN_internal_Vref(struct device
*dev
,
708 struct device_attribute
*attr
,
711 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
712 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
714 if ((chip
->id
& ID_FAMILY_MASK
) != ID_ADT75XX
)
717 return sprintf(buf
, "%d\n",
718 !!(chip
->config3
& ADT7516_AIN_IN_VREF
));
721 static ssize_t
adt7316_store_AIN_internal_Vref(struct device
*dev
,
722 struct device_attribute
*attr
,
726 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
727 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
731 if ((chip
->id
& ID_FAMILY_MASK
) != ID_ADT75XX
)
734 if (memcmp(buf
, "1", 1))
735 config3
= chip
->config3
& (~ADT7516_AIN_IN_VREF
);
737 config3
= chip
->config3
| ADT7516_AIN_IN_VREF
;
739 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG3
, config3
);
743 chip
->config3
= config3
;
748 static IIO_DEVICE_ATTR(AIN_internal_Vref
, S_IRUGO
| S_IWUSR
,
749 adt7316_show_AIN_internal_Vref
,
750 adt7316_store_AIN_internal_Vref
,
754 static ssize_t
adt7316_show_enable_prop_DACA(struct device
*dev
,
755 struct device_attribute
*attr
,
758 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
759 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
761 return sprintf(buf
, "%d\n",
762 !!(chip
->config3
& ADT7316_EN_IN_TEMP_PROP_DACA
));
765 static ssize_t
adt7316_store_enable_prop_DACA(struct device
*dev
,
766 struct device_attribute
*attr
,
770 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
771 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
775 config3
= chip
->config3
& (~ADT7316_EN_IN_TEMP_PROP_DACA
);
776 if (!memcmp(buf
, "1", 1))
777 config3
|= ADT7316_EN_IN_TEMP_PROP_DACA
;
779 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG3
, config3
);
783 chip
->config3
= config3
;
788 static IIO_DEVICE_ATTR(enable_proportion_DACA
, S_IRUGO
| S_IWUSR
,
789 adt7316_show_enable_prop_DACA
,
790 adt7316_store_enable_prop_DACA
,
793 static ssize_t
adt7316_show_enable_prop_DACB(struct device
*dev
,
794 struct device_attribute
*attr
,
797 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
798 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
800 return sprintf(buf
, "%d\n",
801 !!(chip
->config3
& ADT7316_EN_EX_TEMP_PROP_DACB
));
804 static ssize_t
adt7316_store_enable_prop_DACB(struct device
*dev
,
805 struct device_attribute
*attr
,
809 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
810 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
814 config3
= chip
->config3
& (~ADT7316_EN_EX_TEMP_PROP_DACB
);
815 if (!memcmp(buf
, "1", 1))
816 config3
|= ADT7316_EN_EX_TEMP_PROP_DACB
;
818 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG3
, config3
);
822 chip
->config3
= config3
;
827 static IIO_DEVICE_ATTR(enable_proportion_DACB
, S_IRUGO
| S_IWUSR
,
828 adt7316_show_enable_prop_DACB
,
829 adt7316_store_enable_prop_DACB
,
832 static ssize_t
adt7316_show_DAC_2Vref_ch_mask(struct device
*dev
,
833 struct device_attribute
*attr
,
836 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
837 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
839 return sprintf(buf
, "0x%x\n",
840 chip
->dac_config
& ADT7316_DA_2VREF_CH_MASK
);
843 static ssize_t
adt7316_store_DAC_2Vref_ch_mask(struct device
*dev
,
844 struct device_attribute
*attr
,
848 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
849 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
851 unsigned long data
= 0;
854 ret
= strict_strtoul(buf
, 16, &data
);
855 if (ret
|| data
> ADT7316_DA_2VREF_CH_MASK
)
858 dac_config
= chip
->dac_config
& (~ADT7316_DA_2VREF_CH_MASK
);
861 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_DAC_CONFIG
, dac_config
);
865 chip
->dac_config
= dac_config
;
870 static IIO_DEVICE_ATTR(DAC_2Vref_channels_mask
, S_IRUGO
| S_IWUSR
,
871 adt7316_show_DAC_2Vref_ch_mask
,
872 adt7316_store_DAC_2Vref_ch_mask
,
875 static ssize_t
adt7316_show_DAC_update_mode(struct device
*dev
,
876 struct device_attribute
*attr
,
879 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
880 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
882 if (!(chip
->config3
& ADT7316_DA_EN_VIA_DAC_LDCA
))
883 return sprintf(buf
, "manual\n");
885 switch (chip
->dac_config
& ADT7316_DA_EN_MODE_MASK
) {
886 case ADT7316_DA_EN_MODE_SINGLE
:
887 return sprintf(buf
, "0 - auto at any MSB DAC writing\n");
888 case ADT7316_DA_EN_MODE_AB_CD
:
889 return sprintf(buf
, "1 - auto at MSB DAC AB and CD writing\n");
890 case ADT7316_DA_EN_MODE_ABCD
:
891 return sprintf(buf
, "2 - auto at MSB DAC ABCD writing\n");
892 default: /* ADT7316_DA_EN_MODE_LDAC */
893 return sprintf(buf
, "3 - manual\n");
898 static ssize_t
adt7316_store_DAC_update_mode(struct device
*dev
,
899 struct device_attribute
*attr
,
903 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
904 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
909 if (!(chip
->config3
& ADT7316_DA_EN_VIA_DAC_LDCA
))
912 ret
= strict_strtoul(buf
, 10, &data
);
913 if (ret
|| data
> ADT7316_DA_EN_MODE_MASK
)
916 dac_config
= chip
->dac_config
& (~ADT7316_DA_EN_MODE_MASK
);
919 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_DAC_CONFIG
, dac_config
);
923 chip
->dac_config
= dac_config
;
928 static IIO_DEVICE_ATTR(DAC_update_mode
, S_IRUGO
| S_IWUSR
,
929 adt7316_show_DAC_update_mode
,
930 adt7316_store_DAC_update_mode
,
933 static ssize_t
adt7316_show_all_DAC_update_modes(struct device
*dev
,
934 struct device_attribute
*attr
,
937 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
938 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
940 if (chip
->config3
& ADT7316_DA_EN_VIA_DAC_LDCA
)
941 return sprintf(buf
, "0 - auto at any MSB DAC writing\n"
942 "1 - auto at MSB DAC AB and CD writing\n"
943 "2 - auto at MSB DAC ABCD writing\n"
946 return sprintf(buf
, "manual\n");
949 static IIO_DEVICE_ATTR(all_DAC_update_modes
, S_IRUGO
,
950 adt7316_show_all_DAC_update_modes
, NULL
, 0);
953 static ssize_t
adt7316_store_update_DAC(struct device
*dev
,
954 struct device_attribute
*attr
,
958 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
959 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
964 if (chip
->config3
& ADT7316_DA_EN_VIA_DAC_LDCA
) {
965 if ((chip
->dac_config
& ADT7316_DA_EN_MODE_MASK
) !=
966 ADT7316_DA_EN_MODE_LDAC
)
969 ret
= strict_strtoul(buf
, 16, &data
);
970 if (ret
|| data
> ADT7316_LDAC_EN_DA_MASK
)
973 ldac_config
= chip
->ldac_config
& (~ADT7316_LDAC_EN_DA_MASK
);
976 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_LDAC_CONFIG
,
981 gpio_set_value(chip
->ldac_pin
, 0);
982 gpio_set_value(chip
->ldac_pin
, 1);
988 static IIO_DEVICE_ATTR(update_DAC
, S_IRUGO
| S_IWUSR
,
990 adt7316_store_update_DAC
,
993 static ssize_t
adt7316_show_DA_AB_Vref_bypass(struct device
*dev
,
994 struct device_attribute
*attr
,
997 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
998 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1000 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
)
1003 return sprintf(buf
, "%d\n",
1004 !!(chip
->dac_config
& ADT7316_VREF_BYPASS_DAC_AB
));
1007 static ssize_t
adt7316_store_DA_AB_Vref_bypass(struct device
*dev
,
1008 struct device_attribute
*attr
,
1012 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1013 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1017 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
)
1020 dac_config
= chip
->dac_config
& (~ADT7316_VREF_BYPASS_DAC_AB
);
1021 if (!memcmp(buf
, "1", 1))
1022 dac_config
|= ADT7316_VREF_BYPASS_DAC_AB
;
1024 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_DAC_CONFIG
, dac_config
);
1028 chip
->dac_config
= dac_config
;
1033 static IIO_DEVICE_ATTR(DA_AB_Vref_bypass
, S_IRUGO
| S_IWUSR
,
1034 adt7316_show_DA_AB_Vref_bypass
,
1035 adt7316_store_DA_AB_Vref_bypass
,
1038 static ssize_t
adt7316_show_DA_CD_Vref_bypass(struct device
*dev
,
1039 struct device_attribute
*attr
,
1042 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1043 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1045 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
)
1048 return sprintf(buf
, "%d\n",
1049 !!(chip
->dac_config
& ADT7316_VREF_BYPASS_DAC_CD
));
1052 static ssize_t
adt7316_store_DA_CD_Vref_bypass(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
);
1062 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
)
1065 dac_config
= chip
->dac_config
& (~ADT7316_VREF_BYPASS_DAC_CD
);
1066 if (!memcmp(buf
, "1", 1))
1067 dac_config
|= ADT7316_VREF_BYPASS_DAC_CD
;
1069 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_DAC_CONFIG
, dac_config
);
1073 chip
->dac_config
= dac_config
;
1078 static IIO_DEVICE_ATTR(DA_CD_Vref_bypass
, S_IRUGO
| S_IWUSR
,
1079 adt7316_show_DA_CD_Vref_bypass
,
1080 adt7316_store_DA_CD_Vref_bypass
,
1083 static ssize_t
adt7316_show_DAC_internal_Vref(struct device
*dev
,
1084 struct device_attribute
*attr
,
1087 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1088 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1090 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
)
1091 return sprintf(buf
, "0x%x\n",
1092 (chip
->dac_config
& ADT7516_DAC_IN_VREF_MASK
) >>
1093 ADT7516_DAC_IN_VREF_OFFSET
);
1095 return sprintf(buf
, "%d\n",
1096 !!(chip
->dac_config
& ADT7316_DAC_IN_VREF
));
1099 static ssize_t
adt7316_store_DAC_internal_Vref(struct device
*dev
,
1100 struct device_attribute
*attr
,
1104 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1105 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1110 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
) {
1111 ret
= strict_strtoul(buf
, 16, &data
);
1112 if (ret
|| data
> 3)
1115 ldac_config
= chip
->ldac_config
& (~ADT7516_DAC_IN_VREF_MASK
);
1117 ldac_config
|= ADT7516_DAC_AB_IN_VREF
;
1118 else if (data
& 0x2)
1119 ldac_config
|= ADT7516_DAC_CD_IN_VREF
;
1121 ret
= strict_strtoul(buf
, 16, &data
);
1125 ldac_config
= chip
->ldac_config
& (~ADT7316_DAC_IN_VREF
);
1127 ldac_config
= chip
->ldac_config
| ADT7316_DAC_IN_VREF
;
1130 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_LDAC_CONFIG
, ldac_config
);
1134 chip
->ldac_config
= ldac_config
;
1139 static IIO_DEVICE_ATTR(DAC_internal_Vref
, S_IRUGO
| S_IWUSR
,
1140 adt7316_show_DAC_internal_Vref
,
1141 adt7316_store_DAC_internal_Vref
,
1144 static ssize_t
adt7316_show_ad(struct adt7316_chip_info
*chip
,
1145 int channel
, char *buf
)
1152 if ((chip
->config2
& ADT7316_AD_SINGLE_CH_MODE
) &&
1153 channel
!= (chip
->config2
& ADT7516_AD_SINGLE_CH_MASK
))
1157 case ADT7316_AD_SINGLE_CH_IN
:
1158 ret
= chip
->bus
.read(chip
->bus
.client
,
1159 ADT7316_LSB_IN_TEMP_VDD
, &lsb
);
1163 ret
= chip
->bus
.read(chip
->bus
.client
,
1164 ADT7316_AD_MSB_DATA_BASE
+ channel
, &msb
);
1168 data
= msb
<< ADT7316_T_VALUE_FLOAT_OFFSET
;
1169 data
|= lsb
& ADT7316_LSB_IN_TEMP_MASK
;
1171 case ADT7316_AD_SINGLE_CH_VDD
:
1172 ret
= chip
->bus
.read(chip
->bus
.client
,
1173 ADT7316_LSB_IN_TEMP_VDD
, &lsb
);
1177 ret
= chip
->bus
.read(chip
->bus
.client
,
1179 ADT7316_AD_MSB_DATA_BASE
+ channel
, &msb
);
1183 data
= msb
<< ADT7316_T_VALUE_FLOAT_OFFSET
;
1184 data
|= (lsb
& ADT7316_LSB_VDD_MASK
) >> ADT7316_LSB_VDD_OFFSET
;
1185 return sprintf(buf
, "%d\n", data
);
1186 default: /* ex_temp and ain */
1187 ret
= chip
->bus
.read(chip
->bus
.client
,
1188 ADT7316_LSB_EX_TEMP_AIN
, &lsb
);
1192 ret
= chip
->bus
.read(chip
->bus
.client
,
1193 ADT7316_AD_MSB_DATA_BASE
+ channel
, &msb
);
1197 data
= msb
<< ADT7316_T_VALUE_FLOAT_OFFSET
;
1198 data
|= lsb
& (ADT7316_LSB_EX_TEMP_MASK
<<
1199 (ADT7516_LSB_AIN_SHIFT
* (channel
-
1200 (ADT7316_MSB_EX_TEMP
- ADT7316_AD_MSB_DATA_BASE
))));
1202 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
)
1203 return sprintf(buf
, "%d\n", data
);
1208 if (data
& ADT7316_T_VALUE_SIGN
) {
1209 /* convert supplement to positive value */
1210 data
= (ADT7316_T_VALUE_SIGN
<< 1) - data
;
1214 return sprintf(buf
, "%c%d.%.2d\n", sign
,
1215 (data
>> ADT7316_T_VALUE_FLOAT_OFFSET
),
1216 (data
& ADT7316_T_VALUE_FLOAT_MASK
) * 25);
1219 static ssize_t
adt7316_show_VDD(struct device
*dev
,
1220 struct device_attribute
*attr
,
1223 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1224 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1226 return adt7316_show_ad(chip
, ADT7316_AD_SINGLE_CH_VDD
, buf
);
1228 static IIO_DEVICE_ATTR(VDD
, S_IRUGO
, adt7316_show_VDD
, NULL
, 0);
1230 static ssize_t
adt7316_show_in_temp(struct device
*dev
,
1231 struct device_attribute
*attr
,
1234 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1235 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1237 return adt7316_show_ad(chip
, ADT7316_AD_SINGLE_CH_IN
, buf
);
1240 static IIO_DEVICE_ATTR(in_temp
, S_IRUGO
, adt7316_show_in_temp
, NULL
, 0);
1242 static ssize_t
adt7316_show_ex_temp_AIN1(struct device
*dev
,
1243 struct device_attribute
*attr
,
1246 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1247 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1249 return adt7316_show_ad(chip
, ADT7316_AD_SINGLE_CH_EX
, buf
);
1252 static IIO_DEVICE_ATTR(ex_temp_AIN1
, S_IRUGO
, adt7316_show_ex_temp_AIN1
, NULL
, 0);
1253 static IIO_DEVICE_ATTR(ex_temp
, S_IRUGO
, adt7316_show_ex_temp_AIN1
, NULL
, 0);
1255 static ssize_t
adt7316_show_AIN2(struct device
*dev
,
1256 struct device_attribute
*attr
,
1259 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1260 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1262 return adt7316_show_ad(chip
, ADT7516_AD_SINGLE_CH_AIN2
, buf
);
1264 static IIO_DEVICE_ATTR(AIN2
, S_IRUGO
, adt7316_show_AIN2
, NULL
, 0);
1266 static ssize_t
adt7316_show_AIN3(struct device
*dev
,
1267 struct device_attribute
*attr
,
1270 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1271 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1273 return adt7316_show_ad(chip
, ADT7516_AD_SINGLE_CH_AIN3
, buf
);
1275 static IIO_DEVICE_ATTR(AIN3
, S_IRUGO
, adt7316_show_AIN3
, NULL
, 0);
1277 static ssize_t
adt7316_show_AIN4(struct device
*dev
,
1278 struct device_attribute
*attr
,
1281 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1282 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1284 return adt7316_show_ad(chip
, ADT7516_AD_SINGLE_CH_AIN4
, buf
);
1286 static IIO_DEVICE_ATTR(AIN4
, S_IRUGO
, adt7316_show_AIN4
, NULL
, 0);
1288 static ssize_t
adt7316_show_temp_offset(struct adt7316_chip_info
*chip
,
1289 int offset_addr
, char *buf
)
1295 ret
= chip
->bus
.read(chip
->bus
.client
, offset_addr
, &val
);
1303 return sprintf(buf
, "%d\n", data
);
1306 static ssize_t
adt7316_store_temp_offset(struct adt7316_chip_info
*chip
,
1307 int offset_addr
, const char *buf
, size_t len
)
1313 ret
= strict_strtol(buf
, 10, &data
);
1314 if (ret
|| data
> 127 || data
< -128)
1322 ret
= chip
->bus
.write(chip
->bus
.client
, offset_addr
, val
);
1329 static ssize_t
adt7316_show_in_temp_offset(struct device
*dev
,
1330 struct device_attribute
*attr
,
1333 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1334 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1336 return adt7316_show_temp_offset(chip
, ADT7316_IN_TEMP_OFFSET
, buf
);
1339 static ssize_t
adt7316_store_in_temp_offset(struct device
*dev
,
1340 struct device_attribute
*attr
,
1344 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1345 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1347 return adt7316_store_temp_offset(chip
, ADT7316_IN_TEMP_OFFSET
, buf
, len
);
1350 static IIO_DEVICE_ATTR(in_temp_offset
, S_IRUGO
| S_IWUSR
,
1351 adt7316_show_in_temp_offset
,
1352 adt7316_store_in_temp_offset
, 0);
1354 static ssize_t
adt7316_show_ex_temp_offset(struct device
*dev
,
1355 struct device_attribute
*attr
,
1358 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1359 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1361 return adt7316_show_temp_offset(chip
, ADT7316_EX_TEMP_OFFSET
, buf
);
1364 static ssize_t
adt7316_store_ex_temp_offset(struct device
*dev
,
1365 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_store_temp_offset(chip
, ADT7316_EX_TEMP_OFFSET
, buf
, len
);
1375 static IIO_DEVICE_ATTR(ex_temp_offset
, S_IRUGO
| S_IWUSR
,
1376 adt7316_show_ex_temp_offset
,
1377 adt7316_store_ex_temp_offset
, 0);
1379 static ssize_t
adt7316_show_in_analog_temp_offset(struct device
*dev
,
1380 struct device_attribute
*attr
,
1383 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1384 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1386 return adt7316_show_temp_offset(chip
,
1387 ADT7316_IN_ANALOG_TEMP_OFFSET
, buf
);
1390 static ssize_t
adt7316_store_in_analog_temp_offset(struct device
*dev
,
1391 struct device_attribute
*attr
,
1395 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1396 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1398 return adt7316_store_temp_offset(chip
,
1399 ADT7316_IN_ANALOG_TEMP_OFFSET
, buf
, len
);
1402 static IIO_DEVICE_ATTR(in_analog_temp_offset
, S_IRUGO
| S_IWUSR
,
1403 adt7316_show_in_analog_temp_offset
,
1404 adt7316_store_in_analog_temp_offset
, 0);
1406 static ssize_t
adt7316_show_ex_analog_temp_offset(struct device
*dev
,
1407 struct device_attribute
*attr
,
1410 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1411 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1413 return adt7316_show_temp_offset(chip
,
1414 ADT7316_EX_ANALOG_TEMP_OFFSET
, buf
);
1417 static ssize_t
adt7316_store_ex_analog_temp_offset(struct device
*dev
,
1418 struct device_attribute
*attr
,
1422 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1423 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1425 return adt7316_store_temp_offset(chip
,
1426 ADT7316_EX_ANALOG_TEMP_OFFSET
, buf
, len
);
1429 static IIO_DEVICE_ATTR(ex_analog_temp_offset
, S_IRUGO
| S_IWUSR
,
1430 adt7316_show_ex_analog_temp_offset
,
1431 adt7316_store_ex_analog_temp_offset
, 0);
1433 static ssize_t
adt7316_show_DAC(struct adt7316_chip_info
*chip
,
1434 int channel
, char *buf
)
1437 u8 msb
, lsb
, offset
;
1440 if (channel
>= ADT7316_DA_MSB_DATA_REGS
||
1442 (chip
->config3
& ADT7316_EN_IN_TEMP_PROP_DACA
)) ||
1444 (chip
->config3
& ADT7316_EN_EX_TEMP_PROP_DACB
)))
1447 offset
= chip
->dac_bits
- 8;
1449 if (chip
->dac_bits
> 8) {
1450 ret
= chip
->bus
.read(chip
->bus
.client
,
1451 ADT7316_DA_DATA_BASE
+ channel
* 2, &lsb
);
1456 ret
= chip
->bus
.read(chip
->bus
.client
,
1457 ADT7316_DA_DATA_BASE
+ 1 + channel
* 2, &msb
);
1461 data
= (msb
<< offset
) + (lsb
& ((1 << offset
) - 1));
1463 return sprintf(buf
, "%d\n", data
);
1466 static ssize_t
adt7316_store_DAC(struct adt7316_chip_info
*chip
,
1467 int channel
, const char *buf
, size_t len
)
1469 u8 msb
, lsb
, offset
;
1473 if (channel
>= ADT7316_DA_MSB_DATA_REGS
||
1475 (chip
->config3
& ADT7316_EN_IN_TEMP_PROP_DACA
)) ||
1477 (chip
->config3
& ADT7316_EN_EX_TEMP_PROP_DACB
)))
1480 offset
= chip
->dac_bits
- 8;
1482 ret
= strict_strtoul(buf
, 10, &data
);
1483 if (ret
|| data
>= (1 << chip
->dac_bits
))
1486 if (chip
->dac_bits
> 8) {
1487 lsb
= data
& (1 << offset
);
1488 ret
= chip
->bus
.write(chip
->bus
.client
,
1489 ADT7316_DA_DATA_BASE
+ channel
* 2, lsb
);
1494 msb
= data
>> offset
;
1495 ret
= chip
->bus
.write(chip
->bus
.client
,
1496 ADT7316_DA_DATA_BASE
+ 1 + channel
* 2, msb
);
1503 static ssize_t
adt7316_show_DAC_A(struct device
*dev
,
1504 struct device_attribute
*attr
,
1507 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1508 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1510 return adt7316_show_DAC(chip
, 0, buf
);
1513 static ssize_t
adt7316_store_DAC_A(struct device
*dev
,
1514 struct device_attribute
*attr
,
1518 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1519 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1521 return adt7316_store_DAC(chip
, 0, buf
, len
);
1524 static IIO_DEVICE_ATTR(DAC_A
, S_IRUGO
| S_IWUSR
, adt7316_show_DAC_A
,
1525 adt7316_store_DAC_A
, 0);
1527 static ssize_t
adt7316_show_DAC_B(struct device
*dev
,
1528 struct device_attribute
*attr
,
1531 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1532 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1534 return adt7316_show_DAC(chip
, 1, buf
);
1537 static ssize_t
adt7316_store_DAC_B(struct device
*dev
,
1538 struct device_attribute
*attr
,
1542 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1543 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1545 return adt7316_store_DAC(chip
, 1, buf
, len
);
1548 static IIO_DEVICE_ATTR(DAC_B
, S_IRUGO
| S_IWUSR
, adt7316_show_DAC_B
,
1549 adt7316_store_DAC_B
, 0);
1551 static ssize_t
adt7316_show_DAC_C(struct device
*dev
,
1552 struct device_attribute
*attr
,
1555 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1556 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1558 return adt7316_show_DAC(chip
, 2, buf
);
1561 static ssize_t
adt7316_store_DAC_C(struct device
*dev
,
1562 struct device_attribute
*attr
,
1566 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1567 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1569 return adt7316_store_DAC(chip
, 2, buf
, len
);
1572 static IIO_DEVICE_ATTR(DAC_C
, S_IRUGO
| S_IWUSR
, adt7316_show_DAC_C
,
1573 adt7316_store_DAC_C
, 0);
1575 static ssize_t
adt7316_show_DAC_D(struct device
*dev
,
1576 struct device_attribute
*attr
,
1579 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1580 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1582 return adt7316_show_DAC(chip
, 3, buf
);
1585 static ssize_t
adt7316_store_DAC_D(struct device
*dev
,
1586 struct device_attribute
*attr
,
1590 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1591 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1593 return adt7316_store_DAC(chip
, 3, buf
, len
);
1596 static IIO_DEVICE_ATTR(DAC_D
, S_IRUGO
| S_IWUSR
, adt7316_show_DAC_D
,
1597 adt7316_store_DAC_D
, 0);
1599 static ssize_t
adt7316_show_device_id(struct device
*dev
,
1600 struct device_attribute
*attr
,
1603 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1604 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1608 ret
= chip
->bus
.read(chip
->bus
.client
, ADT7316_DEVICE_ID
, &id
);
1612 return sprintf(buf
, "%d\n", id
);
1615 static IIO_DEVICE_ATTR(device_id
, S_IRUGO
, adt7316_show_device_id
, NULL
, 0);
1617 static ssize_t
adt7316_show_manufactorer_id(struct device
*dev
,
1618 struct device_attribute
*attr
,
1621 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1622 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1626 ret
= chip
->bus
.read(chip
->bus
.client
, ADT7316_MANUFACTURE_ID
, &id
);
1630 return sprintf(buf
, "%d\n", id
);
1633 static IIO_DEVICE_ATTR(manufactorer_id
, S_IRUGO
,
1634 adt7316_show_manufactorer_id
, NULL
, 0);
1636 static ssize_t
adt7316_show_device_rev(struct device
*dev
,
1637 struct device_attribute
*attr
,
1640 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1641 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1645 ret
= chip
->bus
.read(chip
->bus
.client
, ADT7316_DEVICE_REV
, &rev
);
1649 return sprintf(buf
, "%d\n", rev
);
1652 static IIO_DEVICE_ATTR(device_rev
, S_IRUGO
, adt7316_show_device_rev
, NULL
, 0);
1654 static ssize_t
adt7316_show_bus_type(struct device
*dev
,
1655 struct device_attribute
*attr
,
1658 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1659 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1663 ret
= chip
->bus
.read(chip
->bus
.client
, ADT7316_SPI_LOCK_STAT
, &stat
);
1668 return sprintf(buf
, "spi\n");
1670 return sprintf(buf
, "i2c\n");
1673 static IIO_DEVICE_ATTR(bus_type
, S_IRUGO
, adt7316_show_bus_type
, NULL
, 0);
1675 static struct attribute
*adt7316_attributes
[] = {
1676 &iio_dev_attr_all_modes
.dev_attr
.attr
,
1677 &iio_dev_attr_mode
.dev_attr
.attr
,
1678 &iio_dev_attr_reset
.dev_attr
.attr
,
1679 &iio_dev_attr_enabled
.dev_attr
.attr
,
1680 &iio_dev_attr_ad_channel
.dev_attr
.attr
,
1681 &iio_dev_attr_all_ad_channels
.dev_attr
.attr
,
1682 &iio_dev_attr_disable_averaging
.dev_attr
.attr
,
1683 &iio_dev_attr_enable_smbus_timeout
.dev_attr
.attr
,
1684 &iio_dev_attr_powerdown
.dev_attr
.attr
,
1685 &iio_dev_attr_fast_ad_clock
.dev_attr
.attr
,
1686 &iio_dev_attr_da_high_resolution
.dev_attr
.attr
,
1687 &iio_dev_attr_enable_proportion_DACA
.dev_attr
.attr
,
1688 &iio_dev_attr_enable_proportion_DACB
.dev_attr
.attr
,
1689 &iio_dev_attr_DAC_2Vref_channels_mask
.dev_attr
.attr
,
1690 &iio_dev_attr_DAC_update_mode
.dev_attr
.attr
,
1691 &iio_dev_attr_all_DAC_update_modes
.dev_attr
.attr
,
1692 &iio_dev_attr_update_DAC
.dev_attr
.attr
,
1693 &iio_dev_attr_DA_AB_Vref_bypass
.dev_attr
.attr
,
1694 &iio_dev_attr_DA_CD_Vref_bypass
.dev_attr
.attr
,
1695 &iio_dev_attr_DAC_internal_Vref
.dev_attr
.attr
,
1696 &iio_dev_attr_VDD
.dev_attr
.attr
,
1697 &iio_dev_attr_in_temp
.dev_attr
.attr
,
1698 &iio_dev_attr_ex_temp
.dev_attr
.attr
,
1699 &iio_dev_attr_in_temp_offset
.dev_attr
.attr
,
1700 &iio_dev_attr_ex_temp_offset
.dev_attr
.attr
,
1701 &iio_dev_attr_in_analog_temp_offset
.dev_attr
.attr
,
1702 &iio_dev_attr_ex_analog_temp_offset
.dev_attr
.attr
,
1703 &iio_dev_attr_DAC_A
.dev_attr
.attr
,
1704 &iio_dev_attr_DAC_B
.dev_attr
.attr
,
1705 &iio_dev_attr_DAC_C
.dev_attr
.attr
,
1706 &iio_dev_attr_DAC_D
.dev_attr
.attr
,
1707 &iio_dev_attr_device_id
.dev_attr
.attr
,
1708 &iio_dev_attr_manufactorer_id
.dev_attr
.attr
,
1709 &iio_dev_attr_device_rev
.dev_attr
.attr
,
1710 &iio_dev_attr_bus_type
.dev_attr
.attr
,
1714 static const struct attribute_group adt7316_attribute_group
= {
1715 .attrs
= adt7316_attributes
,
1718 static struct attribute
*adt7516_attributes
[] = {
1719 &iio_dev_attr_all_modes
.dev_attr
.attr
,
1720 &iio_dev_attr_mode
.dev_attr
.attr
,
1721 &iio_dev_attr_select_ex_temp
.dev_attr
.attr
,
1722 &iio_dev_attr_reset
.dev_attr
.attr
,
1723 &iio_dev_attr_enabled
.dev_attr
.attr
,
1724 &iio_dev_attr_ad_channel
.dev_attr
.attr
,
1725 &iio_dev_attr_all_ad_channels
.dev_attr
.attr
,
1726 &iio_dev_attr_disable_averaging
.dev_attr
.attr
,
1727 &iio_dev_attr_enable_smbus_timeout
.dev_attr
.attr
,
1728 &iio_dev_attr_powerdown
.dev_attr
.attr
,
1729 &iio_dev_attr_fast_ad_clock
.dev_attr
.attr
,
1730 &iio_dev_attr_AIN_internal_Vref
.dev_attr
.attr
,
1731 &iio_dev_attr_da_high_resolution
.dev_attr
.attr
,
1732 &iio_dev_attr_enable_proportion_DACA
.dev_attr
.attr
,
1733 &iio_dev_attr_enable_proportion_DACB
.dev_attr
.attr
,
1734 &iio_dev_attr_DAC_2Vref_channels_mask
.dev_attr
.attr
,
1735 &iio_dev_attr_DAC_update_mode
.dev_attr
.attr
,
1736 &iio_dev_attr_all_DAC_update_modes
.dev_attr
.attr
,
1737 &iio_dev_attr_update_DAC
.dev_attr
.attr
,
1738 &iio_dev_attr_DA_AB_Vref_bypass
.dev_attr
.attr
,
1739 &iio_dev_attr_DA_CD_Vref_bypass
.dev_attr
.attr
,
1740 &iio_dev_attr_DAC_internal_Vref
.dev_attr
.attr
,
1741 &iio_dev_attr_VDD
.dev_attr
.attr
,
1742 &iio_dev_attr_in_temp
.dev_attr
.attr
,
1743 &iio_dev_attr_ex_temp_AIN1
.dev_attr
.attr
,
1744 &iio_dev_attr_AIN2
.dev_attr
.attr
,
1745 &iio_dev_attr_AIN3
.dev_attr
.attr
,
1746 &iio_dev_attr_AIN4
.dev_attr
.attr
,
1747 &iio_dev_attr_in_temp_offset
.dev_attr
.attr
,
1748 &iio_dev_attr_ex_temp_offset
.dev_attr
.attr
,
1749 &iio_dev_attr_in_analog_temp_offset
.dev_attr
.attr
,
1750 &iio_dev_attr_ex_analog_temp_offset
.dev_attr
.attr
,
1751 &iio_dev_attr_DAC_A
.dev_attr
.attr
,
1752 &iio_dev_attr_DAC_B
.dev_attr
.attr
,
1753 &iio_dev_attr_DAC_C
.dev_attr
.attr
,
1754 &iio_dev_attr_DAC_D
.dev_attr
.attr
,
1755 &iio_dev_attr_device_id
.dev_attr
.attr
,
1756 &iio_dev_attr_manufactorer_id
.dev_attr
.attr
,
1757 &iio_dev_attr_device_rev
.dev_attr
.attr
,
1758 &iio_dev_attr_bus_type
.dev_attr
.attr
,
1762 static const struct attribute_group adt7516_attribute_group
= {
1763 .attrs
= adt7516_attributes
,
1766 static irqreturn_t
adt7316_event_handler(int irq
, void *private)
1768 struct iio_dev
*indio_dev
= private;
1769 struct adt7316_chip_info
*chip
= iio_priv(indio_dev
);
1774 ret
= chip
->bus
.read(chip
->bus
.client
, ADT7316_INT_STAT1
, &stat1
);
1776 if ((chip
->id
& ID_FAMILY_MASK
) != ID_ADT75XX
)
1779 time
= iio_get_time_ns();
1780 if (stat1
& (1 << 0))
1781 iio_push_event(indio_dev
,
1782 IIO_UNMOD_EVENT_CODE(IIO_TEMP
, 0,
1786 if (stat1
& (1 << 1))
1787 iio_push_event(indio_dev
,
1788 IIO_UNMOD_EVENT_CODE(IIO_TEMP
, 0,
1790 IIO_EV_DIR_FALLING
),
1792 if (stat1
& (1 << 2))
1793 iio_push_event(indio_dev
,
1794 IIO_UNMOD_EVENT_CODE(IIO_TEMP
, 1,
1798 if (stat1
& (1 << 3))
1799 iio_push_event(indio_dev
,
1800 IIO_UNMOD_EVENT_CODE(IIO_TEMP
, 1,
1802 IIO_EV_DIR_FALLING
),
1804 if (stat1
& (1 << 5))
1805 iio_push_event(indio_dev
,
1806 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE
, 1,
1810 if (stat1
& (1 << 6))
1811 iio_push_event(indio_dev
,
1812 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE
, 2,
1816 if (stat1
& (1 << 7))
1817 iio_push_event(indio_dev
,
1818 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE
, 3,
1823 ret
= chip
->bus
.read(chip
->bus
.client
, ADT7316_INT_STAT2
, &stat2
);
1825 if (stat2
& ADT7316_INT_MASK2_VDD
)
1826 iio_push_event(indio_dev
,
1827 IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE
,
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
= strict_strtoul(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
;
1893 static inline ssize_t
adt7316_show_ad_bound(struct device
*dev
,
1894 struct device_attribute
*attr
,
1897 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
1898 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1899 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1904 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT73XX
&&
1905 this_attr
->address
> ADT7316_EX_TEMP_LOW
)
1908 ret
= chip
->bus
.read(chip
->bus
.client
, this_attr
->address
, &val
);
1914 if (!((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
&&
1915 (chip
->config1
& ADT7516_SEL_AIN1_2_EX_TEMP_MASK
) == 0)) {
1920 return sprintf(buf
, "%d\n", data
);
1923 static inline ssize_t
adt7316_set_ad_bound(struct device
*dev
,
1924 struct device_attribute
*attr
,
1928 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
1929 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1930 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1935 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT73XX
&&
1936 this_attr
->address
> ADT7316_EX_TEMP_LOW
)
1939 ret
= strict_strtol(buf
, 10, &data
);
1943 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
&&
1944 (chip
->config1
& ADT7516_SEL_AIN1_2_EX_TEMP_MASK
) == 0) {
1945 if (data
> 255 || data
< 0)
1948 if (data
> 127 || data
< -128)
1957 ret
= chip
->bus
.write(chip
->bus
.client
, this_attr
->address
, val
);
1964 static ssize_t
adt7316_show_int_enabled(struct device
*dev
,
1965 struct device_attribute
*attr
,
1968 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1969 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1971 return sprintf(buf
, "%d\n", !!(chip
->config1
& ADT7316_INT_EN
));
1974 static ssize_t
adt7316_set_int_enabled(struct device
*dev
,
1975 struct device_attribute
*attr
,
1979 struct iio_dev
*dev_info
= dev_to_iio_dev(dev
);
1980 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
1984 config1
= chip
->config1
& (~ADT7316_INT_EN
);
1985 if (!memcmp(buf
, "1", 1))
1986 config1
|= ADT7316_INT_EN
;
1988 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG1
, config1
);
1992 chip
->config1
= config1
;
1997 static IIO_DEVICE_ATTR(int_mask
,
1999 adt7316_show_int_mask
, adt7316_set_int_mask
,
2001 static IIO_DEVICE_ATTR(in_temp_high_value
,
2003 adt7316_show_ad_bound
, adt7316_set_ad_bound
,
2004 ADT7316_IN_TEMP_HIGH
);
2005 static IIO_DEVICE_ATTR(in_temp_low_value
,
2007 adt7316_show_ad_bound
, adt7316_set_ad_bound
,
2008 ADT7316_IN_TEMP_LOW
);
2009 static IIO_DEVICE_ATTR(ex_temp_high_value
,
2011 adt7316_show_ad_bound
, adt7316_set_ad_bound
,
2012 ADT7316_EX_TEMP_HIGH
);
2013 static IIO_DEVICE_ATTR(ex_temp_low_value
,
2015 adt7316_show_ad_bound
, adt7316_set_ad_bound
,
2016 ADT7316_EX_TEMP_LOW
);
2018 /* NASTY duplication to be fixed */
2019 static IIO_DEVICE_ATTR(ex_temp_ain1_high_value
,
2021 adt7316_show_ad_bound
, adt7316_set_ad_bound
,
2022 ADT7316_EX_TEMP_HIGH
);
2023 static IIO_DEVICE_ATTR(ex_temp_ain1_low_value
,
2025 adt7316_show_ad_bound
, adt7316_set_ad_bound
,
2026 ADT7316_EX_TEMP_LOW
);
2027 static IIO_DEVICE_ATTR(ain2_high_value
,
2029 adt7316_show_ad_bound
, adt7316_set_ad_bound
,
2031 static IIO_DEVICE_ATTR(ain2_low_value
,
2033 adt7316_show_ad_bound
, adt7316_set_ad_bound
,
2035 static IIO_DEVICE_ATTR(ain3_high_value
,
2037 adt7316_show_ad_bound
, adt7316_set_ad_bound
,
2039 static IIO_DEVICE_ATTR(ain3_low_value
,
2041 adt7316_show_ad_bound
, adt7316_set_ad_bound
,
2043 static IIO_DEVICE_ATTR(ain4_high_value
,
2045 adt7316_show_ad_bound
, adt7316_set_ad_bound
,
2047 static IIO_DEVICE_ATTR(ain4_low_value
,
2049 adt7316_show_ad_bound
, adt7316_set_ad_bound
,
2051 static IIO_DEVICE_ATTR(int_enabled
,
2053 adt7316_show_int_enabled
,
2054 adt7316_set_int_enabled
, 0);
2056 static struct attribute
*adt7316_event_attributes
[] = {
2057 &iio_dev_attr_int_mask
.dev_attr
.attr
,
2058 &iio_dev_attr_in_temp_high_value
.dev_attr
.attr
,
2059 &iio_dev_attr_in_temp_low_value
.dev_attr
.attr
,
2060 &iio_dev_attr_ex_temp_high_value
.dev_attr
.attr
,
2061 &iio_dev_attr_ex_temp_low_value
.dev_attr
.attr
,
2062 &iio_dev_attr_int_enabled
.dev_attr
.attr
,
2066 static struct attribute_group adt7316_event_attribute_group
= {
2067 .attrs
= adt7316_event_attributes
,
2071 static struct attribute
*adt7516_event_attributes
[] = {
2072 &iio_dev_attr_int_mask
.dev_attr
.attr
,
2073 &iio_dev_attr_in_temp_high_value
.dev_attr
.attr
,
2074 &iio_dev_attr_in_temp_low_value
.dev_attr
.attr
,
2075 &iio_dev_attr_ex_temp_ain1_high_value
.dev_attr
.attr
,
2076 &iio_dev_attr_ex_temp_ain1_low_value
.dev_attr
.attr
,
2077 &iio_dev_attr_ain2_high_value
.dev_attr
.attr
,
2078 &iio_dev_attr_ain2_low_value
.dev_attr
.attr
,
2079 &iio_dev_attr_ain3_high_value
.dev_attr
.attr
,
2080 &iio_dev_attr_ain3_low_value
.dev_attr
.attr
,
2081 &iio_dev_attr_ain4_high_value
.dev_attr
.attr
,
2082 &iio_dev_attr_ain4_low_value
.dev_attr
.attr
,
2083 &iio_dev_attr_int_enabled
.dev_attr
.attr
,
2087 static struct attribute_group adt7516_event_attribute_group
= {
2088 .attrs
= adt7516_event_attributes
,
2092 #ifdef CONFIG_PM_SLEEP
2093 static int adt7316_disable(struct device
*dev
)
2095 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
2096 struct adt7316_chip_info
*chip
= iio_priv(dev_info
);
2098 return _adt7316_store_enabled(chip
, 0);
2101 static 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);
2109 SIMPLE_DEV_PM_OPS(adt7316_pm_ops
, adt7316_disable
, adt7316_enable
);
2110 EXPORT_SYMBOL_GPL(adt7316_pm_ops
);
2113 static const struct iio_info adt7316_info
= {
2114 .attrs
= &adt7316_attribute_group
,
2115 .event_attrs
= &adt7316_event_attribute_group
,
2116 .driver_module
= THIS_MODULE
,
2119 static const struct iio_info adt7516_info
= {
2120 .attrs
= &adt7516_attribute_group
,
2121 .event_attrs
= &adt7516_event_attribute_group
,
2122 .driver_module
= THIS_MODULE
,
2126 * device probe and remove
2128 int __devinit
adt7316_probe(struct device
*dev
, struct adt7316_bus
*bus
,
2131 struct adt7316_chip_info
*chip
;
2132 struct iio_dev
*indio_dev
;
2133 unsigned short *adt7316_platform_data
= dev
->platform_data
;
2136 indio_dev
= iio_device_alloc(sizeof(*chip
));
2137 if (indio_dev
== NULL
) {
2141 chip
= iio_priv(indio_dev
);
2142 /* this is only used for device removal purposes */
2143 dev_set_drvdata(dev
, indio_dev
);
2148 chip
->id
= ID_ADT7316
+ (name
[6] - '6');
2149 else if (name
[4] == '5')
2150 chip
->id
= ID_ADT7516
+ (name
[6] - '6');
2154 chip
->ldac_pin
= adt7316_platform_data
[1];
2155 if (chip
->ldac_pin
) {
2156 chip
->config3
|= ADT7316_DA_EN_VIA_DAC_LDCA
;
2157 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
)
2158 chip
->config1
|= ADT7516_SEL_AIN3
;
2160 chip
->int_mask
= ADT7316_TEMP_INT_MASK
| ADT7316_VDD_INT_MASK
;
2161 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
)
2162 chip
->int_mask
|= ADT7516_AIN_INT_MASK
;
2164 indio_dev
->dev
.parent
= dev
;
2165 if ((chip
->id
& ID_FAMILY_MASK
) == ID_ADT75XX
)
2166 indio_dev
->info
= &adt7516_info
;
2168 indio_dev
->info
= &adt7316_info
;
2169 indio_dev
->name
= name
;
2170 indio_dev
->modes
= INDIO_DIRECT_MODE
;
2172 if (chip
->bus
.irq
> 0) {
2173 if (adt7316_platform_data
[0])
2174 chip
->bus
.irq_flags
= adt7316_platform_data
[0];
2176 ret
= request_threaded_irq(chip
->bus
.irq
,
2178 &adt7316_event_handler
,
2179 chip
->bus
.irq_flags
| IRQF_ONESHOT
,
2183 goto error_free_dev
;
2185 if (chip
->bus
.irq_flags
& IRQF_TRIGGER_HIGH
)
2186 chip
->config1
|= ADT7316_INT_POLARITY
;
2189 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG1
, chip
->config1
);
2192 goto error_unreg_irq
;
2195 ret
= chip
->bus
.write(chip
->bus
.client
, ADT7316_CONFIG3
, chip
->config3
);
2198 goto error_unreg_irq
;
2201 ret
= iio_device_register(indio_dev
);
2203 goto error_unreg_irq
;
2205 dev_info(dev
, "%s temperature sensor, ADC and DAC registered.\n",
2211 free_irq(chip
->bus
.irq
, indio_dev
);
2213 iio_device_free(indio_dev
);
2217 EXPORT_SYMBOL(adt7316_probe
);
2219 int __devexit
adt7316_remove(struct device
*dev
)
2221 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
2222 struct adt7316_chip_info
*chip
= iio_priv(indio_dev
);
2224 iio_device_unregister(indio_dev
);
2226 free_irq(chip
->bus
.irq
, indio_dev
);
2227 iio_device_free(indio_dev
);
2231 EXPORT_SYMBOL(adt7316_remove
);
2233 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
2234 MODULE_DESCRIPTION("Analog Devices ADT7316/7/8 and ADT7516/7/9 digital"
2235 " temperature sensor, ADC and DAC driver");
2236 MODULE_LICENSE("GPL v2");