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 | a8d6646e | 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 | a8d6646e | 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 | a8d6646e | 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 | |
Michael Abbott | f266889 | 2009-09-21 17:04:46 -0700 | [diff] [blame^] | 88 | int temp_max[2]; /* Register values */ |
| 89 | int temp_min[2]; |
| 90 | int temp[2]; |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 91 | u8 alarms; |
| 92 | /* Special values for ADM1023 only */ |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 93 | u8 remote_temp_offset; |
| 94 | u8 remote_temp_offset_prec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 97 | static int adm1021_probe(struct i2c_client *client, |
| 98 | const struct i2c_device_id *id); |
| 99 | static int adm1021_detect(struct i2c_client *client, int kind, |
| 100 | struct i2c_board_info *info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | static void adm1021_init_client(struct i2c_client *client); |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 102 | static int adm1021_remove(struct i2c_client *client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | static struct adm1021_data *adm1021_update_device(struct device *dev); |
| 104 | |
| 105 | /* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */ |
Jean Delvare | 0200296 | 2005-09-25 16:26:44 +0200 | [diff] [blame] | 106 | static int read_only; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
| 108 | |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 109 | static const struct i2c_device_id adm1021_id[] = { |
| 110 | { "adm1021", adm1021 }, |
| 111 | { "adm1023", adm1023 }, |
| 112 | { "max1617", max1617 }, |
| 113 | { "max1617a", max1617a }, |
| 114 | { "thmc10", thmc10 }, |
| 115 | { "lm84", lm84 }, |
| 116 | { "gl523sm", gl523sm }, |
| 117 | { "mc1066", mc1066 }, |
| 118 | { } |
| 119 | }; |
| 120 | MODULE_DEVICE_TABLE(i2c, adm1021_id); |
| 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | /* This is the driver that will be inserted */ |
| 123 | static struct i2c_driver adm1021_driver = { |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 124 | .class = I2C_CLASS_HWMON, |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 125 | .driver = { |
Laurent Riffard | cdaf793 | 2005-11-26 20:37:41 +0100 | [diff] [blame] | 126 | .name = "adm1021", |
| 127 | }, |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 128 | .probe = adm1021_probe, |
| 129 | .remove = adm1021_remove, |
| 130 | .id_table = adm1021_id, |
| 131 | .detect = adm1021_detect, |
| 132 | .address_data = &addr_data, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 135 | static ssize_t show_temp(struct device *dev, |
| 136 | struct device_attribute *devattr, char *buf) |
| 137 | { |
| 138 | int index = to_sensor_dev_attr(devattr)->index; |
| 139 | struct adm1021_data *data = adm1021_update_device(dev); |
| 140 | |
Michael Abbott | f266889 | 2009-09-21 17:04:46 -0700 | [diff] [blame^] | 141 | return sprintf(buf, "%d\n", data->temp[index]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 143 | |
| 144 | static ssize_t show_temp_max(struct device *dev, |
| 145 | struct device_attribute *devattr, char *buf) |
| 146 | { |
| 147 | int index = to_sensor_dev_attr(devattr)->index; |
| 148 | struct adm1021_data *data = adm1021_update_device(dev); |
| 149 | |
Michael Abbott | f266889 | 2009-09-21 17:04:46 -0700 | [diff] [blame^] | 150 | return sprintf(buf, "%d\n", data->temp_max[index]); |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | static ssize_t show_temp_min(struct device *dev, |
| 154 | struct device_attribute *devattr, char *buf) |
| 155 | { |
| 156 | int index = to_sensor_dev_attr(devattr)->index; |
| 157 | struct adm1021_data *data = adm1021_update_device(dev); |
| 158 | |
Michael Abbott | f266889 | 2009-09-21 17:04:46 -0700 | [diff] [blame^] | 159 | return sprintf(buf, "%d\n", data->temp_min[index]); |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 160 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
Krzysztof Helt | 739ba24 | 2007-09-09 19:16:03 +0200 | [diff] [blame] | 162 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, |
| 163 | char *buf) |
| 164 | { |
| 165 | int index = to_sensor_dev_attr(attr)->index; |
| 166 | struct adm1021_data *data = adm1021_update_device(dev); |
| 167 | return sprintf(buf, "%u\n", (data->alarms >> index) & 1); |
| 168 | } |
| 169 | |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 170 | static ssize_t show_alarms(struct device *dev, |
| 171 | struct device_attribute *attr, |
| 172 | char *buf) |
| 173 | { |
| 174 | struct adm1021_data *data = adm1021_update_device(dev); |
| 175 | return sprintf(buf, "%u\n", data->alarms); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 178 | static ssize_t set_temp_max(struct device *dev, |
| 179 | struct device_attribute *devattr, |
| 180 | const char *buf, size_t count) |
| 181 | { |
| 182 | int index = to_sensor_dev_attr(devattr)->index; |
| 183 | struct i2c_client *client = to_i2c_client(dev); |
| 184 | struct adm1021_data *data = i2c_get_clientdata(client); |
| 185 | long temp = simple_strtol(buf, NULL, 10) / 1000; |
| 186 | |
| 187 | mutex_lock(&data->update_lock); |
| 188 | data->temp_max[index] = SENSORS_LIMIT(temp, -128, 127); |
| 189 | if (!read_only) |
| 190 | i2c_smbus_write_byte_data(client, ADM1021_REG_TOS_W(index), |
| 191 | data->temp_max[index]); |
| 192 | mutex_unlock(&data->update_lock); |
| 193 | |
| 194 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 197 | static ssize_t set_temp_min(struct device *dev, |
| 198 | struct device_attribute *devattr, |
| 199 | const char *buf, size_t count) |
| 200 | { |
| 201 | int index = to_sensor_dev_attr(devattr)->index; |
| 202 | struct i2c_client *client = to_i2c_client(dev); |
| 203 | struct adm1021_data *data = i2c_get_clientdata(client); |
| 204 | long temp = simple_strtol(buf, NULL, 10) / 1000; |
| 205 | |
| 206 | mutex_lock(&data->update_lock); |
| 207 | data->temp_min[index] = SENSORS_LIMIT(temp, -128, 127); |
| 208 | if (!read_only) |
| 209 | i2c_smbus_write_byte_data(client, ADM1021_REG_THYST_W(index), |
| 210 | data->temp_min[index]); |
| 211 | mutex_unlock(&data->update_lock); |
| 212 | |
| 213 | return count; |
| 214 | } |
| 215 | |
| 216 | static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); |
| 217 | static SENSOR_DEVICE_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max, |
| 218 | set_temp_max, 0); |
| 219 | static SENSOR_DEVICE_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min, |
| 220 | set_temp_min, 0); |
| 221 | static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1); |
| 222 | static SENSOR_DEVICE_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max, |
| 223 | set_temp_max, 1); |
| 224 | static SENSOR_DEVICE_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min, |
| 225 | set_temp_min, 1); |
Krzysztof Helt | 739ba24 | 2007-09-09 19:16:03 +0200 | [diff] [blame] | 226 | static SENSOR_DEVICE_ATTR(temp1_max_alarm, S_IRUGO, show_alarm, NULL, 6); |
| 227 | static SENSOR_DEVICE_ATTR(temp1_min_alarm, S_IRUGO, show_alarm, NULL, 5); |
| 228 | static SENSOR_DEVICE_ATTR(temp2_max_alarm, S_IRUGO, show_alarm, NULL, 4); |
| 229 | static SENSOR_DEVICE_ATTR(temp2_min_alarm, S_IRUGO, show_alarm, NULL, 3); |
| 230 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 2); |
| 231 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 234 | static struct attribute *adm1021_attributes[] = { |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 235 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 236 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 237 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 238 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 239 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 240 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
Krzysztof Helt | 739ba24 | 2007-09-09 19:16:03 +0200 | [diff] [blame] | 241 | &sensor_dev_attr_temp1_max_alarm.dev_attr.attr, |
| 242 | &sensor_dev_attr_temp1_min_alarm.dev_attr.attr, |
| 243 | &sensor_dev_attr_temp2_max_alarm.dev_attr.attr, |
| 244 | &sensor_dev_attr_temp2_min_alarm.dev_attr.attr, |
| 245 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 246 | &dev_attr_alarms.attr, |
| 247 | NULL |
| 248 | }; |
| 249 | |
| 250 | static const struct attribute_group adm1021_group = { |
| 251 | .attrs = adm1021_attributes, |
| 252 | }; |
| 253 | |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 254 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
| 255 | static int adm1021_detect(struct i2c_client *client, int kind, |
| 256 | struct i2c_board_info *info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | { |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 258 | struct i2c_adapter *adapter = client->adapter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | const char *type_name = ""; |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 261 | int conv_rate, status, config; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 263 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { |
| 264 | pr_debug("adm1021: detect failed, " |
| 265 | "smbus byte data not supported!\n"); |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 266 | return -ENODEV; |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 267 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 269 | status = i2c_smbus_read_byte_data(client, ADM1021_REG_STATUS); |
| 270 | conv_rate = i2c_smbus_read_byte_data(client, |
| 271 | ADM1021_REG_CONV_RATE_R); |
| 272 | config = i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | |
| 274 | /* Now, we do the remaining detection. */ |
| 275 | if (kind < 0) { |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 276 | if ((status & 0x03) != 0x00 || (config & 0x3F) != 0x00 |
| 277 | || (conv_rate & 0xF8) != 0x00) { |
| 278 | pr_debug("adm1021: detect failed, " |
| 279 | "chip not detected!\n"); |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 280 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
| 284 | /* Determine the chip type. */ |
| 285 | if (kind <= 0) { |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 286 | i = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | if (i == 0x41) |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 288 | if ((i2c_smbus_read_byte_data(client, |
| 289 | ADM1021_REG_DEV_ID) & 0xF0) == 0x30) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | kind = adm1023; |
| 291 | else |
| 292 | kind = adm1021; |
| 293 | else if (i == 0x49) |
| 294 | kind = thmc10; |
| 295 | else if (i == 0x23) |
| 296 | kind = gl523sm; |
| 297 | else if ((i == 0x4d) && |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 298 | (i2c_smbus_read_byte_data(client, |
| 299 | ADM1021_REG_DEV_ID) == 0x01)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | kind = max1617a; |
| 301 | else if (i == 0x54) |
| 302 | kind = mc1066; |
| 303 | /* 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] | 304 | else if (conv_rate == 0x00 |
| 305 | && (kind == 0 /* skip extra detection */ |
| 306 | || ((config & 0x7F) == 0x00 |
| 307 | && (status & 0xAB) == 0x00))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | kind = lm84; |
| 309 | else |
| 310 | kind = max1617; |
| 311 | } |
| 312 | |
| 313 | if (kind == max1617) { |
| 314 | type_name = "max1617"; |
| 315 | } else if (kind == max1617a) { |
| 316 | type_name = "max1617a"; |
| 317 | } else if (kind == adm1021) { |
| 318 | type_name = "adm1021"; |
| 319 | } else if (kind == adm1023) { |
| 320 | type_name = "adm1023"; |
| 321 | } else if (kind == thmc10) { |
| 322 | type_name = "thmc10"; |
| 323 | } else if (kind == lm84) { |
| 324 | type_name = "lm84"; |
| 325 | } else if (kind == gl523sm) { |
| 326 | type_name = "gl523sm"; |
| 327 | } else if (kind == mc1066) { |
| 328 | type_name = "mc1066"; |
| 329 | } |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 330 | 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] | 331 | type_name, i2c_adapter_id(adapter), client->addr); |
| 332 | strlcpy(info->type, type_name, I2C_NAME_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | static int adm1021_probe(struct i2c_client *client, |
| 338 | const struct i2c_device_id *id) |
| 339 | { |
| 340 | struct adm1021_data *data; |
| 341 | int err; |
| 342 | |
| 343 | data = kzalloc(sizeof(struct adm1021_data), GFP_KERNEL); |
| 344 | if (!data) { |
| 345 | pr_debug("adm1021: detect failed, kzalloc failed!\n"); |
| 346 | err = -ENOMEM; |
| 347 | goto error0; |
| 348 | } |
| 349 | |
| 350 | i2c_set_clientdata(client, data); |
| 351 | data->type = id->driver_data; |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 352 | mutex_init(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | /* Initialize the ADM1021 chip */ |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 355 | if (data->type != lm84 && !read_only) |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 356 | adm1021_init_client(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
| 358 | /* Register sysfs hooks */ |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 359 | if ((err = sysfs_create_group(&client->dev.kobj, &adm1021_group))) |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 360 | goto error1; |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 361 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 362 | data->hwmon_dev = hwmon_device_register(&client->dev); |
| 363 | if (IS_ERR(data->hwmon_dev)) { |
| 364 | err = PTR_ERR(data->hwmon_dev); |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 365 | goto error3; |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 366 | } |
| 367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | return 0; |
| 369 | |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 370 | error3: |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 371 | sysfs_remove_group(&client->dev.kobj, &adm1021_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | error1: |
| 373 | kfree(data); |
| 374 | error0: |
| 375 | return err; |
| 376 | } |
| 377 | |
| 378 | static void adm1021_init_client(struct i2c_client *client) |
| 379 | { |
| 380 | /* Enable ADC and disable suspend mode */ |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 381 | i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W, |
| 382 | i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | /* Set Conversion rate to 1/sec (this can be tinkered with) */ |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 384 | i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Jean Delvare | 65817ed | 2008-07-16 19:30:08 +0200 | [diff] [blame] | 387 | static int adm1021_remove(struct i2c_client *client) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | { |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 389 | struct adm1021_data *data = i2c_get_clientdata(client); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 391 | hwmon_device_unregister(data->hwmon_dev); |
Mark M. Hoffman | 681c6f7 | 2006-09-24 21:15:35 +0200 | [diff] [blame] | 392 | sysfs_remove_group(&client->dev.kobj, &adm1021_group); |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 393 | |
Mark M. Hoffman | 943b083 | 2005-07-15 21:39:18 -0400 | [diff] [blame] | 394 | kfree(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | return 0; |
| 396 | } |
| 397 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | static struct adm1021_data *adm1021_update_device(struct device *dev) |
| 399 | { |
| 400 | struct i2c_client *client = to_i2c_client(dev); |
| 401 | struct adm1021_data *data = i2c_get_clientdata(client); |
| 402 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 403 | mutex_lock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
| 405 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) |
| 406 | || !data->valid) { |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 407 | int i; |
| 408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | dev_dbg(&client->dev, "Starting adm1021 update\n"); |
| 410 | |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 411 | for (i = 0; i < 2; i++) { |
Michael Abbott | f266889 | 2009-09-21 17:04:46 -0700 | [diff] [blame^] | 412 | data->temp[i] = 1000 * |
| 413 | (s8) i2c_smbus_read_byte_data( |
| 414 | client, ADM1021_REG_TEMP(i)); |
| 415 | data->temp_max[i] = 1000 * |
| 416 | (s8) i2c_smbus_read_byte_data( |
| 417 | client, ADM1021_REG_TOS_R(i)); |
| 418 | data->temp_min[i] = 1000 * |
| 419 | (s8) i2c_smbus_read_byte_data( |
| 420 | client, ADM1021_REG_THYST_R(i)); |
Krzysztof Helt | a8d6646e | 2007-09-09 19:51:14 +0200 | [diff] [blame] | 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) { |
Michael Abbott | f266889 | 2009-09-21 17:04:46 -0700 | [diff] [blame^] | 425 | /* The ADM1023 provides 3 extra bits of precision for |
| 426 | * the remote sensor in extra registers. */ |
| 427 | data->temp[1] += 125 * (i2c_smbus_read_byte_data( |
| 428 | client, ADM1023_REG_REM_TEMP_PREC) >> 5); |
| 429 | data->temp_max[1] += 125 * (i2c_smbus_read_byte_data( |
| 430 | client, ADM1023_REG_REM_TOS_PREC) >> 5); |
| 431 | data->temp_min[1] += 125 * (i2c_smbus_read_byte_data( |
| 432 | client, ADM1023_REG_REM_THYST_PREC) >> 5); |
Krzysztof Helt | c83c41e | 2007-07-19 08:00:17 +0200 | [diff] [blame] | 433 | data->remote_temp_offset = |
| 434 | i2c_smbus_read_byte_data(client, |
| 435 | ADM1023_REG_REM_OFFSET); |
| 436 | data->remote_temp_offset_prec = |
| 437 | i2c_smbus_read_byte_data(client, |
| 438 | ADM1023_REG_REM_OFFSET_PREC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | } |
| 440 | data->last_updated = jiffies; |
| 441 | data->valid = 1; |
| 442 | } |
| 443 | |
Ingo Molnar | 9a61bf6 | 2006-01-18 23:19:26 +0100 | [diff] [blame] | 444 | mutex_unlock(&data->update_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | |
| 446 | return data; |
| 447 | } |
| 448 | |
| 449 | static int __init sensors_adm1021_init(void) |
| 450 | { |
| 451 | return i2c_add_driver(&adm1021_driver); |
| 452 | } |
| 453 | |
| 454 | static void __exit sensors_adm1021_exit(void) |
| 455 | { |
| 456 | i2c_del_driver(&adm1021_driver); |
| 457 | } |
| 458 | |
| 459 | MODULE_AUTHOR ("Frodo Looijaard <frodol@dds.nl> and " |
| 460 | "Philip Edelbrock <phil@netroedge.com>"); |
| 461 | MODULE_DESCRIPTION("adm1021 driver"); |
| 462 | MODULE_LICENSE("GPL"); |
| 463 | |
| 464 | module_param(read_only, bool, 0); |
| 465 | MODULE_PARM_DESC(read_only, "Don't set any values, read only mode"); |
| 466 | |
| 467 | module_init(sensors_adm1021_init) |
| 468 | module_exit(sensors_adm1021_exit) |