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