xref: /openbmc/linux/drivers/hwmon/max6697.c (revision a325f174)
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 = DIV_ROUND_CLOSEST(temp, 1000) + data->temp_offset;
315 	temp = clamp_val(temp, 0, data->type == max6581 ? 255 : 127);
316 	data->temp[nr][index] = temp;
317 	ret = i2c_smbus_write_byte_data(data->client,
318 					index == 2 ? MAX6697_REG_MAX[nr]
319 						   : MAX6697_REG_CRIT[nr],
320 					temp);
321 	mutex_unlock(&data->update_lock);
322 
323 	return ret < 0 ? ret : count;
324 }
325 
326 static ssize_t offset_store(struct device *dev, struct device_attribute *devattr, const char *buf,
327 			    size_t count)
328 {
329 	int val, ret, index, select;
330 	struct max6697_data *data;
331 	bool channel_enabled;
332 	long temp;
333 
334 	index = to_sensor_dev_attr(devattr)->index;
335 	data = dev_get_drvdata(dev);
336 	ret = kstrtol(buf, 10, &temp);
337 	if (ret < 0)
338 		return ret;
339 
340 	mutex_lock(&data->update_lock);
341 	select = i2c_smbus_read_byte_data(data->client, MAX6581_REG_OFFSET_SELECT);
342 	if (select < 0) {
343 		ret = select;
344 		goto abort;
345 	}
346 	channel_enabled = (select & (1 << (index - 1)));
347 	temp = clamp_val(temp, MAX6581_OFFSET_MIN, MAX6581_OFFSET_MAX);
348 	val = DIV_ROUND_CLOSEST(temp, 250);
349 	/* disable the offset for channel if the new offset is 0 */
350 	if (val == 0) {
351 		if (channel_enabled)
352 			ret = i2c_smbus_write_byte_data(data->client, MAX6581_REG_OFFSET_SELECT,
353 							select & ~(1 << (index - 1)));
354 		ret = ret < 0 ? ret : count;
355 		goto abort;
356 	}
357 	if (!channel_enabled) {
358 		ret = i2c_smbus_write_byte_data(data->client, MAX6581_REG_OFFSET_SELECT,
359 						select | (1 << (index - 1)));
360 		if (ret < 0)
361 			goto abort;
362 	}
363 	ret = i2c_smbus_write_byte_data(data->client, MAX6581_REG_OFFSET, val);
364 	ret = ret < 0 ? ret : count;
365 
366 abort:
367 	mutex_unlock(&data->update_lock);
368 	return ret;
369 }
370 
371 static ssize_t offset_show(struct device *dev, struct device_attribute *devattr, char *buf)
372 {
373 	struct max6697_data *data;
374 	int select, ret, index;
375 
376 	index = to_sensor_dev_attr(devattr)->index;
377 	data = dev_get_drvdata(dev);
378 	mutex_lock(&data->update_lock);
379 	select = i2c_smbus_read_byte_data(data->client, MAX6581_REG_OFFSET_SELECT);
380 	if (select < 0)
381 		ret = select;
382 	else if (select & (1 << (index - 1)))
383 		ret = i2c_smbus_read_byte_data(data->client, MAX6581_REG_OFFSET);
384 	else
385 		ret = 0;
386 	mutex_unlock(&data->update_lock);
387 	return ret < 0 ? ret : sprintf(buf, "%d\n", max6581_offset_to_millic(ret));
388 }
389 
390 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp_input, 0);
391 static SENSOR_DEVICE_ATTR_2_RW(temp1_max, temp, 0, MAX6697_TEMP_MAX);
392 static SENSOR_DEVICE_ATTR_2_RW(temp1_crit, temp, 0, MAX6697_TEMP_CRIT);
393 
394 static SENSOR_DEVICE_ATTR_RO(temp2_input, temp_input, 1);
395 static SENSOR_DEVICE_ATTR_2_RW(temp2_max, temp, 1, MAX6697_TEMP_MAX);
396 static SENSOR_DEVICE_ATTR_2_RW(temp2_crit, temp, 1, MAX6697_TEMP_CRIT);
397 
398 static SENSOR_DEVICE_ATTR_RO(temp3_input, temp_input, 2);
399 static SENSOR_DEVICE_ATTR_2_RW(temp3_max, temp, 2, MAX6697_TEMP_MAX);
400 static SENSOR_DEVICE_ATTR_2_RW(temp3_crit, temp, 2, MAX6697_TEMP_CRIT);
401 
402 static SENSOR_DEVICE_ATTR_RO(temp4_input, temp_input, 3);
403 static SENSOR_DEVICE_ATTR_2_RW(temp4_max, temp, 3, MAX6697_TEMP_MAX);
404 static SENSOR_DEVICE_ATTR_2_RW(temp4_crit, temp, 3, MAX6697_TEMP_CRIT);
405 
406 static SENSOR_DEVICE_ATTR_RO(temp5_input, temp_input, 4);
407 static SENSOR_DEVICE_ATTR_2_RW(temp5_max, temp, 4, MAX6697_TEMP_MAX);
408 static SENSOR_DEVICE_ATTR_2_RW(temp5_crit, temp, 4, MAX6697_TEMP_CRIT);
409 
410 static SENSOR_DEVICE_ATTR_RO(temp6_input, temp_input, 5);
411 static SENSOR_DEVICE_ATTR_2_RW(temp6_max, temp, 5, MAX6697_TEMP_MAX);
412 static SENSOR_DEVICE_ATTR_2_RW(temp6_crit, temp, 5, MAX6697_TEMP_CRIT);
413 
414 static SENSOR_DEVICE_ATTR_RO(temp7_input, temp_input, 6);
415 static SENSOR_DEVICE_ATTR_2_RW(temp7_max, temp, 6, MAX6697_TEMP_MAX);
416 static SENSOR_DEVICE_ATTR_2_RW(temp7_crit, temp, 6, MAX6697_TEMP_CRIT);
417 
418 static SENSOR_DEVICE_ATTR_RO(temp8_input, temp_input, 7);
419 static SENSOR_DEVICE_ATTR_2_RW(temp8_max, temp, 7, MAX6697_TEMP_MAX);
420 static SENSOR_DEVICE_ATTR_2_RW(temp8_crit, temp, 7, MAX6697_TEMP_CRIT);
421 
422 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 22);
423 static SENSOR_DEVICE_ATTR_RO(temp2_max_alarm, alarm, 16);
424 static SENSOR_DEVICE_ATTR_RO(temp3_max_alarm, alarm, 17);
425 static SENSOR_DEVICE_ATTR_RO(temp4_max_alarm, alarm, 18);
426 static SENSOR_DEVICE_ATTR_RO(temp5_max_alarm, alarm, 19);
427 static SENSOR_DEVICE_ATTR_RO(temp6_max_alarm, alarm, 20);
428 static SENSOR_DEVICE_ATTR_RO(temp7_max_alarm, alarm, 21);
429 static SENSOR_DEVICE_ATTR_RO(temp8_max_alarm, alarm, 23);
430 
431 static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 14);
432 static SENSOR_DEVICE_ATTR_RO(temp2_crit_alarm, alarm, 8);
433 static SENSOR_DEVICE_ATTR_RO(temp3_crit_alarm, alarm, 9);
434 static SENSOR_DEVICE_ATTR_RO(temp4_crit_alarm, alarm, 10);
435 static SENSOR_DEVICE_ATTR_RO(temp5_crit_alarm, alarm, 11);
436 static SENSOR_DEVICE_ATTR_RO(temp6_crit_alarm, alarm, 12);
437 static SENSOR_DEVICE_ATTR_RO(temp7_crit_alarm, alarm, 13);
438 static SENSOR_DEVICE_ATTR_RO(temp8_crit_alarm, alarm, 15);
439 
440 static SENSOR_DEVICE_ATTR_RO(temp2_fault, alarm, 1);
441 static SENSOR_DEVICE_ATTR_RO(temp3_fault, alarm, 2);
442 static SENSOR_DEVICE_ATTR_RO(temp4_fault, alarm, 3);
443 static SENSOR_DEVICE_ATTR_RO(temp5_fault, alarm, 4);
444 static SENSOR_DEVICE_ATTR_RO(temp6_fault, alarm, 5);
445 static SENSOR_DEVICE_ATTR_RO(temp7_fault, alarm, 6);
446 static SENSOR_DEVICE_ATTR_RO(temp8_fault, alarm, 7);
447 
448 /* There is no offset for local temperature so starting from temp2 */
449 static SENSOR_DEVICE_ATTR_RW(temp2_offset, offset, 1);
450 static SENSOR_DEVICE_ATTR_RW(temp3_offset, offset, 2);
451 static SENSOR_DEVICE_ATTR_RW(temp4_offset, offset, 3);
452 static SENSOR_DEVICE_ATTR_RW(temp5_offset, offset, 4);
453 static SENSOR_DEVICE_ATTR_RW(temp6_offset, offset, 5);
454 static SENSOR_DEVICE_ATTR_RW(temp7_offset, offset, 6);
455 static SENSOR_DEVICE_ATTR_RW(temp8_offset, offset, 7);
456 
457 static DEVICE_ATTR(dummy, 0, NULL, NULL);
458 
459 static umode_t max6697_is_visible(struct kobject *kobj, struct attribute *attr,
460 				  int index)
461 {
462 	struct device *dev = kobj_to_dev(kobj);
463 	struct max6697_data *data = dev_get_drvdata(dev);
464 	const struct max6697_chip_data *chip = data->chip;
465 	int channel = index / 7;	/* channel number */
466 	int nr = index % 7;		/* attribute index within channel */
467 
468 	if (channel >= chip->channels)
469 		return 0;
470 
471 	if ((nr == 3 || nr == 4) && !(chip->have_crit & (1 << channel)))
472 		return 0;
473 	if (nr == 5 && !(chip->have_fault & (1 << channel)))
474 		return 0;
475 	/* offset reg is only supported on max6581 remote channels */
476 	if (nr == 6)
477 		if (data->type != max6581 || channel == 0)
478 			return 0;
479 
480 	return attr->mode;
481 }
482 
483 /*
484  * max6697_is_visible uses the index into the following array to determine
485  * if attributes should be created or not. Any change in order or content
486  * must be matched in max6697_is_visible.
487  */
488 static struct attribute *max6697_attributes[] = {
489 	&sensor_dev_attr_temp1_input.dev_attr.attr,
490 	&sensor_dev_attr_temp1_max.dev_attr.attr,
491 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
492 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
493 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
494 	&dev_attr_dummy.attr,
495 	&dev_attr_dummy.attr,
496 
497 	&sensor_dev_attr_temp2_input.dev_attr.attr,
498 	&sensor_dev_attr_temp2_max.dev_attr.attr,
499 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
500 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
501 	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
502 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
503 	&sensor_dev_attr_temp2_offset.dev_attr.attr,
504 
505 	&sensor_dev_attr_temp3_input.dev_attr.attr,
506 	&sensor_dev_attr_temp3_max.dev_attr.attr,
507 	&sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
508 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
509 	&sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
510 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
511 	&sensor_dev_attr_temp3_offset.dev_attr.attr,
512 
513 	&sensor_dev_attr_temp4_input.dev_attr.attr,
514 	&sensor_dev_attr_temp4_max.dev_attr.attr,
515 	&sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
516 	&sensor_dev_attr_temp4_crit.dev_attr.attr,
517 	&sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
518 	&sensor_dev_attr_temp4_fault.dev_attr.attr,
519 	&sensor_dev_attr_temp4_offset.dev_attr.attr,
520 
521 	&sensor_dev_attr_temp5_input.dev_attr.attr,
522 	&sensor_dev_attr_temp5_max.dev_attr.attr,
523 	&sensor_dev_attr_temp5_max_alarm.dev_attr.attr,
524 	&sensor_dev_attr_temp5_crit.dev_attr.attr,
525 	&sensor_dev_attr_temp5_crit_alarm.dev_attr.attr,
526 	&sensor_dev_attr_temp5_fault.dev_attr.attr,
527 	&sensor_dev_attr_temp5_offset.dev_attr.attr,
528 
529 	&sensor_dev_attr_temp6_input.dev_attr.attr,
530 	&sensor_dev_attr_temp6_max.dev_attr.attr,
531 	&sensor_dev_attr_temp6_max_alarm.dev_attr.attr,
532 	&sensor_dev_attr_temp6_crit.dev_attr.attr,
533 	&sensor_dev_attr_temp6_crit_alarm.dev_attr.attr,
534 	&sensor_dev_attr_temp6_fault.dev_attr.attr,
535 	&sensor_dev_attr_temp6_offset.dev_attr.attr,
536 
537 	&sensor_dev_attr_temp7_input.dev_attr.attr,
538 	&sensor_dev_attr_temp7_max.dev_attr.attr,
539 	&sensor_dev_attr_temp7_max_alarm.dev_attr.attr,
540 	&sensor_dev_attr_temp7_crit.dev_attr.attr,
541 	&sensor_dev_attr_temp7_crit_alarm.dev_attr.attr,
542 	&sensor_dev_attr_temp7_fault.dev_attr.attr,
543 	&sensor_dev_attr_temp7_offset.dev_attr.attr,
544 
545 	&sensor_dev_attr_temp8_input.dev_attr.attr,
546 	&sensor_dev_attr_temp8_max.dev_attr.attr,
547 	&sensor_dev_attr_temp8_max_alarm.dev_attr.attr,
548 	&sensor_dev_attr_temp8_crit.dev_attr.attr,
549 	&sensor_dev_attr_temp8_crit_alarm.dev_attr.attr,
550 	&sensor_dev_attr_temp8_fault.dev_attr.attr,
551 	&sensor_dev_attr_temp8_offset.dev_attr.attr,
552 	NULL
553 };
554 
555 static const struct attribute_group max6697_group = {
556 	.attrs = max6697_attributes, .is_visible = max6697_is_visible,
557 };
558 __ATTRIBUTE_GROUPS(max6697);
559 
560 static void max6697_get_config_of(struct device_node *node,
561 				  struct max6697_platform_data *pdata)
562 {
563 	int len;
564 	const __be32 *prop;
565 
566 	pdata->smbus_timeout_disable =
567 		of_property_read_bool(node, "smbus-timeout-disable");
568 	pdata->extended_range_enable =
569 		of_property_read_bool(node, "extended-range-enable");
570 	pdata->beta_compensation =
571 		of_property_read_bool(node, "beta-compensation-enable");
572 
573 	prop = of_get_property(node, "alert-mask", &len);
574 	if (prop && len == sizeof(u32))
575 		pdata->alert_mask = be32_to_cpu(prop[0]);
576 	prop = of_get_property(node, "over-temperature-mask", &len);
577 	if (prop && len == sizeof(u32))
578 		pdata->over_temperature_mask = be32_to_cpu(prop[0]);
579 	prop = of_get_property(node, "resistance-cancellation", &len);
580 	if (prop) {
581 		if (len == sizeof(u32))
582 			pdata->resistance_cancellation = be32_to_cpu(prop[0]);
583 		else
584 			pdata->resistance_cancellation = 0xfe;
585 	}
586 	prop = of_get_property(node, "transistor-ideality", &len);
587 	if (prop && len == 2 * sizeof(u32)) {
588 			pdata->ideality_mask = be32_to_cpu(prop[0]);
589 			pdata->ideality_value = be32_to_cpu(prop[1]);
590 	}
591 }
592 
593 static int max6697_init_chip(struct max6697_data *data,
594 			     struct i2c_client *client)
595 {
596 	struct max6697_platform_data *pdata = dev_get_platdata(&client->dev);
597 	struct max6697_platform_data p;
598 	const struct max6697_chip_data *chip = data->chip;
599 	int factor = chip->channels;
600 	int ret, reg;
601 
602 	/*
603 	 * Don't touch configuration if neither platform data nor OF
604 	 * configuration was specified. If that is the case, use the
605 	 * current chip configuration.
606 	 */
607 	if (!pdata && !client->dev.of_node) {
608 		reg = i2c_smbus_read_byte_data(client, MAX6697_REG_CONFIG);
609 		if (reg < 0)
610 			return reg;
611 		if (data->type == max6581) {
612 			if (reg & MAX6581_CONF_EXTENDED)
613 				data->temp_offset = 64;
614 			reg = i2c_smbus_read_byte_data(client,
615 						       MAX6581_REG_RESISTANCE);
616 			if (reg < 0)
617 				return reg;
618 			factor += hweight8(reg);
619 		} else {
620 			if (reg & MAX6697_CONF_RESISTANCE)
621 				factor++;
622 		}
623 		goto done;
624 	}
625 
626 	if (client->dev.of_node) {
627 		memset(&p, 0, sizeof(p));
628 		max6697_get_config_of(client->dev.of_node, &p);
629 		pdata = &p;
630 	}
631 
632 	reg = 0;
633 	if (pdata->smbus_timeout_disable &&
634 	    (chip->valid_conf & MAX6697_CONF_TIMEOUT)) {
635 		reg |= MAX6697_CONF_TIMEOUT;
636 	}
637 	if (pdata->extended_range_enable &&
638 	    (chip->valid_conf & MAX6581_CONF_EXTENDED)) {
639 		reg |= MAX6581_CONF_EXTENDED;
640 		data->temp_offset = 64;
641 	}
642 	if (pdata->resistance_cancellation &&
643 	    (chip->valid_conf & MAX6697_CONF_RESISTANCE)) {
644 		reg |= MAX6697_CONF_RESISTANCE;
645 		factor++;
646 	}
647 	if (pdata->beta_compensation &&
648 	    (chip->valid_conf & MAX6693_CONF_BETA)) {
649 		reg |= MAX6693_CONF_BETA;
650 	}
651 
652 	ret = i2c_smbus_write_byte_data(client, MAX6697_REG_CONFIG, reg);
653 	if (ret < 0)
654 		return ret;
655 
656 	ret = i2c_smbus_write_byte_data(client, MAX6697_REG_ALERT_MASK,
657 				MAX6697_ALERT_MAP_BITS(pdata->alert_mask));
658 	if (ret < 0)
659 		return ret;
660 
661 	ret = i2c_smbus_write_byte_data(client, MAX6697_REG_OVERT_MASK,
662 			MAX6697_OVERT_MAP_BITS(pdata->over_temperature_mask));
663 	if (ret < 0)
664 		return ret;
665 
666 	if (data->type == max6581) {
667 		factor += hweight8(pdata->resistance_cancellation >> 1);
668 		ret = i2c_smbus_write_byte_data(client, MAX6581_REG_RESISTANCE,
669 					pdata->resistance_cancellation >> 1);
670 		if (ret < 0)
671 			return ret;
672 		ret = i2c_smbus_write_byte_data(client, MAX6581_REG_IDEALITY,
673 						pdata->ideality_value);
674 		if (ret < 0)
675 			return ret;
676 		ret = i2c_smbus_write_byte_data(client,
677 						MAX6581_REG_IDEALITY_SELECT,
678 						pdata->ideality_mask >> 1);
679 		if (ret < 0)
680 			return ret;
681 	}
682 done:
683 	data->update_interval = factor * MAX6697_CONV_TIME;
684 	return 0;
685 }
686 
687 static const struct i2c_device_id max6697_id[];
688 
689 static int max6697_probe(struct i2c_client *client)
690 {
691 	struct i2c_adapter *adapter = client->adapter;
692 	struct device *dev = &client->dev;
693 	struct max6697_data *data;
694 	struct device *hwmon_dev;
695 	int err;
696 
697 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
698 		return -ENODEV;
699 
700 	data = devm_kzalloc(dev, sizeof(struct max6697_data), GFP_KERNEL);
701 	if (!data)
702 		return -ENOMEM;
703 
704 	if (client->dev.of_node)
705 		data->type = (uintptr_t)of_device_get_match_data(&client->dev);
706 	else
707 		data->type = i2c_match_id(max6697_id, client)->driver_data;
708 	data->chip = &max6697_chip_data[data->type];
709 	data->client = client;
710 	mutex_init(&data->update_lock);
711 
712 	err = max6697_init_chip(data, client);
713 	if (err)
714 		return err;
715 
716 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
717 							   data,
718 							   max6697_groups);
719 	return PTR_ERR_OR_ZERO(hwmon_dev);
720 }
721 
722 static const struct i2c_device_id max6697_id[] = {
723 	{ "max6581", max6581 },
724 	{ "max6602", max6602 },
725 	{ "max6622", max6622 },
726 	{ "max6636", max6636 },
727 	{ "max6689", max6689 },
728 	{ "max6693", max6693 },
729 	{ "max6694", max6694 },
730 	{ "max6697", max6697 },
731 	{ "max6698", max6698 },
732 	{ "max6699", max6699 },
733 	{ }
734 };
735 MODULE_DEVICE_TABLE(i2c, max6697_id);
736 
737 static const struct of_device_id __maybe_unused max6697_of_match[] = {
738 	{
739 		.compatible = "maxim,max6581",
740 		.data = (void *)max6581
741 	},
742 	{
743 		.compatible = "maxim,max6602",
744 		.data = (void *)max6602
745 	},
746 	{
747 		.compatible = "maxim,max6622",
748 		.data = (void *)max6622
749 	},
750 	{
751 		.compatible = "maxim,max6636",
752 		.data = (void *)max6636
753 	},
754 	{
755 		.compatible = "maxim,max6689",
756 		.data = (void *)max6689
757 	},
758 	{
759 		.compatible = "maxim,max6693",
760 		.data = (void *)max6693
761 	},
762 	{
763 		.compatible = "maxim,max6694",
764 		.data = (void *)max6694
765 	},
766 	{
767 		.compatible = "maxim,max6697",
768 		.data = (void *)max6697
769 	},
770 	{
771 		.compatible = "maxim,max6698",
772 		.data = (void *)max6698
773 	},
774 	{
775 		.compatible = "maxim,max6699",
776 		.data = (void *)max6699
777 	},
778 	{ },
779 };
780 MODULE_DEVICE_TABLE(of, max6697_of_match);
781 
782 static struct i2c_driver max6697_driver = {
783 	.class = I2C_CLASS_HWMON,
784 	.driver = {
785 		.name	= "max6697",
786 		.of_match_table = of_match_ptr(max6697_of_match),
787 	},
788 	.probe = max6697_probe,
789 	.id_table = max6697_id,
790 };
791 
792 module_i2c_driver(max6697_driver);
793 
794 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
795 MODULE_DESCRIPTION("MAX6697 temperature sensor driver");
796 MODULE_LICENSE("GPL");
797