xref: /openbmc/linux/drivers/hwmon/lm90.c (revision 643d1f7f)
1 /*
2  * lm90.c - Part of lm_sensors, Linux kernel modules for hardware
3  *          monitoring
4  * Copyright (C) 2003-2006  Jean Delvare <khali@linux-fr.org>
5  *
6  * Based on the lm83 driver. The LM90 is a sensor chip made by National
7  * Semiconductor. It reports up to two temperatures (its own plus up to
8  * one external one) with a 0.125 deg resolution (1 deg for local
9  * temperature) and a 3-4 deg accuracy. Complete datasheet can be
10  * obtained from National's website at:
11  *   http://www.national.com/pf/LM/LM90.html
12  *
13  * This driver also supports the LM89 and LM99, two other sensor chips
14  * made by National Semiconductor. Both have an increased remote
15  * temperature measurement accuracy (1 degree), and the LM99
16  * additionally shifts remote temperatures (measured and limits) by 16
17  * degrees, which allows for higher temperatures measurement. The
18  * driver doesn't handle it since it can be done easily in user-space.
19  * Complete datasheets can be obtained from National's website at:
20  *   http://www.national.com/pf/LM/LM89.html
21  *   http://www.national.com/pf/LM/LM99.html
22  * Note that there is no way to differentiate between both chips.
23  *
24  * This driver also supports the LM86, another sensor chip made by
25  * National Semiconductor. It is exactly similar to the LM90 except it
26  * has a higher accuracy.
27  * Complete datasheet can be obtained from National's website at:
28  *   http://www.national.com/pf/LM/LM86.html
29  *
30  * This driver also supports the ADM1032, a sensor chip made by Analog
31  * Devices. That chip is similar to the LM90, with a few differences
32  * that are not handled by this driver. Complete datasheet can be
33  * obtained from Analog's website at:
34  *   http://www.analog.com/en/prod/0,2877,ADM1032,00.html
35  * Among others, it has a higher accuracy than the LM90, much like the
36  * LM86 does.
37  *
38  * This driver also supports the MAX6657, MAX6658 and MAX6659 sensor
39  * chips made by Maxim. These chips are similar to the LM86. Complete
40  * datasheet can be obtained at Maxim's website at:
41  *   http://www.maxim-ic.com/quick_view2.cfm/qv_pk/2578
42  * Note that there is no easy way to differentiate between the three
43  * variants. The extra address and features of the MAX6659 are not
44  * supported by this driver. These chips lack the remote temperature
45  * offset feature.
46  *
47  * This driver also supports the MAX6680 and MAX6681, two other sensor
48  * chips made by Maxim. These are quite similar to the other Maxim
49  * chips. Complete datasheet can be obtained at:
50  *   http://www.maxim-ic.com/quick_view2.cfm/qv_pk/3370
51  * The MAX6680 and MAX6681 only differ in the pinout so they can be
52  * treated identically.
53  *
54  * This driver also supports the ADT7461 chip from Analog Devices but
55  * only in its "compatability mode". If an ADT7461 chip is found but
56  * is configured in non-compatible mode (where its temperature
57  * register values are decoded differently) it is ignored by this
58  * driver. Complete datasheet can be obtained from Analog's website
59  * at:
60  *   http://www.analog.com/en/prod/0,2877,ADT7461,00.html
61  *
62  * Since the LM90 was the first chipset supported by this driver, most
63  * comments will refer to this chipset, but are actually general and
64  * concern all supported chipsets, unless mentioned otherwise.
65  *
66  * This program is free software; you can redistribute it and/or modify
67  * it under the terms of the GNU General Public License as published by
68  * the Free Software Foundation; either version 2 of the License, or
69  * (at your option) any later version.
70  *
71  * This program is distributed in the hope that it will be useful,
72  * but WITHOUT ANY WARRANTY; without even the implied warranty of
73  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
74  * GNU General Public License for more details.
75  *
76  * You should have received a copy of the GNU General Public License
77  * along with this program; if not, write to the Free Software
78  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
79  */
80 
81 #include <linux/module.h>
82 #include <linux/init.h>
83 #include <linux/slab.h>
84 #include <linux/jiffies.h>
85 #include <linux/i2c.h>
86 #include <linux/hwmon-sysfs.h>
87 #include <linux/hwmon.h>
88 #include <linux/err.h>
89 #include <linux/mutex.h>
90 #include <linux/sysfs.h>
91 
92 /*
93  * Addresses to scan
94  * Address is fully defined internally and cannot be changed except for
95  * MAX6659, MAX6680 and MAX6681.
96  * LM86, LM89, LM90, LM99, ADM1032, ADM1032-1, ADT7461, MAX6657 and MAX6658
97  * have address 0x4c.
98  * ADM1032-2, ADT7461-2, LM89-1, and LM99-1 have address 0x4d.
99  * MAX6659 can have address 0x4c, 0x4d or 0x4e (unsupported).
100  * MAX6680 and MAX6681 can have address 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b,
101  * 0x4c, 0x4d or 0x4e.
102  */
103 
104 static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a,
105 				       0x29, 0x2a, 0x2b,
106 				       0x4c, 0x4d, 0x4e,
107 				       I2C_CLIENT_END };
108 
109 /*
110  * Insmod parameters
111  */
112 
113 I2C_CLIENT_INSMOD_7(lm90, adm1032, lm99, lm86, max6657, adt7461, max6680);
114 
115 /*
116  * The LM90 registers
117  */
118 
119 #define LM90_REG_R_MAN_ID		0xFE
120 #define LM90_REG_R_CHIP_ID		0xFF
121 #define LM90_REG_R_CONFIG1		0x03
122 #define LM90_REG_W_CONFIG1		0x09
123 #define LM90_REG_R_CONFIG2		0xBF
124 #define LM90_REG_W_CONFIG2		0xBF
125 #define LM90_REG_R_CONVRATE		0x04
126 #define LM90_REG_W_CONVRATE		0x0A
127 #define LM90_REG_R_STATUS		0x02
128 #define LM90_REG_R_LOCAL_TEMP		0x00
129 #define LM90_REG_R_LOCAL_HIGH		0x05
130 #define LM90_REG_W_LOCAL_HIGH		0x0B
131 #define LM90_REG_R_LOCAL_LOW		0x06
132 #define LM90_REG_W_LOCAL_LOW		0x0C
133 #define LM90_REG_R_LOCAL_CRIT		0x20
134 #define LM90_REG_W_LOCAL_CRIT		0x20
135 #define LM90_REG_R_REMOTE_TEMPH		0x01
136 #define LM90_REG_R_REMOTE_TEMPL		0x10
137 #define LM90_REG_R_REMOTE_OFFSH		0x11
138 #define LM90_REG_W_REMOTE_OFFSH		0x11
139 #define LM90_REG_R_REMOTE_OFFSL		0x12
140 #define LM90_REG_W_REMOTE_OFFSL		0x12
141 #define LM90_REG_R_REMOTE_HIGHH		0x07
142 #define LM90_REG_W_REMOTE_HIGHH		0x0D
143 #define LM90_REG_R_REMOTE_HIGHL		0x13
144 #define LM90_REG_W_REMOTE_HIGHL		0x13
145 #define LM90_REG_R_REMOTE_LOWH		0x08
146 #define LM90_REG_W_REMOTE_LOWH		0x0E
147 #define LM90_REG_R_REMOTE_LOWL		0x14
148 #define LM90_REG_W_REMOTE_LOWL		0x14
149 #define LM90_REG_R_REMOTE_CRIT		0x19
150 #define LM90_REG_W_REMOTE_CRIT		0x19
151 #define LM90_REG_R_TCRIT_HYST		0x21
152 #define LM90_REG_W_TCRIT_HYST		0x21
153 
154 /*
155  * Conversions and various macros
156  * For local temperatures and limits, critical limits and the hysteresis
157  * value, the LM90 uses signed 8-bit values with LSB = 1 degree Celsius.
158  * For remote temperatures and limits, it uses signed 11-bit values with
159  * LSB = 0.125 degree Celsius, left-justified in 16-bit registers.
160  */
161 
162 #define TEMP1_FROM_REG(val)	((val) * 1000)
163 #define TEMP1_TO_REG(val)	((val) <= -128000 ? -128 : \
164 				 (val) >= 127000 ? 127 : \
165 				 (val) < 0 ? ((val) - 500) / 1000 : \
166 				 ((val) + 500) / 1000)
167 #define TEMP2_FROM_REG(val)	((val) / 32 * 125)
168 #define TEMP2_TO_REG(val)	((val) <= -128000 ? 0x8000 : \
169 				 (val) >= 127875 ? 0x7FE0 : \
170 				 (val) < 0 ? ((val) - 62) / 125 * 32 : \
171 				 ((val) + 62) / 125 * 32)
172 #define HYST_TO_REG(val)	((val) <= 0 ? 0 : (val) >= 30500 ? 31 : \
173 				 ((val) + 500) / 1000)
174 
175 /*
176  * ADT7461 is almost identical to LM90 except that attempts to write
177  * values that are outside the range 0 < temp < 127 are treated as
178  * the boundary value.
179  */
180 
181 #define TEMP1_TO_REG_ADT7461(val) ((val) <= 0 ? 0 : \
182 				 (val) >= 127000 ? 127 : \
183 				 ((val) + 500) / 1000)
184 #define TEMP2_TO_REG_ADT7461(val) ((val) <= 0 ? 0 : \
185 				 (val) >= 127750 ? 0x7FC0 : \
186 				 ((val) + 125) / 250 * 64)
187 
188 /*
189  * Functions declaration
190  */
191 
192 static int lm90_attach_adapter(struct i2c_adapter *adapter);
193 static int lm90_detect(struct i2c_adapter *adapter, int address,
194 	int kind);
195 static void lm90_init_client(struct i2c_client *client);
196 static int lm90_detach_client(struct i2c_client *client);
197 static struct lm90_data *lm90_update_device(struct device *dev);
198 
199 /*
200  * Driver data (common to all clients)
201  */
202 
203 static struct i2c_driver lm90_driver = {
204 	.driver = {
205 		.name	= "lm90",
206 	},
207 	.id		= I2C_DRIVERID_LM90,
208 	.attach_adapter	= lm90_attach_adapter,
209 	.detach_client	= lm90_detach_client,
210 };
211 
212 /*
213  * Client data (each client gets its own)
214  */
215 
216 struct lm90_data {
217 	struct i2c_client client;
218 	struct device *hwmon_dev;
219 	struct mutex update_lock;
220 	char valid; /* zero until following fields are valid */
221 	unsigned long last_updated; /* in jiffies */
222 	int kind;
223 
224 	/* registers values */
225 	s8 temp8[5];	/* 0: local input
226 			   1: local low limit
227 			   2: local high limit
228 			   3: local critical limit
229 			   4: remote critical limit */
230 	s16 temp11[4];	/* 0: remote input
231 			   1: remote low limit
232 			   2: remote high limit
233 			   3: remote offset (except max6657) */
234 	u8 temp_hyst;
235 	u8 alarms; /* bitvector */
236 };
237 
238 /*
239  * Sysfs stuff
240  */
241 
242 static ssize_t show_temp8(struct device *dev, struct device_attribute *devattr,
243 			  char *buf)
244 {
245 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
246 	struct lm90_data *data = lm90_update_device(dev);
247 	return sprintf(buf, "%d\n", TEMP1_FROM_REG(data->temp8[attr->index]));
248 }
249 
250 static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr,
251 			 const char *buf, size_t count)
252 {
253 	static const u8 reg[4] = {
254 		LM90_REG_W_LOCAL_LOW,
255 		LM90_REG_W_LOCAL_HIGH,
256 		LM90_REG_W_LOCAL_CRIT,
257 		LM90_REG_W_REMOTE_CRIT,
258 	};
259 
260 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
261 	struct i2c_client *client = to_i2c_client(dev);
262 	struct lm90_data *data = i2c_get_clientdata(client);
263 	long val = simple_strtol(buf, NULL, 10);
264 	int nr = attr->index;
265 
266 	mutex_lock(&data->update_lock);
267 	if (data->kind == adt7461)
268 		data->temp8[nr] = TEMP1_TO_REG_ADT7461(val);
269 	else
270 		data->temp8[nr] = TEMP1_TO_REG(val);
271 	i2c_smbus_write_byte_data(client, reg[nr - 1], data->temp8[nr]);
272 	mutex_unlock(&data->update_lock);
273 	return count;
274 }
275 
276 static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr,
277 			   char *buf)
278 {
279 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
280 	struct lm90_data *data = lm90_update_device(dev);
281 	return sprintf(buf, "%d\n", TEMP2_FROM_REG(data->temp11[attr->index]));
282 }
283 
284 static ssize_t set_temp11(struct device *dev, struct device_attribute *devattr,
285 			  const char *buf, size_t count)
286 {
287 	static const u8 reg[6] = {
288 		LM90_REG_W_REMOTE_LOWH,
289 		LM90_REG_W_REMOTE_LOWL,
290 		LM90_REG_W_REMOTE_HIGHH,
291 		LM90_REG_W_REMOTE_HIGHL,
292 		LM90_REG_W_REMOTE_OFFSH,
293 		LM90_REG_W_REMOTE_OFFSL,
294 	};
295 
296 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
297 	struct i2c_client *client = to_i2c_client(dev);
298 	struct lm90_data *data = i2c_get_clientdata(client);
299 	long val = simple_strtol(buf, NULL, 10);
300 	int nr = attr->index;
301 
302 	mutex_lock(&data->update_lock);
303 	if (data->kind == adt7461)
304 		data->temp11[nr] = TEMP2_TO_REG_ADT7461(val);
305 	else
306 		data->temp11[nr] = TEMP2_TO_REG(val);
307 	i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2],
308 				  data->temp11[nr] >> 8);
309 	i2c_smbus_write_byte_data(client, reg[(nr - 1) * 2 + 1],
310 				  data->temp11[nr] & 0xff);
311 	mutex_unlock(&data->update_lock);
312 	return count;
313 }
314 
315 static ssize_t show_temphyst(struct device *dev, struct device_attribute *devattr,
316 			     char *buf)
317 {
318 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
319 	struct lm90_data *data = lm90_update_device(dev);
320 	return sprintf(buf, "%d\n", TEMP1_FROM_REG(data->temp8[attr->index])
321 		       - TEMP1_FROM_REG(data->temp_hyst));
322 }
323 
324 static ssize_t set_temphyst(struct device *dev, struct device_attribute *dummy,
325 			    const char *buf, size_t count)
326 {
327 	struct i2c_client *client = to_i2c_client(dev);
328 	struct lm90_data *data = i2c_get_clientdata(client);
329 	long val = simple_strtol(buf, NULL, 10);
330 	long hyst;
331 
332 	mutex_lock(&data->update_lock);
333 	hyst = TEMP1_FROM_REG(data->temp8[3]) - val;
334 	i2c_smbus_write_byte_data(client, LM90_REG_W_TCRIT_HYST,
335 				  HYST_TO_REG(hyst));
336 	mutex_unlock(&data->update_lock);
337 	return count;
338 }
339 
340 static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
341 			   char *buf)
342 {
343 	struct lm90_data *data = lm90_update_device(dev);
344 	return sprintf(buf, "%d\n", data->alarms);
345 }
346 
347 static ssize_t show_alarm(struct device *dev, struct device_attribute
348 			  *devattr, char *buf)
349 {
350 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
351 	struct lm90_data *data = lm90_update_device(dev);
352 	int bitnr = attr->index;
353 
354 	return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
355 }
356 
357 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp8, NULL, 0);
358 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp11, NULL, 0);
359 static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp8,
360 	set_temp8, 1);
361 static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp11,
362 	set_temp11, 1);
363 static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp8,
364 	set_temp8, 2);
365 static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp11,
366 	set_temp11, 2);
367 static SENSOR_DEVICE_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp8,
368 	set_temp8, 3);
369 static SENSOR_DEVICE_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp8,
370 	set_temp8, 4);
371 static SENSOR_DEVICE_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temphyst,
372 	set_temphyst, 3);
373 static SENSOR_DEVICE_ATTR(temp2_crit_hyst, S_IRUGO, show_temphyst, NULL, 4);
374 static SENSOR_DEVICE_ATTR(temp2_offset, S_IWUSR | S_IRUGO, show_temp11,
375 	set_temp11, 3);
376 
377 /* Individual alarm files */
378 static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
379 static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
380 static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2);
381 static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3);
382 static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4);
383 static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5);
384 static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
385 /* Raw alarm file for compatibility */
386 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
387 
388 static struct attribute *lm90_attributes[] = {
389 	&sensor_dev_attr_temp1_input.dev_attr.attr,
390 	&sensor_dev_attr_temp2_input.dev_attr.attr,
391 	&sensor_dev_attr_temp1_min.dev_attr.attr,
392 	&sensor_dev_attr_temp2_min.dev_attr.attr,
393 	&sensor_dev_attr_temp1_max.dev_attr.attr,
394 	&sensor_dev_attr_temp2_max.dev_attr.attr,
395 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
396 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
397 	&sensor_dev_attr_temp1_crit_hyst.dev_attr.attr,
398 	&sensor_dev_attr_temp2_crit_hyst.dev_attr.attr,
399 
400 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
401 	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
402 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
403 	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr,
404 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
405 	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr,
406 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
407 	&dev_attr_alarms.attr,
408 	NULL
409 };
410 
411 static const struct attribute_group lm90_group = {
412 	.attrs = lm90_attributes,
413 };
414 
415 /* pec used for ADM1032 only */
416 static ssize_t show_pec(struct device *dev, struct device_attribute *dummy,
417 			char *buf)
418 {
419 	struct i2c_client *client = to_i2c_client(dev);
420 	return sprintf(buf, "%d\n", !!(client->flags & I2C_CLIENT_PEC));
421 }
422 
423 static ssize_t set_pec(struct device *dev, struct device_attribute *dummy,
424 		       const char *buf, size_t count)
425 {
426 	struct i2c_client *client = to_i2c_client(dev);
427 	long val = simple_strtol(buf, NULL, 10);
428 
429 	switch (val) {
430 	case 0:
431 		client->flags &= ~I2C_CLIENT_PEC;
432 		break;
433 	case 1:
434 		client->flags |= I2C_CLIENT_PEC;
435 		break;
436 	default:
437 		return -EINVAL;
438 	}
439 
440 	return count;
441 }
442 
443 static DEVICE_ATTR(pec, S_IWUSR | S_IRUGO, show_pec, set_pec);
444 
445 /*
446  * Real code
447  */
448 
449 /* The ADM1032 supports PEC but not on write byte transactions, so we need
450    to explicitly ask for a transaction without PEC. */
451 static inline s32 adm1032_write_byte(struct i2c_client *client, u8 value)
452 {
453 	return i2c_smbus_xfer(client->adapter, client->addr,
454 			      client->flags & ~I2C_CLIENT_PEC,
455 			      I2C_SMBUS_WRITE, value, I2C_SMBUS_BYTE, NULL);
456 }
457 
458 /* It is assumed that client->update_lock is held (unless we are in
459    detection or initialization steps). This matters when PEC is enabled,
460    because we don't want the address pointer to change between the write
461    byte and the read byte transactions. */
462 static int lm90_read_reg(struct i2c_client* client, u8 reg, u8 *value)
463 {
464 	int err;
465 
466  	if (client->flags & I2C_CLIENT_PEC) {
467  		err = adm1032_write_byte(client, reg);
468  		if (err >= 0)
469  			err = i2c_smbus_read_byte(client);
470  	} else
471  		err = i2c_smbus_read_byte_data(client, reg);
472 
473 	if (err < 0) {
474 		dev_warn(&client->dev, "Register %#02x read failed (%d)\n",
475 			 reg, err);
476 		return err;
477 	}
478 	*value = err;
479 
480 	return 0;
481 }
482 
483 static int lm90_attach_adapter(struct i2c_adapter *adapter)
484 {
485 	if (!(adapter->class & I2C_CLASS_HWMON))
486 		return 0;
487 	return i2c_probe(adapter, &addr_data, lm90_detect);
488 }
489 
490 /*
491  * The following function does more than just detection. If detection
492  * succeeds, it also registers the new chip.
493  */
494 static int lm90_detect(struct i2c_adapter *adapter, int address, int kind)
495 {
496 	struct i2c_client *new_client;
497 	struct lm90_data *data;
498 	int err = 0;
499 	const char *name = "";
500 
501 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
502 		goto exit;
503 
504 	if (!(data = kzalloc(sizeof(struct lm90_data), GFP_KERNEL))) {
505 		err = -ENOMEM;
506 		goto exit;
507 	}
508 
509 	/* The common I2C client data is placed right before the
510 	   LM90-specific data. */
511 	new_client = &data->client;
512 	i2c_set_clientdata(new_client, data);
513 	new_client->addr = address;
514 	new_client->adapter = adapter;
515 	new_client->driver = &lm90_driver;
516 	new_client->flags = 0;
517 
518 	/*
519 	 * Now we do the remaining detection. A negative kind means that
520 	 * the driver was loaded with no force parameter (default), so we
521 	 * must both detect and identify the chip. A zero kind means that
522 	 * the driver was loaded with the force parameter, the detection
523 	 * step shall be skipped. A positive kind means that the driver
524 	 * was loaded with the force parameter and a given kind of chip is
525 	 * requested, so both the detection and the identification steps
526 	 * are skipped.
527 	 */
528 
529 	/* Default to an LM90 if forced */
530 	if (kind == 0)
531 		kind = lm90;
532 
533 	if (kind < 0) { /* detection and identification */
534 		u8 man_id, chip_id, reg_config1, reg_convrate;
535 
536 		if (lm90_read_reg(new_client, LM90_REG_R_MAN_ID,
537 				  &man_id) < 0
538 		 || lm90_read_reg(new_client, LM90_REG_R_CHIP_ID,
539 		 		  &chip_id) < 0
540 		 || lm90_read_reg(new_client, LM90_REG_R_CONFIG1,
541 		 		  &reg_config1) < 0
542 		 || lm90_read_reg(new_client, LM90_REG_R_CONVRATE,
543 		 		  &reg_convrate) < 0)
544 			goto exit_free;
545 
546 		if ((address == 0x4C || address == 0x4D)
547 		 && man_id == 0x01) { /* National Semiconductor */
548 			u8 reg_config2;
549 
550 			if (lm90_read_reg(new_client, LM90_REG_R_CONFIG2,
551 					  &reg_config2) < 0)
552 				goto exit_free;
553 
554 			if ((reg_config1 & 0x2A) == 0x00
555 			 && (reg_config2 & 0xF8) == 0x00
556 			 && reg_convrate <= 0x09) {
557 				if (address == 0x4C
558 				 && (chip_id & 0xF0) == 0x20) { /* LM90 */
559 					kind = lm90;
560 				} else
561 				if ((chip_id & 0xF0) == 0x30) { /* LM89/LM99 */
562 					kind = lm99;
563 				} else
564 				if (address == 0x4C
565 				 && (chip_id & 0xF0) == 0x10) { /* LM86 */
566 					kind = lm86;
567 				}
568 			}
569 		} else
570 		if ((address == 0x4C || address == 0x4D)
571 		 && man_id == 0x41) { /* Analog Devices */
572 			if ((chip_id & 0xF0) == 0x40 /* ADM1032 */
573 			 && (reg_config1 & 0x3F) == 0x00
574 			 && reg_convrate <= 0x0A) {
575 				kind = adm1032;
576 			} else
577 			if (chip_id == 0x51 /* ADT7461 */
578 			 && (reg_config1 & 0x1F) == 0x00 /* check compat mode */
579 			 && reg_convrate <= 0x0A) {
580 				kind = adt7461;
581 			}
582 		} else
583 		if (man_id == 0x4D) { /* Maxim */
584 			/*
585 			 * The MAX6657, MAX6658 and MAX6659 do NOT have a
586 			 * chip_id register. Reading from that address will
587 			 * return the last read value, which in our case is
588 			 * those of the man_id register. Likewise, the config1
589 			 * register seems to lack a low nibble, so the value
590 			 * will be those of the previous read, so in our case
591 			 * those of the man_id register.
592 			 */
593 			if (chip_id == man_id
594 			 && (address == 0x4C || address == 0x4D)
595 			 && (reg_config1 & 0x1F) == (man_id & 0x0F)
596 			 && reg_convrate <= 0x09) {
597 			 	kind = max6657;
598 			} else
599 			/* The chip_id register of the MAX6680 and MAX6681
600 			 * holds the revision of the chip.
601 			 * the lowest bit of the config1 register is unused
602 			 * and should return zero when read, so should the
603 			 * second to last bit of config1 (software reset)
604 			 */
605 			if (chip_id == 0x01
606 			 && (reg_config1 & 0x03) == 0x00
607 			 && reg_convrate <= 0x07) {
608 			 	kind = max6680;
609 			}
610 		}
611 
612 		if (kind <= 0) { /* identification failed */
613 			dev_info(&adapter->dev,
614 			    "Unsupported chip (man_id=0x%02X, "
615 			    "chip_id=0x%02X).\n", man_id, chip_id);
616 			goto exit_free;
617 		}
618 	}
619 
620 	if (kind == lm90) {
621 		name = "lm90";
622 	} else if (kind == adm1032) {
623 		name = "adm1032";
624 		/* The ADM1032 supports PEC, but only if combined
625 		   transactions are not used. */
626 		if (i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE))
627 			new_client->flags |= I2C_CLIENT_PEC;
628 	} else if (kind == lm99) {
629 		name = "lm99";
630 	} else if (kind == lm86) {
631 		name = "lm86";
632 	} else if (kind == max6657) {
633 		name = "max6657";
634 	} else if (kind == max6680) {
635 		name = "max6680";
636 	} else if (kind == adt7461) {
637 		name = "adt7461";
638 	}
639 
640 	/* We can fill in the remaining client fields */
641 	strlcpy(new_client->name, name, I2C_NAME_SIZE);
642 	data->valid = 0;
643 	data->kind = kind;
644 	mutex_init(&data->update_lock);
645 
646 	/* Tell the I2C layer a new client has arrived */
647 	if ((err = i2c_attach_client(new_client)))
648 		goto exit_free;
649 
650 	/* Initialize the LM90 chip */
651 	lm90_init_client(new_client);
652 
653 	/* Register sysfs hooks */
654 	if ((err = sysfs_create_group(&new_client->dev.kobj, &lm90_group)))
655 		goto exit_detach;
656 	if (new_client->flags & I2C_CLIENT_PEC) {
657 		if ((err = device_create_file(&new_client->dev,
658 					      &dev_attr_pec)))
659 			goto exit_remove_files;
660 	}
661 	if (data->kind != max6657) {
662 		if ((err = device_create_file(&new_client->dev,
663 				&sensor_dev_attr_temp2_offset.dev_attr)))
664 			goto exit_remove_files;
665 	}
666 
667 	data->hwmon_dev = hwmon_device_register(&new_client->dev);
668 	if (IS_ERR(data->hwmon_dev)) {
669 		err = PTR_ERR(data->hwmon_dev);
670 		goto exit_remove_files;
671 	}
672 
673 	return 0;
674 
675 exit_remove_files:
676 	sysfs_remove_group(&new_client->dev.kobj, &lm90_group);
677 	device_remove_file(&new_client->dev, &dev_attr_pec);
678 exit_detach:
679 	i2c_detach_client(new_client);
680 exit_free:
681 	kfree(data);
682 exit:
683 	return err;
684 }
685 
686 static void lm90_init_client(struct i2c_client *client)
687 {
688 	u8 config, config_orig;
689 	struct lm90_data *data = i2c_get_clientdata(client);
690 
691 	/*
692 	 * Start the conversions.
693 	 */
694 	i2c_smbus_write_byte_data(client, LM90_REG_W_CONVRATE,
695 				  5); /* 2 Hz */
696 	if (lm90_read_reg(client, LM90_REG_R_CONFIG1, &config) < 0) {
697 		dev_warn(&client->dev, "Initialization failed!\n");
698 		return;
699 	}
700 	config_orig = config;
701 
702 	/*
703 	 * Put MAX6680/MAX8881 into extended resolution (bit 0x10,
704 	 * 0.125 degree resolution) and range (0x08, extend range
705 	 * to -64 degree) mode for the remote temperature sensor.
706 	 */
707 	if (data->kind == max6680) {
708 		config |= 0x18;
709 	}
710 
711 	config &= 0xBF;	/* run */
712 	if (config != config_orig) /* Only write if changed */
713 		i2c_smbus_write_byte_data(client, LM90_REG_W_CONFIG1, config);
714 }
715 
716 static int lm90_detach_client(struct i2c_client *client)
717 {
718 	struct lm90_data *data = i2c_get_clientdata(client);
719 	int err;
720 
721 	hwmon_device_unregister(data->hwmon_dev);
722 	sysfs_remove_group(&client->dev.kobj, &lm90_group);
723 	device_remove_file(&client->dev, &dev_attr_pec);
724 	if (data->kind != max6657)
725 		device_remove_file(&client->dev,
726 				   &sensor_dev_attr_temp2_offset.dev_attr);
727 
728 	if ((err = i2c_detach_client(client)))
729 		return err;
730 
731 	kfree(data);
732 	return 0;
733 }
734 
735 static struct lm90_data *lm90_update_device(struct device *dev)
736 {
737 	struct i2c_client *client = to_i2c_client(dev);
738 	struct lm90_data *data = i2c_get_clientdata(client);
739 
740 	mutex_lock(&data->update_lock);
741 
742 	if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
743 		u8 oldh, newh, l;
744 
745 		dev_dbg(&client->dev, "Updating lm90 data.\n");
746 		lm90_read_reg(client, LM90_REG_R_LOCAL_TEMP, &data->temp8[0]);
747 		lm90_read_reg(client, LM90_REG_R_LOCAL_LOW, &data->temp8[1]);
748 		lm90_read_reg(client, LM90_REG_R_LOCAL_HIGH, &data->temp8[2]);
749 		lm90_read_reg(client, LM90_REG_R_LOCAL_CRIT, &data->temp8[3]);
750 		lm90_read_reg(client, LM90_REG_R_REMOTE_CRIT, &data->temp8[4]);
751 		lm90_read_reg(client, LM90_REG_R_TCRIT_HYST, &data->temp_hyst);
752 
753 		/*
754 		 * There is a trick here. We have to read two registers to
755 		 * have the remote sensor temperature, but we have to beware
756 		 * a conversion could occur inbetween the readings. The
757 		 * datasheet says we should either use the one-shot
758 		 * conversion register, which we don't want to do (disables
759 		 * hardware monitoring) or monitor the busy bit, which is
760 		 * impossible (we can't read the values and monitor that bit
761 		 * at the exact same time). So the solution used here is to
762 		 * read the high byte once, then the low byte, then the high
763 		 * byte again. If the new high byte matches the old one,
764 		 * then we have a valid reading. Else we have to read the low
765 		 * byte again, and now we believe we have a correct reading.
766 		 */
767 		if (lm90_read_reg(client, LM90_REG_R_REMOTE_TEMPH, &oldh) == 0
768 		 && lm90_read_reg(client, LM90_REG_R_REMOTE_TEMPL, &l) == 0
769 		 && lm90_read_reg(client, LM90_REG_R_REMOTE_TEMPH, &newh) == 0
770 		 && (newh == oldh
771 		  || lm90_read_reg(client, LM90_REG_R_REMOTE_TEMPL, &l) == 0))
772 			data->temp11[0] = (newh << 8) | l;
773 
774 		if (lm90_read_reg(client, LM90_REG_R_REMOTE_LOWH, &newh) == 0
775 		 && lm90_read_reg(client, LM90_REG_R_REMOTE_LOWL, &l) == 0)
776 			data->temp11[1] = (newh << 8) | l;
777 		if (lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHH, &newh) == 0
778 		 && lm90_read_reg(client, LM90_REG_R_REMOTE_HIGHL, &l) == 0)
779 			data->temp11[2] = (newh << 8) | l;
780 		if (data->kind != max6657) {
781 			if (lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSH,
782 					  &newh) == 0
783 			 && lm90_read_reg(client, LM90_REG_R_REMOTE_OFFSL,
784 					  &l) == 0)
785 				data->temp11[3] = (newh << 8) | l;
786 		}
787 		lm90_read_reg(client, LM90_REG_R_STATUS, &data->alarms);
788 
789 		data->last_updated = jiffies;
790 		data->valid = 1;
791 	}
792 
793 	mutex_unlock(&data->update_lock);
794 
795 	return data;
796 }
797 
798 static int __init sensors_lm90_init(void)
799 {
800 	return i2c_add_driver(&lm90_driver);
801 }
802 
803 static void __exit sensors_lm90_exit(void)
804 {
805 	i2c_del_driver(&lm90_driver);
806 }
807 
808 MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
809 MODULE_DESCRIPTION("LM90/ADM1032 driver");
810 MODULE_LICENSE("GPL");
811 
812 module_init(sensors_lm90_init);
813 module_exit(sensors_lm90_exit);
814