xref: /openbmc/linux/drivers/hwmon/lm83.c (revision 1beeffe4)
18d5d45fbSJean Delvare /*
28d5d45fbSJean Delvare  * lm83.c - Part of lm_sensors, Linux kernel modules for hardware
38d5d45fbSJean Delvare  *          monitoring
42d45771eSJean Delvare  * Copyright (C) 2003-2006  Jean Delvare <khali@linux-fr.org>
58d5d45fbSJean Delvare  *
68d5d45fbSJean Delvare  * Heavily inspired from the lm78, lm75 and adm1021 drivers. The LM83 is
78d5d45fbSJean Delvare  * a sensor chip made by National Semiconductor. It reports up to four
88d5d45fbSJean Delvare  * temperatures (its own plus up to three external ones) with a 1 deg
98d5d45fbSJean Delvare  * resolution and a 3-4 deg accuracy. Complete datasheet can be obtained
108d5d45fbSJean Delvare  * from National's website at:
118d5d45fbSJean Delvare  *   http://www.national.com/pf/LM/LM83.html
128d5d45fbSJean Delvare  * Since the datasheet omits to give the chip stepping code, I give it
138d5d45fbSJean Delvare  * here: 0x03 (at register 0xff).
148d5d45fbSJean Delvare  *
1543cb7ebeSJordan Crouse  * Also supports the LM82 temp sensor, which is basically a stripped down
1643cb7ebeSJordan Crouse  * model of the LM83.  Datasheet is here:
1743cb7ebeSJordan Crouse  * http://www.national.com/pf/LM/LM82.html
1843cb7ebeSJordan Crouse  *
198d5d45fbSJean Delvare  * This program is free software; you can redistribute it and/or modify
208d5d45fbSJean Delvare  * it under the terms of the GNU General Public License as published by
218d5d45fbSJean Delvare  * the Free Software Foundation; either version 2 of the License, or
228d5d45fbSJean Delvare  * (at your option) any later version.
238d5d45fbSJean Delvare  *
248d5d45fbSJean Delvare  * This program is distributed in the hope that it will be useful,
258d5d45fbSJean Delvare  * but WITHOUT ANY WARRANTY; without even the implied warranty of
268d5d45fbSJean Delvare  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
278d5d45fbSJean Delvare  * GNU General Public License for more details.
288d5d45fbSJean Delvare  *
298d5d45fbSJean Delvare  * You should have received a copy of the GNU General Public License
308d5d45fbSJean Delvare  * along with this program; if not, write to the Free Software
318d5d45fbSJean Delvare  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
328d5d45fbSJean Delvare  */
338d5d45fbSJean Delvare 
348d5d45fbSJean Delvare #include <linux/module.h>
358d5d45fbSJean Delvare #include <linux/init.h>
368d5d45fbSJean Delvare #include <linux/slab.h>
378d5d45fbSJean Delvare #include <linux/jiffies.h>
388d5d45fbSJean Delvare #include <linux/i2c.h>
398d5d45fbSJean Delvare #include <linux/hwmon-sysfs.h>
40943b0830SMark M. Hoffman #include <linux/hwmon.h>
41943b0830SMark M. Hoffman #include <linux/err.h>
429a61bf63SIngo Molnar #include <linux/mutex.h>
430e39e01cSJean Delvare #include <linux/sysfs.h>
448d5d45fbSJean Delvare 
458d5d45fbSJean Delvare /*
468d5d45fbSJean Delvare  * Addresses to scan
478d5d45fbSJean Delvare  * Address is selected using 2 three-level pins, resulting in 9 possible
488d5d45fbSJean Delvare  * addresses.
498d5d45fbSJean Delvare  */
508d5d45fbSJean Delvare 
518d5d45fbSJean Delvare static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a,
528d5d45fbSJean Delvare 					0x29, 0x2a, 0x2b,
538d5d45fbSJean Delvare 					0x4c, 0x4d, 0x4e,
548d5d45fbSJean Delvare 					I2C_CLIENT_END };
558d5d45fbSJean Delvare 
568d5d45fbSJean Delvare /*
578d5d45fbSJean Delvare  * Insmod parameters
588d5d45fbSJean Delvare  */
598d5d45fbSJean Delvare 
6043cb7ebeSJordan Crouse I2C_CLIENT_INSMOD_2(lm83, lm82);
618d5d45fbSJean Delvare 
628d5d45fbSJean Delvare /*
638d5d45fbSJean Delvare  * The LM83 registers
648d5d45fbSJean Delvare  * Manufacturer ID is 0x01 for National Semiconductor.
658d5d45fbSJean Delvare  */
668d5d45fbSJean Delvare 
678d5d45fbSJean Delvare #define LM83_REG_R_MAN_ID		0xFE
688d5d45fbSJean Delvare #define LM83_REG_R_CHIP_ID		0xFF
698d5d45fbSJean Delvare #define LM83_REG_R_CONFIG		0x03
708d5d45fbSJean Delvare #define LM83_REG_W_CONFIG		0x09
718d5d45fbSJean Delvare #define LM83_REG_R_STATUS1		0x02
728d5d45fbSJean Delvare #define LM83_REG_R_STATUS2		0x35
738d5d45fbSJean Delvare #define LM83_REG_R_LOCAL_TEMP		0x00
748d5d45fbSJean Delvare #define LM83_REG_R_LOCAL_HIGH		0x05
758d5d45fbSJean Delvare #define LM83_REG_W_LOCAL_HIGH		0x0B
768d5d45fbSJean Delvare #define LM83_REG_R_REMOTE1_TEMP		0x30
778d5d45fbSJean Delvare #define LM83_REG_R_REMOTE1_HIGH		0x38
788d5d45fbSJean Delvare #define LM83_REG_W_REMOTE1_HIGH		0x50
798d5d45fbSJean Delvare #define LM83_REG_R_REMOTE2_TEMP		0x01
808d5d45fbSJean Delvare #define LM83_REG_R_REMOTE2_HIGH		0x07
818d5d45fbSJean Delvare #define LM83_REG_W_REMOTE2_HIGH		0x0D
828d5d45fbSJean Delvare #define LM83_REG_R_REMOTE3_TEMP		0x31
838d5d45fbSJean Delvare #define LM83_REG_R_REMOTE3_HIGH		0x3A
848d5d45fbSJean Delvare #define LM83_REG_W_REMOTE3_HIGH		0x52
858d5d45fbSJean Delvare #define LM83_REG_R_TCRIT		0x42
868d5d45fbSJean Delvare #define LM83_REG_W_TCRIT		0x5A
878d5d45fbSJean Delvare 
888d5d45fbSJean Delvare /*
898d5d45fbSJean Delvare  * Conversions and various macros
908d5d45fbSJean Delvare  * The LM83 uses signed 8-bit values with LSB = 1 degree Celsius.
918d5d45fbSJean Delvare  */
928d5d45fbSJean Delvare 
938d5d45fbSJean Delvare #define TEMP_FROM_REG(val)	((val) * 1000)
948d5d45fbSJean Delvare #define TEMP_TO_REG(val)	((val) <= -128000 ? -128 : \
958d5d45fbSJean Delvare 				 (val) >= 127000 ? 127 : \
968d5d45fbSJean Delvare 				 (val) < 0 ? ((val) - 500) / 1000 : \
978d5d45fbSJean Delvare 				 ((val) + 500) / 1000)
988d5d45fbSJean Delvare 
998d5d45fbSJean Delvare static const u8 LM83_REG_R_TEMP[] = {
1008d5d45fbSJean Delvare 	LM83_REG_R_LOCAL_TEMP,
1018d5d45fbSJean Delvare 	LM83_REG_R_REMOTE1_TEMP,
1028d5d45fbSJean Delvare 	LM83_REG_R_REMOTE2_TEMP,
1038d5d45fbSJean Delvare 	LM83_REG_R_REMOTE3_TEMP,
1048d5d45fbSJean Delvare 	LM83_REG_R_LOCAL_HIGH,
1058d5d45fbSJean Delvare 	LM83_REG_R_REMOTE1_HIGH,
1068d5d45fbSJean Delvare 	LM83_REG_R_REMOTE2_HIGH,
1078d5d45fbSJean Delvare 	LM83_REG_R_REMOTE3_HIGH,
1088d5d45fbSJean Delvare 	LM83_REG_R_TCRIT,
1098d5d45fbSJean Delvare };
1108d5d45fbSJean Delvare 
1118d5d45fbSJean Delvare static const u8 LM83_REG_W_HIGH[] = {
1128d5d45fbSJean Delvare 	LM83_REG_W_LOCAL_HIGH,
1138d5d45fbSJean Delvare 	LM83_REG_W_REMOTE1_HIGH,
1148d5d45fbSJean Delvare 	LM83_REG_W_REMOTE2_HIGH,
1158d5d45fbSJean Delvare 	LM83_REG_W_REMOTE3_HIGH,
1168d5d45fbSJean Delvare 	LM83_REG_W_TCRIT,
1178d5d45fbSJean Delvare };
1188d5d45fbSJean Delvare 
1198d5d45fbSJean Delvare /*
1208d5d45fbSJean Delvare  * Functions declaration
1218d5d45fbSJean Delvare  */
1228d5d45fbSJean Delvare 
1238d5d45fbSJean Delvare static int lm83_attach_adapter(struct i2c_adapter *adapter);
1248d5d45fbSJean Delvare static int lm83_detect(struct i2c_adapter *adapter, int address, int kind);
1258d5d45fbSJean Delvare static int lm83_detach_client(struct i2c_client *client);
1268d5d45fbSJean Delvare static struct lm83_data *lm83_update_device(struct device *dev);
1278d5d45fbSJean Delvare 
1288d5d45fbSJean Delvare /*
1298d5d45fbSJean Delvare  * Driver data (common to all clients)
1308d5d45fbSJean Delvare  */
1318d5d45fbSJean Delvare 
1328d5d45fbSJean Delvare static struct i2c_driver lm83_driver = {
133cdaf7934SLaurent Riffard 	.driver = {
1348d5d45fbSJean Delvare 		.name	= "lm83",
135cdaf7934SLaurent Riffard 	},
1368d5d45fbSJean Delvare 	.id		= I2C_DRIVERID_LM83,
1378d5d45fbSJean Delvare 	.attach_adapter	= lm83_attach_adapter,
1388d5d45fbSJean Delvare 	.detach_client	= lm83_detach_client,
1398d5d45fbSJean Delvare };
1408d5d45fbSJean Delvare 
1418d5d45fbSJean Delvare /*
1428d5d45fbSJean Delvare  * Client data (each client gets its own)
1438d5d45fbSJean Delvare  */
1448d5d45fbSJean Delvare 
1458d5d45fbSJean Delvare struct lm83_data {
1468d5d45fbSJean Delvare 	struct i2c_client client;
1471beeffe4STony Jones 	struct device *hwmon_dev;
1489a61bf63SIngo Molnar 	struct mutex update_lock;
1498d5d45fbSJean Delvare 	char valid; /* zero until following fields are valid */
1508d5d45fbSJean Delvare 	unsigned long last_updated; /* in jiffies */
1518d5d45fbSJean Delvare 
1528d5d45fbSJean Delvare 	/* registers values */
1538d5d45fbSJean Delvare 	s8 temp[9];	/* 0..3: input 1-4,
1548d5d45fbSJean Delvare 			   4..7: high limit 1-4,
1558d5d45fbSJean Delvare 			   8   : critical limit */
1568d5d45fbSJean Delvare 	u16 alarms; /* bitvector, combined */
1578d5d45fbSJean Delvare };
1588d5d45fbSJean Delvare 
1598d5d45fbSJean Delvare /*
1608d5d45fbSJean Delvare  * Sysfs stuff
1618d5d45fbSJean Delvare  */
1628d5d45fbSJean Delvare 
1638d5d45fbSJean Delvare static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
1648d5d45fbSJean Delvare 			 char *buf)
1658d5d45fbSJean Delvare {
1668d5d45fbSJean Delvare 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1678d5d45fbSJean Delvare 	struct lm83_data *data = lm83_update_device(dev);
1688d5d45fbSJean Delvare 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
1698d5d45fbSJean Delvare }
1708d5d45fbSJean Delvare 
1718d5d45fbSJean Delvare static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
1728d5d45fbSJean Delvare 			const char *buf, size_t count)
1738d5d45fbSJean Delvare {
1748d5d45fbSJean Delvare 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1758d5d45fbSJean Delvare 	struct i2c_client *client = to_i2c_client(dev);
1768d5d45fbSJean Delvare 	struct lm83_data *data = i2c_get_clientdata(client);
1778d5d45fbSJean Delvare 	long val = simple_strtol(buf, NULL, 10);
1788d5d45fbSJean Delvare 	int nr = attr->index;
1798d5d45fbSJean Delvare 
1809a61bf63SIngo Molnar 	mutex_lock(&data->update_lock);
1818d5d45fbSJean Delvare 	data->temp[nr] = TEMP_TO_REG(val);
1828d5d45fbSJean Delvare 	i2c_smbus_write_byte_data(client, LM83_REG_W_HIGH[nr - 4],
1838d5d45fbSJean Delvare 				  data->temp[nr]);
1849a61bf63SIngo Molnar 	mutex_unlock(&data->update_lock);
1858d5d45fbSJean Delvare 	return count;
1868d5d45fbSJean Delvare }
1878d5d45fbSJean Delvare 
1888d5d45fbSJean Delvare static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
1898d5d45fbSJean Delvare 			   char *buf)
1908d5d45fbSJean Delvare {
1918d5d45fbSJean Delvare 	struct lm83_data *data = lm83_update_device(dev);
1928d5d45fbSJean Delvare 	return sprintf(buf, "%d\n", data->alarms);
1938d5d45fbSJean Delvare }
1948d5d45fbSJean Delvare 
1952d45771eSJean Delvare static ssize_t show_alarm(struct device *dev, struct device_attribute
1962d45771eSJean Delvare 			  *devattr, char *buf)
1972d45771eSJean Delvare {
1982d45771eSJean Delvare 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1992d45771eSJean Delvare 	struct lm83_data *data = lm83_update_device(dev);
2002d45771eSJean Delvare 	int bitnr = attr->index;
2012d45771eSJean Delvare 
2022d45771eSJean Delvare 	return sprintf(buf, "%d\n", (data->alarms >> bitnr) & 1);
2032d45771eSJean Delvare }
2042d45771eSJean Delvare 
2058d5d45fbSJean Delvare static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
2068d5d45fbSJean Delvare static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
2078d5d45fbSJean Delvare static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
2088d5d45fbSJean Delvare static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
2098d5d45fbSJean Delvare static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp,
2108d5d45fbSJean Delvare 	set_temp, 4);
2118d5d45fbSJean Delvare static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp,
2128d5d45fbSJean Delvare 	set_temp, 5);
2138d5d45fbSJean Delvare static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp,
2148d5d45fbSJean Delvare 	set_temp, 6);
2158d5d45fbSJean Delvare static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp,
2168d5d45fbSJean Delvare 	set_temp, 7);
2178d5d45fbSJean Delvare static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL, 8);
2188d5d45fbSJean Delvare static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp, NULL, 8);
2198d5d45fbSJean Delvare static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp,
2208d5d45fbSJean Delvare 	set_temp, 8);
2218d5d45fbSJean Delvare static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp, NULL, 8);
2222d45771eSJean Delvare 
2232d45771eSJean Delvare /* Individual alarm files */
2242d45771eSJean Delvare static SENSOR_DEVICE_ATTR(temp1_crit_alarm, S_IRUGO, show_alarm, NULL, 0);
2252d45771eSJean Delvare static SENSOR_DEVICE_ATTR(temp3_crit_alarm, S_IRUGO, show_alarm, NULL, 1);
2267817a39eSJean Delvare static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 2);
2272d45771eSJean Delvare static SENSOR_DEVICE_ATTR(temp3_max_alarm, S_IRUGO, show_alarm, NULL, 4);
2282d45771eSJean Delvare static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6);
2292d45771eSJean Delvare static SENSOR_DEVICE_ATTR(temp2_crit_alarm, S_IRUGO, show_alarm, NULL, 8);
2302d45771eSJean Delvare static SENSOR_DEVICE_ATTR(temp4_crit_alarm, S_IRUGO, show_alarm, NULL, 9);
2317817a39eSJean Delvare static SENSOR_DEVICE_ATTR(temp4_fault, S_IRUGO, show_alarm, NULL, 10);
2322d45771eSJean Delvare static SENSOR_DEVICE_ATTR(temp4_max_alarm, S_IRUGO, show_alarm, NULL, 12);
2337817a39eSJean Delvare static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 13);
2342d45771eSJean Delvare static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 15);
2352d45771eSJean Delvare /* Raw alarm file for compatibility */
2368d5d45fbSJean Delvare static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
2378d5d45fbSJean Delvare 
2380e39e01cSJean Delvare static struct attribute *lm83_attributes[] = {
2390e39e01cSJean Delvare 	&sensor_dev_attr_temp1_input.dev_attr.attr,
2400e39e01cSJean Delvare 	&sensor_dev_attr_temp3_input.dev_attr.attr,
2410e39e01cSJean Delvare 	&sensor_dev_attr_temp1_max.dev_attr.attr,
2420e39e01cSJean Delvare 	&sensor_dev_attr_temp3_max.dev_attr.attr,
2430e39e01cSJean Delvare 	&sensor_dev_attr_temp1_crit.dev_attr.attr,
2440e39e01cSJean Delvare 	&sensor_dev_attr_temp3_crit.dev_attr.attr,
2450e39e01cSJean Delvare 
2460e39e01cSJean Delvare 	&sensor_dev_attr_temp1_crit_alarm.dev_attr.attr,
2470e39e01cSJean Delvare 	&sensor_dev_attr_temp3_crit_alarm.dev_attr.attr,
2487817a39eSJean Delvare 	&sensor_dev_attr_temp3_fault.dev_attr.attr,
2490e39e01cSJean Delvare 	&sensor_dev_attr_temp3_max_alarm.dev_attr.attr,
2500e39e01cSJean Delvare 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr,
2510e39e01cSJean Delvare 	&dev_attr_alarms.attr,
2520e39e01cSJean Delvare 	NULL
2530e39e01cSJean Delvare };
2540e39e01cSJean Delvare 
2550e39e01cSJean Delvare static const struct attribute_group lm83_group = {
2560e39e01cSJean Delvare 	.attrs = lm83_attributes,
2570e39e01cSJean Delvare };
2580e39e01cSJean Delvare 
2590e39e01cSJean Delvare static struct attribute *lm83_attributes_opt[] = {
2600e39e01cSJean Delvare 	&sensor_dev_attr_temp2_input.dev_attr.attr,
2610e39e01cSJean Delvare 	&sensor_dev_attr_temp4_input.dev_attr.attr,
2620e39e01cSJean Delvare 	&sensor_dev_attr_temp2_max.dev_attr.attr,
2630e39e01cSJean Delvare 	&sensor_dev_attr_temp4_max.dev_attr.attr,
2640e39e01cSJean Delvare 	&sensor_dev_attr_temp2_crit.dev_attr.attr,
2650e39e01cSJean Delvare 	&sensor_dev_attr_temp4_crit.dev_attr.attr,
2660e39e01cSJean Delvare 
2670e39e01cSJean Delvare 	&sensor_dev_attr_temp2_crit_alarm.dev_attr.attr,
2680e39e01cSJean Delvare 	&sensor_dev_attr_temp4_crit_alarm.dev_attr.attr,
2697817a39eSJean Delvare 	&sensor_dev_attr_temp4_fault.dev_attr.attr,
2700e39e01cSJean Delvare 	&sensor_dev_attr_temp4_max_alarm.dev_attr.attr,
2717817a39eSJean Delvare 	&sensor_dev_attr_temp2_fault.dev_attr.attr,
2720e39e01cSJean Delvare 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr,
2730e39e01cSJean Delvare 	NULL
2740e39e01cSJean Delvare };
2750e39e01cSJean Delvare 
2760e39e01cSJean Delvare static const struct attribute_group lm83_group_opt = {
2770e39e01cSJean Delvare 	.attrs = lm83_attributes_opt,
2780e39e01cSJean Delvare };
2790e39e01cSJean Delvare 
2808d5d45fbSJean Delvare /*
2818d5d45fbSJean Delvare  * Real code
2828d5d45fbSJean Delvare  */
2838d5d45fbSJean Delvare 
2848d5d45fbSJean Delvare static int lm83_attach_adapter(struct i2c_adapter *adapter)
2858d5d45fbSJean Delvare {
2868d5d45fbSJean Delvare 	if (!(adapter->class & I2C_CLASS_HWMON))
2878d5d45fbSJean Delvare 		return 0;
2882ed2dc3cSJean Delvare 	return i2c_probe(adapter, &addr_data, lm83_detect);
2898d5d45fbSJean Delvare }
2908d5d45fbSJean Delvare 
2918d5d45fbSJean Delvare /*
2928d5d45fbSJean Delvare  * The following function does more than just detection. If detection
2938d5d45fbSJean Delvare  * succeeds, it also registers the new chip.
2948d5d45fbSJean Delvare  */
2958d5d45fbSJean Delvare static int lm83_detect(struct i2c_adapter *adapter, int address, int kind)
2968d5d45fbSJean Delvare {
2978d5d45fbSJean Delvare 	struct i2c_client *new_client;
2988d5d45fbSJean Delvare 	struct lm83_data *data;
2998d5d45fbSJean Delvare 	int err = 0;
3008d5d45fbSJean Delvare 	const char *name = "";
3018d5d45fbSJean Delvare 
3028d5d45fbSJean Delvare 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
3038d5d45fbSJean Delvare 		goto exit;
3048d5d45fbSJean Delvare 
305ba9c2e8dSDeepak Saxena 	if (!(data = kzalloc(sizeof(struct lm83_data), GFP_KERNEL))) {
3068d5d45fbSJean Delvare 		err = -ENOMEM;
3078d5d45fbSJean Delvare 		goto exit;
3088d5d45fbSJean Delvare 	}
3098d5d45fbSJean Delvare 
3108d5d45fbSJean Delvare 	/* The common I2C client data is placed right after the
3118d5d45fbSJean Delvare 	 * LM83-specific data. */
3128d5d45fbSJean Delvare 	new_client = &data->client;
3138d5d45fbSJean Delvare 	i2c_set_clientdata(new_client, data);
3148d5d45fbSJean Delvare 	new_client->addr = address;
3158d5d45fbSJean Delvare 	new_client->adapter = adapter;
3168d5d45fbSJean Delvare 	new_client->driver = &lm83_driver;
3178d5d45fbSJean Delvare 	new_client->flags = 0;
3188d5d45fbSJean Delvare 
3198d5d45fbSJean Delvare 	/* Now we do the detection and identification. A negative kind
3208d5d45fbSJean Delvare 	 * means that the driver was loaded with no force parameter
3218d5d45fbSJean Delvare 	 * (default), so we must both detect and identify the chip
3228d5d45fbSJean Delvare 	 * (actually there is only one possible kind of chip for now, LM83).
3238d5d45fbSJean Delvare 	 * A zero kind means that the driver was loaded with the force
3248d5d45fbSJean Delvare 	 * parameter, the detection step shall be skipped. A positive kind
3258d5d45fbSJean Delvare 	 * means that the driver was loaded with the force parameter and a
3268d5d45fbSJean Delvare 	 * given kind of chip is requested, so both the detection and the
3278d5d45fbSJean Delvare 	 * identification steps are skipped. */
3288d5d45fbSJean Delvare 
3298d5d45fbSJean Delvare 	/* Default to an LM83 if forced */
3308d5d45fbSJean Delvare 	if (kind == 0)
3318d5d45fbSJean Delvare 		kind = lm83;
3328d5d45fbSJean Delvare 
3338d5d45fbSJean Delvare 	if (kind < 0) { /* detection */
3348d5d45fbSJean Delvare 		if (((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1)
3358d5d45fbSJean Delvare 		    & 0xA8) != 0x00) ||
3368d5d45fbSJean Delvare 		    ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2)
3378d5d45fbSJean Delvare 		    & 0x48) != 0x00) ||
3388d5d45fbSJean Delvare 		    ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG)
3398d5d45fbSJean Delvare 		    & 0x41) != 0x00)) {
3408d5d45fbSJean Delvare 			dev_dbg(&adapter->dev,
3418d5d45fbSJean Delvare 			    "LM83 detection failed at 0x%02x.\n", address);
3428d5d45fbSJean Delvare 			goto exit_free;
3438d5d45fbSJean Delvare 		}
3448d5d45fbSJean Delvare 	}
3458d5d45fbSJean Delvare 
3468d5d45fbSJean Delvare 	if (kind <= 0) { /* identification */
3478d5d45fbSJean Delvare 		u8 man_id, chip_id;
3488d5d45fbSJean Delvare 
3498d5d45fbSJean Delvare 		man_id = i2c_smbus_read_byte_data(new_client,
3508d5d45fbSJean Delvare 		    LM83_REG_R_MAN_ID);
3518d5d45fbSJean Delvare 		chip_id = i2c_smbus_read_byte_data(new_client,
3528d5d45fbSJean Delvare 		    LM83_REG_R_CHIP_ID);
3538d5d45fbSJean Delvare 
3548d5d45fbSJean Delvare 		if (man_id == 0x01) { /* National Semiconductor */
3558d5d45fbSJean Delvare 			if (chip_id == 0x03) {
3568d5d45fbSJean Delvare 				kind = lm83;
35743cb7ebeSJordan Crouse 			} else
35843cb7ebeSJordan Crouse 			if (chip_id == 0x01) {
35943cb7ebeSJordan Crouse 				kind = lm82;
3608d5d45fbSJean Delvare 			}
3618d5d45fbSJean Delvare 		}
3628d5d45fbSJean Delvare 
3638d5d45fbSJean Delvare 		if (kind <= 0) { /* identification failed */
3648d5d45fbSJean Delvare 			dev_info(&adapter->dev,
3658d5d45fbSJean Delvare 			    "Unsupported chip (man_id=0x%02X, "
3668d5d45fbSJean Delvare 			    "chip_id=0x%02X).\n", man_id, chip_id);
3678d5d45fbSJean Delvare 			goto exit_free;
3688d5d45fbSJean Delvare 		}
3698d5d45fbSJean Delvare 	}
3708d5d45fbSJean Delvare 
3718d5d45fbSJean Delvare 	if (kind == lm83) {
3728d5d45fbSJean Delvare 		name = "lm83";
37343cb7ebeSJordan Crouse 	} else
37443cb7ebeSJordan Crouse 	if (kind == lm82) {
37543cb7ebeSJordan Crouse 		name = "lm82";
3768d5d45fbSJean Delvare 	}
3778d5d45fbSJean Delvare 
3788d5d45fbSJean Delvare 	/* We can fill in the remaining client fields */
3798d5d45fbSJean Delvare 	strlcpy(new_client->name, name, I2C_NAME_SIZE);
3808d5d45fbSJean Delvare 	data->valid = 0;
3819a61bf63SIngo Molnar 	mutex_init(&data->update_lock);
3828d5d45fbSJean Delvare 
3838d5d45fbSJean Delvare 	/* Tell the I2C layer a new client has arrived */
3848d5d45fbSJean Delvare 	if ((err = i2c_attach_client(new_client)))
3858d5d45fbSJean Delvare 		goto exit_free;
3868d5d45fbSJean Delvare 
3878d5d45fbSJean Delvare 	/*
3880e39e01cSJean Delvare 	 * Register sysfs hooks
38943cb7ebeSJordan Crouse 	 * The LM82 can only monitor one external diode which is
39043cb7ebeSJordan Crouse 	 * at the same register as the LM83 temp3 entry - so we
39143cb7ebeSJordan Crouse 	 * declare 1 and 3 common, and then 2 and 4 only for the LM83.
39243cb7ebeSJordan Crouse 	 */
39343cb7ebeSJordan Crouse 
3940e39e01cSJean Delvare 	if ((err = sysfs_create_group(&new_client->dev.kobj, &lm83_group)))
3950e39e01cSJean Delvare 		goto exit_detach;
39643cb7ebeSJordan Crouse 
39743cb7ebeSJordan Crouse 	if (kind == lm83) {
3980e39e01cSJean Delvare 		if ((err = sysfs_create_group(&new_client->dev.kobj,
3990e39e01cSJean Delvare 					      &lm83_group_opt)))
4000e39e01cSJean Delvare 			goto exit_remove_files;
4010e39e01cSJean Delvare 	}
40243cb7ebeSJordan Crouse 
4031beeffe4STony Jones 	data->hwmon_dev = hwmon_device_register(&new_client->dev);
4041beeffe4STony Jones 	if (IS_ERR(data->hwmon_dev)) {
4051beeffe4STony Jones 		err = PTR_ERR(data->hwmon_dev);
4060e39e01cSJean Delvare 		goto exit_remove_files;
40743cb7ebeSJordan Crouse 	}
4088d5d45fbSJean Delvare 
4098d5d45fbSJean Delvare 	return 0;
4108d5d45fbSJean Delvare 
4110e39e01cSJean Delvare exit_remove_files:
4120e39e01cSJean Delvare 	sysfs_remove_group(&new_client->dev.kobj, &lm83_group);
4130e39e01cSJean Delvare 	sysfs_remove_group(&new_client->dev.kobj, &lm83_group_opt);
414943b0830SMark M. Hoffman exit_detach:
415943b0830SMark M. Hoffman 	i2c_detach_client(new_client);
4168d5d45fbSJean Delvare exit_free:
4178d5d45fbSJean Delvare 	kfree(data);
4188d5d45fbSJean Delvare exit:
4198d5d45fbSJean Delvare 	return err;
4208d5d45fbSJean Delvare }
4218d5d45fbSJean Delvare 
4228d5d45fbSJean Delvare static int lm83_detach_client(struct i2c_client *client)
4238d5d45fbSJean Delvare {
424943b0830SMark M. Hoffman 	struct lm83_data *data = i2c_get_clientdata(client);
4258d5d45fbSJean Delvare 	int err;
4268d5d45fbSJean Delvare 
4271beeffe4STony Jones 	hwmon_device_unregister(data->hwmon_dev);
4280e39e01cSJean Delvare 	sysfs_remove_group(&client->dev.kobj, &lm83_group);
4290e39e01cSJean Delvare 	sysfs_remove_group(&client->dev.kobj, &lm83_group_opt);
430943b0830SMark M. Hoffman 
4317bef5594SJean Delvare 	if ((err = i2c_detach_client(client)))
4328d5d45fbSJean Delvare 		return err;
4338d5d45fbSJean Delvare 
434943b0830SMark M. Hoffman 	kfree(data);
4358d5d45fbSJean Delvare 	return 0;
4368d5d45fbSJean Delvare }
4378d5d45fbSJean Delvare 
4388d5d45fbSJean Delvare static struct lm83_data *lm83_update_device(struct device *dev)
4398d5d45fbSJean Delvare {
4408d5d45fbSJean Delvare 	struct i2c_client *client = to_i2c_client(dev);
4418d5d45fbSJean Delvare 	struct lm83_data *data = i2c_get_clientdata(client);
4428d5d45fbSJean Delvare 
4439a61bf63SIngo Molnar 	mutex_lock(&data->update_lock);
4448d5d45fbSJean Delvare 
4458d5d45fbSJean Delvare 	if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
4468d5d45fbSJean Delvare 		int nr;
4478d5d45fbSJean Delvare 
4488d5d45fbSJean Delvare 		dev_dbg(&client->dev, "Updating lm83 data.\n");
4498d5d45fbSJean Delvare 		for (nr = 0; nr < 9; nr++) {
4508d5d45fbSJean Delvare 			data->temp[nr] =
4518d5d45fbSJean Delvare 			    i2c_smbus_read_byte_data(client,
4528d5d45fbSJean Delvare 			    LM83_REG_R_TEMP[nr]);
4538d5d45fbSJean Delvare 		}
4548d5d45fbSJean Delvare 		data->alarms =
4558d5d45fbSJean Delvare 		    i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1)
4568d5d45fbSJean Delvare 		    + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2)
4578d5d45fbSJean Delvare 		    << 8);
4588d5d45fbSJean Delvare 
4598d5d45fbSJean Delvare 		data->last_updated = jiffies;
4608d5d45fbSJean Delvare 		data->valid = 1;
4618d5d45fbSJean Delvare 	}
4628d5d45fbSJean Delvare 
4639a61bf63SIngo Molnar 	mutex_unlock(&data->update_lock);
4648d5d45fbSJean Delvare 
4658d5d45fbSJean Delvare 	return data;
4668d5d45fbSJean Delvare }
4678d5d45fbSJean Delvare 
4688d5d45fbSJean Delvare static int __init sensors_lm83_init(void)
4698d5d45fbSJean Delvare {
4708d5d45fbSJean Delvare 	return i2c_add_driver(&lm83_driver);
4718d5d45fbSJean Delvare }
4728d5d45fbSJean Delvare 
4738d5d45fbSJean Delvare static void __exit sensors_lm83_exit(void)
4748d5d45fbSJean Delvare {
4758d5d45fbSJean Delvare 	i2c_del_driver(&lm83_driver);
4768d5d45fbSJean Delvare }
4778d5d45fbSJean Delvare 
4788d5d45fbSJean Delvare MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
4798d5d45fbSJean Delvare MODULE_DESCRIPTION("LM83 driver");
4808d5d45fbSJean Delvare MODULE_LICENSE("GPL");
4818d5d45fbSJean Delvare 
4828d5d45fbSJean Delvare module_init(sensors_lm83_init);
4838d5d45fbSJean Delvare module_exit(sensors_lm83_exit);
484