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