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