| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |     adm1021.c - Part of lm_sensors, Linux kernel modules for hardware | 
| Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 3 | 		monitoring | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 |     Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl> and | 
 | 5 |     Philip Edelbrock <phil@netroedge.com> | 
 | 6 |  | 
 | 7 |     This program is free software; you can redistribute it and/or modify | 
 | 8 |     it under the terms of the GNU General Public License as published by | 
 | 9 |     the Free Software Foundation; either version 2 of the License, or | 
 | 10 |     (at your option) any later version. | 
 | 11 |  | 
 | 12 |     This program is distributed in the hope that it will be useful, | 
 | 13 |     but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 14 |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
 | 15 |     GNU General Public License for more details. | 
 | 16 |  | 
 | 17 |     You should have received a copy of the GNU General Public License | 
 | 18 |     along with this program; if not, write to the Free Software | 
 | 19 |     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
 | 20 | */ | 
 | 21 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/module.h> | 
 | 23 | #include <linux/init.h> | 
 | 24 | #include <linux/slab.h> | 
 | 25 | #include <linux/jiffies.h> | 
 | 26 | #include <linux/i2c.h> | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 27 | #include <linux/hwmon.h> | 
| Krzysztof Helt | a8d6646 | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 28 | #include <linux/hwmon-sysfs.h> | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 29 | #include <linux/err.h> | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 30 | #include <linux/mutex.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 |  | 
 | 32 |  | 
 | 33 | /* Addresses to scan */ | 
| Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 34 | static const unsigned short normal_i2c[] = { | 
 | 35 | 	0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 |  | 
 | 37 | /* Insmod parameters */ | 
| Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 38 | I2C_CLIENT_INSMOD_8(adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm, | 
 | 39 | 			mc1066); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 |  | 
 | 41 | /* adm1021 constants specified below */ | 
 | 42 |  | 
 | 43 | /* The adm1021 registers */ | 
 | 44 | /* Read-only */ | 
| Krzysztof Helt | a8d6646 | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 45 | /* For nr in 0-1 */ | 
 | 46 | #define ADM1021_REG_TEMP(nr)		(nr) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #define ADM1021_REG_STATUS		0x02 | 
| Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 48 | /* 0x41 = AD, 0x49 = TI, 0x4D = Maxim, 0x23 = Genesys , 0x54 = Onsemi */ | 
 | 49 | #define ADM1021_REG_MAN_ID		0xFE | 
 | 50 | /* ADM1021 = 0x0X, ADM1023 = 0x3X */ | 
 | 51 | #define ADM1021_REG_DEV_ID		0xFF | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | /* These use different addresses for reading/writing */ | 
 | 53 | #define ADM1021_REG_CONFIG_R		0x03 | 
 | 54 | #define ADM1021_REG_CONFIG_W		0x09 | 
 | 55 | #define ADM1021_REG_CONV_RATE_R		0x04 | 
 | 56 | #define ADM1021_REG_CONV_RATE_W		0x0A | 
 | 57 | /* These are for the ADM1023's additional precision on the remote temp sensor */ | 
| Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 58 | #define ADM1023_REG_REM_TEMP_PREC	0x10 | 
 | 59 | #define ADM1023_REG_REM_OFFSET		0x11 | 
 | 60 | #define ADM1023_REG_REM_OFFSET_PREC	0x12 | 
 | 61 | #define ADM1023_REG_REM_TOS_PREC	0x13 | 
 | 62 | #define ADM1023_REG_REM_THYST_PREC	0x14 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | /* limits */ | 
| Krzysztof Helt | a8d6646 | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 64 | /* For nr in 0-1 */ | 
 | 65 | #define ADM1021_REG_TOS_R(nr)		(0x05 + 2 * (nr)) | 
 | 66 | #define ADM1021_REG_TOS_W(nr)		(0x0B + 2 * (nr)) | 
 | 67 | #define ADM1021_REG_THYST_R(nr)		(0x06 + 2 * (nr)) | 
 | 68 | #define ADM1021_REG_THYST_W(nr)		(0x0C + 2 * (nr)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | /* write-only */ | 
 | 70 | #define ADM1021_REG_ONESHOT		0x0F | 
 | 71 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | /* Initial values */ | 
 | 73 |  | 
| Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 74 | /* Note: Even though I left the low and high limits named os and hyst, | 
 | 75 | they don't quite work like a thermostat the way the LM75 does.  I.e., | 
 | 76 | a lower temp than THYST actually triggers an alarm instead of | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | clearing it.  Weird, ey?   --Phil  */ | 
 | 78 |  | 
 | 79 | /* Each client has this additional data */ | 
 | 80 | struct adm1021_data { | 
| Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 81 | 	struct device *hwmon_dev; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | 	enum chips type; | 
 | 83 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 84 | 	struct mutex update_lock; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | 	char valid;		/* !=0 if following fields are valid */ | 
 | 86 | 	unsigned long last_updated;	/* In jiffies */ | 
 | 87 |  | 
| Krzysztof Helt | a8d6646 | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 88 | 	s8 temp_max[2];		/* Register values */ | 
 | 89 | 	s8 temp_min[2]; | 
 | 90 | 	s8 temp[2]; | 
 | 91 | 	u8 alarms; | 
 | 92 | 	/* Special values for ADM1023 only */ | 
 | 93 | 	u8 remote_temp_prec; | 
 | 94 | 	u8 remote_temp_os_prec; | 
 | 95 | 	u8 remote_temp_hyst_prec; | 
 | 96 | 	u8 remote_temp_offset; | 
 | 97 | 	u8 remote_temp_offset_prec; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | }; | 
 | 99 |  | 
| Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 100 | static int adm1021_probe(struct i2c_client *client, | 
 | 101 | 			 const struct i2c_device_id *id); | 
 | 102 | static int adm1021_detect(struct i2c_client *client, int kind, | 
 | 103 | 			  struct i2c_board_info *info); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | static void adm1021_init_client(struct i2c_client *client); | 
| Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 105 | static int adm1021_remove(struct i2c_client *client); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | static struct adm1021_data *adm1021_update_device(struct device *dev); | 
 | 107 |  | 
 | 108 | /* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */ | 
| Jean Delvare | 0200296 | 2005-09-25 16:26:44 +0200 | [diff] [blame] | 109 | static int read_only; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 |  | 
 | 111 |  | 
| Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 112 | static const struct i2c_device_id adm1021_id[] = { | 
 | 113 | 	{ "adm1021", adm1021 }, | 
 | 114 | 	{ "adm1023", adm1023 }, | 
 | 115 | 	{ "max1617", max1617 }, | 
 | 116 | 	{ "max1617a", max1617a }, | 
 | 117 | 	{ "thmc10", thmc10 }, | 
 | 118 | 	{ "lm84", lm84 }, | 
 | 119 | 	{ "gl523sm", gl523sm }, | 
 | 120 | 	{ "mc1066", mc1066 }, | 
 | 121 | 	{ } | 
 | 122 | }; | 
 | 123 | MODULE_DEVICE_TABLE(i2c, adm1021_id); | 
 | 124 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | /* This is the driver that will be inserted */ | 
 | 126 | static struct i2c_driver adm1021_driver = { | 
| Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 127 | 	.class		= I2C_CLASS_HWMON, | 
| Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 128 | 	.driver = { | 
| Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 129 | 		.name	= "adm1021", | 
 | 130 | 	}, | 
| Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 131 | 	.probe		= adm1021_probe, | 
 | 132 | 	.remove		= adm1021_remove, | 
 | 133 | 	.id_table	= adm1021_id, | 
 | 134 | 	.detect		= adm1021_detect, | 
 | 135 | 	.address_data	= &addr_data, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | }; | 
 | 137 |  | 
| Krzysztof Helt | a8d6646 | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 138 | static ssize_t show_temp(struct device *dev, | 
 | 139 | 			 struct device_attribute *devattr, char *buf) | 
 | 140 | { | 
 | 141 | 	int index = to_sensor_dev_attr(devattr)->index; | 
 | 142 | 	struct adm1021_data *data = adm1021_update_device(dev); | 
 | 143 |  | 
 | 144 | 	return sprintf(buf, "%d\n", 1000 * data->temp[index]); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | } | 
| Krzysztof Helt | a8d6646 | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 146 |  | 
 | 147 | static ssize_t show_temp_max(struct device *dev, | 
 | 148 | 			     struct device_attribute *devattr, char *buf) | 
 | 149 | { | 
 | 150 | 	int index = to_sensor_dev_attr(devattr)->index; | 
 | 151 | 	struct adm1021_data *data = adm1021_update_device(dev); | 
 | 152 |  | 
 | 153 | 	return sprintf(buf, "%d\n", 1000 * data->temp_max[index]); | 
 | 154 | } | 
 | 155 |  | 
 | 156 | static ssize_t show_temp_min(struct device *dev, | 
 | 157 | 			     struct device_attribute *devattr, char *buf) | 
 | 158 | { | 
 | 159 | 	int index = to_sensor_dev_attr(devattr)->index; | 
 | 160 | 	struct adm1021_data *data = adm1021_update_device(dev); | 
 | 161 |  | 
 | 162 | 	return sprintf(buf, "%d\n", 1000 * data->temp_min[index]); | 
 | 163 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 |  | 
| Krzysztof Helt | 739ba24 | 2007-09-09 19:16:03 +0200 | [diff] [blame] | 165 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, | 
 | 166 | 			  char *buf) | 
 | 167 | { | 
 | 168 | 	int index = to_sensor_dev_attr(attr)->index; | 
 | 169 | 	struct adm1021_data *data = adm1021_update_device(dev); | 
 | 170 | 	return sprintf(buf, "%u\n", (data->alarms >> index) & 1); | 
 | 171 | } | 
 | 172 |  | 
| Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 173 | static ssize_t show_alarms(struct device *dev, | 
 | 174 | 			   struct device_attribute *attr, | 
 | 175 | 			   char *buf) | 
 | 176 | { | 
 | 177 | 	struct adm1021_data *data = adm1021_update_device(dev); | 
 | 178 | 	return sprintf(buf, "%u\n", data->alarms); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 |  | 
| Krzysztof Helt | a8d6646 | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 181 | static ssize_t set_temp_max(struct device *dev, | 
 | 182 | 			    struct device_attribute *devattr, | 
 | 183 | 			    const char *buf, size_t count) | 
 | 184 | { | 
 | 185 | 	int index = to_sensor_dev_attr(devattr)->index; | 
 | 186 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 187 | 	struct adm1021_data *data = i2c_get_clientdata(client); | 
 | 188 | 	long temp = simple_strtol(buf, NULL, 10) / 1000; | 
 | 189 |  | 
 | 190 | 	mutex_lock(&data->update_lock); | 
 | 191 | 	data->temp_max[index] = SENSORS_LIMIT(temp, -128, 127); | 
 | 192 | 	if (!read_only) | 
 | 193 | 		i2c_smbus_write_byte_data(client, ADM1021_REG_TOS_W(index), | 
 | 194 | 					  data->temp_max[index]); | 
 | 195 | 	mutex_unlock(&data->update_lock); | 
 | 196 |  | 
 | 197 | 	return count; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 |  | 
| Krzysztof Helt | a8d6646 | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 200 | static ssize_t set_temp_min(struct device *dev, | 
 | 201 | 			    struct device_attribute *devattr, | 
 | 202 | 			    const char *buf, size_t count) | 
 | 203 | { | 
 | 204 | 	int index = to_sensor_dev_attr(devattr)->index; | 
 | 205 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 206 | 	struct adm1021_data *data = i2c_get_clientdata(client); | 
 | 207 | 	long temp = simple_strtol(buf, NULL, 10) / 1000; | 
 | 208 |  | 
 | 209 | 	mutex_lock(&data->update_lock); | 
 | 210 | 	data->temp_min[index] = SENSORS_LIMIT(temp, -128, 127); | 
 | 211 | 	if (!read_only) | 
 | 212 | 		i2c_smbus_write_byte_data(client, ADM1021_REG_THYST_W(index), | 
 | 213 | 					  data->temp_min[index]); | 
 | 214 | 	mutex_unlock(&data->update_lock); | 
 | 215 |  | 
 | 216 | 	return count; | 
 | 217 | } | 
 | 218 |  | 
 | 219 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); | 
 | 220 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, | 
 | 221 | 			  set_temp_max, 0); | 
 | 222 | static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min, | 
 | 223 | 			  set_temp_min, 0); | 
 | 224 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1); | 
 | 225 | static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max, | 
 | 226 | 			  set_temp_max, 1); | 
 | 227 | static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min, | 
 | 228 | 			  set_temp_min, 1); | 
