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