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