| Krzysztof Helt | 739ba24 | 2007-09-09 19:16:03 +0200 | [diff] [blame] | 229 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6); | 
 | 230 | static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5); | 
 | 231 | static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4); | 
 | 232 | static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3); | 
 | 233 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2); | 
 | 234 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 |  | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 237 | static struct attribute *adm1021_attributes[] = { | 
| Krzysztof Helt | a8d6646 | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 238 | 	&sensor_dev_attr_temp1_max.dev_attr.attr, | 
 | 239 | 	&sensor_dev_attr_temp1_min.dev_attr.attr, | 
 | 240 | 	&sensor_dev_attr_temp1_input.dev_attr.attr, | 
 | 241 | 	&sensor_dev_attr_temp2_max.dev_attr.attr, | 
 | 242 | 	&sensor_dev_attr_temp2_min.dev_attr.attr, | 
 | 243 | 	&sensor_dev_attr_temp2_input.dev_attr.attr, | 
| Krzysztof Helt | 739ba24 | 2007-09-09 19:16:03 +0200 | [diff] [blame] | 244 | 	&sensor_dev_attr_temp1_max_alarm.dev_attr.attr, | 
 | 245 | 	&sensor_dev_attr_temp1_min_alarm.dev_attr.attr, | 
 | 246 | 	&sensor_dev_attr_temp2_max_alarm.dev_attr.attr, | 
 | 247 | 	&sensor_dev_attr_temp2_min_alarm.dev_attr.attr, | 
 | 248 | 	&sensor_dev_attr_temp2_fault.dev_attr.attr, | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 249 | 	&dev_attr_alarms.attr, | 
 | 250 | 	NULL | 
 | 251 | }; | 
 | 252 |  | 
 | 253 | static const struct attribute_group adm1021_group = { | 
 | 254 | 	.attrs = adm1021_attributes, | 
 | 255 | }; | 
 | 256 |  | 
| Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 257 | /* Return 0 if detection is successful, -ENODEV otherwise */ | 
 | 258 | static int adm1021_detect(struct i2c_client *client, int kind, | 
 | 259 | 			  struct i2c_board_info *info) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | { | 
| Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 261 | 	struct i2c_adapter *adapter = client->adapter; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | 	int i; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | 	const char *type_name = ""; | 
| Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 264 | 	int conv_rate, status, config; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 |  | 
| Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 266 | 	if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { | 
 | 267 | 		pr_debug("adm1021: detect failed, " | 
 | 268 | 			 "smbus byte data not supported!\n"); | 
| Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 269 | 		return -ENODEV; | 
| Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 270 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 |  | 
| Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 272 | 	status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS); | 
 | 273 | 	conv_rate = i2c_smbus_read_byte_data(client, | 
 | 274 | 					     ADM1021_REG_CONV_RATE_R); | 
 | 275 | 	config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 |  | 
 | 277 | 	/* Now, we do the remaining detection. */ | 
 | 278 | 	if (kind < 0) { | 
| Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 279 | 		if ((status & 0x03) != 0x00 || (config & 0x3F) != 0x00 | 
 | 280 | 		    || (conv_rate & 0xF8) != 0x00) { | 
 | 281 | 			pr_debug("adm1021: detect failed, " | 
 | 282 | 				 "chip not detected!\n"); | 
| Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 283 | 			return -ENODEV; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | 		} | 
 | 285 | 	} | 
 | 286 |  | 
 | 287 | 	/* Determine the chip type. */ | 
 | 288 | 	if (kind <= 0) { | 
| Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 289 | 		i = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | 		if (i == 0x41) | 
| Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 291 | 			if ((i2c_smbus_read_byte_data(client, | 
 | 292 | 					ADM1021_REG_DEV_ID) & 0xF0) == 0x30) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | 				kind = adm1023; | 
 | 294 | 			else | 
 | 295 | 				kind = adm1021; | 
 | 296 | 		else if (i == 0x49) | 
 | 297 | 			kind = thmc10; | 
 | 298 | 		else if (i == 0x23) | 
 | 299 | 			kind = gl523sm; | 
 | 300 | 		else if ((i == 0x4d) && | 
| Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 301 | 			 (i2c_smbus_read_byte_data(client, | 
 | 302 | 						   ADM1021_REG_DEV_ID) == 0x01)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | 			kind = max1617a; | 
 | 304 | 		else if (i == 0x54) | 
 | 305 | 			kind = mc1066; | 
 | 306 | 		/* LM84 Mfr ID in a different place, and it has more unused bits */ | 
| Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 307 | 		else if (conv_rate == 0x00 | 
 | 308 | 			 && (kind == 0 /* skip extra detection */ | 
 | 309 | 			     || ((config & 0x7F) == 0x00 | 
 | 310 | 				 && (status & 0xAB) == 0x00))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | 			kind = lm84; | 
 | 312 | 		else | 
 | 313 | 			kind = max1617; | 
 | 314 | 	} | 
 | 315 |  | 
 | 316 | 	if (kind == max1617) { | 
 | 317 | 		type_name = "max1617"; | 
 | 318 | 	} else if (kind == max1617a) { | 
 | 319 | 		type_name = "max1617a"; | 
 | 320 | 	} else if (kind == adm1021) { | 
 | 321 | 		type_name = "adm1021"; | 
 | 322 | 	} else if (kind == adm1023) { | 
 | 323 | 		type_name = "adm1023"; | 
 | 324 | 	} else if (kind == thmc10) { | 
 | 325 | 		type_name = "thmc10"; | 
 | 326 | 	} else if (kind == lm84) { | 
 | 327 | 		type_name = "lm84"; | 
 | 328 | 	} else if (kind == gl523sm) { | 
 | 329 | 		type_name = "gl523sm"; | 
 | 330 | 	} else if (kind == mc1066) { | 
 | 331 | 		type_name = "mc1066"; | 
 | 332 | 	} | 
| Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 333 | 	pr_debug("adm1021: Detected chip %s at adapter %d, address 0x%02x.\n", | 
| Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 334 | 		 type_name, i2c_adapter_id(adapter), client->addr); | 
 | 335 | 	strlcpy(info->type, type_name, I2C_NAME_SIZE); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 |  | 
| Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 337 | 	return 0; | 
 | 338 | } | 
 | 339 |  | 
 | 340 | static int adm1021_probe(struct i2c_client *client, | 
 | 341 | 			 const struct i2c_device_id *id) | 
 | 342 | { | 
 | 343 | 	struct adm1021_data *data; | 
 | 344 | 	int err; | 
 | 345 |  | 
 | 346 | 	data = kzalloc(sizeof(struct adm1021_data), GFP_KERNEL); | 
 | 347 | 	if (!data) { | 
 | 348 | 		pr_debug("adm1021: detect failed, kzalloc failed!\n"); | 
 | 349 | 		err = -ENOMEM; | 
 | 350 | 		goto error0; | 
 | 351 | 	} | 
 | 352 |  | 
 | 353 | 	i2c_set_clientdata(client, data); | 
 | 354 | 	data->type = id->driver_data; | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 355 | 	mutex_init(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | 	/* Initialize the ADM1021 chip */ | 
| Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 358 | 	if (data->type != lm84 && !read_only) | 
| Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 359 | 		adm1021_init_client(client); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 |  | 
 | 361 | 	/* Register sysfs hooks */ | 
| Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 362 | 	if ((err = sysfs_create_group(&client->dev.kobj, &adm1021_group))) | 
| Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 363 | 		goto error1; | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 364 |  | 
| Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 365 | 	data->hwmon_dev = hwmon_device_register(&client->dev); | 
 | 366 | 	if (IS_ERR(data->hwmon_dev)) { | 
 | 367 | 		err = PTR_ERR(data->hwmon_dev); | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 368 | 		goto error3; | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 369 | 	} | 
 | 370 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | 	return 0; | 
 | 372 |  | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 373 | error3: | 
| Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 374 | 	sysfs_remove_group(&client->dev.kobj, &adm1021_group); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | error1: | 
 | 376 | 	kfree(data); | 
 | 377 | error0: | 
 | 378 | 	return err; | 
 | 379 | } | 
 | 380 |  | 
 | 381 | static void adm1021_init_client(struct i2c_client *client) | 
 | 382 | { | 
 | 383 | 	/* Enable ADC and disable suspend mode */ | 
| Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 384 | 	i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W, | 
 | 385 | 		i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | 	/* Set Conversion rate to 1/sec (this can be tinkered with) */ | 
| Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 387 | 	i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | } | 
 | 389 |  | 
| Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 390 | static int adm1021_remove(struct i2c_client *client) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | { | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 392 | 	struct adm1021_data *data = i2c_get_clientdata(client); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 |  | 
| Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 394 | 	hwmon_device_unregister(data->hwmon_dev); | 
| Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 395 | 	sysfs_remove_group(&client->dev.kobj, &adm1021_group); | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 396 |  | 
| Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 397 | 	kfree(data); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | 	return 0; | 
 | 399 | } | 
 | 400 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | static struct adm1021_data *adm1021_update_device(struct device *dev) | 
 | 402 | { | 
 | 403 | 	struct i2c_client *client = to_i2c_client(dev); | 
 | 404 | 	struct adm1021_data *data = i2c_get_clientdata(client); | 
 | 405 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 406 | 	mutex_lock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 |  | 
 | 408 | 	if (time_after(jiffies, data->last_updated + HZ + HZ / 2) | 
 | 409 | 	    || !data->valid) { | 
| Krzysztof Helt | a8d6646 | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 410 | 		int i; | 
 | 411 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | 		dev_dbg(&client->dev, "Starting adm1021 update\n"); | 
 | 413 |  | 
| Krzysztof Helt | a8d6646 | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 414 | 		for (i = 0; i < 2; i++) { | 
 | 415 | 			data->temp[i] = i2c_smbus_read_byte_data(client, | 
 | 416 | 						ADM1021_REG_TEMP(i)); | 
 | 417 | 			data->temp_max[i] = i2c_smbus_read_byte_data(client, | 
 | 418 | 						ADM1021_REG_TOS_R(i)); | 
 | 419 | 			data->temp_min[i] = i2c_smbus_read_byte_data(client, | 
 | 420 | 						ADM1021_REG_THYST_R(i)); | 
 | 421 | 		} | 
| Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 422 | 		data->alarms = i2c_smbus_read_byte_data(client, | 
 | 423 | 						ADM1021_REG_STATUS) & 0x7c; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | 		if (data->type == adm1023) { | 
| Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 425 | 			data->remote_temp_prec = | 
 | 426 | 				i2c_smbus_read_byte_data(client, | 
 | 427 | 						ADM1023_REG_REM_TEMP_PREC); | 
 | 428 | 			data->remote_temp_os_prec = | 
 | 429 | 				i2c_smbus_read_byte_data(client, | 
 | 430 | 						ADM1023_REG_REM_TOS_PREC); | 
 | 431 | 			data->remote_temp_hyst_prec = | 
 | 432 | 				i2c_smbus_read_byte_data(client, | 
 | 433 | 						ADM1023_REG_REM_THYST_PREC); | 
 | 434 | 			data->remote_temp_offset = | 
 | 435 | 				i2c_smbus_read_byte_data(client, | 
 | 436 | 						ADM1023_REG_REM_OFFSET); | 
 | 437 | 			data->remote_temp_offset_prec = | 
 | 438 | 				i2c_smbus_read_byte_data(client, | 
 | 439 | 						ADM1023_REG_REM_OFFSET_PREC); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | 		} | 
 | 441 | 		data->last_updated = jiffies; | 
 | 442 | 		data->valid = 1; | 
 | 443 | 	} | 
 | 444 |  | 
| Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 445 | 	mutex_unlock(&data->update_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 |  | 
 | 447 | 	return data; | 
 | 448 | } | 
 | 449 |  | 
 | 450 | static int __init sensors_adm1021_init(void) | 
 | 451 | { | 
 | 452 | 	return i2c_add_driver(&adm1021_driver); | 
 | 453 | } | 
 | 454 |  | 
 | 455 | static void __exit sensors_adm1021_exit(void) | 
 | 456 | { | 
 | 457 | 	i2c_del_driver(&adm1021_driver); | 
 | 458 | } | 
 | 459 |  | 
 | 460 | MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl> and " | 
 | 461 | 		"Philip Edelbrock <phil@netroedge.com>"); | 
 | 462 | MODULE_DESCRIPTION("adm1021 driver"); | 
 | 463 | MODULE_LICENSE("GPL"); | 
 | 464 |  | 
 | 465 | module_param(read_only, bool, 0); | 
 | 466 | MODULE_PARM_DESC(read_only, "Don't set any values, read only mode"); | 
 | 467 |  | 
 | 468 | module_init(sensors_adm1021_init) | 
 | 469 | module_exit(sensors_adm1021_exit) |