xref: /linux/drivers/hwmon/max6697.c (revision 4b132aacb0768ac1e652cf517097ea6f237214b9)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2012 Guenter Roeck <linux@roeck-us.net>
4  *
5  * based on max1668.c
6  * Copyright (c) 2011 David George <david.george@ska.ac.za>
7  */
8 
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/slab.h>
12 #include <linux/jiffies.h>
13 #include <linux/i2c.h>
14 #include <linux/hwmon.h>
15 #include <linux/hwmon-sysfs.h>
16 #include <linux/err.h>
17 #include <linux/mutex.h>
18 #include <linux/of.h>
19 
20 #include <linux/platform_data/max6697.h>
21 
22 enum chips { max6581, max6602, max6622, max6636, max6689, max6693, max6694,
23 	     max6697, max6698, max6699 };
24 
25 /* Report local sensor as temp1 */
26 
27 static const u8 MAX6697_REG_TEMP[] = {
28 			0x07, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x08 };
29 static const u8 MAX6697_REG_TEMP_EXT[] = {
30 			0x57, 0x09, 0x52, 0x53, 0x54, 0x55, 0x56, 0 };
31 static const u8 MAX6697_REG_MAX[] = {
32 			0x17, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x18 };
33 static const u8 MAX6697_REG_CRIT[] = {
34 			0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27 };
35 
36 /*
37  * Map device tree / platform data register bit map to chip bit map.
38  * Applies to alert register and over-temperature register.
39  */
40 #define MAX6697_ALERT_MAP_BITS(reg)	((((reg) & 0x7e) >> 1) | \
41 				 (((reg) & 0x01) << 6) | ((reg) & 0x80))
42 #define MAX6697_OVERT_MAP_BITS(reg) (((reg) >> 1) | (((reg) & 0x01) << 7))
43 
44 #define MAX6697_REG_STAT(n)		(0x44 + (n))
45 
46 #define MAX6697_REG_CONFIG		0x41
47 #define MAX6581_CONF_EXTENDED		(1 << 1)
48 #define MAX6693_CONF_BETA		(1 << 2)
49 #define MAX6697_CONF_RESISTANCE		(1 << 3)
50 #define MAX6697_CONF_TIMEOUT		(1 << 5)
51 #define MAX6697_REG_ALERT_MASK		0x42
52 #define MAX6697_REG_OVERT_MASK		0x43
53 
54 #define MAX6581_REG_RESISTANCE		0x4a
55 #define MAX6581_REG_IDEALITY		0x4b
56 #define MAX6581_REG_IDEALITY_SELECT	0x4c
57 #define MAX6581_REG_OFFSET		0x4d
58 #define MAX6581_REG_OFFSET_SELECT	0x4e
59 #define MAX6581_OFFSET_MIN		-31750
60 #define MAX6581_OFFSET_MAX		31750
61 
62 #define MAX6697_CONV_TIME		156	/* ms per channel, worst case */
63 
64 struct max6697_chip_data {
65 	int channels;
66 	u32 have_ext;
67 	u32 have_crit;
68 	u32 have_fault;
69 	u8 valid_conf;
70 	const u8 *alarm_map;
71 };
72 
73 struct max6697_data {
74 	struct i2c_client *client;
75 
76 	enum chips type;
77 	const struct max6697_chip_data *chip;
78 
79 	int update_interval;	/* in milli-seconds */
80 	int temp_offset;	/* in degrees C */
81 
82 	struct mutex update_lock;
83 	unsigned long last_updated;	/* In jiffies */
84 	bool valid;		/* true if following fields are valid */
85 
86 	/* 1x local and up to 7x remote */
87 	u8 temp[8][4];		/* [nr][0]=temp [1]=ext [2]=max [3]=crit */
88 #define MAX6697_TEMP_INPUT	0
89 #define MAX6697_TEMP_EXT	1
90 #define MAX6697_TEMP_MAX	2
91 #define MAX6697_TEMP_CRIT	3
92 	u32 alarms;
93 };
94 
95 /* Diode fault status bits on MAX6581 are right shifted by one bit */
96 static const u8 max6581_alarm_map[] = {
97 	 0, 0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15,
98 	 16, 17, 18, 19, 20, 21, 22, 23 };
99 
100 static const struct max6697_chip_data max6697_chip_data[] = {
101 	[max6581] = {
102 		.channels = 8,
103 		.have_crit = 0xff,
104 		.have_ext = 0x7f,
105 		.have_fault = 0xfe,
106 		.valid_conf = MAX6581_CONF_EXTENDED | MAX6697_CONF_TIMEOUT,
107 		.alarm_map = max6581_alarm_map,
108 	},
109 	[max6602] = {
110 		.channels = 5,
111 		.have_crit = 0x12,
112 		.have_ext = 0x02,
113 		.have_fault = 0x1e,
114 		.valid_conf = MAX6697_CONF_RESISTANCE | MAX6697_CONF_TIMEOUT,
115 	},
116 	[max6622] = {
117 		.channels = 5,
118 		.have_crit = 0x12,
119 		.have_ext = 0x02,
120 		.have_fault = 0x1e,
121 		.valid_conf = MAX6697_CONF_RESISTANCE | MAX6697_CONF_TIMEOUT,
122 	},
123 	[max6636] = {
124 		.channels = 7,
125 		.have_crit = 0x72,
126 		.have_ext = 0x02,
127 		.have_fault = 0x7e,
128 		.valid_conf = MAX6697_CONF_RESISTANCE | MAX6697_CONF_TIMEOUT,
129 	},
130 	[max6689] = {
131 		.channels = 7,
132 		.have_crit = 0x72,
133 		.have_ext = 0x02,
134 		.have_fault = 0x7e,
135 		.valid_conf = MAX6697_CONF_RESISTANCE | MAX6697_CONF_TIMEOUT,
136 	},
137 	[max6693] = {
138 		.channels = 7,
139 		.have_crit = 0x72,
140 		.have_ext = 0x02,
141 		.have_fault = 0x7e,
142 		.valid_conf = MAX6697_CONF_RESISTANCE | MAX6693_CONF_BETA |
143 		  MAX6697_CONF_TIMEOUT,
144 	},
145 	[max6694] = {
146 		.channels = 5,
147 		.have_crit = 0x12,
148 		.have_ext = 0x02,
149 		.have_fault = 0x1e,
150 		.valid_conf = MAX6697_CONF_RESISTANCE | MAX6693_CONF_BETA |
151 		  MAX6697_CONF_TIMEOUT,
152 	},
153 	[max6697] = {
154 		.channels = 7,
155 		.have_crit = 0x72,
156 		.have_ext = 0x02,
157 		.have_fault = 0x7e,
158 		.valid_conf = MAX6697_CONF_RESISTANCE | MAX6697_CONF_TIMEOUT,
159 	},
160 	[max6698] = {
161 		.channels = 7,
162 		.have_crit = 0x72,
163 		.have_ext = 0x02,
164 		.have_fault = 0x0e,
165 		.valid_conf = MAX6697_CONF_RESISTANCE | MAX6697_CONF_TIMEOUT,
166 	},
167 	[max6699] = {
168 		.channels = 5,
169 		.have_crit = 0x12,
170 		.have_ext = 0x02,
171 		.have_fault = 0x1e,
172 		.valid_conf = MAX6697_CONF_RESISTANCE | MAX6697_CONF_TIMEOUT,
173 	},
174 };
175 
176 static inline int max6581_offset_to_millic(int val)
177 {
178 	return sign_extend32(val, 7) * 250;
179 }
180 
181 static struct max6697_data *max6697_update_device(struct device *dev)
182 {
183 	struct max6697_data *data = dev_get_drvdata(dev);
184 	struct i2c_client *client = data->client;
185 	struct max6697_data *ret = data;
186 	int val;
187 	int i;
188 	u32 alarms;
189 
190 	mutex_lock(&data->update_lock);
191 
192 	if (data->valid &&
193 	    !time_after(jiffies, data->last_updated
194 			+ msecs_to_jiffies(data->update_interval)))
195 		goto abort;
196 
197 	for (i = 0; i < data->chip->channels; i++) {
198 		if (data->chip->have_ext & (1 << i)) {
199 			val = i2c_smbus_read_byte_data(client,
200 						       MAX6697_REG_TEMP_EXT[i]);
201 			if (unlikely(val < 0)) {
202 				ret = ERR_PTR(val);
203 				goto abort;
204 			}
205 			data->temp[i][MAX6697_TEMP_EXT] = val;
206 		}
207 
208 		val = i2c_smbus_read_byte_data(client, MAX6697_REG_TEMP[i]);
209 		if (unlikely(val < 0)) {
210 			ret = ERR_PTR(val);
211 			goto abort;
212 		}
213 		data->temp[i][MAX6697_TEMP_INPUT] = val;
214 
215 		val = i2c_smbus_read_byte_data(client, MAX6697_REG_MAX[i]);
216 		if (unlikely(val < 0)) {
217 			ret = ERR_PTR(val);
218 			goto abort;
219 		}
220 		data->temp[i][MAX6697_TEMP_MAX] = val;
221 
222 		if (data->chip->have_crit & (1 << i)) {
223 			val = i2c_smbus_read_byte_data(client,
224 						       MAX6697_REG_CRIT[i]);
225 			if (unlikely(val < 0)) {
226 				ret = ERR_PTR(val);
227 				goto abort;
228 			}
229 			data->temp[i][MAX6697_TEMP_CRIT] = val;
230 		}
231 	}
232 
233 	alarms = 0;
234 	for (i = 0; i < 3; i++) {
235 		val = i2c_smbus_read_byte_data(client, MAX6697_REG_STAT(i));
236 		if (unlikely(val < 0)) {
237 			ret = ERR_PTR(val);
238 			goto abort;
239 		}
240 		alarms = (alarms << 8) | val;
241 	}
242 	data->alarms = alarms;
243 	data->last_updated = jiffies;
244 	data->valid = true;
245 abort:
246 	mutex_unlock(&data->update_lock);
247 
248 	return ret;
249 }
250 
251 static ssize_t temp_input_show(struct device *dev,
252 			       struct device_attribute *devattr, char *buf)
253 {
254 	int index = to_sensor_dev_attr(devattr)->index;
255 	struct max6697_data *data = max6697_update_device(dev);
256 	int temp;
257 
258 	if (IS_ERR(data))
259 		return PTR_ERR(data);
260 
261 	temp = (data->temp[index][MAX6697_TEMP_INPUT] - data->temp_offset) << 3;
262 	temp |= data->temp[index][MAX6697_TEMP_EXT] >> 5;
263 
264 	return sprintf(buf, "%d\n", temp * 125);
265 }
266 
267 static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
268 			 char *buf)
269 {
270 	int nr = to_sensor_dev_attr_2(devattr)->nr;
271 	int index = to_sensor_dev_attr_2(devattr)->index;
272 	struct max6697_data *data = max6697_update_device(dev);
273 	int temp;
274 
275 	if (IS_ERR(data))
276 		return PTR_ERR(data);
277 
278 	temp = data->temp[nr][index];
279 	temp -= data->temp_offset;
280 
281 	return sprintf(buf, "%d\n", temp * 1000);
282 }
283 
284 static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
285 			  char *buf)
286 {
287 	int index = to_sensor_dev_attr(attr)->index;
288 	struct max6697_data *data = max6697_update_device(dev);
289 
290 	if (IS_ERR(data))
291 		return PTR_ERR(data);
292 
293 	if (data->chip->alarm_map)
294 		index = data->chip->alarm_map[index];
295 
296 	return sprintf(buf, "%u\n", (data->alarms >> index) & 0x1);
297 }
298 
299 static ssize_t temp_store(struct device *dev,
300 			  struct device_attribute *devattr, const char *buf,
301 			  size_t count)
302 {
303 	int nr = to_sensor_dev_attr_2(devattr)->nr;
304 	int index = to_sensor_dev_attr_2(devattr)->index;
305 	struct max6697_data *data = dev_get_drvdata(dev);
306 	long temp;
307 	int ret;
308 
309 	ret = kstrtol(buf, 10, &temp);
310 	if (ret < 0)
311 		return ret;
312 
313 	mutex_lock(&data->update_lock);
314 	temp = clamp_val(temp, -1000000, 1000000);	/* prevent underflow */
315 	temp = DIV_ROUND_CLOSEST(temp, 1000) + data->temp_offset;
316 	temp = clamp_val(temp, 0, data->type == max6581 ? 255 : 127);
317 	data->temp[nr][index] = temp;
318 	ret = i2c_smbus_write_byte_data(data->client,
319 					index == 2 ? MAX6697_REG_MAX[nr]
320 						   : MAX6697_REG_CRIT[nr],
321 					temp);
322 	mutex_unlock(&data->update_lock);
323 
324 	return ret < 0 ? ret : count;
325 }
326 
327 static ssize_t offset_store(struct device *dev, struct device_attribute *devattr, const char *buf,
328 			    size_t count)
329 {
330 	int val, ret, index, select;
331 	struct max6697_data *data;
332 	bool channel_enabled;
333 	long temp;
334 
335 	index = to_sensor_dev_attr(devattr)->index;
336 	data = dev_get_drvdata(dev);
337 	ret = kstrtol(buf, 10, &temp);
338 	if (ret < 0)
339 		return ret;
340 
341 	mutex_lock(&data->update_lock);
342 	select = i2c_smbus_read_byte_data(data->client, MAX6581_REG_OFFSET_SELECT);
343 	if (select < 0) {
344 		ret = select;
345 		goto abort;
346 	}
347 	channel_enabled = (select & (1 << (index - 1)));
348 	temp = clamp_val(temp, MAX6581_OFFSET_MIN, MAX6581_OFFSET_MAX);
349 	val = DIV_ROUND_CLOSEST(temp, 250);
350 	/* disable the offset for channel if the new offset is 0 */
351 	if (val == 0) {
352 		if (channel_enabled)
353 			ret = i2c_smbus_write_byte_data(data->client, MAX6581_REG_OFFSET_SELECT,
354 							select & ~(1 << (index - 1)));
355 		ret = ret < 0 ? ret : count;
356 		goto abort;
357 	}
358 	if (!channel_enabled) {
359 		ret = i2c_smbus_write_byte_data(data->client, MAX6581_REG_OFFSET_SELECT,
360 						select | (1 << (index - 1)));
361 		if (ret < 0)
362 			goto abort;
363 	}
364 	ret = i2c_smbus_write_byte_data(data->client, MAX6581_REG_OFFSET, val);
365 	ret = ret < 0 ? ret : count;
366 
367 abort:
368 	mutex_unlock(&data->update_lock);
369 	return ret;
370 }
371 
372 static ssize_t offset_show(struct device *dev, struct device_attribute *devattr, char *buf)
373 {
374 	struct max6697_data *data;
375 	int select, ret, index;
376 
377 	index = to_sensor_dev_attr(devattr)->index;
378 	data = dev_get_drvdata(dev);
379 	mutex_lock(&data->update_lock);
380 	select = i2c_smbus_read_byte_data(data->client, MAX6581_REG_OFFSET_SELECT);
381 	if (select < 0)
382 		ret = select;
383 	else if (select & (1 << (index - 1)))
384 		ret = i2c_smbus_read_byte_data(data->client, MAX6581_REG_OFFSET);
385 	else
386 		ret = 0;
387 	mutex_unlock(&data->update_lock);
388 	return ret < 0 ? ret : sprintf(buf, "%d\n", max6581_offset_to_millic(ret));
389 }
390 
391 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0);
392 static SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp, 0, MAX6697_TEMP_MAX);
393 static SENSOR_DEVICE_ATTR_2_RW(temp1_crit, temp, 0, MAX6697_TEMP_CRIT);
394 
395 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp_input, 1);
396 static SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp, 1, MAX6697_TEMP_MAX);
397 static SENSOR_DEVICE_ATTR_2_RW(temp2_crit, temp, 1, MAX6697_TEMP_CRIT);
398 
399 static SENSOR_DEVICE_ATTR_RO(temp3_input, temp_input, 2);
400 static SENSOR_DEVICE_ATTR_2_RW(temp3_max, temp, 2, MAX6697_TEMP_MAX);
401 static SENSOR_DEVICE_ATTR_2_RW(temp3_crit, temp, 2, MAX6697_TEMP_CRIT);
402 
403 static SENSOR_DEVICE_ATTR_RO(temp4_input, temp_input, 3);
404 static SENSOR_DEVICE_ATTR_2_RW(temp4_max, temp, 3, MAX6697_TEMP_MAX);
405 static SENSOR_DEVICE_ATTR_2_RW(temp4_crit, temp, 3, MAX6697_TEMP_CRIT);
406 
407 static SENSOR_DEVICE_ATTR_RO(temp5_input, temp_input, 4);
408 static SENSOR_DEVICE_ATTR_2_RW(temp5_max, temp, 4, MAX6697_TEMP_MAX);
409 static SENSOR_DEVICE_ATTR_2_RW(temp5_crit, temp, 4, MAX6697_TEMP_CRIT);
410 
411 static SENSOR_DEVICE_ATTR_RO(temp6_input, temp_input, 5);
412 static SENSOR_DEVICE_ATTR_2_RW(temp6_max, temp, 5, MAX6697_TEMP_MAX);
413 static SENSOR_DEVICE_ATTR_2_RW(temp6_crit, temp, 5, MAX6697_TEMP_CRIT);
414 
415 static SENSOR_DEVICE_ATTR_RO(temp7_input, temp_input, 6);
416 static SENSOR_DEVICE_ATTR_2_RW(temp7_max, temp, 6, MAX6697_TEMP_MAX);
417 static SENSOR_DEVICE_ATTR_2_RW(temp7_crit, temp, 6, MAX6697_TEMP_CRIT);
418 
419 static SENSOR_DEVICE_ATTR_RO(temp8_input, temp_input, 7);
420 static SENSOR_DEVICE_ATTR_2_RW(temp8_max, temp, 7, MAX6697_TEMP_MAX);
421 static SENSOR_DEVICE_ATTR_2_RW(temp8_crit, temp, 7, MAX6697_TEMP_CRIT);
422 
423 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 22);
424 static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, 16);
425 static SENSOR_DEVICE_ATTR_RO(temp3_max_alarm, alarm, 17);
426 static SENSOR_DEVICE_ATTR_RO(temp4_max_alarm, alarm, 18);
427 static SENSOR_DEVICE_ATTR_RO(temp5_max_alarm, alarm, 19);
428 static SENSOR_DEVICE_ATTR_RO(temp6_max_alarm, alarm, 20);
429 static SENSOR_DEVICE_ATTR_RO(temp7_max_alarm, alarm, 21);
430 static SENSOR_DEVICE_ATTR_RO(temp8_max_alarm, alarm, 23);
431 
432 static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 15);
433 static SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm, alarm, 8);
434 static SENSOR_DEVICE_ATTR_RO(temp3_crit_alarm, alarm, 9);
435 static SENSOR_DEVICE_ATTR_RO(temp4_crit_alarm, alarm, 10);
436 static SENSOR_DEVICE_ATTR_RO(temp5_crit_alarm, alarm, 11);
437 static SENSOR_DEVICE_ATTR_RO(temp6_crit_alarm, alarm, 12);
438 static SENSOR_DEVICE_ATTR_RO(temp7_crit_alarm, alarm, 13);
439 static SENSOR_DEVICE_ATTR_RO(temp8_crit_alarm, alarm, 14);
440 
441 static SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, 1);
442 static SENSOR_DEVICE_ATTR_RO(temp3_fault, alarm, 2);
443 static SENSOR_DEVICE_ATTR_RO(temp4_fault, alarm, 3);
444 static SENSOR_DEVICE_ATTR_RO(temp5_fault, alarm, 4);
445 static SENSOR_DEVICE_ATTR_RO(temp6_fault, alarm, 5);
446 static SENSOR_DEVICE_ATTR_RO(temp7_fault, alarm, 6);
447 static SENSOR_DEVICE_ATTR_RO(temp8_fault, alarm, 7);
448 
449 /* There is no offset for local temperature so starting from temp2 */
450 static SENSOR_DEVICE_ATTR_RW(temp2_offset, offset, 1);
451 static SENSOR_DEVICE_ATTR_RW(temp3_offset, offset, 2);
452 static SENSOR_DEVICE_ATTR_RW(temp4_offset, offset, 3);
453 static SENSOR_DEVICE_ATTR_RW(temp5_offset, offset, 4);
454 static SENSOR_DEVICE_ATTR_RW(temp6_offset, offset, 5);
455 static SENSOR_DEVICE_ATTR_RW(temp7_offset, offset, 6);
456 static SENSOR_DEVICE_ATTR_RW(temp8_offset, offset, 7);
457 
458 static DEVICE_ATTR(dummy, 0, NULL, NULL);
459 
460 static umode_t max6697_is_visible(struct kobject *kobj, struct attribute *attr,
461 				  int index)
462 {
463 	struct device *dev = kobj_to_dev(kobj);
464 	struct max6697_data *data = dev_get_drvdata(dev);
465 	const struct max6697_chip_data *chip = data->chip;
466 	int channel = index / 7;	/* channel number */
467 	int nr = index % 7;		/* attribute index within channel */
468 
469 	if (channel >= chip->channels)
470 		return 0;
471 
472 	if ((nr == 3 || nr == 4) && !(chip->have_crit & (1 << channel)))
473 		return 0;
474 	if (nr == 5 && !(chip->have_fault & (1 << channel)))
475 		return 0;
476 	/* offset reg is only supported on max6581 remote channels */
477 	if (nr == 6)
478 		if (data->type != max6581 || channel == 0)
479 			return 0;
480 
481 	return attr->mode;
482 }
483 
484 /*
485  * max6697_is_visible uses the index into the following array to determine
486  * if attributes should be created or not. Any change in order or content
487  * must be matched in max6697_is_visible.
488  */
489 static struct attribute *max6697_attributes[] = {
490 	&sensor_dev_attr_temp1_input.dev_attr.attr,
491 	&sensor_dev_attr_temp1_max.dev_attr.attr,
492 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
493 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
494 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
495 	&dev_attr_dummy.attr,
496 	&dev_attr_dummy.attr,
497 
498 	&sensor_dev_attr_temp2_input.dev_attr.attr,
499 	&sensor_dev_attr_temp2_max.dev_attr.attr,
500 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
501 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
502 	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
503 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
504 	&sensor_dev_attr_temp2_offset.dev_attr.attr,
505 
506 	&sensor_dev_attr_temp3_input.dev_attr.attr,
507 	&sensor_dev_attr_temp3_max.dev_attr.attr,
508 	&sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
509 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
510 	&sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
511 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
512 	&sensor_dev_attr_temp3_offset.dev_attr.attr,
513 
514 	&sensor_dev_attr_temp4_input.dev_attr.attr,
515 	&sensor_dev_attr_temp4_max.dev_attr.attr,
516 	&sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
517 	&sensor_dev_attr_temp4_crit.dev_attr.attr,
518 	&sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
519 	&sensor_dev_attr_temp4_fault.dev_attr.attr,
520 	&sensor_dev_attr_temp4_offset.dev_attr.attr,
521 
522 	&sensor_dev_attr_temp5_input.dev_attr.attr,
523 	&sensor_dev_attr_temp5_max.dev_attr.attr,
524 	&sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
525 	&sensor_dev_attr_temp5_crit.dev_attr.attr,
526 	&sensor_dev_attr_temp5_crit_alarm.dev_attr.attr,
527 	&sensor_dev_attr_temp5_fault.dev_attr.attr,
528 	&sensor_dev_attr_temp5_offset.dev_attr.attr,
529 
530 	&sensor_dev_attr_temp6_input.dev_attr.attr,
531 	&sensor_dev_attr_temp6_max.dev_attr.attr,
532 	&sensor_dev_attr_temp6_max_alarm.dev_attr.attr,
533 	&sensor_dev_attr_temp6_crit.dev_attr.attr,
534 	&sensor_dev_attr_temp6_crit_alarm.dev_attr.attr,
535 	&sensor_dev_attr_temp6_fault.dev_attr.attr,
536 	&sensor_dev_attr_temp6_offset.dev_attr.attr,
537 
538 	&sensor_dev_attr_temp7_input.dev_attr.attr,
539 	&sensor_dev_attr_temp7_max.dev_attr.attr,
540 	&sensor_dev_attr_temp7_max_alarm.dev_attr.attr,
541 	&sensor_dev_attr_temp7_crit.dev_attr.attr,
542 	&sensor_dev_attr_temp7_crit_alarm.dev_attr.attr,
543 	&sensor_dev_attr_temp7_fault.dev_attr.attr,
544 	&sensor_dev_attr_temp7_offset.dev_attr.attr,
545 
546 	&sensor_dev_attr_temp8_input.dev_attr.attr,
547 	&sensor_dev_attr_temp8_max.dev_attr.attr,
548 	&sensor_dev_attr_temp8_max_alarm.dev_attr.attr,
549 	&sensor_dev_attr_temp8_crit.dev_attr.attr,
550 	&sensor_dev_attr_temp8_crit_alarm.dev_attr.attr,
551 	&sensor_dev_attr_temp8_fault.dev_attr.attr,
552 	&sensor_dev_attr_temp8_offset.dev_attr.attr,
553 	NULL
554 };
555 
556 static const struct attribute_group max6697_group = {
557 	.attrs = max6697_attributes, .is_visible = max6697_is_visible,
558 };
559 __ATTRIBUTE_GROUPS(max6697);
560 
561 static void max6697_get_config_of(struct device_node *node,
562 				  struct max6697_platform_data *pdata)
563 {
564 	int len;
565 	const __be32 *prop;
566 
567 	pdata->smbus_timeout_disable =
568 		of_property_read_bool(node, "smbus-timeout-disable");
569 	pdata->extended_range_enable =
570 		of_property_read_bool(node, "extended-range-enable");
571 	pdata->beta_compensation =
572 		of_property_read_bool(node, "beta-compensation-enable");
573 
574 	prop = of_get_property(node, "alert-mask", &len);
575 	if (prop && len == sizeof(u32))
576 		pdata->alert_mask = be32_to_cpu(prop[0]);
577 	prop = of_get_property(node, "over-temperature-mask", &len);
578 	if (prop && len == sizeof(u32))
579 		pdata->over_temperature_mask = be32_to_cpu(prop[0]);
580 	prop = of_get_property(node, "resistance-cancellation", &len);
581 	if (prop) {
582 		if (len == sizeof(u32))
583 			pdata->resistance_cancellation = be32_to_cpu(prop[0]);
584 		else
585 			pdata->resistance_cancellation = 0xfe;
586 	}
587 	prop = of_get_property(node, "transistor-ideality", &len);
588 	if (prop && len == 2 * sizeof(u32)) {
589 			pdata->ideality_mask = be32_to_cpu(prop[0]);
590 			pdata->ideality_value = be32_to_cpu(prop[1]);
591 	}
592 }
593 
594 static int max6697_init_chip(struct max6697_data *data,
595 			     struct i2c_client *client)
596 {
597 	struct max6697_platform_data *pdata = dev_get_platdata(&client->dev);
598 	struct max6697_platform_data p;
599 	const struct max6697_chip_data *chip = data->chip;
600 	int factor = chip->channels;
601 	int ret, reg;
602 
603 	/*
604 	 * Don't touch configuration if neither platform data nor OF
605 	 * configuration was specified. If that is the case, use the
606 	 * current chip configuration.
607 	 */
608 	if (!pdata && !client->dev.of_node) {
609 		reg = i2c_smbus_read_byte_data(client, MAX6697_REG_CONFIG);
610 		if (reg < 0)
611 			return reg;
612 		if (data->type == max6581) {
613 			if (reg & MAX6581_CONF_EXTENDED)
614 				data->temp_offset = 64;
615 			reg = i2c_smbus_read_byte_data(client,
616 						       MAX6581_REG_RESISTANCE);
617 			if (reg < 0)
618 				return reg;
619 			factor += hweight8(reg);
620 		} else {
621 			if (reg & MAX6697_CONF_RESISTANCE)
622 				factor++;
623 		}
624 		goto done;
625 	}
626 
627 	if (client->dev.of_node) {
628 		memset(&p, 0, sizeof(p));
629 		max6697_get_config_of(client->dev.of_node, &p);
630 		pdata = &p;
631 	}
632 
633 	reg = 0;
634 	if (pdata->smbus_timeout_disable &&
635 	    (chip->valid_conf & MAX6697_CONF_TIMEOUT)) {
636 		reg |= MAX6697_CONF_TIMEOUT;
637 	}
638 	if (pdata->extended_range_enable &&
639 	    (chip->valid_conf & MAX6581_CONF_EXTENDED)) {
640 		reg |= MAX6581_CONF_EXTENDED;
641 		data->temp_offset = 64;
642 	}
643 	if (pdata->resistance_cancellation &&
644 	    (chip->valid_conf & MAX6697_CONF_RESISTANCE)) {
645 		reg |= MAX6697_CONF_RESISTANCE;
646 		factor++;
647 	}
648 	if (pdata->beta_compensation &&
649 	    (chip->valid_conf & MAX6693_CONF_BETA)) {
650 		reg |= MAX6693_CONF_BETA;
651 	}
652 
653 	ret = i2c_smbus_write_byte_data(client, MAX6697_REG_CONFIG, reg);
654 	if (ret < 0)
655 		return ret;
656 
657 	ret = i2c_smbus_write_byte_data(client, MAX6697_REG_ALERT_MASK,
658 				MAX6697_ALERT_MAP_BITS(pdata->alert_mask));
659 	if (ret < 0)
660 		return ret;
661 
662 	ret = i2c_smbus_write_byte_data(client, MAX6697_REG_OVERT_MASK,
663 			MAX6697_OVERT_MAP_BITS(pdata->over_temperature_mask));
664 	if (ret < 0)
665 		return ret;
666 
667 	if (data->type == max6581) {
668 		factor += hweight8(pdata->resistance_cancellation >> 1);
669 		ret = i2c_smbus_write_byte_data(client, MAX6581_REG_RESISTANCE,
670 					pdata->resistance_cancellation >> 1);
671 		if (ret < 0)
672 			return ret;
673 		ret = i2c_smbus_write_byte_data(client, MAX6581_REG_IDEALITY,
674 						pdata->ideality_value);
675 		if (ret < 0)
676 			return ret;
677 		ret = i2c_smbus_write_byte_data(client,
678 						MAX6581_REG_IDEALITY_SELECT,
679 						pdata->ideality_mask >> 1);
680 		if (ret < 0)
681 			return ret;
682 	}
683 done:
684 	data->update_interval = factor * MAX6697_CONV_TIME;
685 	return 0;
686 }
687 
688 static int max6697_probe(struct i2c_client *client)
689 {
690 	struct i2c_adapter *adapter = client->adapter;
691 	struct device *dev = &client->dev;
692 	struct max6697_data *data;
693 	struct device *hwmon_dev;
694 	int err;
695 
696 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
697 		return -ENODEV;
698 
699 	data = devm_kzalloc(dev, sizeof(struct max6697_data), GFP_KERNEL);
700 	if (!data)
701 		return -ENOMEM;
702 
703 	data->type = (uintptr_t)i2c_get_match_data(client);
704 	data->chip = &max6697_chip_data[data->type];
705 	data->client = client;
706 	mutex_init(&data->update_lock);
707 
708 	err = max6697_init_chip(data, client);
709 	if (err)
710 		return err;
711 
712 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
713 							   data,
714 							   max6697_groups);
715 	return PTR_ERR_OR_ZERO(hwmon_dev);
716 }
717 
718 static const struct i2c_device_id max6697_id[] = {
719 	{ "max6581", max6581 },
720 	{ "max6602", max6602 },
721 	{ "max6622", max6622 },
722 	{ "max6636", max6636 },
723 	{ "max6689", max6689 },
724 	{ "max6693", max6693 },
725 	{ "max6694", max6694 },
726 	{ "max6697", max6697 },
727 	{ "max6698", max6698 },
728 	{ "max6699", max6699 },
729 	{ }
730 };
731 MODULE_DEVICE_TABLE(i2c, max6697_id);
732 
733 static const struct of_device_id __maybe_unused max6697_of_match[] = {
734 	{
735 		.compatible = "maxim,max6581",
736 		.data = (void *)max6581
737 	},
738 	{
739 		.compatible = "maxim,max6602",
740 		.data = (void *)max6602
741 	},
742 	{
743 		.compatible = "maxim,max6622",
744 		.data = (void *)max6622
745 	},
746 	{
747 		.compatible = "maxim,max6636",
748 		.data = (void *)max6636
749 	},
750 	{
751 		.compatible = "maxim,max6689",
752 		.data = (void *)max6689
753 	},
754 	{
755 		.compatible = "maxim,max6693",
756 		.data = (void *)max6693
757 	},
758 	{
759 		.compatible = "maxim,max6694",
760 		.data = (void *)max6694
761 	},
762 	{
763 		.compatible = "maxim,max6697",
764 		.data = (void *)max6697
765 	},
766 	{
767 		.compatible = "maxim,max6698",
768 		.data = (void *)max6698
769 	},
770 	{
771 		.compatible = "maxim,max6699",
772 		.data = (void *)max6699
773 	},
774 	{ },
775 };
776 MODULE_DEVICE_TABLE(of, max6697_of_match);
777 
778 static struct i2c_driver max6697_driver = {
779 	.driver = {
780 		.name	= "max6697",
781 		.of_match_table = of_match_ptr(max6697_of_match),
782 	},
783 	.probe = max6697_probe,
784 	.id_table = max6697_id,
785 };
786 
787 module_i2c_driver(max6697_driver);
788 
789 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
790 MODULE_DESCRIPTION("MAX6697 temperature sensor driver");
791 MODULE_LICENSE("GPL");
792