xref: /openbmc/linux/drivers/hwmon/lm83.c (revision 7bef5594)
18d5d45fbSJean Delvare /*
28d5d45fbSJean Delvare  * lm83.c - Part of lm_sensors, Linux kernel modules for hardware
38d5d45fbSJean Delvare  *          monitoring
48d5d45fbSJean Delvare  * Copyright (C) 2003-2005  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  *
158d5d45fbSJean Delvare  * This program is free software; you can redistribute it and/or modify
168d5d45fbSJean Delvare  * it under the terms of the GNU General Public License as published by
178d5d45fbSJean Delvare  * the Free Software Foundation; either version 2 of the License, or
188d5d45fbSJean Delvare  * (at your option) any later version.
198d5d45fbSJean Delvare  *
208d5d45fbSJean Delvare  * This program is distributed in the hope that it will be useful,
218d5d45fbSJean Delvare  * but WITHOUT ANY WARRANTY; without even the implied warranty of
228d5d45fbSJean Delvare  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
238d5d45fbSJean Delvare  * GNU General Public License for more details.
248d5d45fbSJean Delvare  *
258d5d45fbSJean Delvare  * You should have received a copy of the GNU General Public License
268d5d45fbSJean Delvare  * along with this program; if not, write to the Free Software
278d5d45fbSJean Delvare  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
288d5d45fbSJean Delvare  */
298d5d45fbSJean Delvare 
308d5d45fbSJean Delvare #include <linux/module.h>
318d5d45fbSJean Delvare #include <linux/init.h>
328d5d45fbSJean Delvare #include <linux/slab.h>
338d5d45fbSJean Delvare #include <linux/jiffies.h>
348d5d45fbSJean Delvare #include <linux/i2c.h>
358d5d45fbSJean Delvare #include <linux/i2c-sensor.h>
368d5d45fbSJean Delvare #include <linux/hwmon-sysfs.h>
37943b0830SMark M. Hoffman #include <linux/hwmon.h>
38943b0830SMark M. Hoffman #include <linux/err.h>
398d5d45fbSJean Delvare 
408d5d45fbSJean Delvare /*
418d5d45fbSJean Delvare  * Addresses to scan
428d5d45fbSJean Delvare  * Address is selected using 2 three-level pins, resulting in 9 possible
438d5d45fbSJean Delvare  * addresses.
448d5d45fbSJean Delvare  */
458d5d45fbSJean Delvare 
468d5d45fbSJean Delvare static unsigned short normal_i2c[] = { 0x18, 0x19, 0x1a,
478d5d45fbSJean Delvare 					0x29, 0x2a, 0x2b,
488d5d45fbSJean Delvare 					0x4c, 0x4d, 0x4e,
498d5d45fbSJean Delvare 					I2C_CLIENT_END };
508d5d45fbSJean Delvare 
518d5d45fbSJean Delvare /*
528d5d45fbSJean Delvare  * Insmod parameters
538d5d45fbSJean Delvare  */
548d5d45fbSJean Delvare 
558d5d45fbSJean Delvare SENSORS_INSMOD_1(lm83);
568d5d45fbSJean Delvare 
578d5d45fbSJean Delvare /*
588d5d45fbSJean Delvare  * The LM83 registers
598d5d45fbSJean Delvare  * Manufacturer ID is 0x01 for National Semiconductor.
608d5d45fbSJean Delvare  */
618d5d45fbSJean Delvare 
628d5d45fbSJean Delvare #define LM83_REG_R_MAN_ID		0xFE
638d5d45fbSJean Delvare #define LM83_REG_R_CHIP_ID		0xFF
648d5d45fbSJean Delvare #define LM83_REG_R_CONFIG		0x03
658d5d45fbSJean Delvare #define LM83_REG_W_CONFIG		0x09
668d5d45fbSJean Delvare #define LM83_REG_R_STATUS1		0x02
678d5d45fbSJean Delvare #define LM83_REG_R_STATUS2		0x35
688d5d45fbSJean Delvare #define LM83_REG_R_LOCAL_TEMP		0x00
698d5d45fbSJean Delvare #define LM83_REG_R_LOCAL_HIGH		0x05
708d5d45fbSJean Delvare #define LM83_REG_W_LOCAL_HIGH		0x0B
718d5d45fbSJean Delvare #define LM83_REG_R_REMOTE1_TEMP		0x30
728d5d45fbSJean Delvare #define LM83_REG_R_REMOTE1_HIGH		0x38
738d5d45fbSJean Delvare #define LM83_REG_W_REMOTE1_HIGH		0x50
748d5d45fbSJean Delvare #define LM83_REG_R_REMOTE2_TEMP		0x01
758d5d45fbSJean Delvare #define LM83_REG_R_REMOTE2_HIGH		0x07
768d5d45fbSJean Delvare #define LM83_REG_W_REMOTE2_HIGH		0x0D
778d5d45fbSJean Delvare #define LM83_REG_R_REMOTE3_TEMP		0x31
788d5d45fbSJean Delvare #define LM83_REG_R_REMOTE3_HIGH		0x3A
798d5d45fbSJean Delvare #define LM83_REG_W_REMOTE3_HIGH		0x52
808d5d45fbSJean Delvare #define LM83_REG_R_TCRIT		0x42
818d5d45fbSJean Delvare #define LM83_REG_W_TCRIT		0x5A
828d5d45fbSJean Delvare 
838d5d45fbSJean Delvare /*
848d5d45fbSJean Delvare  * Conversions and various macros
858d5d45fbSJean Delvare  * The LM83 uses signed 8-bit values with LSB = 1 degree Celsius.
868d5d45fbSJean Delvare  */
878d5d45fbSJean Delvare 
888d5d45fbSJean Delvare #define TEMP_FROM_REG(val)	((val) * 1000)
898d5d45fbSJean Delvare #define TEMP_TO_REG(val)	((val) <= -128000 ? -128 : \
908d5d45fbSJean Delvare 				 (val) >= 127000 ? 127 : \
918d5d45fbSJean Delvare 				 (val) < 0 ? ((val) - 500) / 1000 : \
928d5d45fbSJean Delvare 				 ((val) + 500) / 1000)
938d5d45fbSJean Delvare 
948d5d45fbSJean Delvare static const u8 LM83_REG_R_TEMP[] = {
958d5d45fbSJean Delvare 	LM83_REG_R_LOCAL_TEMP,
968d5d45fbSJean Delvare 	LM83_REG_R_REMOTE1_TEMP,
978d5d45fbSJean Delvare 	LM83_REG_R_REMOTE2_TEMP,
988d5d45fbSJean Delvare 	LM83_REG_R_REMOTE3_TEMP,
998d5d45fbSJean Delvare 	LM83_REG_R_LOCAL_HIGH,
1008d5d45fbSJean Delvare 	LM83_REG_R_REMOTE1_HIGH,
1018d5d45fbSJean Delvare 	LM83_REG_R_REMOTE2_HIGH,
1028d5d45fbSJean Delvare 	LM83_REG_R_REMOTE3_HIGH,
1038d5d45fbSJean Delvare 	LM83_REG_R_TCRIT,
1048d5d45fbSJean Delvare };
1058d5d45fbSJean Delvare 
1068d5d45fbSJean Delvare static const u8 LM83_REG_W_HIGH[] = {
1078d5d45fbSJean Delvare 	LM83_REG_W_LOCAL_HIGH,
1088d5d45fbSJean Delvare 	LM83_REG_W_REMOTE1_HIGH,
1098d5d45fbSJean Delvare 	LM83_REG_W_REMOTE2_HIGH,
1108d5d45fbSJean Delvare 	LM83_REG_W_REMOTE3_HIGH,
1118d5d45fbSJean Delvare 	LM83_REG_W_TCRIT,
1128d5d45fbSJean Delvare };
1138d5d45fbSJean Delvare 
1148d5d45fbSJean Delvare /*
1158d5d45fbSJean Delvare  * Functions declaration
1168d5d45fbSJean Delvare  */
1178d5d45fbSJean Delvare 
1188d5d45fbSJean Delvare static int lm83_attach_adapter(struct i2c_adapter *adapter);
1198d5d45fbSJean Delvare static int lm83_detect(struct i2c_adapter *adapter, int address, int kind);
1208d5d45fbSJean Delvare static int lm83_detach_client(struct i2c_client *client);
1218d5d45fbSJean Delvare static struct lm83_data *lm83_update_device(struct device *dev);
1228d5d45fbSJean Delvare 
1238d5d45fbSJean Delvare /*
1248d5d45fbSJean Delvare  * Driver data (common to all clients)
1258d5d45fbSJean Delvare  */
1268d5d45fbSJean Delvare 
1278d5d45fbSJean Delvare static struct i2c_driver lm83_driver = {
1288d5d45fbSJean Delvare 	.owner		= THIS_MODULE,
1298d5d45fbSJean Delvare 	.name		= "lm83",
1308d5d45fbSJean Delvare 	.id		= I2C_DRIVERID_LM83,
1318d5d45fbSJean Delvare 	.flags		= I2C_DF_NOTIFY,
1328d5d45fbSJean Delvare 	.attach_adapter	= lm83_attach_adapter,
1338d5d45fbSJean Delvare 	.detach_client	= lm83_detach_client,
1348d5d45fbSJean Delvare };
1358d5d45fbSJean Delvare 
1368d5d45fbSJean Delvare /*
1378d5d45fbSJean Delvare  * Client data (each client gets its own)
1388d5d45fbSJean Delvare  */
1398d5d45fbSJean Delvare 
1408d5d45fbSJean Delvare struct lm83_data {
1418d5d45fbSJean Delvare 	struct i2c_client client;
142943b0830SMark M. Hoffman 	struct class_device *class_dev;
1438d5d45fbSJean Delvare 	struct semaphore update_lock;
1448d5d45fbSJean Delvare 	char valid; /* zero until following fields are valid */
1458d5d45fbSJean Delvare 	unsigned long last_updated; /* in jiffies */
1468d5d45fbSJean Delvare 
1478d5d45fbSJean Delvare 	/* registers values */
1488d5d45fbSJean Delvare 	s8 temp[9];	/* 0..3: input 1-4,
1498d5d45fbSJean Delvare 			   4..7: high limit 1-4,
1508d5d45fbSJean Delvare 			   8   : critical limit */
1518d5d45fbSJean Delvare 	u16 alarms; /* bitvector, combined */
1528d5d45fbSJean Delvare };
1538d5d45fbSJean Delvare 
1548d5d45fbSJean Delvare /*
1558d5d45fbSJean Delvare  * Sysfs stuff
1568d5d45fbSJean Delvare  */
1578d5d45fbSJean Delvare 
1588d5d45fbSJean Delvare static ssize_t show_temp(struct device *dev, struct device_attribute *devattr,
1598d5d45fbSJean Delvare 			 char *buf)
1608d5d45fbSJean Delvare {
1618d5d45fbSJean Delvare 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1628d5d45fbSJean Delvare 	struct lm83_data *data = lm83_update_device(dev);
1638d5d45fbSJean Delvare 	return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index]));
1648d5d45fbSJean Delvare }
1658d5d45fbSJean Delvare 
1668d5d45fbSJean Delvare static ssize_t set_temp(struct device *dev, struct device_attribute *devattr,
1678d5d45fbSJean Delvare 			const char *buf, size_t count)
1688d5d45fbSJean Delvare {
1698d5d45fbSJean Delvare 	struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr);
1708d5d45fbSJean Delvare 	struct i2c_client *client = to_i2c_client(dev);
1718d5d45fbSJean Delvare 	struct lm83_data *data = i2c_get_clientdata(client);
1728d5d45fbSJean Delvare 	long val = simple_strtol(buf, NULL, 10);
1738d5d45fbSJean Delvare 	int nr = attr->index;
1748d5d45fbSJean Delvare 
1758d5d45fbSJean Delvare 	down(&data->update_lock);
1768d5d45fbSJean Delvare 	data->temp[nr] = TEMP_TO_REG(val);
1778d5d45fbSJean Delvare 	i2c_smbus_write_byte_data(client, LM83_REG_W_HIGH[nr - 4],
1788d5d45fbSJean Delvare 				  data->temp[nr]);
1798d5d45fbSJean Delvare 	up(&data->update_lock);
1808d5d45fbSJean Delvare 	return count;
1818d5d45fbSJean Delvare }
1828d5d45fbSJean Delvare 
1838d5d45fbSJean Delvare static ssize_t show_alarms(struct device *dev, struct device_attribute *dummy,
1848d5d45fbSJean Delvare 			   char *buf)
1858d5d45fbSJean Delvare {
1868d5d45fbSJean Delvare 	struct lm83_data *data = lm83_update_device(dev);
1878d5d45fbSJean Delvare 	return sprintf(buf, "%d\n", data->alarms);
1888d5d45fbSJean Delvare }
1898d5d45fbSJean Delvare 
1908d5d45fbSJean Delvare static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0);
1918d5d45fbSJean Delvare static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1);
1928d5d45fbSJean Delvare static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2);
1938d5d45fbSJean Delvare static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, show_temp, NULL, 3);
1948d5d45fbSJean Delvare static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp,
1958d5d45fbSJean Delvare 	set_temp, 4);
1968d5d45fbSJean Delvare static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp,
1978d5d45fbSJean Delvare 	set_temp, 5);
1988d5d45fbSJean Delvare static SENSOR_DEVICE_ATTR(temp3_max, S_IWUSR | S_IRUGO, show_temp,
1998d5d45fbSJean Delvare 	set_temp, 6);
2008d5d45fbSJean Delvare static SENSOR_DEVICE_ATTR(temp4_max, S_IWUSR | S_IRUGO, show_temp,
2018d5d45fbSJean Delvare 	set_temp, 7);
2028d5d45fbSJean Delvare static SENSOR_DEVICE_ATTR(temp1_crit, S_IRUGO, show_temp, NULL, 8);
2038d5d45fbSJean Delvare static SENSOR_DEVICE_ATTR(temp2_crit, S_IRUGO, show_temp, NULL, 8);
2048d5d45fbSJean Delvare static SENSOR_DEVICE_ATTR(temp3_crit, S_IWUSR | S_IRUGO, show_temp,
2058d5d45fbSJean Delvare 	set_temp, 8);
2068d5d45fbSJean Delvare static SENSOR_DEVICE_ATTR(temp4_crit, S_IRUGO, show_temp, NULL, 8);
2078d5d45fbSJean Delvare static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL);
2088d5d45fbSJean Delvare 
2098d5d45fbSJean Delvare /*
2108d5d45fbSJean Delvare  * Real code
2118d5d45fbSJean Delvare  */
2128d5d45fbSJean Delvare 
2138d5d45fbSJean Delvare static int lm83_attach_adapter(struct i2c_adapter *adapter)
2148d5d45fbSJean Delvare {
2158d5d45fbSJean Delvare 	if (!(adapter->class & I2C_CLASS_HWMON))
2168d5d45fbSJean Delvare 		return 0;
2178d5d45fbSJean Delvare 	return i2c_detect(adapter, &addr_data, lm83_detect);
2188d5d45fbSJean Delvare }
2198d5d45fbSJean Delvare 
2208d5d45fbSJean Delvare /*
2218d5d45fbSJean Delvare  * The following function does more than just detection. If detection
2228d5d45fbSJean Delvare  * succeeds, it also registers the new chip.
2238d5d45fbSJean Delvare  */
2248d5d45fbSJean Delvare static int lm83_detect(struct i2c_adapter *adapter, int address, int kind)
2258d5d45fbSJean Delvare {
2268d5d45fbSJean Delvare 	struct i2c_client *new_client;
2278d5d45fbSJean Delvare 	struct lm83_data *data;
2288d5d45fbSJean Delvare 	int err = 0;
2298d5d45fbSJean Delvare 	const char *name = "";
2308d5d45fbSJean Delvare 
2318d5d45fbSJean Delvare 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
2328d5d45fbSJean Delvare 		goto exit;
2338d5d45fbSJean Delvare 
2348d5d45fbSJean Delvare 	if (!(data = kmalloc(sizeof(struct lm83_data), GFP_KERNEL))) {
2358d5d45fbSJean Delvare 		err = -ENOMEM;
2368d5d45fbSJean Delvare 		goto exit;
2378d5d45fbSJean Delvare 	}
2388d5d45fbSJean Delvare 	memset(data, 0, sizeof(struct lm83_data));
2398d5d45fbSJean Delvare 
2408d5d45fbSJean Delvare 	/* The common I2C client data is placed right after the
2418d5d45fbSJean Delvare 	 * LM83-specific data. */
2428d5d45fbSJean Delvare 	new_client = &data->client;
2438d5d45fbSJean Delvare 	i2c_set_clientdata(new_client, data);
2448d5d45fbSJean Delvare 	new_client->addr = address;
2458d5d45fbSJean Delvare 	new_client->adapter = adapter;
2468d5d45fbSJean Delvare 	new_client->driver = &lm83_driver;
2478d5d45fbSJean Delvare 	new_client->flags = 0;
2488d5d45fbSJean Delvare 
2498d5d45fbSJean Delvare 	/* Now we do the detection and identification. A negative kind
2508d5d45fbSJean Delvare 	 * means that the driver was loaded with no force parameter
2518d5d45fbSJean Delvare 	 * (default), so we must both detect and identify the chip
2528d5d45fbSJean Delvare 	 * (actually there is only one possible kind of chip for now, LM83).
2538d5d45fbSJean Delvare 	 * A zero kind means that the driver was loaded with the force
2548d5d45fbSJean Delvare 	 * parameter, the detection step shall be skipped. A positive kind
2558d5d45fbSJean Delvare 	 * means that the driver was loaded with the force parameter and a
2568d5d45fbSJean Delvare 	 * given kind of chip is requested, so both the detection and the
2578d5d45fbSJean Delvare 	 * identification steps are skipped. */
2588d5d45fbSJean Delvare 
2598d5d45fbSJean Delvare 	/* Default to an LM83 if forced */
2608d5d45fbSJean Delvare 	if (kind == 0)
2618d5d45fbSJean Delvare 		kind = lm83;
2628d5d45fbSJean Delvare 
2638d5d45fbSJean Delvare 	if (kind < 0) { /* detection */
2648d5d45fbSJean Delvare 		if (((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS1)
2658d5d45fbSJean Delvare 		    & 0xA8) != 0x00) ||
2668d5d45fbSJean Delvare 		    ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_STATUS2)
2678d5d45fbSJean Delvare 		    & 0x48) != 0x00) ||
2688d5d45fbSJean Delvare 		    ((i2c_smbus_read_byte_data(new_client, LM83_REG_R_CONFIG)
2698d5d45fbSJean Delvare 		    & 0x41) != 0x00)) {
2708d5d45fbSJean Delvare 			dev_dbg(&adapter->dev,
2718d5d45fbSJean Delvare 			    "LM83 detection failed at 0x%02x.\n", address);
2728d5d45fbSJean Delvare 			goto exit_free;
2738d5d45fbSJean Delvare 		}
2748d5d45fbSJean Delvare 	}
2758d5d45fbSJean Delvare 
2768d5d45fbSJean Delvare 	if (kind <= 0) { /* identification */
2778d5d45fbSJean Delvare 		u8 man_id, chip_id;
2788d5d45fbSJean Delvare 
2798d5d45fbSJean Delvare 		man_id = i2c_smbus_read_byte_data(new_client,
2808d5d45fbSJean Delvare 		    LM83_REG_R_MAN_ID);
2818d5d45fbSJean Delvare 		chip_id = i2c_smbus_read_byte_data(new_client,
2828d5d45fbSJean Delvare 		    LM83_REG_R_CHIP_ID);
2838d5d45fbSJean Delvare 
2848d5d45fbSJean Delvare 		if (man_id == 0x01) { /* National Semiconductor */
2858d5d45fbSJean Delvare 			if (chip_id == 0x03) {
2868d5d45fbSJean Delvare 				kind = lm83;
2878d5d45fbSJean Delvare 			}
2888d5d45fbSJean Delvare 		}
2898d5d45fbSJean Delvare 
2908d5d45fbSJean Delvare 		if (kind <= 0) { /* identification failed */
2918d5d45fbSJean Delvare 			dev_info(&adapter->dev,
2928d5d45fbSJean Delvare 			    "Unsupported chip (man_id=0x%02X, "
2938d5d45fbSJean Delvare 			    "chip_id=0x%02X).\n", man_id, chip_id);
2948d5d45fbSJean Delvare 			goto exit_free;
2958d5d45fbSJean Delvare 		}
2968d5d45fbSJean Delvare 	}
2978d5d45fbSJean Delvare 
2988d5d45fbSJean Delvare 	if (kind == lm83) {
2998d5d45fbSJean Delvare 		name = "lm83";
3008d5d45fbSJean Delvare 	}
3018d5d45fbSJean Delvare 
3028d5d45fbSJean Delvare 	/* We can fill in the remaining client fields */
3038d5d45fbSJean Delvare 	strlcpy(new_client->name, name, I2C_NAME_SIZE);
3048d5d45fbSJean Delvare 	data->valid = 0;
3058d5d45fbSJean Delvare 	init_MUTEX(&data->update_lock);
3068d5d45fbSJean Delvare 
3078d5d45fbSJean Delvare 	/* Tell the I2C layer a new client has arrived */
3088d5d45fbSJean Delvare 	if ((err = i2c_attach_client(new_client)))
3098d5d45fbSJean Delvare 		goto exit_free;
3108d5d45fbSJean Delvare 
3118d5d45fbSJean Delvare 	/*
3128d5d45fbSJean Delvare 	 * Initialize the LM83 chip
3138d5d45fbSJean Delvare 	 * (Nothing to do for this one.)
3148d5d45fbSJean Delvare 	 */
3158d5d45fbSJean Delvare 
3168d5d45fbSJean Delvare 	/* Register sysfs hooks */
317943b0830SMark M. Hoffman 	data->class_dev = hwmon_device_register(&new_client->dev);
318943b0830SMark M. Hoffman 	if (IS_ERR(data->class_dev)) {
319943b0830SMark M. Hoffman 		err = PTR_ERR(data->class_dev);
320943b0830SMark M. Hoffman 		goto exit_detach;
321943b0830SMark M. Hoffman 	}
322943b0830SMark M. Hoffman 
3238d5d45fbSJean Delvare 	device_create_file(&new_client->dev,
3248d5d45fbSJean Delvare 			   &sensor_dev_attr_temp1_input.dev_attr);
3258d5d45fbSJean Delvare 	device_create_file(&new_client->dev,
3268d5d45fbSJean Delvare 			   &sensor_dev_attr_temp2_input.dev_attr);
3278d5d45fbSJean Delvare 	device_create_file(&new_client->dev,
3288d5d45fbSJean Delvare 			   &sensor_dev_attr_temp3_input.dev_attr);
3298d5d45fbSJean Delvare 	device_create_file(&new_client->dev,
3308d5d45fbSJean Delvare 			   &sensor_dev_attr_temp4_input.dev_attr);
3318d5d45fbSJean Delvare 	device_create_file(&new_client->dev,
3328d5d45fbSJean Delvare 			   &sensor_dev_attr_temp1_max.dev_attr);
3338d5d45fbSJean Delvare 	device_create_file(&new_client->dev,
3348d5d45fbSJean Delvare 			   &sensor_dev_attr_temp2_max.dev_attr);
3358d5d45fbSJean Delvare 	device_create_file(&new_client->dev,
3368d5d45fbSJean Delvare 			   &sensor_dev_attr_temp3_max.dev_attr);
3378d5d45fbSJean Delvare 	device_create_file(&new_client->dev,
3388d5d45fbSJean Delvare 			   &sensor_dev_attr_temp4_max.dev_attr);
3398d5d45fbSJean Delvare 	device_create_file(&new_client->dev,
3408d5d45fbSJean Delvare 			   &sensor_dev_attr_temp1_crit.dev_attr);
3418d5d45fbSJean Delvare 	device_create_file(&new_client->dev,
3428d5d45fbSJean Delvare 			   &sensor_dev_attr_temp2_crit.dev_attr);
3438d5d45fbSJean Delvare 	device_create_file(&new_client->dev,
3448d5d45fbSJean Delvare 			   &sensor_dev_attr_temp3_crit.dev_attr);
3458d5d45fbSJean Delvare 	device_create_file(&new_client->dev,
3468d5d45fbSJean Delvare 			   &sensor_dev_attr_temp4_crit.dev_attr);
3478d5d45fbSJean Delvare 	device_create_file(&new_client->dev, &dev_attr_alarms);
3488d5d45fbSJean Delvare 
3498d5d45fbSJean Delvare 	return 0;
3508d5d45fbSJean Delvare 
351943b0830SMark M. Hoffman exit_detach:
352943b0830SMark M. Hoffman 	i2c_detach_client(new_client);
3538d5d45fbSJean Delvare exit_free:
3548d5d45fbSJean Delvare 	kfree(data);
3558d5d45fbSJean Delvare exit:
3568d5d45fbSJean Delvare 	return err;
3578d5d45fbSJean Delvare }
3588d5d45fbSJean Delvare 
3598d5d45fbSJean Delvare static int lm83_detach_client(struct i2c_client *client)
3608d5d45fbSJean Delvare {
361943b0830SMark M. Hoffman 	struct lm83_data *data = i2c_get_clientdata(client);
3628d5d45fbSJean Delvare 	int err;
3638d5d45fbSJean Delvare 
364943b0830SMark M. Hoffman 	hwmon_device_unregister(data->class_dev);
365943b0830SMark M. Hoffman 
3667bef5594SJean Delvare 	if ((err = i2c_detach_client(client)))
3678d5d45fbSJean Delvare 		return err;
3688d5d45fbSJean Delvare 
369943b0830SMark M. Hoffman 	kfree(data);
3708d5d45fbSJean Delvare 	return 0;
3718d5d45fbSJean Delvare }
3728d5d45fbSJean Delvare 
3738d5d45fbSJean Delvare static struct lm83_data *lm83_update_device(struct device *dev)
3748d5d45fbSJean Delvare {
3758d5d45fbSJean Delvare 	struct i2c_client *client = to_i2c_client(dev);
3768d5d45fbSJean Delvare 	struct lm83_data *data = i2c_get_clientdata(client);
3778d5d45fbSJean Delvare 
3788d5d45fbSJean Delvare 	down(&data->update_lock);
3798d5d45fbSJean Delvare 
3808d5d45fbSJean Delvare 	if (time_after(jiffies, data->last_updated + HZ * 2) || !data->valid) {
3818d5d45fbSJean Delvare 		int nr;
3828d5d45fbSJean Delvare 
3838d5d45fbSJean Delvare 		dev_dbg(&client->dev, "Updating lm83 data.\n");
3848d5d45fbSJean Delvare 		for (nr = 0; nr < 9; nr++) {
3858d5d45fbSJean Delvare 			data->temp[nr] =
3868d5d45fbSJean Delvare 			    i2c_smbus_read_byte_data(client,
3878d5d45fbSJean Delvare 			    LM83_REG_R_TEMP[nr]);
3888d5d45fbSJean Delvare 		}
3898d5d45fbSJean Delvare 		data->alarms =
3908d5d45fbSJean Delvare 		    i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS1)
3918d5d45fbSJean Delvare 		    + (i2c_smbus_read_byte_data(client, LM83_REG_R_STATUS2)
3928d5d45fbSJean Delvare 		    << 8);
3938d5d45fbSJean Delvare 
3948d5d45fbSJean Delvare 		data->last_updated = jiffies;
3958d5d45fbSJean Delvare 		data->valid = 1;
3968d5d45fbSJean Delvare 	}
3978d5d45fbSJean Delvare 
3988d5d45fbSJean Delvare 	up(&data->update_lock);
3998d5d45fbSJean Delvare 
4008d5d45fbSJean Delvare 	return data;
4018d5d45fbSJean Delvare }
4028d5d45fbSJean Delvare 
4038d5d45fbSJean Delvare static int __init sensors_lm83_init(void)
4048d5d45fbSJean Delvare {
4058d5d45fbSJean Delvare 	return i2c_add_driver(&lm83_driver);
4068d5d45fbSJean Delvare }
4078d5d45fbSJean Delvare 
4088d5d45fbSJean Delvare static void __exit sensors_lm83_exit(void)
4098d5d45fbSJean Delvare {
4108d5d45fbSJean Delvare 	i2c_del_driver(&lm83_driver);
4118d5d45fbSJean Delvare }
4128d5d45fbSJean Delvare 
4138d5d45fbSJean Delvare MODULE_AUTHOR("Jean Delvare <khali@linux-fr.org>");
4148d5d45fbSJean Delvare MODULE_DESCRIPTION("LM83 driver");
4158d5d45fbSJean Delvare MODULE_LICENSE("GPL");
4168d5d45fbSJean Delvare 
4178d5d45fbSJean Delvare module_init(sensors_lm83_init);
4188d5d45fbSJean Delvare module_exit(sensors_lm83_exit);
419