Linux-libre 4.14.132-gnu
[librecmc/linux-libre.git] / drivers / staging / iio / addac / adt7316.c
1 /*
2  * ADT7316 digital temperature sensor driver supporting ADT7316/7/8 ADT7516/7/9
3  *
4  *
5  * Copyright 2010 Analog Devices Inc.
6  *
7  * Licensed under the GPL-2 or later.
8  */
9
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>
21
22 #include <linux/iio/iio.h>
23 #include <linux/iio/events.h>
24 #include <linux/iio/sysfs.h>
25 #include "adt7316.h"
26
27 /*
28  * ADT7316 registers definition
29  */
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_10_BIT_LSB_SHIFT     6
51 #define ADT7316_DA_12_BIT_LSB_SHIFT     4
52 #define ADT7316_DA_MSB_DATA_REGS        4
53 #define ADT7316_LSB_DAC_A               0x10
54 #define ADT7316_MSB_DAC_A               0x11
55 #define ADT7316_LSB_DAC_B               0x12
56 #define ADT7316_MSB_DAC_B               0x13
57 #define ADT7316_LSB_DAC_C               0x14
58 #define ADT7316_MSB_DAC_C               0x15
59 #define ADT7316_LSB_DAC_D               0x16
60 #define ADT7316_MSB_DAC_D               0x17
61 #define ADT7316_CONFIG1                 0x18
62 #define ADT7316_CONFIG2                 0x19
63 #define ADT7316_CONFIG3                 0x1A
64 #define ADT7316_LDAC_CONFIG             0x1B
65 #define ADT7316_DAC_CONFIG              0x1C
66 #define ADT7316_INT_MASK1               0x1D
67 #define ADT7316_INT_MASK2               0x1E
68 #define ADT7316_IN_TEMP_OFFSET          0x1F
69 #define ADT7316_EX_TEMP_OFFSET          0x20
70 #define ADT7316_IN_ANALOG_TEMP_OFFSET   0x21
71 #define ADT7316_EX_ANALOG_TEMP_OFFSET   0x22
72 #define ADT7316_VDD_HIGH                0x23
73 #define ADT7316_VDD_LOW                 0x24
74 #define ADT7316_IN_TEMP_HIGH            0x25
75 #define ADT7316_IN_TEMP_LOW             0x26
76 #define ADT7316_EX_TEMP_HIGH            0x27
77 #define ADT7316_EX_TEMP_LOW             0x28
78 #define ADT7516_AIN2_HIGH               0x2B
79 #define ADT7516_AIN2_LOW                0x2C
80 #define ADT7516_AIN3_HIGH               0x2D
81 #define ADT7516_AIN3_LOW                0x2E
82 #define ADT7516_AIN4_HIGH               0x2F
83 #define ADT7516_AIN4_LOW                0x30
84 #define ADT7316_DEVICE_ID               0x4D
85 #define ADT7316_MANUFACTURE_ID          0x4E
86 #define ADT7316_DEVICE_REV              0x4F
87 #define ADT7316_SPI_LOCK_STAT           0x7F
88
89 /*
90  * ADT7316 config1
91  */
92 #define ADT7316_EN                      0x1
93 #define ADT7516_SEL_EX_TEMP             0x4
94 #define ADT7516_SEL_AIN1_2_EX_TEMP_MASK 0x6
95 #define ADT7516_SEL_AIN3                0x8
96 #define ADT7316_INT_EN                  0x20
97 #define ADT7316_INT_POLARITY            0x40
98 #define ADT7316_PD                      0x80
99
100 /*
101  * ADT7316 config2
102  */
103 #define ADT7316_AD_SINGLE_CH_MASK       0x3
104 #define ADT7516_AD_SINGLE_CH_MASK       0x7
105 #define ADT7316_AD_SINGLE_CH_VDD        0
106 #define ADT7316_AD_SINGLE_CH_IN         1
107 #define ADT7316_AD_SINGLE_CH_EX         2
108 #define ADT7516_AD_SINGLE_CH_AIN1       2
109 #define ADT7516_AD_SINGLE_CH_AIN2       3
110 #define ADT7516_AD_SINGLE_CH_AIN3       4
111 #define ADT7516_AD_SINGLE_CH_AIN4       5
112 #define ADT7316_AD_SINGLE_CH_MODE       0x10
113 #define ADT7316_DISABLE_AVERAGING       0x20
114 #define ADT7316_EN_SMBUS_TIMEOUT        0x40
115 #define ADT7316_RESET                   0x80
116
117 /*
118  * ADT7316 config3
119  */
120 #define ADT7316_ADCLK_22_5              0x1
121 #define ADT7316_DA_HIGH_RESOLUTION      0x2
122 #define ADT7316_DA_EN_VIA_DAC_LDCA      0x4
123 #define ADT7516_AIN_IN_VREF             0x10
124 #define ADT7316_EN_IN_TEMP_PROP_DACA    0x20
125 #define ADT7316_EN_EX_TEMP_PROP_DACB    0x40
126
127 /*
128  * ADT7316 DAC config
129  */
130 #define ADT7316_DA_2VREF_CH_MASK        0xF
131 #define ADT7316_DA_EN_MODE_MASK         0x30
132 #define ADT7316_DA_EN_MODE_SINGLE       0x00
133 #define ADT7316_DA_EN_MODE_AB_CD        0x10
134 #define ADT7316_DA_EN_MODE_ABCD         0x20
135 #define ADT7316_DA_EN_MODE_LDAC         0x30
136 #define ADT7316_VREF_BYPASS_DAC_AB      0x40
137 #define ADT7316_VREF_BYPASS_DAC_CD      0x80
138
139 /*
140  * ADT7316 LDAC config
141  */
142 #define ADT7316_LDAC_EN_DA_MASK         0xF
143 #define ADT7316_DAC_IN_VREF             0x10
144 #define ADT7516_DAC_AB_IN_VREF          0x10
145 #define ADT7516_DAC_CD_IN_VREF          0x20
146 #define ADT7516_DAC_IN_VREF_OFFSET      4
147 #define ADT7516_DAC_IN_VREF_MASK        0x30
148
149 /*
150  * ADT7316 INT_MASK2
151  */
152 #define ADT7316_INT_MASK2_VDD           0x10
153
154 /*
155  * ADT7316 value masks
156  */
157 #define ADT7316_VALUE_MASK              0xfff
158 #define ADT7316_T_VALUE_SIGN            0x400
159 #define ADT7316_T_VALUE_FLOAT_OFFSET    2
160 #define ADT7316_T_VALUE_FLOAT_MASK      0x2
161
162 /*
163  * Chip ID
164  */
165 #define ID_ADT7316              0x1
166 #define ID_ADT7317              0x2
167 #define ID_ADT7318              0x3
168 #define ID_ADT7516              0x11
169 #define ID_ADT7517              0x12
170 #define ID_ADT7519              0x14
171
172 #define ID_FAMILY_MASK          0xF0
173 #define ID_ADT73XX              0x0
174 #define ID_ADT75XX              0x10
175
176 /*
177  * struct adt7316_chip_info - chip specific information
178  */
179
180 struct adt7316_chip_info {
181         struct adt7316_bus      bus;
182         u16                     ldac_pin;
183         u16                     int_mask;       /* 0x2f */
184         u8                      config1;
185         u8                      config2;
186         u8                      config3;
187         u8                      dac_config;     /* DAC config */
188         u8                      ldac_config;    /* LDAC config */
189         u8                      dac_bits;       /* 8, 10, 12 */
190         u8                      id;             /* chip id */
191 };
192
193 /*
194  * Logic interrupt mask for user application to enable
195  * interrupts.
196  */
197 #define ADT7316_IN_TEMP_HIGH_INT_MASK   0x1
198 #define ADT7316_IN_TEMP_LOW_INT_MASK    0x2
199 #define ADT7316_EX_TEMP_HIGH_INT_MASK   0x4
200 #define ADT7316_EX_TEMP_LOW_INT_MASK    0x8
201 #define ADT7316_EX_TEMP_FAULT_INT_MASK  0x10
202 #define ADT7516_AIN1_INT_MASK           0x4
203 #define ADT7516_AIN2_INT_MASK           0x20
204 #define ADT7516_AIN3_INT_MASK           0x40
205 #define ADT7516_AIN4_INT_MASK           0x80
206 #define ADT7316_VDD_INT_MASK            0x100
207 #define ADT7316_TEMP_INT_MASK           0x1F
208 #define ADT7516_AIN_INT_MASK            0xE0
209 #define ADT7316_TEMP_AIN_INT_MASK       \
210         (ADT7316_TEMP_INT_MASK)
211
212 /*
213  * struct adt7316_chip_info - chip specific information
214  */
215
216 struct adt7316_limit_regs {
217         u16     data_high;
218         u16     data_low;
219 };
220
221 static ssize_t adt7316_show_enabled(struct device *dev,
222                 struct device_attribute *attr,
223                 char *buf)
224 {
225         struct iio_dev *dev_info = dev_to_iio_dev(dev);
226         struct adt7316_chip_info *chip = iio_priv(dev_info);
227
228         return sprintf(buf, "%d\n", !!(chip->config1 & ADT7316_EN));
229 }
230
231 static ssize_t _adt7316_store_enabled(struct adt7316_chip_info *chip,
232                 int enable)
233 {
234         u8 config1;
235         int ret;
236
237         if (enable)
238                 config1 = chip->config1 | ADT7316_EN;
239         else
240                 config1 = chip->config1 & ~ADT7316_EN;
241
242         ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG1, config1);
243         if (ret)
244                 return -EIO;
245
246         chip->config1 = config1;
247
248         return ret;
249 }
250
251 static ssize_t adt7316_store_enabled(struct device *dev,
252                 struct device_attribute *attr,
253                 const char *buf,
254                 size_t len)
255 {
256         struct iio_dev *dev_info = dev_to_iio_dev(dev);
257         struct adt7316_chip_info *chip = iio_priv(dev_info);
258         int enable;
259
260         if (buf[0] == '1')
261                 enable = 1;
262         else
263                 enable = 0;
264
265         if (_adt7316_store_enabled(chip, enable) < 0)
266                 return -EIO;
267
268         return len;
269 }
270
271 static IIO_DEVICE_ATTR(enabled, 0644,
272                 adt7316_show_enabled,
273                 adt7316_store_enabled,
274                 0);
275
276 static ssize_t adt7316_show_select_ex_temp(struct device *dev,
277                 struct device_attribute *attr,
278                 char *buf)
279 {
280         struct iio_dev *dev_info = dev_to_iio_dev(dev);
281         struct adt7316_chip_info *chip = iio_priv(dev_info);
282
283         if ((chip->id & ID_FAMILY_MASK) != ID_ADT75XX)
284                 return -EPERM;
285
286         return sprintf(buf, "%d\n", !!(chip->config1 & ADT7516_SEL_EX_TEMP));
287 }
288
289 static ssize_t adt7316_store_select_ex_temp(struct device *dev,
290                 struct device_attribute *attr,
291                 const char *buf,
292                 size_t len)
293 {
294         struct iio_dev *dev_info = dev_to_iio_dev(dev);
295         struct adt7316_chip_info *chip = iio_priv(dev_info);
296         u8 config1;
297         int ret;
298
299         if ((chip->id & ID_FAMILY_MASK) != ID_ADT75XX)
300                 return -EPERM;
301
302         config1 = chip->config1 & (~ADT7516_SEL_EX_TEMP);
303         if (buf[0] == '1')
304                 config1 |= ADT7516_SEL_EX_TEMP;
305
306         ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG1, config1);
307         if (ret)
308                 return -EIO;
309
310         chip->config1 = config1;
311
312         return len;
313 }
314
315 static IIO_DEVICE_ATTR(select_ex_temp, 0644,
316                 adt7316_show_select_ex_temp,
317                 adt7316_store_select_ex_temp,
318                 0);
319
320 static ssize_t adt7316_show_mode(struct device *dev,
321                 struct device_attribute *attr,
322                 char *buf)
323 {
324         struct iio_dev *dev_info = dev_to_iio_dev(dev);
325         struct adt7316_chip_info *chip = iio_priv(dev_info);
326
327         if (chip->config2 & ADT7316_AD_SINGLE_CH_MODE)
328                 return sprintf(buf, "single_channel\n");
329
330         return sprintf(buf, "round_robin\n");
331 }
332
333 static ssize_t adt7316_store_mode(struct device *dev,
334                 struct device_attribute *attr,
335                 const char *buf,
336                 size_t len)
337 {
338         struct iio_dev *dev_info = dev_to_iio_dev(dev);
339         struct adt7316_chip_info *chip = iio_priv(dev_info);
340         u8 config2;
341         int ret;
342
343         config2 = chip->config2 & (~ADT7316_AD_SINGLE_CH_MODE);
344         if (!memcmp(buf, "single_channel", 14))
345                 config2 |= ADT7316_AD_SINGLE_CH_MODE;
346
347         ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG2, config2);
348         if (ret)
349                 return -EIO;
350
351         chip->config2 = config2;
352
353         return len;
354 }
355
356 static IIO_DEVICE_ATTR(mode, 0644,
357                 adt7316_show_mode,
358                 adt7316_store_mode,
359                 0);
360
361 static ssize_t adt7316_show_all_modes(struct device *dev,
362                 struct device_attribute *attr,
363                 char *buf)
364 {
365         return sprintf(buf, "single_channel\nround_robin\n");
366 }
367
368 static IIO_DEVICE_ATTR(all_modes, 0444, adt7316_show_all_modes, NULL, 0);
369
370 static ssize_t adt7316_show_ad_channel(struct device *dev,
371                 struct device_attribute *attr,
372                 char *buf)
373 {
374         struct iio_dev *dev_info = dev_to_iio_dev(dev);
375         struct adt7316_chip_info *chip = iio_priv(dev_info);
376
377         if (!(chip->config2 & ADT7316_AD_SINGLE_CH_MODE))
378                 return -EPERM;
379
380         switch (chip->config2 & ADT7516_AD_SINGLE_CH_MASK) {
381         case ADT7316_AD_SINGLE_CH_VDD:
382                 return sprintf(buf, "0 - VDD\n");
383         case ADT7316_AD_SINGLE_CH_IN:
384                 return sprintf(buf, "1 - Internal Temperature\n");
385         case ADT7316_AD_SINGLE_CH_EX:
386                 if (((chip->id & ID_FAMILY_MASK) == ID_ADT75XX) &&
387                         (chip->config1 & ADT7516_SEL_AIN1_2_EX_TEMP_MASK) == 0)
388                         return sprintf(buf, "2 - AIN1\n");
389
390                 return sprintf(buf, "2 - External Temperature\n");
391         case ADT7516_AD_SINGLE_CH_AIN2:
392                 if ((chip->config1 & ADT7516_SEL_AIN1_2_EX_TEMP_MASK) == 0)
393                         return sprintf(buf, "3 - AIN2\n");
394
395                 return sprintf(buf, "N/A\n");
396         case ADT7516_AD_SINGLE_CH_AIN3:
397                 if (chip->config1 & ADT7516_SEL_AIN3)
398                         return sprintf(buf, "4 - AIN3\n");
399
400                 return sprintf(buf, "N/A\n");
401         case ADT7516_AD_SINGLE_CH_AIN4:
402                 return sprintf(buf, "5 - AIN4\n");
403         default:
404                 return sprintf(buf, "N/A\n");
405         }
406 }
407
408 static ssize_t adt7316_store_ad_channel(struct device *dev,
409                 struct device_attribute *attr,
410                 const char *buf,
411                 size_t len)
412 {
413         struct iio_dev *dev_info = dev_to_iio_dev(dev);
414         struct adt7316_chip_info *chip = iio_priv(dev_info);
415         u8 config2;
416         u8 data;
417         int ret;
418
419         if (!(chip->config2 & ADT7316_AD_SINGLE_CH_MODE))
420                 return -EPERM;
421
422         ret = kstrtou8(buf, 10, &data);
423         if (ret)
424                 return -EINVAL;
425
426         if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX) {
427                 if (data > 5)
428                         return -EINVAL;
429
430                 config2 = chip->config2 & (~ADT7516_AD_SINGLE_CH_MASK);
431         } else {
432                 if (data > 2)
433                         return -EINVAL;
434
435                 config2 = chip->config2 & (~ADT7316_AD_SINGLE_CH_MASK);
436         }
437
438         config2 |= data;
439
440         ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG2, config2);
441         if (ret)
442                 return -EIO;
443
444         chip->config2 = config2;
445
446         return len;
447 }
448
449 static IIO_DEVICE_ATTR(ad_channel, 0644,
450                 adt7316_show_ad_channel,
451                 adt7316_store_ad_channel,
452                 0);
453
454 static ssize_t adt7316_show_all_ad_channels(struct device *dev,
455                 struct device_attribute *attr,
456                 char *buf)
457 {
458         struct iio_dev *dev_info = dev_to_iio_dev(dev);
459         struct adt7316_chip_info *chip = iio_priv(dev_info);
460
461         if (!(chip->config2 & ADT7316_AD_SINGLE_CH_MODE))
462                 return -EPERM;
463
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");
468         return sprintf(buf, "0 - VDD\n1 - Internal Temperature\n"
469                         "2 - External Temperature\n");
470 }
471
472 static IIO_DEVICE_ATTR(all_ad_channels, 0444,
473                 adt7316_show_all_ad_channels, NULL, 0);
474
475 static ssize_t adt7316_show_disable_averaging(struct device *dev,
476                 struct device_attribute *attr,
477                 char *buf)
478 {
479         struct iio_dev *dev_info = dev_to_iio_dev(dev);
480         struct adt7316_chip_info *chip = iio_priv(dev_info);
481
482         return sprintf(buf, "%d\n",
483                 !!(chip->config2 & ADT7316_DISABLE_AVERAGING));
484 }
485
486 static ssize_t adt7316_store_disable_averaging(struct device *dev,
487                 struct device_attribute *attr,
488                 const char *buf,
489                 size_t len)
490 {
491         struct iio_dev *dev_info = dev_to_iio_dev(dev);
492         struct adt7316_chip_info *chip = iio_priv(dev_info);
493         u8 config2;
494         int ret;
495
496         config2 = chip->config2 & (~ADT7316_DISABLE_AVERAGING);
497         if (buf[0] == '1')
498                 config2 |= ADT7316_DISABLE_AVERAGING;
499
500         ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG2, config2);
501         if (ret)
502                 return -EIO;
503
504         chip->config2 = config2;
505
506         return len;
507 }
508
509 static IIO_DEVICE_ATTR(disable_averaging, 0644,
510                 adt7316_show_disable_averaging,
511                 adt7316_store_disable_averaging,
512                 0);
513
514 static ssize_t adt7316_show_enable_smbus_timeout(struct device *dev,
515                 struct device_attribute *attr,
516                 char *buf)
517 {
518         struct iio_dev *dev_info = dev_to_iio_dev(dev);
519         struct adt7316_chip_info *chip = iio_priv(dev_info);
520
521         return sprintf(buf, "%d\n",
522                 !!(chip->config2 & ADT7316_EN_SMBUS_TIMEOUT));
523 }
524
525 static ssize_t adt7316_store_enable_smbus_timeout(struct device *dev,
526                 struct device_attribute *attr,
527                 const char *buf,
528                 size_t len)
529 {
530         struct iio_dev *dev_info = dev_to_iio_dev(dev);
531         struct adt7316_chip_info *chip = iio_priv(dev_info);
532         u8 config2;
533         int ret;
534
535         config2 = chip->config2 & (~ADT7316_EN_SMBUS_TIMEOUT);
536         if (buf[0] == '1')
537                 config2 |= ADT7316_EN_SMBUS_TIMEOUT;
538
539         ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG2, config2);
540         if (ret)
541                 return -EIO;
542
543         chip->config2 = config2;
544
545         return len;
546 }
547
548 static IIO_DEVICE_ATTR(enable_smbus_timeout, 0644,
549                 adt7316_show_enable_smbus_timeout,
550                 adt7316_store_enable_smbus_timeout,
551                 0);
552
553 static ssize_t adt7316_show_powerdown(struct device *dev,
554                 struct device_attribute *attr,
555                 char *buf)
556 {
557         struct iio_dev *dev_info = dev_to_iio_dev(dev);
558         struct adt7316_chip_info *chip = iio_priv(dev_info);
559
560         return sprintf(buf, "%d\n", !!(chip->config1 & ADT7316_PD));
561 }
562
563 static ssize_t adt7316_store_powerdown(struct device *dev,
564                 struct device_attribute *attr,
565                 const char *buf,
566                 size_t len)
567 {
568         struct iio_dev *dev_info = dev_to_iio_dev(dev);
569         struct adt7316_chip_info *chip = iio_priv(dev_info);
570         u8 config1;
571         int ret;
572
573         config1 = chip->config1 & (~ADT7316_PD);
574         if (buf[0] == '1')
575                 config1 |= ADT7316_PD;
576
577         ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG1, config1);
578         if (ret)
579                 return -EIO;
580
581         chip->config1 = config1;
582
583         return len;
584 }
585
586 static IIO_DEVICE_ATTR(powerdown, 0644,
587                 adt7316_show_powerdown,
588                 adt7316_store_powerdown,
589                 0);
590
591 static ssize_t adt7316_show_fast_ad_clock(struct device *dev,
592                 struct device_attribute *attr,
593                 char *buf)
594 {
595         struct iio_dev *dev_info = dev_to_iio_dev(dev);
596         struct adt7316_chip_info *chip = iio_priv(dev_info);
597
598         return sprintf(buf, "%d\n", !!(chip->config3 & ADT7316_ADCLK_22_5));
599 }
600
601 static ssize_t adt7316_store_fast_ad_clock(struct device *dev,
602                 struct device_attribute *attr,
603                 const char *buf,
604                 size_t len)
605 {
606         struct iio_dev *dev_info = dev_to_iio_dev(dev);
607         struct adt7316_chip_info *chip = iio_priv(dev_info);
608         u8 config3;
609         int ret;
610
611         config3 = chip->config3 & (~ADT7316_ADCLK_22_5);
612         if (buf[0] == '1')
613                 config3 |= ADT7316_ADCLK_22_5;
614
615         ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, config3);
616         if (ret)
617                 return -EIO;
618
619         chip->config3 = config3;
620
621         return len;
622 }
623
624 static IIO_DEVICE_ATTR(fast_ad_clock, 0644,
625                 adt7316_show_fast_ad_clock,
626                 adt7316_store_fast_ad_clock,
627                 0);
628
629 static ssize_t adt7316_show_da_high_resolution(struct device *dev,
630                 struct device_attribute *attr,
631                 char *buf)
632 {
633         struct iio_dev *dev_info = dev_to_iio_dev(dev);
634         struct adt7316_chip_info *chip = iio_priv(dev_info);
635
636         if (chip->config3 & ADT7316_DA_HIGH_RESOLUTION) {
637                 if (chip->id == ID_ADT7316 || chip->id == ID_ADT7516)
638                         return sprintf(buf, "1 (12 bits)\n");
639                 if (chip->id == ID_ADT7317 || chip->id == ID_ADT7517)
640                         return sprintf(buf, "1 (10 bits)\n");
641         }
642
643         return sprintf(buf, "0 (8 bits)\n");
644 }
645
646 static ssize_t adt7316_store_da_high_resolution(struct device *dev,
647                 struct device_attribute *attr,
648                 const char *buf,
649                 size_t len)
650 {
651         struct iio_dev *dev_info = dev_to_iio_dev(dev);
652         struct adt7316_chip_info *chip = iio_priv(dev_info);
653         u8 config3;
654         int ret;
655
656         chip->dac_bits = 8;
657
658         if (buf[0] == '1') {
659                 config3 = chip->config3 | ADT7316_DA_HIGH_RESOLUTION;
660                 if (chip->id == ID_ADT7316 || chip->id == ID_ADT7516)
661                         chip->dac_bits = 12;
662                 else if (chip->id == ID_ADT7317 || chip->id == ID_ADT7517)
663                         chip->dac_bits = 10;
664         } else {
665                 config3 = chip->config3 & (~ADT7316_DA_HIGH_RESOLUTION);
666         }
667
668         ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, config3);
669         if (ret)
670                 return -EIO;
671
672         chip->config3 = config3;
673
674         return len;
675 }
676
677 static IIO_DEVICE_ATTR(da_high_resolution, 0644,
678                 adt7316_show_da_high_resolution,
679                 adt7316_store_da_high_resolution,
680                 0);
681
682 static ssize_t adt7316_show_AIN_internal_Vref(struct device *dev,
683                 struct device_attribute *attr,
684                 char *buf)
685 {
686         struct iio_dev *dev_info = dev_to_iio_dev(dev);
687         struct adt7316_chip_info *chip = iio_priv(dev_info);
688
689         if ((chip->id & ID_FAMILY_MASK) != ID_ADT75XX)
690                 return -EPERM;
691
692         return sprintf(buf, "%d\n",
693                 !!(chip->config3 & ADT7516_AIN_IN_VREF));
694 }
695
696 static ssize_t adt7316_store_AIN_internal_Vref(struct device *dev,
697                 struct device_attribute *attr,
698                 const char *buf,
699                 size_t len)
700 {
701         struct iio_dev *dev_info = dev_to_iio_dev(dev);
702         struct adt7316_chip_info *chip = iio_priv(dev_info);
703         u8 config3;
704         int ret;
705
706         if ((chip->id & ID_FAMILY_MASK) != ID_ADT75XX)
707                 return -EPERM;
708
709         if (buf[0] != '1')
710                 config3 = chip->config3 & (~ADT7516_AIN_IN_VREF);
711         else
712                 config3 = chip->config3 | ADT7516_AIN_IN_VREF;
713
714         ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, config3);
715         if (ret)
716                 return -EIO;
717
718         chip->config3 = config3;
719
720         return len;
721 }
722
723 static IIO_DEVICE_ATTR(AIN_internal_Vref, 0644,
724                 adt7316_show_AIN_internal_Vref,
725                 adt7316_store_AIN_internal_Vref,
726                 0);
727
728 static ssize_t adt7316_show_enable_prop_DACA(struct device *dev,
729                 struct device_attribute *attr,
730                 char *buf)
731 {
732         struct iio_dev *dev_info = dev_to_iio_dev(dev);
733         struct adt7316_chip_info *chip = iio_priv(dev_info);
734
735         return sprintf(buf, "%d\n",
736                 !!(chip->config3 & ADT7316_EN_IN_TEMP_PROP_DACA));
737 }
738
739 static ssize_t adt7316_store_enable_prop_DACA(struct device *dev,
740                 struct device_attribute *attr,
741                 const char *buf,
742                 size_t len)
743 {
744         struct iio_dev *dev_info = dev_to_iio_dev(dev);
745         struct adt7316_chip_info *chip = iio_priv(dev_info);
746         u8 config3;
747         int ret;
748
749         config3 = chip->config3 & (~ADT7316_EN_IN_TEMP_PROP_DACA);
750         if (buf[0] == '1')
751                 config3 |= ADT7316_EN_IN_TEMP_PROP_DACA;
752
753         ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, config3);
754         if (ret)
755                 return -EIO;
756
757         chip->config3 = config3;
758
759         return len;
760 }
761
762 static IIO_DEVICE_ATTR(enable_proportion_DACA, 0644,
763                 adt7316_show_enable_prop_DACA,
764                 adt7316_store_enable_prop_DACA,
765                 0);
766
767 static ssize_t adt7316_show_enable_prop_DACB(struct device *dev,
768                 struct device_attribute *attr,
769                 char *buf)
770 {
771         struct iio_dev *dev_info = dev_to_iio_dev(dev);
772         struct adt7316_chip_info *chip = iio_priv(dev_info);
773
774         return sprintf(buf, "%d\n",
775                 !!(chip->config3 & ADT7316_EN_EX_TEMP_PROP_DACB));
776 }
777
778 static ssize_t adt7316_store_enable_prop_DACB(struct device *dev,
779                 struct device_attribute *attr,
780                 const char *buf,
781                 size_t len)
782 {
783         struct iio_dev *dev_info = dev_to_iio_dev(dev);
784         struct adt7316_chip_info *chip = iio_priv(dev_info);
785         u8 config3;
786         int ret;
787
788         config3 = chip->config3 & (~ADT7316_EN_EX_TEMP_PROP_DACB);
789         if (buf[0] == '1')
790                 config3 |= ADT7316_EN_EX_TEMP_PROP_DACB;
791
792         ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, config3);
793         if (ret)
794                 return -EIO;
795
796         chip->config3 = config3;
797
798         return len;
799 }
800
801 static IIO_DEVICE_ATTR(enable_proportion_DACB, 0644,
802                 adt7316_show_enable_prop_DACB,
803                 adt7316_store_enable_prop_DACB,
804                 0);
805
806 static ssize_t adt7316_show_DAC_2Vref_ch_mask(struct device *dev,
807                 struct device_attribute *attr,
808                 char *buf)
809 {
810         struct iio_dev *dev_info = dev_to_iio_dev(dev);
811         struct adt7316_chip_info *chip = iio_priv(dev_info);
812
813         return sprintf(buf, "0x%x\n",
814                 chip->dac_config & ADT7316_DA_2VREF_CH_MASK);
815 }
816
817 static ssize_t adt7316_store_DAC_2Vref_ch_mask(struct device *dev,
818                 struct device_attribute *attr,
819                 const char *buf,
820                 size_t len)
821 {
822         struct iio_dev *dev_info = dev_to_iio_dev(dev);
823         struct adt7316_chip_info *chip = iio_priv(dev_info);
824         u8 dac_config;
825         u8 data;
826         int ret;
827
828         ret = kstrtou8(buf, 16, &data);
829         if (ret || data > ADT7316_DA_2VREF_CH_MASK)
830                 return -EINVAL;
831
832         dac_config = chip->dac_config & (~ADT7316_DA_2VREF_CH_MASK);
833         dac_config |= data;
834
835         ret = chip->bus.write(chip->bus.client, ADT7316_DAC_CONFIG, dac_config);
836         if (ret)
837                 return -EIO;
838
839         chip->dac_config = dac_config;
840
841         return len;
842 }
843
844 static IIO_DEVICE_ATTR(DAC_2Vref_channels_mask, 0644,
845                 adt7316_show_DAC_2Vref_ch_mask,
846                 adt7316_store_DAC_2Vref_ch_mask,
847                 0);
848
849 static ssize_t adt7316_show_DAC_update_mode(struct device *dev,
850                 struct device_attribute *attr,
851                 char *buf)
852 {
853         struct iio_dev *dev_info = dev_to_iio_dev(dev);
854         struct adt7316_chip_info *chip = iio_priv(dev_info);
855
856         if (!(chip->config3 & ADT7316_DA_EN_VIA_DAC_LDCA))
857                 return sprintf(buf, "manual\n");
858
859         switch (chip->dac_config & ADT7316_DA_EN_MODE_MASK) {
860         case ADT7316_DA_EN_MODE_SINGLE:
861                 return sprintf(buf,
862                         "0 - auto at any MSB DAC writing\n");
863         case ADT7316_DA_EN_MODE_AB_CD:
864                 return sprintf(buf,
865                         "1 - auto at MSB DAC AB and CD writing\n");
866         case ADT7316_DA_EN_MODE_ABCD:
867                 return sprintf(buf,
868                         "2 - auto at MSB DAC ABCD writing\n");
869         default: /* ADT7316_DA_EN_MODE_LDAC */
870                 return sprintf(buf, "3 - manual\n");
871         }
872 }
873
874 static ssize_t adt7316_store_DAC_update_mode(struct device *dev,
875                 struct device_attribute *attr,
876                 const char *buf,
877                 size_t len)
878 {
879         struct iio_dev *dev_info = dev_to_iio_dev(dev);
880         struct adt7316_chip_info *chip = iio_priv(dev_info);
881         u8 dac_config;
882         u8 data;
883         int ret;
884
885         if (!(chip->config3 & ADT7316_DA_EN_VIA_DAC_LDCA))
886                 return -EPERM;
887
888         ret = kstrtou8(buf, 10, &data);
889         if (ret || data > ADT7316_DA_EN_MODE_MASK)
890                 return -EINVAL;
891
892         dac_config = chip->dac_config & (~ADT7316_DA_EN_MODE_MASK);
893         dac_config |= data;
894
895         ret = chip->bus.write(chip->bus.client, ADT7316_DAC_CONFIG, dac_config);
896         if (ret)
897                 return -EIO;
898
899         chip->dac_config = dac_config;
900
901         return len;
902 }
903
904 static IIO_DEVICE_ATTR(DAC_update_mode, 0644,
905                 adt7316_show_DAC_update_mode,
906                 adt7316_store_DAC_update_mode,
907                 0);
908
909 static ssize_t adt7316_show_all_DAC_update_modes(struct device *dev,
910                 struct device_attribute *attr,
911                 char *buf)
912 {
913         struct iio_dev *dev_info = dev_to_iio_dev(dev);
914         struct adt7316_chip_info *chip = iio_priv(dev_info);
915
916         if (chip->config3 & ADT7316_DA_EN_VIA_DAC_LDCA)
917                 return sprintf(buf, "0 - auto at any MSB DAC writing\n"
918                                 "1 - auto at MSB DAC AB and CD writing\n"
919                                 "2 - auto at MSB DAC ABCD writing\n"
920                                 "3 - manual\n");
921         return sprintf(buf, "manual\n");
922 }
923
924 static IIO_DEVICE_ATTR(all_DAC_update_modes, 0444,
925                 adt7316_show_all_DAC_update_modes, NULL, 0);
926
927 static ssize_t adt7316_store_update_DAC(struct device *dev,
928                 struct device_attribute *attr,
929                 const char *buf,
930                 size_t len)
931 {
932         struct iio_dev *dev_info = dev_to_iio_dev(dev);
933         struct adt7316_chip_info *chip = iio_priv(dev_info);
934         u8 ldac_config;
935         u8 data;
936         int ret;
937
938         if (chip->config3 & ADT7316_DA_EN_VIA_DAC_LDCA) {
939                 if ((chip->dac_config & ADT7316_DA_EN_MODE_MASK) !=
940                         ADT7316_DA_EN_MODE_LDAC)
941                         return -EPERM;
942
943                 ret = kstrtou8(buf, 16, &data);
944                 if (ret || data > ADT7316_LDAC_EN_DA_MASK)
945                         return -EINVAL;
946
947                 ldac_config = chip->ldac_config & (~ADT7316_LDAC_EN_DA_MASK);
948                 ldac_config |= data;
949
950                 ret = chip->bus.write(chip->bus.client, ADT7316_LDAC_CONFIG,
951                         ldac_config);
952                 if (ret)
953                         return -EIO;
954         } else {
955                 gpio_set_value(chip->ldac_pin, 0);
956                 gpio_set_value(chip->ldac_pin, 1);
957         }
958
959         return len;
960 }
961
962 static IIO_DEVICE_ATTR(update_DAC, 0644,
963                 NULL,
964                 adt7316_store_update_DAC,
965                 0);
966
967 static ssize_t adt7316_show_DA_AB_Vref_bypass(struct device *dev,
968                 struct device_attribute *attr,
969                 char *buf)
970 {
971         struct iio_dev *dev_info = dev_to_iio_dev(dev);
972         struct adt7316_chip_info *chip = iio_priv(dev_info);
973
974         if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
975                 return -EPERM;
976
977         return sprintf(buf, "%d\n",
978                 !!(chip->dac_config & ADT7316_VREF_BYPASS_DAC_AB));
979 }
980
981 static ssize_t adt7316_store_DA_AB_Vref_bypass(struct device *dev,
982                 struct device_attribute *attr,
983                 const char *buf,
984                 size_t len)
985 {
986         struct iio_dev *dev_info = dev_to_iio_dev(dev);
987         struct adt7316_chip_info *chip = iio_priv(dev_info);
988         u8 dac_config;
989         int ret;
990
991         if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
992                 return -EPERM;
993
994         dac_config = chip->dac_config & (~ADT7316_VREF_BYPASS_DAC_AB);
995         if (buf[0] == '1')
996                 dac_config |= ADT7316_VREF_BYPASS_DAC_AB;
997
998         ret = chip->bus.write(chip->bus.client, ADT7316_DAC_CONFIG, dac_config);
999         if (ret)
1000                 return -EIO;
1001
1002         chip->dac_config = dac_config;
1003
1004         return len;
1005 }
1006
1007 static IIO_DEVICE_ATTR(DA_AB_Vref_bypass, 0644,
1008                 adt7316_show_DA_AB_Vref_bypass,
1009                 adt7316_store_DA_AB_Vref_bypass,
1010                 0);
1011
1012 static ssize_t adt7316_show_DA_CD_Vref_bypass(struct device *dev,
1013                 struct device_attribute *attr,
1014                 char *buf)
1015 {
1016         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1017         struct adt7316_chip_info *chip = iio_priv(dev_info);
1018
1019         if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
1020                 return -EPERM;
1021
1022         return sprintf(buf, "%d\n",
1023                 !!(chip->dac_config & ADT7316_VREF_BYPASS_DAC_CD));
1024 }
1025
1026 static ssize_t adt7316_store_DA_CD_Vref_bypass(struct device *dev,
1027                 struct device_attribute *attr,
1028                 const char *buf,
1029                 size_t len)
1030 {
1031         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1032         struct adt7316_chip_info *chip = iio_priv(dev_info);
1033         u8 dac_config;
1034         int ret;
1035
1036         if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
1037                 return -EPERM;
1038
1039         dac_config = chip->dac_config & (~ADT7316_VREF_BYPASS_DAC_CD);
1040         if (buf[0] == '1')
1041                 dac_config |= ADT7316_VREF_BYPASS_DAC_CD;
1042
1043         ret = chip->bus.write(chip->bus.client, ADT7316_DAC_CONFIG, dac_config);
1044         if (ret)
1045                 return -EIO;
1046
1047         chip->dac_config = dac_config;
1048
1049         return len;
1050 }
1051
1052 static IIO_DEVICE_ATTR(DA_CD_Vref_bypass, 0644,
1053                 adt7316_show_DA_CD_Vref_bypass,
1054                 adt7316_store_DA_CD_Vref_bypass,
1055                 0);
1056
1057 static ssize_t adt7316_show_DAC_internal_Vref(struct device *dev,
1058                 struct device_attribute *attr,
1059                 char *buf)
1060 {
1061         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1062         struct adt7316_chip_info *chip = iio_priv(dev_info);
1063
1064         if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
1065                 return sprintf(buf, "0x%x\n",
1066                         (chip->dac_config & ADT7516_DAC_IN_VREF_MASK) >>
1067                         ADT7516_DAC_IN_VREF_OFFSET);
1068         return sprintf(buf, "%d\n",
1069                        !!(chip->dac_config & ADT7316_DAC_IN_VREF));
1070 }
1071
1072 static ssize_t adt7316_store_DAC_internal_Vref(struct device *dev,
1073                 struct device_attribute *attr,
1074                 const char *buf,
1075                 size_t len)
1076 {
1077         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1078         struct adt7316_chip_info *chip = iio_priv(dev_info);
1079         u8 ldac_config;
1080         u8 data;
1081         int ret;
1082
1083         if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX) {
1084                 ret = kstrtou8(buf, 16, &data);
1085                 if (ret || data > 3)
1086                         return -EINVAL;
1087
1088                 ldac_config = chip->ldac_config & (~ADT7516_DAC_IN_VREF_MASK);
1089                 if (data & 0x1)
1090                         ldac_config |= ADT7516_DAC_AB_IN_VREF;
1091                 if (data & 0x2)
1092                         ldac_config |= ADT7516_DAC_CD_IN_VREF;
1093         } else {
1094                 ret = kstrtou8(buf, 16, &data);
1095                 if (ret)
1096                         return -EINVAL;
1097
1098                 ldac_config = chip->ldac_config & (~ADT7316_DAC_IN_VREF);
1099                 if (data)
1100                         ldac_config = chip->ldac_config | ADT7316_DAC_IN_VREF;
1101         }
1102
1103         ret = chip->bus.write(chip->bus.client, ADT7316_LDAC_CONFIG,
1104                         ldac_config);
1105         if (ret)
1106                 return -EIO;
1107
1108         chip->ldac_config = ldac_config;
1109
1110         return len;
1111 }
1112
1113 static IIO_DEVICE_ATTR(DAC_internal_Vref, 0644,
1114                 adt7316_show_DAC_internal_Vref,
1115                 adt7316_store_DAC_internal_Vref,
1116                 0);
1117
1118 static ssize_t adt7316_show_ad(struct adt7316_chip_info *chip,
1119                 int channel, char *buf)
1120 {
1121         u16 data;
1122         u8 msb, lsb;
1123         char sign = ' ';
1124         int ret;
1125
1126         if ((chip->config2 & ADT7316_AD_SINGLE_CH_MODE) &&
1127                 channel != (chip->config2 & ADT7516_AD_SINGLE_CH_MASK))
1128                 return -EPERM;
1129
1130         switch (channel) {
1131         case ADT7316_AD_SINGLE_CH_IN:
1132                 ret = chip->bus.read(chip->bus.client,
1133                         ADT7316_LSB_IN_TEMP_VDD, &lsb);
1134                 if (ret)
1135                         return -EIO;
1136
1137                 ret = chip->bus.read(chip->bus.client,
1138                         ADT7316_AD_MSB_DATA_BASE + channel, &msb);
1139                 if (ret)
1140                         return -EIO;
1141
1142                 data = msb << ADT7316_T_VALUE_FLOAT_OFFSET;
1143                 data |= lsb & ADT7316_LSB_IN_TEMP_MASK;
1144                 break;
1145         case ADT7316_AD_SINGLE_CH_VDD:
1146                 ret = chip->bus.read(chip->bus.client,
1147                         ADT7316_LSB_IN_TEMP_VDD, &lsb);
1148                 if (ret)
1149                         return -EIO;
1150
1151                 ret = chip->bus.read(chip->bus.client,
1152
1153                         ADT7316_AD_MSB_DATA_BASE + channel, &msb);
1154                 if (ret)
1155                         return -EIO;
1156
1157                 data = msb << ADT7316_T_VALUE_FLOAT_OFFSET;
1158                 data |= (lsb & ADT7316_LSB_VDD_MASK) >> ADT7316_LSB_VDD_OFFSET;
1159                 return sprintf(buf, "%d\n", data);
1160         default: /* ex_temp and ain */
1161                 ret = chip->bus.read(chip->bus.client,
1162                         ADT7316_LSB_EX_TEMP_AIN, &lsb);
1163                 if (ret)
1164                         return -EIO;
1165
1166                 ret = chip->bus.read(chip->bus.client,
1167                         ADT7316_AD_MSB_DATA_BASE + channel, &msb);
1168                 if (ret)
1169                         return -EIO;
1170
1171                 data = msb << ADT7316_T_VALUE_FLOAT_OFFSET;
1172                 data |= lsb & (ADT7316_LSB_EX_TEMP_MASK <<
1173                         (ADT7516_LSB_AIN_SHIFT * (channel -
1174                         (ADT7316_MSB_EX_TEMP - ADT7316_AD_MSB_DATA_BASE))));
1175
1176                 if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
1177                         return sprintf(buf, "%d\n", data);
1178
1179                 break;
1180         }
1181
1182         if (data & ADT7316_T_VALUE_SIGN) {
1183                 /* convert supplement to positive value */
1184                 data = (ADT7316_T_VALUE_SIGN << 1) - data;
1185                 sign = '-';
1186         }
1187
1188         return sprintf(buf, "%c%d.%.2d\n", sign,
1189                 (data >> ADT7316_T_VALUE_FLOAT_OFFSET),
1190                 (data & ADT7316_T_VALUE_FLOAT_MASK) * 25);
1191 }
1192
1193 static ssize_t adt7316_show_VDD(struct device *dev,
1194                 struct device_attribute *attr,
1195                 char *buf)
1196 {
1197         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1198         struct adt7316_chip_info *chip = iio_priv(dev_info);
1199
1200         return adt7316_show_ad(chip, ADT7316_AD_SINGLE_CH_VDD, buf);
1201 }
1202 static IIO_DEVICE_ATTR(VDD, 0444, adt7316_show_VDD, NULL, 0);
1203
1204 static ssize_t adt7316_show_in_temp(struct device *dev,
1205                 struct device_attribute *attr,
1206                 char *buf)
1207 {
1208         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1209         struct adt7316_chip_info *chip = iio_priv(dev_info);
1210
1211         return adt7316_show_ad(chip, ADT7316_AD_SINGLE_CH_IN, buf);
1212 }
1213
1214 static IIO_DEVICE_ATTR(in_temp, 0444, adt7316_show_in_temp, NULL, 0);
1215
1216 static ssize_t adt7316_show_ex_temp_AIN1(struct device *dev,
1217                 struct device_attribute *attr,
1218                 char *buf)
1219 {
1220         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1221         struct adt7316_chip_info *chip = iio_priv(dev_info);
1222
1223         return adt7316_show_ad(chip, ADT7316_AD_SINGLE_CH_EX, buf);
1224 }
1225
1226 static IIO_DEVICE_ATTR(ex_temp_AIN1, 0444, adt7316_show_ex_temp_AIN1,
1227                 NULL, 0);
1228 static IIO_DEVICE_ATTR(ex_temp, 0444, adt7316_show_ex_temp_AIN1, NULL, 0);
1229
1230 static ssize_t adt7316_show_AIN2(struct device *dev,
1231                 struct device_attribute *attr,
1232                 char *buf)
1233 {
1234         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1235         struct adt7316_chip_info *chip = iio_priv(dev_info);
1236
1237         return adt7316_show_ad(chip, ADT7516_AD_SINGLE_CH_AIN2, buf);
1238 }
1239 static IIO_DEVICE_ATTR(AIN2, 0444, adt7316_show_AIN2, NULL, 0);
1240
1241 static ssize_t adt7316_show_AIN3(struct device *dev,
1242                 struct device_attribute *attr,
1243                 char *buf)
1244 {
1245         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1246         struct adt7316_chip_info *chip = iio_priv(dev_info);
1247
1248         return adt7316_show_ad(chip, ADT7516_AD_SINGLE_CH_AIN3, buf);
1249 }
1250 static IIO_DEVICE_ATTR(AIN3, 0444, adt7316_show_AIN3, NULL, 0);
1251
1252 static ssize_t adt7316_show_AIN4(struct device *dev,
1253                 struct device_attribute *attr,
1254                 char *buf)
1255 {
1256         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1257         struct adt7316_chip_info *chip = iio_priv(dev_info);
1258
1259         return adt7316_show_ad(chip, ADT7516_AD_SINGLE_CH_AIN4, buf);
1260 }
1261 static IIO_DEVICE_ATTR(AIN4, 0444, adt7316_show_AIN4, NULL, 0);
1262
1263 static ssize_t adt7316_show_temp_offset(struct adt7316_chip_info *chip,
1264                 int offset_addr, char *buf)
1265 {
1266         int data;
1267         u8 val;
1268         int ret;
1269
1270         ret = chip->bus.read(chip->bus.client, offset_addr, &val);
1271         if (ret)
1272                 return -EIO;
1273
1274         data = (int)val;
1275         if (val & 0x80)
1276                 data -= 256;
1277
1278         return sprintf(buf, "%d\n", data);
1279 }
1280
1281 static ssize_t adt7316_store_temp_offset(struct adt7316_chip_info *chip,
1282                 int offset_addr, const char *buf, size_t len)
1283 {
1284         int data;
1285         u8 val;
1286         int ret;
1287
1288         ret = kstrtoint(buf, 10, &data);
1289         if (ret || data > 127 || data < -128)
1290                 return -EINVAL;
1291
1292         if (data < 0)
1293                 data += 256;
1294
1295         val = (u8)data;
1296
1297         ret = chip->bus.write(chip->bus.client, offset_addr, val);
1298         if (ret)
1299                 return -EIO;
1300
1301         return len;
1302 }
1303
1304 static ssize_t adt7316_show_in_temp_offset(struct device *dev,
1305                 struct device_attribute *attr,
1306                 char *buf)
1307 {
1308         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1309         struct adt7316_chip_info *chip = iio_priv(dev_info);
1310
1311         return adt7316_show_temp_offset(chip, ADT7316_IN_TEMP_OFFSET, buf);
1312 }
1313
1314 static ssize_t adt7316_store_in_temp_offset(struct device *dev,
1315                 struct device_attribute *attr,
1316                 const char *buf,
1317                 size_t len)
1318 {
1319         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1320         struct adt7316_chip_info *chip = iio_priv(dev_info);
1321
1322         return adt7316_store_temp_offset(chip, ADT7316_IN_TEMP_OFFSET, buf,
1323                         len);
1324 }
1325
1326 static IIO_DEVICE_ATTR(in_temp_offset, 0644,
1327                 adt7316_show_in_temp_offset,
1328                 adt7316_store_in_temp_offset, 0);
1329
1330 static ssize_t adt7316_show_ex_temp_offset(struct device *dev,
1331                 struct device_attribute *attr,
1332                 char *buf)
1333 {
1334         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1335         struct adt7316_chip_info *chip = iio_priv(dev_info);
1336
1337         return adt7316_show_temp_offset(chip, ADT7316_EX_TEMP_OFFSET, buf);
1338 }
1339
1340 static ssize_t adt7316_store_ex_temp_offset(struct device *dev,
1341                 struct device_attribute *attr,
1342                 const char *buf,
1343                 size_t len)
1344 {
1345         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1346         struct adt7316_chip_info *chip = iio_priv(dev_info);
1347
1348         return adt7316_store_temp_offset(chip, ADT7316_EX_TEMP_OFFSET, buf,
1349                         len);
1350 }
1351
1352 static IIO_DEVICE_ATTR(ex_temp_offset, 0644,
1353                 adt7316_show_ex_temp_offset,
1354                 adt7316_store_ex_temp_offset, 0);
1355
1356 static ssize_t adt7316_show_in_analog_temp_offset(struct device *dev,
1357                 struct device_attribute *attr,
1358                 char *buf)
1359 {
1360         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1361         struct adt7316_chip_info *chip = iio_priv(dev_info);
1362
1363         return adt7316_show_temp_offset(chip,
1364                         ADT7316_IN_ANALOG_TEMP_OFFSET, buf);
1365 }
1366
1367 static ssize_t adt7316_store_in_analog_temp_offset(struct device *dev,
1368                 struct device_attribute *attr,
1369                 const char *buf,
1370                 size_t len)
1371 {
1372         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1373         struct adt7316_chip_info *chip = iio_priv(dev_info);
1374
1375         return adt7316_store_temp_offset(chip,
1376                         ADT7316_IN_ANALOG_TEMP_OFFSET, buf, len);
1377 }
1378
1379 static IIO_DEVICE_ATTR(in_analog_temp_offset, 0644,
1380                 adt7316_show_in_analog_temp_offset,
1381                 adt7316_store_in_analog_temp_offset, 0);
1382
1383 static ssize_t adt7316_show_ex_analog_temp_offset(struct device *dev,
1384                 struct device_attribute *attr,
1385                 char *buf)
1386 {
1387         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1388         struct adt7316_chip_info *chip = iio_priv(dev_info);
1389
1390         return adt7316_show_temp_offset(chip,
1391                         ADT7316_EX_ANALOG_TEMP_OFFSET, buf);
1392 }
1393
1394 static ssize_t adt7316_store_ex_analog_temp_offset(struct device *dev,
1395                 struct device_attribute *attr,
1396                 const char *buf,
1397                 size_t len)
1398 {
1399         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1400         struct adt7316_chip_info *chip = iio_priv(dev_info);
1401
1402         return adt7316_store_temp_offset(chip,
1403                         ADT7316_EX_ANALOG_TEMP_OFFSET, buf, len);
1404 }
1405
1406 static IIO_DEVICE_ATTR(ex_analog_temp_offset, 0644,
1407                 adt7316_show_ex_analog_temp_offset,
1408                 adt7316_store_ex_analog_temp_offset, 0);
1409
1410 static ssize_t adt7316_show_DAC(struct adt7316_chip_info *chip,
1411                 int channel, char *buf)
1412 {
1413         u16 data = 0;
1414         u8 msb, lsb, offset;
1415         int ret;
1416
1417         if (channel >= ADT7316_DA_MSB_DATA_REGS ||
1418                 (channel == 0 &&
1419                 (chip->config3 & ADT7316_EN_IN_TEMP_PROP_DACA)) ||
1420                 (channel == 1 &&
1421                 (chip->config3 & ADT7316_EN_EX_TEMP_PROP_DACB)))
1422                 return -EPERM;
1423
1424         offset = chip->dac_bits - 8;
1425
1426         if (chip->dac_bits > 8) {
1427                 ret = chip->bus.read(chip->bus.client,
1428                         ADT7316_DA_DATA_BASE + channel * 2, &lsb);
1429                 if (ret)
1430                         return -EIO;
1431         }
1432
1433         ret = chip->bus.read(chip->bus.client,
1434                 ADT7316_DA_DATA_BASE + 1 + channel * 2, &msb);
1435         if (ret)
1436                 return -EIO;
1437
1438         if (chip->dac_bits == 12)
1439                 data = lsb >> ADT7316_DA_12_BIT_LSB_SHIFT;
1440         else if (chip->dac_bits == 10)
1441                 data = lsb >> ADT7316_DA_10_BIT_LSB_SHIFT;
1442         data |= msb << offset;
1443
1444         return sprintf(buf, "%d\n", data);
1445 }
1446
1447 static ssize_t adt7316_store_DAC(struct adt7316_chip_info *chip,
1448                 int channel, const char *buf, size_t len)
1449 {
1450         u8 msb, lsb, lsb_reg, offset;
1451         u16 data;
1452         int ret;
1453
1454         if (channel >= ADT7316_DA_MSB_DATA_REGS ||
1455                 (channel == 0 &&
1456                 (chip->config3 & ADT7316_EN_IN_TEMP_PROP_DACA)) ||
1457                 (channel == 1 &&
1458                 (chip->config3 & ADT7316_EN_EX_TEMP_PROP_DACB)))
1459                 return -EPERM;
1460
1461         offset = chip->dac_bits - 8;
1462
1463         ret = kstrtou16(buf, 10, &data);
1464         if (ret || data >= (1 << chip->dac_bits))
1465                 return -EINVAL;
1466
1467         if (chip->dac_bits > 8) {
1468                 lsb = data & ((1 << offset) - 1);
1469                 if (chip->dac_bits == 12)
1470                         lsb_reg = lsb << ADT7316_DA_12_BIT_LSB_SHIFT;
1471                 else
1472                         lsb_reg = lsb << ADT7316_DA_10_BIT_LSB_SHIFT;
1473                 ret = chip->bus.write(chip->bus.client,
1474                         ADT7316_DA_DATA_BASE + channel * 2, lsb_reg);
1475                 if (ret)
1476                         return -EIO;
1477         }
1478
1479         msb = data >> offset;
1480         ret = chip->bus.write(chip->bus.client,
1481                 ADT7316_DA_DATA_BASE + 1 + channel * 2, msb);
1482         if (ret)
1483                 return -EIO;
1484
1485         return len;
1486 }
1487
1488 static ssize_t adt7316_show_DAC_A(struct device *dev,
1489                 struct device_attribute *attr,
1490                 char *buf)
1491 {
1492         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1493         struct adt7316_chip_info *chip = iio_priv(dev_info);
1494
1495         return adt7316_show_DAC(chip, 0, buf);
1496 }
1497
1498 static ssize_t adt7316_store_DAC_A(struct device *dev,
1499                 struct device_attribute *attr,
1500                 const char *buf,
1501                 size_t len)
1502 {
1503         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1504         struct adt7316_chip_info *chip = iio_priv(dev_info);
1505
1506         return adt7316_store_DAC(chip, 0, buf, len);
1507 }
1508
1509 static IIO_DEVICE_ATTR(DAC_A, 0644, adt7316_show_DAC_A,
1510                 adt7316_store_DAC_A, 0);
1511
1512 static ssize_t adt7316_show_DAC_B(struct device *dev,
1513                 struct device_attribute *attr,
1514                 char *buf)
1515 {
1516         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1517         struct adt7316_chip_info *chip = iio_priv(dev_info);
1518
1519         return adt7316_show_DAC(chip, 1, buf);
1520 }
1521
1522 static ssize_t adt7316_store_DAC_B(struct device *dev,
1523                 struct device_attribute *attr,
1524                 const char *buf,
1525                 size_t len)
1526 {
1527         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1528         struct adt7316_chip_info *chip = iio_priv(dev_info);
1529
1530         return adt7316_store_DAC(chip, 1, buf, len);
1531 }
1532
1533 static IIO_DEVICE_ATTR(DAC_B, 0644, adt7316_show_DAC_B,
1534                 adt7316_store_DAC_B, 0);
1535
1536 static ssize_t adt7316_show_DAC_C(struct device *dev,
1537                 struct device_attribute *attr,
1538                 char *buf)
1539 {
1540         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1541         struct adt7316_chip_info *chip = iio_priv(dev_info);
1542
1543         return adt7316_show_DAC(chip, 2, buf);
1544 }
1545
1546 static ssize_t adt7316_store_DAC_C(struct device *dev,
1547                 struct device_attribute *attr,
1548                 const char *buf,
1549                 size_t len)
1550 {
1551         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1552         struct adt7316_chip_info *chip = iio_priv(dev_info);
1553
1554         return adt7316_store_DAC(chip, 2, buf, len);
1555 }
1556
1557 static IIO_DEVICE_ATTR(DAC_C, 0644, adt7316_show_DAC_C,
1558                 adt7316_store_DAC_C, 0);
1559
1560 static ssize_t adt7316_show_DAC_D(struct device *dev,
1561                 struct device_attribute *attr,
1562                 char *buf)
1563 {
1564         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1565         struct adt7316_chip_info *chip = iio_priv(dev_info);
1566
1567         return adt7316_show_DAC(chip, 3, buf);
1568 }
1569
1570 static ssize_t adt7316_store_DAC_D(struct device *dev,
1571                 struct device_attribute *attr,
1572                 const char *buf,
1573                 size_t len)
1574 {
1575         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1576         struct adt7316_chip_info *chip = iio_priv(dev_info);
1577
1578         return adt7316_store_DAC(chip, 3, buf, len);
1579 }
1580
1581 static IIO_DEVICE_ATTR(DAC_D, 0644, adt7316_show_DAC_D,
1582                 adt7316_store_DAC_D, 0);
1583
1584 static ssize_t adt7316_show_device_id(struct device *dev,
1585                 struct device_attribute *attr,
1586                 char *buf)
1587 {
1588         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1589         struct adt7316_chip_info *chip = iio_priv(dev_info);
1590         u8 id;
1591         int ret;
1592
1593         ret = chip->bus.read(chip->bus.client, ADT7316_DEVICE_ID, &id);
1594         if (ret)
1595                 return -EIO;
1596
1597         return sprintf(buf, "%d\n", id);
1598 }
1599
1600 static IIO_DEVICE_ATTR(device_id, 0444, adt7316_show_device_id, NULL, 0);
1601
1602 static ssize_t adt7316_show_manufactorer_id(struct device *dev,
1603                 struct device_attribute *attr,
1604                 char *buf)
1605 {
1606         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1607         struct adt7316_chip_info *chip = iio_priv(dev_info);
1608         u8 id;
1609         int ret;
1610
1611         ret = chip->bus.read(chip->bus.client, ADT7316_MANUFACTURE_ID, &id);
1612         if (ret)
1613                 return -EIO;
1614
1615         return sprintf(buf, "%d\n", id);
1616 }
1617
1618 static IIO_DEVICE_ATTR(manufactorer_id, 0444,
1619                 adt7316_show_manufactorer_id, NULL, 0);
1620
1621 static ssize_t adt7316_show_device_rev(struct device *dev,
1622                 struct device_attribute *attr,
1623                 char *buf)
1624 {
1625         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1626         struct adt7316_chip_info *chip = iio_priv(dev_info);
1627         u8 rev;
1628         int ret;
1629
1630         ret = chip->bus.read(chip->bus.client, ADT7316_DEVICE_REV, &rev);
1631         if (ret)
1632                 return -EIO;
1633
1634         return sprintf(buf, "%d\n", rev);
1635 }
1636
1637 static IIO_DEVICE_ATTR(device_rev, 0444, adt7316_show_device_rev, NULL, 0);
1638
1639 static ssize_t adt7316_show_bus_type(struct device *dev,
1640                 struct device_attribute *attr,
1641                 char *buf)
1642 {
1643         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1644         struct adt7316_chip_info *chip = iio_priv(dev_info);
1645         u8 stat;
1646         int ret;
1647
1648         ret = chip->bus.read(chip->bus.client, ADT7316_SPI_LOCK_STAT, &stat);
1649         if (ret)
1650                 return -EIO;
1651
1652         if (stat)
1653                 return sprintf(buf, "spi\n");
1654
1655         return sprintf(buf, "i2c\n");
1656 }
1657
1658 static IIO_DEVICE_ATTR(bus_type, 0444, adt7316_show_bus_type, NULL, 0);
1659
1660 static struct attribute *adt7316_attributes[] = {
1661         &iio_dev_attr_all_modes.dev_attr.attr,
1662         &iio_dev_attr_mode.dev_attr.attr,
1663         &iio_dev_attr_enabled.dev_attr.attr,
1664         &iio_dev_attr_ad_channel.dev_attr.attr,
1665         &iio_dev_attr_all_ad_channels.dev_attr.attr,
1666         &iio_dev_attr_disable_averaging.dev_attr.attr,
1667         &iio_dev_attr_enable_smbus_timeout.dev_attr.attr,
1668         &iio_dev_attr_powerdown.dev_attr.attr,
1669         &iio_dev_attr_fast_ad_clock.dev_attr.attr,
1670         &iio_dev_attr_da_high_resolution.dev_attr.attr,
1671         &iio_dev_attr_enable_proportion_DACA.dev_attr.attr,
1672         &iio_dev_attr_enable_proportion_DACB.dev_attr.attr,
1673         &iio_dev_attr_DAC_2Vref_channels_mask.dev_attr.attr,
1674         &iio_dev_attr_DAC_update_mode.dev_attr.attr,
1675         &iio_dev_attr_all_DAC_update_modes.dev_attr.attr,
1676         &iio_dev_attr_update_DAC.dev_attr.attr,
1677         &iio_dev_attr_DA_AB_Vref_bypass.dev_attr.attr,
1678         &iio_dev_attr_DA_CD_Vref_bypass.dev_attr.attr,
1679         &iio_dev_attr_DAC_internal_Vref.dev_attr.attr,
1680         &iio_dev_attr_VDD.dev_attr.attr,
1681         &iio_dev_attr_in_temp.dev_attr.attr,
1682         &iio_dev_attr_ex_temp.dev_attr.attr,
1683         &iio_dev_attr_in_temp_offset.dev_attr.attr,
1684         &iio_dev_attr_ex_temp_offset.dev_attr.attr,
1685         &iio_dev_attr_in_analog_temp_offset.dev_attr.attr,
1686         &iio_dev_attr_ex_analog_temp_offset.dev_attr.attr,
1687         &iio_dev_attr_DAC_A.dev_attr.attr,
1688         &iio_dev_attr_DAC_B.dev_attr.attr,
1689         &iio_dev_attr_DAC_C.dev_attr.attr,
1690         &iio_dev_attr_DAC_D.dev_attr.attr,
1691         &iio_dev_attr_device_id.dev_attr.attr,
1692         &iio_dev_attr_manufactorer_id.dev_attr.attr,
1693         &iio_dev_attr_device_rev.dev_attr.attr,
1694         &iio_dev_attr_bus_type.dev_attr.attr,
1695         NULL,
1696 };
1697
1698 static const struct attribute_group adt7316_attribute_group = {
1699         .attrs = adt7316_attributes,
1700 };
1701
1702 static struct attribute *adt7516_attributes[] = {
1703         &iio_dev_attr_all_modes.dev_attr.attr,
1704         &iio_dev_attr_mode.dev_attr.attr,
1705         &iio_dev_attr_select_ex_temp.dev_attr.attr,
1706         &iio_dev_attr_enabled.dev_attr.attr,
1707         &iio_dev_attr_ad_channel.dev_attr.attr,
1708         &iio_dev_attr_all_ad_channels.dev_attr.attr,
1709         &iio_dev_attr_disable_averaging.dev_attr.attr,
1710         &iio_dev_attr_enable_smbus_timeout.dev_attr.attr,
1711         &iio_dev_attr_powerdown.dev_attr.attr,
1712         &iio_dev_attr_fast_ad_clock.dev_attr.attr,
1713         &iio_dev_attr_AIN_internal_Vref.dev_attr.attr,
1714         &iio_dev_attr_da_high_resolution.dev_attr.attr,
1715         &iio_dev_attr_enable_proportion_DACA.dev_attr.attr,
1716         &iio_dev_attr_enable_proportion_DACB.dev_attr.attr,
1717         &iio_dev_attr_DAC_2Vref_channels_mask.dev_attr.attr,
1718         &iio_dev_attr_DAC_update_mode.dev_attr.attr,
1719         &iio_dev_attr_all_DAC_update_modes.dev_attr.attr,
1720         &iio_dev_attr_update_DAC.dev_attr.attr,
1721         &iio_dev_attr_DA_AB_Vref_bypass.dev_attr.attr,
1722         &iio_dev_attr_DA_CD_Vref_bypass.dev_attr.attr,
1723         &iio_dev_attr_DAC_internal_Vref.dev_attr.attr,
1724         &iio_dev_attr_VDD.dev_attr.attr,
1725         &iio_dev_attr_in_temp.dev_attr.attr,
1726         &iio_dev_attr_ex_temp_AIN1.dev_attr.attr,
1727         &iio_dev_attr_AIN2.dev_attr.attr,
1728         &iio_dev_attr_AIN3.dev_attr.attr,
1729         &iio_dev_attr_AIN4.dev_attr.attr,
1730         &iio_dev_attr_in_temp_offset.dev_attr.attr,
1731         &iio_dev_attr_ex_temp_offset.dev_attr.attr,
1732         &iio_dev_attr_in_analog_temp_offset.dev_attr.attr,
1733         &iio_dev_attr_ex_analog_temp_offset.dev_attr.attr,
1734         &iio_dev_attr_DAC_A.dev_attr.attr,
1735         &iio_dev_attr_DAC_B.dev_attr.attr,
1736         &iio_dev_attr_DAC_C.dev_attr.attr,
1737         &iio_dev_attr_DAC_D.dev_attr.attr,
1738         &iio_dev_attr_device_id.dev_attr.attr,
1739         &iio_dev_attr_manufactorer_id.dev_attr.attr,
1740         &iio_dev_attr_device_rev.dev_attr.attr,
1741         &iio_dev_attr_bus_type.dev_attr.attr,
1742         NULL,
1743 };
1744
1745 static const struct attribute_group adt7516_attribute_group = {
1746         .attrs = adt7516_attributes,
1747 };
1748
1749 static irqreturn_t adt7316_event_handler(int irq, void *private)
1750 {
1751         struct iio_dev *indio_dev = private;
1752         struct adt7316_chip_info *chip = iio_priv(indio_dev);
1753         u8 stat1, stat2;
1754         int ret;
1755         s64 time;
1756
1757         ret = chip->bus.read(chip->bus.client, ADT7316_INT_STAT1, &stat1);
1758         if (!ret) {
1759                 if ((chip->id & ID_FAMILY_MASK) != ID_ADT75XX)
1760                         stat1 &= 0x1F;
1761
1762                 time = iio_get_time_ns(indio_dev);
1763                 if (stat1 & BIT(0))
1764                         iio_push_event(indio_dev,
1765                                        IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
1766                                                             IIO_EV_TYPE_THRESH,
1767                                                             IIO_EV_DIR_RISING),
1768                                        time);
1769                 if (stat1 & BIT(1))
1770                         iio_push_event(indio_dev,
1771                                        IIO_UNMOD_EVENT_CODE(IIO_TEMP, 0,
1772                                                             IIO_EV_TYPE_THRESH,
1773                                                             IIO_EV_DIR_FALLING),
1774                                        time);
1775                 if (stat1 & BIT(2))
1776                         iio_push_event(indio_dev,
1777                                        IIO_UNMOD_EVENT_CODE(IIO_TEMP, 1,
1778                                                             IIO_EV_TYPE_THRESH,
1779                                                             IIO_EV_DIR_RISING),
1780                                        time);
1781                 if (stat1 & BIT(3))
1782                         iio_push_event(indio_dev,
1783                                        IIO_UNMOD_EVENT_CODE(IIO_TEMP, 1,
1784                                                             IIO_EV_TYPE_THRESH,
1785                                                             IIO_EV_DIR_FALLING),
1786                                        time);
1787                 if (stat1 & BIT(5))
1788                         iio_push_event(indio_dev,
1789                                        IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 1,
1790                                                             IIO_EV_TYPE_THRESH,
1791                                                             IIO_EV_DIR_EITHER),
1792                                        time);
1793                 if (stat1 & BIT(6))
1794                         iio_push_event(indio_dev,
1795                                        IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 2,
1796                                                             IIO_EV_TYPE_THRESH,
1797                                                             IIO_EV_DIR_EITHER),
1798                                        time);
1799                 if (stat1 & BIT(7))
1800                         iio_push_event(indio_dev,
1801                                        IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, 3,
1802                                                             IIO_EV_TYPE_THRESH,
1803                                                             IIO_EV_DIR_EITHER),
1804                                        time);
1805                 }
1806         ret = chip->bus.read(chip->bus.client, ADT7316_INT_STAT2, &stat2);
1807         if (!ret) {
1808                 if (stat2 & ADT7316_INT_MASK2_VDD)
1809                         iio_push_event(indio_dev,
1810                                        IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE,
1811                                                             0,
1812                                                             IIO_EV_TYPE_THRESH,
1813                                                             IIO_EV_DIR_RISING),
1814                                        iio_get_time_ns(indio_dev));
1815         }
1816
1817         return IRQ_HANDLED;
1818 }
1819
1820 /*
1821  * Show mask of enabled interrupts in Hex.
1822  */
1823 static ssize_t adt7316_show_int_mask(struct device *dev,
1824                 struct device_attribute *attr,
1825                 char *buf)
1826 {
1827         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1828         struct adt7316_chip_info *chip = iio_priv(dev_info);
1829
1830         return sprintf(buf, "0x%x\n", chip->int_mask);
1831 }
1832
1833 /*
1834  * Set 1 to the mask in Hex to enabled interrupts.
1835  */
1836 static ssize_t adt7316_set_int_mask(struct device *dev,
1837                 struct device_attribute *attr,
1838                 const char *buf,
1839                 size_t len)
1840 {
1841         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1842         struct adt7316_chip_info *chip = iio_priv(dev_info);
1843         u16 data;
1844         int ret;
1845         u8 mask;
1846
1847         ret = kstrtou16(buf, 16, &data);
1848         if (ret || data >= ADT7316_VDD_INT_MASK + 1)
1849                 return -EINVAL;
1850
1851         if (data & ADT7316_VDD_INT_MASK)
1852                 mask = 0;                       /* enable vdd int */
1853         else
1854                 mask = ADT7316_INT_MASK2_VDD;   /* disable vdd int */
1855
1856         ret = chip->bus.write(chip->bus.client, ADT7316_INT_MASK2, mask);
1857         if (!ret) {
1858                 chip->int_mask &= ~ADT7316_VDD_INT_MASK;
1859                 chip->int_mask |= data & ADT7316_VDD_INT_MASK;
1860         }
1861
1862         if (data & ADT7316_TEMP_AIN_INT_MASK) {
1863                 if ((chip->id & ID_FAMILY_MASK) == ID_ADT73XX)
1864                         /* mask in reg is opposite, set 1 to disable */
1865                         mask = (~data) & ADT7316_TEMP_INT_MASK;
1866                 else
1867                         /* mask in reg is opposite, set 1 to disable */
1868                         mask = (~data) & ADT7316_TEMP_AIN_INT_MASK;
1869         }
1870         ret = chip->bus.write(chip->bus.client, ADT7316_INT_MASK1, mask);
1871
1872         chip->int_mask = mask;
1873
1874         return len;
1875 }
1876
1877 static inline ssize_t adt7316_show_ad_bound(struct device *dev,
1878                 struct device_attribute *attr,
1879                 char *buf)
1880 {
1881         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
1882         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1883         struct adt7316_chip_info *chip = iio_priv(dev_info);
1884         u8 val;
1885         int data;
1886         int ret;
1887
1888         if ((chip->id & ID_FAMILY_MASK) == ID_ADT73XX &&
1889                 this_attr->address > ADT7316_EX_TEMP_LOW)
1890                 return -EPERM;
1891
1892         ret = chip->bus.read(chip->bus.client, this_attr->address, &val);
1893         if (ret)
1894                 return -EIO;
1895
1896         data = (int)val;
1897
1898         if (!((chip->id & ID_FAMILY_MASK) == ID_ADT75XX &&
1899                 (chip->config1 & ADT7516_SEL_AIN1_2_EX_TEMP_MASK) == 0)) {
1900                 if (data & 0x80)
1901                         data -= 256;
1902         }
1903
1904         return sprintf(buf, "%d\n", data);
1905 }
1906
1907 static inline ssize_t adt7316_set_ad_bound(struct device *dev,
1908                 struct device_attribute *attr,
1909                 const char *buf,
1910                 size_t len)
1911 {
1912         struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
1913         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1914         struct adt7316_chip_info *chip = iio_priv(dev_info);
1915         int data;
1916         u8 val;
1917         int ret;
1918
1919         if ((chip->id & ID_FAMILY_MASK) == ID_ADT73XX &&
1920                 this_attr->address > ADT7316_EX_TEMP_LOW)
1921                 return -EPERM;
1922
1923         ret = kstrtoint(buf, 10, &data);
1924         if (ret)
1925                 return -EINVAL;
1926
1927         if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX &&
1928                 (chip->config1 & ADT7516_SEL_AIN1_2_EX_TEMP_MASK) == 0) {
1929                 if (data > 255 || data < 0)
1930                         return -EINVAL;
1931         } else {
1932                 if (data > 127 || data < -128)
1933                         return -EINVAL;
1934
1935                 if (data < 0)
1936                         data += 256;
1937         }
1938
1939         val = (u8)data;
1940
1941         ret = chip->bus.write(chip->bus.client, this_attr->address, val);
1942         if (ret)
1943                 return -EIO;
1944
1945         return len;
1946 }
1947
1948 static ssize_t adt7316_show_int_enabled(struct device *dev,
1949                 struct device_attribute *attr,
1950                 char *buf)
1951 {
1952         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1953         struct adt7316_chip_info *chip = iio_priv(dev_info);
1954
1955         return sprintf(buf, "%d\n", !!(chip->config1 & ADT7316_INT_EN));
1956 }
1957
1958 static ssize_t adt7316_set_int_enabled(struct device *dev,
1959                 struct device_attribute *attr,
1960                 const char *buf,
1961                 size_t len)
1962 {
1963         struct iio_dev *dev_info = dev_to_iio_dev(dev);
1964         struct adt7316_chip_info *chip = iio_priv(dev_info);
1965         u8 config1;
1966         int ret;
1967
1968         config1 = chip->config1 & (~ADT7316_INT_EN);
1969         if (buf[0] == '1')
1970                 config1 |= ADT7316_INT_EN;
1971
1972         ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG1, config1);
1973         if (ret)
1974                 return -EIO;
1975
1976         chip->config1 = config1;
1977
1978         return len;
1979 }
1980
1981 static IIO_DEVICE_ATTR(int_mask,
1982                        0644,
1983                        adt7316_show_int_mask, adt7316_set_int_mask,
1984                        0);
1985 static IIO_DEVICE_ATTR(in_temp_high_value,
1986                        0644,
1987                        adt7316_show_ad_bound, adt7316_set_ad_bound,
1988                        ADT7316_IN_TEMP_HIGH);
1989 static IIO_DEVICE_ATTR(in_temp_low_value,
1990                        0644,
1991                        adt7316_show_ad_bound, adt7316_set_ad_bound,
1992                        ADT7316_IN_TEMP_LOW);
1993 static IIO_DEVICE_ATTR(ex_temp_high_value,
1994                        0644,
1995                        adt7316_show_ad_bound, adt7316_set_ad_bound,
1996                        ADT7316_EX_TEMP_HIGH);
1997 static IIO_DEVICE_ATTR(ex_temp_low_value,
1998                        0644,
1999                        adt7316_show_ad_bound, adt7316_set_ad_bound,
2000                        ADT7316_EX_TEMP_LOW);
2001
2002 /* NASTY duplication to be fixed */
2003 static IIO_DEVICE_ATTR(ex_temp_ain1_high_value,
2004                        0644,
2005                        adt7316_show_ad_bound, adt7316_set_ad_bound,
2006                        ADT7316_EX_TEMP_HIGH);
2007 static IIO_DEVICE_ATTR(ex_temp_ain1_low_value,
2008                        0644,
2009                        adt7316_show_ad_bound, adt7316_set_ad_bound,
2010                        ADT7316_EX_TEMP_LOW);
2011 static IIO_DEVICE_ATTR(ain2_high_value,
2012                        0644,
2013                        adt7316_show_ad_bound, adt7316_set_ad_bound,
2014                        ADT7516_AIN2_HIGH);
2015 static IIO_DEVICE_ATTR(ain2_low_value,
2016                        0644,
2017                        adt7316_show_ad_bound, adt7316_set_ad_bound,
2018                        ADT7516_AIN2_LOW);
2019 static IIO_DEVICE_ATTR(ain3_high_value,
2020                        0644,
2021                        adt7316_show_ad_bound, adt7316_set_ad_bound,
2022                        ADT7516_AIN3_HIGH);
2023 static IIO_DEVICE_ATTR(ain3_low_value,
2024                        0644,
2025                        adt7316_show_ad_bound, adt7316_set_ad_bound,
2026                        ADT7516_AIN3_LOW);
2027 static IIO_DEVICE_ATTR(ain4_high_value,
2028                        0644,
2029                        adt7316_show_ad_bound, adt7316_set_ad_bound,
2030                        ADT7516_AIN4_HIGH);
2031 static IIO_DEVICE_ATTR(ain4_low_value,
2032                        0644,
2033                        adt7316_show_ad_bound, adt7316_set_ad_bound,
2034                        ADT7516_AIN4_LOW);
2035 static IIO_DEVICE_ATTR(int_enabled,
2036                        0644,
2037                        adt7316_show_int_enabled,
2038                        adt7316_set_int_enabled, 0);
2039
2040 static struct attribute *adt7316_event_attributes[] = {
2041         &iio_dev_attr_int_mask.dev_attr.attr,
2042         &iio_dev_attr_in_temp_high_value.dev_attr.attr,
2043         &iio_dev_attr_in_temp_low_value.dev_attr.attr,
2044         &iio_dev_attr_ex_temp_high_value.dev_attr.attr,
2045         &iio_dev_attr_ex_temp_low_value.dev_attr.attr,
2046         &iio_dev_attr_int_enabled.dev_attr.attr,
2047         NULL,
2048 };
2049
2050 static const struct attribute_group adt7316_event_attribute_group = {
2051         .attrs = adt7316_event_attributes,
2052         .name = "events",
2053 };
2054
2055 static struct attribute *adt7516_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_ain1_high_value.dev_attr.attr,
2060         &iio_dev_attr_ex_temp_ain1_low_value.dev_attr.attr,
2061         &iio_dev_attr_ain2_high_value.dev_attr.attr,
2062         &iio_dev_attr_ain2_low_value.dev_attr.attr,
2063         &iio_dev_attr_ain3_high_value.dev_attr.attr,
2064         &iio_dev_attr_ain3_low_value.dev_attr.attr,
2065         &iio_dev_attr_ain4_high_value.dev_attr.attr,
2066         &iio_dev_attr_ain4_low_value.dev_attr.attr,
2067         &iio_dev_attr_int_enabled.dev_attr.attr,
2068         NULL,
2069 };
2070
2071 static const struct attribute_group adt7516_event_attribute_group = {
2072         .attrs = adt7516_event_attributes,
2073         .name = "events",
2074 };
2075
2076 #ifdef CONFIG_PM_SLEEP
2077 static int adt7316_disable(struct device *dev)
2078 {
2079         struct iio_dev *dev_info = dev_get_drvdata(dev);
2080         struct adt7316_chip_info *chip = iio_priv(dev_info);
2081
2082         return _adt7316_store_enabled(chip, 0);
2083 }
2084
2085 static int adt7316_enable(struct device *dev)
2086 {
2087         struct iio_dev *dev_info = dev_get_drvdata(dev);
2088         struct adt7316_chip_info *chip = iio_priv(dev_info);
2089
2090         return _adt7316_store_enabled(chip, 1);
2091 }
2092
2093 SIMPLE_DEV_PM_OPS(adt7316_pm_ops, adt7316_disable, adt7316_enable);
2094 EXPORT_SYMBOL_GPL(adt7316_pm_ops);
2095 #endif
2096
2097 static const struct iio_info adt7316_info = {
2098         .attrs = &adt7316_attribute_group,
2099         .event_attrs = &adt7316_event_attribute_group,
2100         .driver_module = THIS_MODULE,
2101 };
2102
2103 static const struct iio_info adt7516_info = {
2104         .attrs = &adt7516_attribute_group,
2105         .event_attrs = &adt7516_event_attribute_group,
2106         .driver_module = THIS_MODULE,
2107 };
2108
2109 /*
2110  * device probe and remove
2111  */
2112 int adt7316_probe(struct device *dev, struct adt7316_bus *bus,
2113                 const char *name)
2114 {
2115         struct adt7316_chip_info *chip;
2116         struct iio_dev *indio_dev;
2117         unsigned short *adt7316_platform_data = dev->platform_data;
2118         int ret = 0;
2119
2120         indio_dev = devm_iio_device_alloc(dev, sizeof(*chip));
2121         if (!indio_dev)
2122                 return -ENOMEM;
2123         chip = iio_priv(indio_dev);
2124         /* this is only used for device removal purposes */
2125         dev_set_drvdata(dev, indio_dev);
2126
2127         chip->bus = *bus;
2128
2129         if (name[4] == '3')
2130                 chip->id = ID_ADT7316 + (name[6] - '6');
2131         else if (name[4] == '5')
2132                 chip->id = ID_ADT7516 + (name[6] - '6');
2133         else
2134                 return -ENODEV;
2135
2136         chip->ldac_pin = adt7316_platform_data[1];
2137         if (chip->ldac_pin) {
2138                 chip->config3 |= ADT7316_DA_EN_VIA_DAC_LDCA;
2139                 if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
2140                         chip->config1 |= ADT7516_SEL_AIN3;
2141         }
2142         chip->int_mask = ADT7316_TEMP_INT_MASK | ADT7316_VDD_INT_MASK;
2143         if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
2144                 chip->int_mask |= ADT7516_AIN_INT_MASK;
2145
2146         indio_dev->dev.parent = dev;
2147         if ((chip->id & ID_FAMILY_MASK) == ID_ADT75XX)
2148                 indio_dev->info = &adt7516_info;
2149         else
2150                 indio_dev->info = &adt7316_info;
2151         indio_dev->name = name;
2152         indio_dev->modes = INDIO_DIRECT_MODE;
2153
2154         if (chip->bus.irq > 0) {
2155                 if (adt7316_platform_data[0])
2156                         chip->bus.irq_flags = adt7316_platform_data[0];
2157
2158                 ret = devm_request_threaded_irq(dev, chip->bus.irq,
2159                                                 NULL,
2160                                                 adt7316_event_handler,
2161                                                 chip->bus.irq_flags |
2162                                                 IRQF_ONESHOT,
2163                                                 indio_dev->name,
2164                                                 indio_dev);
2165                 if (ret)
2166                         return ret;
2167
2168                 if (chip->bus.irq_flags & IRQF_TRIGGER_HIGH)
2169                         chip->config1 |= ADT7316_INT_POLARITY;
2170         }
2171
2172         ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG1, chip->config1);
2173         if (ret)
2174                 return -EIO;
2175
2176         ret = chip->bus.write(chip->bus.client, ADT7316_CONFIG3, chip->config3);
2177         if (ret)
2178                 return -EIO;
2179
2180         ret = devm_iio_device_register(dev, indio_dev);
2181         if (ret)
2182                 return ret;
2183
2184         dev_info(dev, "%s temperature sensor, ADC and DAC registered.\n",
2185                         indio_dev->name);
2186
2187         return 0;
2188 }
2189 EXPORT_SYMBOL(adt7316_probe);
2190
2191 MODULE_AUTHOR("Sonic Zhang <sonic.zhang@analog.com>");
2192 MODULE_DESCRIPTION("Analog Devices ADT7316/7/8 and ADT7516/7/9 digital temperature sensor, ADC and DAC driver");
2193 MODULE_LICENSE("GPL v2");