xref: /openbmc/linux/drivers/hwmon/lm80.c (revision 3fc41476)
1 /*
2  * lm80.c - From lm_sensors, Linux kernel modules for hardware
3  *	    monitoring
4  * Copyright (C) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
5  *			     and Philip Edelbrock <phil@netroedge.com>
6  *
7  * Ported to Linux 2.6 by Tiago Sousa <mirage@kaotik.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23 
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/jiffies.h>
28 #include <linux/i2c.h>
29 #include <linux/hwmon.h>
30 #include <linux/hwmon-sysfs.h>
31 #include <linux/err.h>
32 #include <linux/mutex.h>
33 
34 /* Addresses to scan */
35 static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d,
36 						0x2e, 0x2f, I2C_CLIENT_END };
37 
38 /* Many LM80 constants specified below */
39 
40 /* The LM80 registers */
41 #define LM80_REG_IN_MAX(nr)		(0x2a + (nr) * 2)
42 #define LM80_REG_IN_MIN(nr)		(0x2b + (nr) * 2)
43 #define LM80_REG_IN(nr)			(0x20 + (nr))
44 
45 #define LM80_REG_FAN1			0x28
46 #define LM80_REG_FAN2			0x29
47 #define LM80_REG_FAN_MIN(nr)		(0x3b + (nr))
48 
49 #define LM80_REG_TEMP			0x27
50 #define LM80_REG_TEMP_HOT_MAX		0x38
51 #define LM80_REG_TEMP_HOT_HYST		0x39
52 #define LM80_REG_TEMP_OS_MAX		0x3a
53 #define LM80_REG_TEMP_OS_HYST		0x3b
54 
55 #define LM80_REG_CONFIG			0x00
56 #define LM80_REG_ALARM1			0x01
57 #define LM80_REG_ALARM2			0x02
58 #define LM80_REG_MASK1			0x03
59 #define LM80_REG_MASK2			0x04
60 #define LM80_REG_FANDIV			0x05
61 #define LM80_REG_RES			0x06
62 
63 #define LM96080_REG_CONV_RATE		0x07
64 #define LM96080_REG_MAN_ID		0x3e
65 #define LM96080_REG_DEV_ID		0x3f
66 
67 
68 /*
69  * Conversions. Rounding and limit checking is only done on the TO_REG
70  * variants. Note that you should be a bit careful with which arguments
71  * these macros are called: arguments may be evaluated more than once.
72  * Fixing this is just not worth it.
73  */
74 
75 #define IN_TO_REG(val)		(clamp_val(((val) + 5) / 10, 0, 255))
76 #define IN_FROM_REG(val)	((val) * 10)
77 
78 static inline unsigned char FAN_TO_REG(unsigned rpm, unsigned div)
79 {
80 	if (rpm == 0)
81 		return 255;
82 	rpm = clamp_val(rpm, 1, 1000000);
83 	return clamp_val((1350000 + rpm * div / 2) / (rpm * div), 1, 254);
84 }
85 
86 #define FAN_FROM_REG(val, div)	((val) == 0 ? -1 : \
87 				(val) == 255 ? 0 : 1350000/((div) * (val)))
88 
89 #define TEMP_FROM_REG(reg)	((reg) * 125 / 32)
90 #define TEMP_TO_REG(temp)	(DIV_ROUND_CLOSEST(clamp_val((temp), \
91 					-128000, 127000), 1000) << 8)
92 
93 #define DIV_FROM_REG(val)		(1 << (val))
94 
95 enum temp_index {
96 	t_input = 0,
97 	t_hot_max,
98 	t_hot_hyst,
99 	t_os_max,
100 	t_os_hyst,
101 	t_num_temp
102 };
103 
104 static const u8 temp_regs[t_num_temp] = {
105 	[t_input] = LM80_REG_TEMP,
106 	[t_hot_max] = LM80_REG_TEMP_HOT_MAX,
107 	[t_hot_hyst] = LM80_REG_TEMP_HOT_HYST,
108 	[t_os_max] = LM80_REG_TEMP_OS_MAX,
109 	[t_os_hyst] = LM80_REG_TEMP_OS_HYST,
110 };
111 
112 enum in_index {
113 	i_input = 0,
114 	i_max,
115 	i_min,
116 	i_num_in
117 };
118 
119 enum fan_index {
120 	f_input,
121 	f_min,
122 	f_num_fan
123 };
124 
125 /*
126  * Client data (each client gets its own)
127  */
128 
129 struct lm80_data {
130 	struct i2c_client *client;
131 	struct mutex update_lock;
132 	char error;		/* !=0 if error occurred during last update */
133 	char valid;		/* !=0 if following fields are valid */
134 	unsigned long last_updated;	/* In jiffies */
135 
136 	u8 in[i_num_in][7];	/* Register value, 1st index is enum in_index */
137 	u8 fan[f_num_fan][2];	/* Register value, 1st index enum fan_index */
138 	u8 fan_div[2];		/* Register encoding, shifted right */
139 	s16 temp[t_num_temp];	/* Register values, normalized to 16 bit */
140 	u16 alarms;		/* Register encoding, combined */
141 };
142 
143 static int lm80_read_value(struct i2c_client *client, u8 reg)
144 {
145 	return i2c_smbus_read_byte_data(client, reg);
146 }
147 
148 static int lm80_write_value(struct i2c_client *client, u8 reg, u8 value)
149 {
150 	return i2c_smbus_write_byte_data(client, reg, value);
151 }
152 
153 /* Called when we have found a new LM80 and after read errors */
154 static void lm80_init_client(struct i2c_client *client)
155 {
156 	/*
157 	 * Reset all except Watchdog values and last conversion values
158 	 * This sets fan-divs to 2, among others. This makes most other
159 	 * initializations unnecessary
160 	 */
161 	lm80_write_value(client, LM80_REG_CONFIG, 0x80);
162 	/* Set 11-bit temperature resolution */
163 	lm80_write_value(client, LM80_REG_RES, 0x08);
164 
165 	/* Start monitoring */
166 	lm80_write_value(client, LM80_REG_CONFIG, 0x01);
167 }
168 
169 static struct lm80_data *lm80_update_device(struct device *dev)
170 {
171 	struct lm80_data *data = dev_get_drvdata(dev);
172 	struct i2c_client *client = data->client;
173 	int i;
174 	int rv;
175 	int prev_rv;
176 	struct lm80_data *ret = data;
177 
178 	mutex_lock(&data->update_lock);
179 
180 	if (data->error)
181 		lm80_init_client(client);
182 
183 	if (time_after(jiffies, data->last_updated + 2 * HZ) || !data->valid) {
184 		dev_dbg(dev, "Starting lm80 update\n");
185 		for (i = 0; i <= 6; i++) {
186 			rv = lm80_read_value(client, LM80_REG_IN(i));
187 			if (rv < 0)
188 				goto abort;
189 			data->in[i_input][i] = rv;
190 
191 			rv = lm80_read_value(client, LM80_REG_IN_MIN(i));
192 			if (rv < 0)
193 				goto abort;
194 			data->in[i_min][i] = rv;
195 
196 			rv = lm80_read_value(client, LM80_REG_IN_MAX(i));
197 			if (rv < 0)
198 				goto abort;
199 			data->in[i_max][i] = rv;
200 		}
201 
202 		rv = lm80_read_value(client, LM80_REG_FAN1);
203 		if (rv < 0)
204 			goto abort;
205 		data->fan[f_input][0] = rv;
206 
207 		rv = lm80_read_value(client, LM80_REG_FAN_MIN(1));
208 		if (rv < 0)
209 			goto abort;
210 		data->fan[f_min][0] = rv;
211 
212 		rv = lm80_read_value(client, LM80_REG_FAN2);
213 		if (rv < 0)
214 			goto abort;
215 		data->fan[f_input][1] = rv;
216 
217 		rv = lm80_read_value(client, LM80_REG_FAN_MIN(2));
218 		if (rv < 0)
219 			goto abort;
220 		data->fan[f_min][1] = rv;
221 
222 		prev_rv = rv = lm80_read_value(client, LM80_REG_TEMP);
223 		if (rv < 0)
224 			goto abort;
225 		rv = lm80_read_value(client, LM80_REG_RES);
226 		if (rv < 0)
227 			goto abort;
228 		data->temp[t_input] = (prev_rv << 8) | (rv & 0xf0);
229 
230 		for (i = t_input + 1; i < t_num_temp; i++) {
231 			rv = lm80_read_value(client, temp_regs[i]);
232 			if (rv < 0)
233 				goto abort;
234 			data->temp[i] = rv << 8;
235 		}
236 
237 		rv = lm80_read_value(client, LM80_REG_FANDIV);
238 		if (rv < 0)
239 			goto abort;
240 		data->fan_div[0] = (rv >> 2) & 0x03;
241 		data->fan_div[1] = (rv >> 4) & 0x03;
242 
243 		prev_rv = rv = lm80_read_value(client, LM80_REG_ALARM1);
244 		if (rv < 0)
245 			goto abort;
246 		rv = lm80_read_value(client, LM80_REG_ALARM2);
247 		if (rv < 0)
248 			goto abort;
249 		data->alarms = prev_rv + (rv << 8);
250 
251 		data->last_updated = jiffies;
252 		data->valid = 1;
253 		data->error = 0;
254 	}
255 	goto done;
256 
257 abort:
258 	ret = ERR_PTR(rv);
259 	data->valid = 0;
260 	data->error = 1;
261 
262 done:
263 	mutex_unlock(&data->update_lock);
264 
265 	return ret;
266 }
267 
268 /*
269  * Sysfs stuff
270  */
271 
272 static ssize_t in_show(struct device *dev, struct device_attribute *attr,
273 		       char *buf)
274 {
275 	struct lm80_data *data = lm80_update_device(dev);
276 	int index = to_sensor_dev_attr_2(attr)->index;
277 	int nr = to_sensor_dev_attr_2(attr)->nr;
278 
279 	if (IS_ERR(data))
280 		return PTR_ERR(data);
281 	return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr][index]));
282 }
283 
284 static ssize_t in_store(struct device *dev, struct device_attribute *attr,
285 			const char *buf, size_t count)
286 {
287 	struct lm80_data *data = dev_get_drvdata(dev);
288 	struct i2c_client *client = data->client;
289 	int index = to_sensor_dev_attr_2(attr)->index;
290 	int nr = to_sensor_dev_attr_2(attr)->nr;
291 	long val;
292 	u8 reg;
293 	int err = kstrtol(buf, 10, &val);
294 	if (err < 0)
295 		return err;
296 
297 	reg = nr == i_min ? LM80_REG_IN_MIN(index) : LM80_REG_IN_MAX(index);
298 
299 	mutex_lock(&data->update_lock);
300 	data->in[nr][index] = IN_TO_REG(val);
301 	lm80_write_value(client, reg, data->in[nr][index]);
302 	mutex_unlock(&data->update_lock);
303 	return count;
304 }
305 
306 static ssize_t fan_show(struct device *dev, struct device_attribute *attr,
307 			char *buf)
308 {
309 	int index = to_sensor_dev_attr_2(attr)->index;
310 	int nr = to_sensor_dev_attr_2(attr)->nr;
311 	struct lm80_data *data = lm80_update_device(dev);
312 	if (IS_ERR(data))
313 		return PTR_ERR(data);
314 	return sprintf(buf, "%d\n", FAN_FROM_REG(data->fan[nr][index],
315 		       DIV_FROM_REG(data->fan_div[index])));
316 }
317 
318 static ssize_t fan_div_show(struct device *dev, struct device_attribute *attr,
319 			    char *buf)
320 {
321 	int nr = to_sensor_dev_attr(attr)->index;
322 	struct lm80_data *data = lm80_update_device(dev);
323 	if (IS_ERR(data))
324 		return PTR_ERR(data);
325 	return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[nr]));
326 }
327 
328 static ssize_t fan_store(struct device *dev, struct device_attribute *attr,
329 			 const char *buf, size_t count)
330 {
331 	int index = to_sensor_dev_attr_2(attr)->index;
332 	int nr = to_sensor_dev_attr_2(attr)->nr;
333 	struct lm80_data *data = dev_get_drvdata(dev);
334 	struct i2c_client *client = data->client;
335 	unsigned long val;
336 	int err = kstrtoul(buf, 10, &val);
337 	if (err < 0)
338 		return err;
339 
340 	mutex_lock(&data->update_lock);
341 	data->fan[nr][index] = FAN_TO_REG(val,
342 					  DIV_FROM_REG(data->fan_div[index]));
343 	lm80_write_value(client, LM80_REG_FAN_MIN(index + 1),
344 			 data->fan[nr][index]);
345 	mutex_unlock(&data->update_lock);
346 	return count;
347 }
348 
349 /*
350  * Note: we save and restore the fan minimum here, because its value is
351  * determined in part by the fan divisor.  This follows the principle of
352  * least surprise; the user doesn't expect the fan minimum to change just
353  * because the divisor changed.
354  */
355 static ssize_t fan_div_store(struct device *dev,
356 			     struct device_attribute *attr, const char *buf,
357 			     size_t count)
358 {
359 	int nr = to_sensor_dev_attr(attr)->index;
360 	struct lm80_data *data = dev_get_drvdata(dev);
361 	struct i2c_client *client = data->client;
362 	unsigned long min, val;
363 	u8 reg;
364 	int rv;
365 
366 	rv = kstrtoul(buf, 10, &val);
367 	if (rv < 0)
368 		return rv;
369 
370 	/* Save fan_min */
371 	mutex_lock(&data->update_lock);
372 	min = FAN_FROM_REG(data->fan[f_min][nr],
373 			   DIV_FROM_REG(data->fan_div[nr]));
374 
375 	switch (val) {
376 	case 1:
377 		data->fan_div[nr] = 0;
378 		break;
379 	case 2:
380 		data->fan_div[nr] = 1;
381 		break;
382 	case 4:
383 		data->fan_div[nr] = 2;
384 		break;
385 	case 8:
386 		data->fan_div[nr] = 3;
387 		break;
388 	default:
389 		dev_err(dev,
390 			"fan_div value %ld not supported. Choose one of 1, 2, 4 or 8!\n",
391 			val);
392 		mutex_unlock(&data->update_lock);
393 		return -EINVAL;
394 	}
395 
396 	rv = lm80_read_value(client, LM80_REG_FANDIV);
397 	if (rv < 0) {
398 		mutex_unlock(&data->update_lock);
399 		return rv;
400 	}
401 	reg = (rv & ~(3 << (2 * (nr + 1))))
402 	    | (data->fan_div[nr] << (2 * (nr + 1)));
403 	lm80_write_value(client, LM80_REG_FANDIV, reg);
404 
405 	/* Restore fan_min */
406 	data->fan[f_min][nr] = FAN_TO_REG(min, DIV_FROM_REG(data->fan_div[nr]));
407 	lm80_write_value(client, LM80_REG_FAN_MIN(nr + 1),
408 			 data->fan[f_min][nr]);
409 	mutex_unlock(&data->update_lock);
410 
411 	return count;
412 }
413 
414 static ssize_t temp_show(struct device *dev, struct device_attribute *devattr,
415 			 char *buf)
416 {
417 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
418 	struct lm80_data *data = lm80_update_device(dev);
419 	if (IS_ERR(data))
420 		return PTR_ERR(data);
421 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
422 }
423 
424 static ssize_t temp_store(struct device *dev,
425 			  struct device_attribute *devattr, const char *buf,
426 			  size_t count)
427 {
428 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
429 	struct lm80_data *data = dev_get_drvdata(dev);
430 	struct i2c_client *client = data->client;
431 	int nr = attr->index;
432 	long val;
433 	int err = kstrtol(buf, 10, &val);
434 	if (err < 0)
435 		return err;
436 
437 	mutex_lock(&data->update_lock);
438 	data->temp[nr] = TEMP_TO_REG(val);
439 	lm80_write_value(client, temp_regs[nr], data->temp[nr] >> 8);
440 	mutex_unlock(&data->update_lock);
441 	return count;
442 }
443 
444 static ssize_t alarms_show(struct device *dev, struct device_attribute *attr,
445 			   char *buf)
446 {
447 	struct lm80_data *data = lm80_update_device(dev);
448 	if (IS_ERR(data))
449 		return PTR_ERR(data);
450 	return sprintf(buf, "%u\n", data->alarms);
451 }
452 
453 static ssize_t alarm_show(struct device *dev, struct device_attribute *attr,
454 			  char *buf)
455 {
456 	int bitnr = to_sensor_dev_attr(attr)->index;
457 	struct lm80_data *data = lm80_update_device(dev);
458 	if (IS_ERR(data))
459 		return PTR_ERR(data);
460 	return sprintf(buf, "%u\n", (data->alarms >> bitnr) & 1);
461 }
462 
463 static SENSOR_DEVICE_ATTR_2_RW(in0_min, in, i_min, 0);
464 static SENSOR_DEVICE_ATTR_2_RW(in1_min, in, i_min, 1);
465 static SENSOR_DEVICE_ATTR_2_RW(in2_min, in, i_min, 2);
466 static SENSOR_DEVICE_ATTR_2_RW(in3_min, in, i_min, 3);
467 static SENSOR_DEVICE_ATTR_2_RW(in4_min, in, i_min, 4);
468 static SENSOR_DEVICE_ATTR_2_RW(in5_min, in, i_min, 5);
469 static SENSOR_DEVICE_ATTR_2_RW(in6_min, in, i_min, 6);
470 static SENSOR_DEVICE_ATTR_2_RW(in0_max, in, i_max, 0);
471 static SENSOR_DEVICE_ATTR_2_RW(in1_max, in, i_max, 1);
472 static SENSOR_DEVICE_ATTR_2_RW(in2_max, in, i_max, 2);
473 static SENSOR_DEVICE_ATTR_2_RW(in3_max, in, i_max, 3);
474 static SENSOR_DEVICE_ATTR_2_RW(in4_max, in, i_max, 4);
475 static SENSOR_DEVICE_ATTR_2_RW(in5_max, in, i_max, 5);
476 static SENSOR_DEVICE_ATTR_2_RW(in6_max, in, i_max, 6);
477 static SENSOR_DEVICE_ATTR_2_RO(in0_input, in, i_input, 0);
478 static SENSOR_DEVICE_ATTR_2_RO(in1_input, in, i_input, 1);
479 static SENSOR_DEVICE_ATTR_2_RO(in2_input, in, i_input, 2);
480 static SENSOR_DEVICE_ATTR_2_RO(in3_input, in, i_input, 3);
481 static SENSOR_DEVICE_ATTR_2_RO(in4_input, in, i_input, 4);
482 static SENSOR_DEVICE_ATTR_2_RO(in5_input, in, i_input, 5);
483 static SENSOR_DEVICE_ATTR_2_RO(in6_input, in, i_input, 6);
484 static SENSOR_DEVICE_ATTR_2_RW(fan1_min, fan, f_min, 0);
485 static SENSOR_DEVICE_ATTR_2_RW(fan2_min, fan, f_min, 1);
486 static SENSOR_DEVICE_ATTR_2_RO(fan1_input, fan, f_input, 0);
487 static SENSOR_DEVICE_ATTR_2_RO(fan2_input, fan, f_input, 1);
488 static SENSOR_DEVICE_ATTR_RW(fan1_div, fan_div, 0);
489 static SENSOR_DEVICE_ATTR_RW(fan2_div, fan_div, 1);
490 static SENSOR_DEVICE_ATTR_RO(temp1_input, temp, t_input);
491 static SENSOR_DEVICE_ATTR_RW(temp1_max, temp, t_hot_max);
492 static SENSOR_DEVICE_ATTR_RW(temp1_max_hyst, temp, t_hot_hyst);
493 static SENSOR_DEVICE_ATTR_RW(temp1_crit, temp, t_os_max);
494 static SENSOR_DEVICE_ATTR_RW(temp1_crit_hyst, temp, t_os_hyst);
495 static DEVICE_ATTR_RO(alarms);
496 static SENSOR_DEVICE_ATTR_RO(in0_alarm, alarm, 0);
497 static SENSOR_DEVICE_ATTR_RO(in1_alarm, alarm, 1);
498 static SENSOR_DEVICE_ATTR_RO(in2_alarm, alarm, 2);
499 static SENSOR_DEVICE_ATTR_RO(in3_alarm, alarm, 3);
500 static SENSOR_DEVICE_ATTR_RO(in4_alarm, alarm, 4);
501 static SENSOR_DEVICE_ATTR_RO(in5_alarm, alarm, 5);
502 static SENSOR_DEVICE_ATTR_RO(in6_alarm, alarm, 6);
503 static SENSOR_DEVICE_ATTR_RO(fan1_alarm, alarm, 10);
504 static SENSOR_DEVICE_ATTR_RO(fan2_alarm, alarm, 11);
505 static SENSOR_DEVICE_ATTR_RO(temp1_max_alarm, alarm, 8);
506 static SENSOR_DEVICE_ATTR_RO(temp1_crit_alarm, alarm, 13);
507 
508 /*
509  * Real code
510  */
511 
512 static struct attribute *lm80_attrs[] = {
513 	&sensor_dev_attr_in0_min.dev_attr.attr,
514 	&sensor_dev_attr_in1_min.dev_attr.attr,
515 	&sensor_dev_attr_in2_min.dev_attr.attr,
516 	&sensor_dev_attr_in3_min.dev_attr.attr,
517 	&sensor_dev_attr_in4_min.dev_attr.attr,
518 	&sensor_dev_attr_in5_min.dev_attr.attr,
519 	&sensor_dev_attr_in6_min.dev_attr.attr,
520 	&sensor_dev_attr_in0_max.dev_attr.attr,
521 	&sensor_dev_attr_in1_max.dev_attr.attr,
522 	&sensor_dev_attr_in2_max.dev_attr.attr,
523 	&sensor_dev_attr_in3_max.dev_attr.attr,
524 	&sensor_dev_attr_in4_max.dev_attr.attr,
525 	&sensor_dev_attr_in5_max.dev_attr.attr,
526 	&sensor_dev_attr_in6_max.dev_attr.attr,
527 	&sensor_dev_attr_in0_input.dev_attr.attr,
528 	&sensor_dev_attr_in1_input.dev_attr.attr,
529 	&sensor_dev_attr_in2_input.dev_attr.attr,
530 	&sensor_dev_attr_in3_input.dev_attr.attr,
531 	&sensor_dev_attr_in4_input.dev_attr.attr,
532 	&sensor_dev_attr_in5_input.dev_attr.attr,
533 	&sensor_dev_attr_in6_input.dev_attr.attr,
534 	&sensor_dev_attr_fan1_min.dev_attr.attr,
535 	&sensor_dev_attr_fan2_min.dev_attr.attr,
536 	&sensor_dev_attr_fan1_input.dev_attr.attr,
537 	&sensor_dev_attr_fan2_input.dev_attr.attr,
538 	&sensor_dev_attr_fan1_div.dev_attr.attr,
539 	&sensor_dev_attr_fan2_div.dev_attr.attr,
540 	&sensor_dev_attr_temp1_input.dev_attr.attr,
541 	&sensor_dev_attr_temp1_max.dev_attr.attr,
542 	&sensor_dev_attr_temp1_max_hyst.dev_attr.attr,
543 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
544 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
545 	&dev_attr_alarms.attr,
546 	&sensor_dev_attr_in0_alarm.dev_attr.attr,
547 	&sensor_dev_attr_in1_alarm.dev_attr.attr,
548 	&sensor_dev_attr_in2_alarm.dev_attr.attr,
549 	&sensor_dev_attr_in3_alarm.dev_attr.attr,
550 	&sensor_dev_attr_in4_alarm.dev_attr.attr,
551 	&sensor_dev_attr_in5_alarm.dev_attr.attr,
552 	&sensor_dev_attr_in6_alarm.dev_attr.attr,
553 	&sensor_dev_attr_fan1_alarm.dev_attr.attr,
554 	&sensor_dev_attr_fan2_alarm.dev_attr.attr,
555 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
556 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
557 	NULL
558 };
559 ATTRIBUTE_GROUPS(lm80);
560 
561 /* Return 0 if detection is successful, -ENODEV otherwise */
562 static int lm80_detect(struct i2c_client *client, struct i2c_board_info *info)
563 {
564 	struct i2c_adapter *adapter = client->adapter;
565 	int i, cur, man_id, dev_id;
566 	const char *name = NULL;
567 
568 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
569 		return -ENODEV;
570 
571 	/* First check for unused bits, common to both chip types */
572 	if ((lm80_read_value(client, LM80_REG_ALARM2) & 0xc0)
573 	 || (lm80_read_value(client, LM80_REG_CONFIG) & 0x80))
574 		return -ENODEV;
575 
576 	/*
577 	 * The LM96080 has manufacturer and stepping/die rev registers so we
578 	 * can just check that. The LM80 does not have such registers so we
579 	 * have to use a more expensive trick.
580 	 */
581 	man_id = lm80_read_value(client, LM96080_REG_MAN_ID);
582 	dev_id = lm80_read_value(client, LM96080_REG_DEV_ID);
583 	if (man_id == 0x01 && dev_id == 0x08) {
584 		/* Check more unused bits for confirmation */
585 		if (lm80_read_value(client, LM96080_REG_CONV_RATE) & 0xfe)
586 			return -ENODEV;
587 
588 		name = "lm96080";
589 	} else {
590 		/* Check 6-bit addressing */
591 		for (i = 0x2a; i <= 0x3d; i++) {
592 			cur = i2c_smbus_read_byte_data(client, i);
593 			if ((i2c_smbus_read_byte_data(client, i + 0x40) != cur)
594 			 || (i2c_smbus_read_byte_data(client, i + 0x80) != cur)
595 			 || (i2c_smbus_read_byte_data(client, i + 0xc0) != cur))
596 				return -ENODEV;
597 		}
598 
599 		name = "lm80";
600 	}
601 
602 	strlcpy(info->type, name, I2C_NAME_SIZE);
603 
604 	return 0;
605 }
606 
607 static int lm80_probe(struct i2c_client *client,
608 		      const struct i2c_device_id *id)
609 {
610 	struct device *dev = &client->dev;
611 	struct device *hwmon_dev;
612 	struct lm80_data *data;
613 	int rv;
614 
615 	data = devm_kzalloc(dev, sizeof(struct lm80_data), GFP_KERNEL);
616 	if (!data)
617 		return -ENOMEM;
618 
619 	data->client = client;
620 	mutex_init(&data->update_lock);
621 
622 	/* Initialize the LM80 chip */
623 	lm80_init_client(client);
624 
625 	/* A few vars need to be filled upon startup */
626 	rv = lm80_read_value(client, LM80_REG_FAN_MIN(1));
627 	if (rv < 0)
628 		return rv;
629 	data->fan[f_min][0] = rv;
630 	rv = lm80_read_value(client, LM80_REG_FAN_MIN(2));
631 	if (rv < 0)
632 		return rv;
633 	data->fan[f_min][1] = rv;
634 
635 	hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
636 							   data, lm80_groups);
637 
638 	return PTR_ERR_OR_ZERO(hwmon_dev);
639 }
640 
641 /*
642  * Driver data (common to all clients)
643  */
644 
645 static const struct i2c_device_id lm80_id[] = {
646 	{ "lm80", 0 },
647 	{ "lm96080", 1 },
648 	{ }
649 };
650 MODULE_DEVICE_TABLE(i2c, lm80_id);
651 
652 static struct i2c_driver lm80_driver = {
653 	.class		= I2C_CLASS_HWMON,
654 	.driver = {
655 		.name	= "lm80",
656 	},
657 	.probe		= lm80_probe,
658 	.id_table	= lm80_id,
659 	.detect		= lm80_detect,
660 	.address_list	= normal_i2c,
661 };
662 
663 module_i2c_driver(lm80_driver);
664 
665 MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl> and "
666 	"Philip Edelbrock <phil@netroedge.com>");
667 MODULE_DESCRIPTION("LM80 driver");
668 MODULE_LICENSE("GPL");
669