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