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