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