Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 1 | /* |
| 2 | smsc47m192.c - Support for hardware monitoring block of |
Jean Delvare | 00cb473 | 2007-05-08 17:21:59 +0200 | [diff] [blame] | 3 | SMSC LPC47M192 and compatible Super I/O chips |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 4 | |
| 5 | Copyright (C) 2006 Hartmut Rick <linux@rick.claranet.de> |
| 6 | |
| 7 | Derived from lm78.c and other chip drivers. |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation; either version 2 of the License, or |
| 12 | (at your option) any later version. |
| 13 | |
| 14 | This program is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with this program; if not, write to the Free Software |
| 21 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 22 | */ |
| 23 | |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/slab.h> |
| 27 | #include <linux/jiffies.h> |
| 28 | #include <linux/i2c.h> |
| 29 | #include <linux/hwmon.h> |
| 30 | #include <linux/hwmon-sysfs.h> |
| 31 | #include <linux/hwmon-vid.h> |
| 32 | #include <linux/err.h> |
Jean Delvare | ce8c6ce1 | 2006-09-24 21:25:12 +0200 | [diff] [blame] | 33 | #include <linux/sysfs.h> |
Jean Delvare | e4a7167 | 2007-06-09 10:11:15 -0400 | [diff] [blame] | 34 | #include <linux/mutex.h> |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 35 | |
| 36 | /* Addresses to scan */ |
Mark M. Hoffman | 25e9c86 | 2008-02-17 22:28:03 -0500 | [diff] [blame] | 37 | static const unsigned short normal_i2c[] = { 0x2c, 0x2d, I2C_CLIENT_END }; |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 38 | |
| 39 | /* Insmod parameters */ |
| 40 | I2C_CLIENT_INSMOD_1(smsc47m192); |
| 41 | |
| 42 | /* SMSC47M192 registers */ |
| 43 | #define SMSC47M192_REG_IN(nr) ((nr)<6 ? (0x20 + (nr)) : \ |
| 44 | (0x50 + (nr) - 6)) |
| 45 | #define SMSC47M192_REG_IN_MAX(nr) ((nr)<6 ? (0x2b + (nr) * 2) : \ |
| 46 | (0x54 + (((nr) - 6) * 2))) |
| 47 | #define SMSC47M192_REG_IN_MIN(nr) ((nr)<6 ? (0x2c + (nr) * 2) : \ |
| 48 | (0x55 + (((nr) - 6) * 2))) |
| 49 | static u8 SMSC47M192_REG_TEMP[3] = { 0x27, 0x26, 0x52 }; |
| 50 | static u8 SMSC47M192_REG_TEMP_MAX[3] = { 0x39, 0x37, 0x58 }; |
| 51 | static u8 SMSC47M192_REG_TEMP_MIN[3] = { 0x3A, 0x38, 0x59 }; |
| 52 | #define SMSC47M192_REG_TEMP_OFFSET(nr) ((nr)==2 ? 0x1e : 0x1f) |
| 53 | #define SMSC47M192_REG_ALARM1 0x41 |
| 54 | #define SMSC47M192_REG_ALARM2 0x42 |
| 55 | #define SMSC47M192_REG_VID 0x47 |
| 56 | #define SMSC47M192_REG_VID4 0x49 |
| 57 | #define SMSC47M192_REG_CONFIG 0x40 |
| 58 | #define SMSC47M192_REG_SFR 0x4f |
| 59 | #define SMSC47M192_REG_COMPANY_ID 0x3e |
| 60 | #define SMSC47M192_REG_VERSION 0x3f |
| 61 | |
| 62 | /* generalised scaling with integer rounding */ |
| 63 | static inline int SCALE(long val, int mul, int div) |
| 64 | { |
| 65 | if (val < 0) |
| 66 | return (val * mul - div / 2) / div; |
| 67 | else |
| 68 | return (val * mul + div / 2) / div; |
| 69 | } |
| 70 | |
| 71 | /* Conversions */ |
| 72 | |
| 73 | /* smsc47m192 internally scales voltage measurements */ |
| 74 | static const u16 nom_mv[] = { 2500, 2250, 3300, 5000, 12000, 3300, 1500, 1800 }; |
| 75 | |
| 76 | static inline unsigned int IN_FROM_REG(u8 reg, int n) |
| 77 | { |
| 78 | return SCALE(reg, nom_mv[n], 192); |
| 79 | } |
| 80 | |
| 81 | static inline u8 IN_TO_REG(unsigned long val, int n) |
| 82 | { |
| 83 | return SENSORS_LIMIT(SCALE(val, 192, nom_mv[n]), 0, 255); |
| 84 | } |
| 85 | |
| 86 | /* TEMP: 0.001 degC units (-128C to +127C) |
| 87 | REG: 1C/bit, two's complement */ |
| 88 | static inline s8 TEMP_TO_REG(int val) |
| 89 | { |
| 90 | return SENSORS_LIMIT(SCALE(val, 1, 1000), -128000, 127000); |
| 91 | } |
| 92 | |
| 93 | static inline int TEMP_FROM_REG(s8 val) |
| 94 | { |
| 95 | return val * 1000; |
| 96 | } |
| 97 | |
| 98 | struct smsc47m192_data { |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 99 | struct device *hwmon_dev; |
Jean Delvare | e4a7167 | 2007-06-09 10:11:15 -0400 | [diff] [blame] | 100 | struct mutex update_lock; |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 101 | char valid; /* !=0 if following fields are valid */ |
| 102 | unsigned long last_updated; /* In jiffies */ |
| 103 | |
| 104 | u8 in[8]; /* Register value */ |
| 105 | u8 in_max[8]; /* Register value */ |
| 106 | u8 in_min[8]; /* Register value */ |
| 107 | s8 temp[3]; /* Register value */ |
| 108 | s8 temp_max[3]; /* Register value */ |
| 109 | s8 temp_min[3]; /* Register value */ |
| 110 | s8 temp_offset[3]; /* Register value */ |
| 111 | u16 alarms; /* Register encoding, combined */ |
| 112 | u8 vid; /* Register encoding, combined */ |
| 113 | u8 vrm; |
| 114 | }; |
| 115 | |
Jean Delvare | 8fb597b | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 116 | static int smsc47m192_probe(struct i2c_client *client, |
| 117 | const struct i2c_device_id *id); |
| 118 | static int smsc47m192_detect(struct i2c_client *client, int kind, |
| 119 | struct i2c_board_info *info); |
| 120 | static int smsc47m192_remove(struct i2c_client *client); |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 121 | static struct smsc47m192_data *smsc47m192_update_device(struct device *dev); |
| 122 | |
Jean Delvare | 8fb597b | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 123 | static const struct i2c_device_id smsc47m192_id[] = { |
| 124 | { "smsc47m192", smsc47m192 }, |
| 125 | { } |
| 126 | }; |
| 127 | MODULE_DEVICE_TABLE(i2c, smsc47m192_id); |
| 128 | |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 129 | static struct i2c_driver smsc47m192_driver = { |
Jean Delvare | 8fb597b | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 130 | .class = I2C_CLASS_HWMON, |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 131 | .driver = { |
| 132 | .name = "smsc47m192", |
| 133 | }, |
Jean Delvare | 8fb597b | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 134 | .probe = smsc47m192_probe, |
| 135 | .remove = smsc47m192_remove, |
| 136 | .id_table = smsc47m192_id, |
| 137 | .detect = smsc47m192_detect, |
| 138 | .address_data = &addr_data, |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | /* Voltages */ |
| 142 | static ssize_t show_in(struct device *dev, struct device_attribute *attr, |
| 143 | char *buf) |
| 144 | { |
| 145 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 146 | int nr = sensor_attr->index; |
| 147 | struct smsc47m192_data *data = smsc47m192_update_device(dev); |
| 148 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in[nr], nr)); |
| 149 | } |
| 150 | |
| 151 | static ssize_t show_in_min(struct device *dev, struct device_attribute *attr, |
| 152 | char *buf) |
| 153 | { |
| 154 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 155 | int nr = sensor_attr->index; |
| 156 | struct smsc47m192_data *data = smsc47m192_update_device(dev); |
| 157 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_min[nr], nr)); |
| 158 | } |
| 159 | |
| 160 | static ssize_t show_in_max(struct device *dev, struct device_attribute *attr, |
| 161 | char *buf) |
| 162 | { |
| 163 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 164 | int nr = sensor_attr->index; |
| 165 | struct smsc47m192_data *data = smsc47m192_update_device(dev); |
| 166 | return sprintf(buf, "%d\n", IN_FROM_REG(data->in_max[nr], nr)); |
| 167 | } |
| 168 | |
| 169 | static ssize_t set_in_min(struct device *dev, struct device_attribute *attr, |
| 170 | const char *buf, size_t count) |
| 171 | { |
| 172 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 173 | int nr = sensor_attr->index; |
| 174 | struct i2c_client *client = to_i2c_client(dev); |
| 175 | struct smsc47m192_data *data = i2c_get_clientdata(client); |
| 176 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 177 | |
Jean Delvare | e4a7167 | 2007-06-09 10:11:15 -0400 | [diff] [blame] | 178 | mutex_lock(&data->update_lock); |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 179 | data->in_min[nr] = IN_TO_REG(val, nr); |
| 180 | i2c_smbus_write_byte_data(client, SMSC47M192_REG_IN_MIN(nr), |
| 181 | data->in_min[nr]); |
Jean Delvare | e4a7167 | 2007-06-09 10:11:15 -0400 | [diff] [blame] | 182 | mutex_unlock(&data->update_lock); |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 183 | return count; |
| 184 | } |
| 185 | |
| 186 | static ssize_t set_in_max(struct device *dev, struct device_attribute *attr, |
| 187 | const char *buf, size_t count) |
| 188 | { |
| 189 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 190 | int nr = sensor_attr->index; |
| 191 | struct i2c_client *client = to_i2c_client(dev); |
| 192 | struct smsc47m192_data *data = i2c_get_clientdata(client); |
| 193 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 194 | |
Jean Delvare | e4a7167 | 2007-06-09 10:11:15 -0400 | [diff] [blame] | 195 | mutex_lock(&data->update_lock); |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 196 | data->in_max[nr] = IN_TO_REG(val, nr); |
| 197 | i2c_smbus_write_byte_data(client, SMSC47M192_REG_IN_MAX(nr), |
| 198 | data->in_max[nr]); |
Jean Delvare | e4a7167 | 2007-06-09 10:11:15 -0400 | [diff] [blame] | 199 | mutex_unlock(&data->update_lock); |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 200 | return count; |
| 201 | } |
| 202 | |
| 203 | #define show_in_offset(offset) \ |
| 204 | static SENSOR_DEVICE_ATTR(in##offset##_input, S_IRUGO, \ |
| 205 | show_in, NULL, offset); \ |
| 206 | static SENSOR_DEVICE_ATTR(in##offset##_min, S_IRUGO | S_IWUSR, \ |
| 207 | show_in_min, set_in_min, offset); \ |
| 208 | static SENSOR_DEVICE_ATTR(in##offset##_max, S_IRUGO | S_IWUSR, \ |
| 209 | show_in_max, set_in_max, offset); |
| 210 | |
| 211 | show_in_offset(0) |
| 212 | show_in_offset(1) |
| 213 | show_in_offset(2) |
| 214 | show_in_offset(3) |
| 215 | show_in_offset(4) |
| 216 | show_in_offset(5) |
| 217 | show_in_offset(6) |
| 218 | show_in_offset(7) |
| 219 | |
| 220 | /* Temperatures */ |
| 221 | static ssize_t show_temp(struct device *dev, struct device_attribute *attr, |
| 222 | char *buf) |
| 223 | { |
| 224 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 225 | int nr = sensor_attr->index; |
| 226 | struct smsc47m192_data *data = smsc47m192_update_device(dev); |
| 227 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[nr])); |
| 228 | } |
| 229 | |
| 230 | static ssize_t show_temp_min(struct device *dev, struct device_attribute *attr, |
| 231 | char *buf) |
| 232 | { |
| 233 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 234 | int nr = sensor_attr->index; |
| 235 | struct smsc47m192_data *data = smsc47m192_update_device(dev); |
| 236 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_min[nr])); |
| 237 | } |
| 238 | |
| 239 | static ssize_t show_temp_max(struct device *dev, struct device_attribute *attr, |
| 240 | char *buf) |
| 241 | { |
| 242 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 243 | int nr = sensor_attr->index; |
| 244 | struct smsc47m192_data *data = smsc47m192_update_device(dev); |
| 245 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_max[nr])); |
| 246 | } |
| 247 | |
| 248 | static ssize_t set_temp_min(struct device *dev, struct device_attribute *attr, |
| 249 | const char *buf, size_t count) |
| 250 | { |
| 251 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 252 | int nr = sensor_attr->index; |
| 253 | struct i2c_client *client = to_i2c_client(dev); |
| 254 | struct smsc47m192_data *data = i2c_get_clientdata(client); |
| 255 | long val = simple_strtol(buf, NULL, 10); |
| 256 | |
Jean Delvare | e4a7167 | 2007-06-09 10:11:15 -0400 | [diff] [blame] | 257 | mutex_lock(&data->update_lock); |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 258 | data->temp_min[nr] = TEMP_TO_REG(val); |
| 259 | i2c_smbus_write_byte_data(client, SMSC47M192_REG_TEMP_MIN[nr], |
| 260 | data->temp_min[nr]); |
Jean Delvare | e4a7167 | 2007-06-09 10:11:15 -0400 | [diff] [blame] | 261 | mutex_unlock(&data->update_lock); |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 262 | return count; |
| 263 | } |
| 264 | |
| 265 | static ssize_t set_temp_max(struct device *dev, struct device_attribute *attr, |
| 266 | const char *buf, size_t count) |
| 267 | { |
| 268 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 269 | int nr = sensor_attr->index; |
| 270 | struct i2c_client *client = to_i2c_client(dev); |
| 271 | struct smsc47m192_data *data = i2c_get_clientdata(client); |
| 272 | long val = simple_strtol(buf, NULL, 10); |
| 273 | |
Jean Delvare | e4a7167 | 2007-06-09 10:11:15 -0400 | [diff] [blame] | 274 | mutex_lock(&data->update_lock); |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 275 | data->temp_max[nr] = TEMP_TO_REG(val); |
| 276 | i2c_smbus_write_byte_data(client, SMSC47M192_REG_TEMP_MAX[nr], |
| 277 | data->temp_max[nr]); |
Jean Delvare | e4a7167 | 2007-06-09 10:11:15 -0400 | [diff] [blame] | 278 | mutex_unlock(&data->update_lock); |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 279 | return count; |
| 280 | } |
| 281 | |
| 282 | static ssize_t show_temp_offset(struct device *dev, struct device_attribute |
| 283 | *attr, char *buf) |
| 284 | { |
| 285 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 286 | int nr = sensor_attr->index; |
| 287 | struct smsc47m192_data *data = smsc47m192_update_device(dev); |
| 288 | return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp_offset[nr])); |
| 289 | } |
| 290 | |
| 291 | static ssize_t set_temp_offset(struct device *dev, struct device_attribute |
| 292 | *attr, const char *buf, size_t count) |
| 293 | { |
| 294 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 295 | int nr = sensor_attr->index; |
| 296 | struct i2c_client *client = to_i2c_client(dev); |
| 297 | struct smsc47m192_data *data = i2c_get_clientdata(client); |
| 298 | u8 sfr = i2c_smbus_read_byte_data(client, SMSC47M192_REG_SFR); |
| 299 | long val = simple_strtol(buf, NULL, 10); |
| 300 | |
Jean Delvare | e4a7167 | 2007-06-09 10:11:15 -0400 | [diff] [blame] | 301 | mutex_lock(&data->update_lock); |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 302 | data->temp_offset[nr] = TEMP_TO_REG(val); |
| 303 | if (nr>1) |
| 304 | i2c_smbus_write_byte_data(client, |
| 305 | SMSC47M192_REG_TEMP_OFFSET(nr), data->temp_offset[nr]); |
| 306 | else if (data->temp_offset[nr] != 0) { |
| 307 | /* offset[0] and offset[1] share the same register, |
| 308 | SFR bit 4 activates offset[0] */ |
| 309 | i2c_smbus_write_byte_data(client, SMSC47M192_REG_SFR, |
| 310 | (sfr & 0xef) | (nr==0 ? 0x10 : 0)); |
| 311 | data->temp_offset[1-nr] = 0; |
| 312 | i2c_smbus_write_byte_data(client, |
| 313 | SMSC47M192_REG_TEMP_OFFSET(nr), data->temp_offset[nr]); |
| 314 | } else if ((sfr & 0x10) == (nr==0 ? 0x10 : 0)) |
| 315 | i2c_smbus_write_byte_data(client, |
| 316 | SMSC47M192_REG_TEMP_OFFSET(nr), 0); |
Jean Delvare | e4a7167 | 2007-06-09 10:11:15 -0400 | [diff] [blame] | 317 | mutex_unlock(&data->update_lock); |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 318 | return count; |
| 319 | } |
| 320 | |
| 321 | #define show_temp_index(index) \ |
| 322 | static SENSOR_DEVICE_ATTR(temp##index##_input, S_IRUGO, \ |
| 323 | show_temp, NULL, index-1); \ |
| 324 | static SENSOR_DEVICE_ATTR(temp##index##_min, S_IRUGO | S_IWUSR, \ |
| 325 | show_temp_min, set_temp_min, index-1); \ |
| 326 | static SENSOR_DEVICE_ATTR(temp##index##_max, S_IRUGO | S_IWUSR, \ |
| 327 | show_temp_max, set_temp_max, index-1); \ |
| 328 | static SENSOR_DEVICE_ATTR(temp##index##_offset, S_IRUGO | S_IWUSR, \ |
| 329 | show_temp_offset, set_temp_offset, index-1); |
| 330 | |
| 331 | show_temp_index(1) |
| 332 | show_temp_index(2) |
| 333 | show_temp_index(3) |
| 334 | |
| 335 | /* VID */ |
| 336 | static ssize_t show_vid(struct device *dev, struct device_attribute *attr, |
| 337 | char *buf) |
| 338 | { |
| 339 | struct smsc47m192_data *data = smsc47m192_update_device(dev); |
| 340 | return sprintf(buf, "%d\n", vid_from_reg(data->vid, data->vrm)); |
| 341 | } |
| 342 | static DEVICE_ATTR(cpu0_vid, S_IRUGO, show_vid, NULL); |
| 343 | |
| 344 | static ssize_t show_vrm(struct device *dev, struct device_attribute *attr, |
| 345 | char *buf) |
| 346 | { |
Jean Delvare | 90d6619 | 2007-10-08 18:24:35 +0200 | [diff] [blame] | 347 | struct smsc47m192_data *data = dev_get_drvdata(dev); |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 348 | return sprintf(buf, "%d\n", data->vrm); |
| 349 | } |
| 350 | |
| 351 | static ssize_t set_vrm(struct device *dev, struct device_attribute *attr, |
| 352 | const char *buf, size_t count) |
| 353 | { |
Jean Delvare | 8f74efe | 2007-12-01 11:25:33 +0100 | [diff] [blame] | 354 | struct smsc47m192_data *data = dev_get_drvdata(dev); |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 355 | data->vrm = simple_strtoul(buf, NULL, 10); |
| 356 | return count; |
| 357 | } |
| 358 | static DEVICE_ATTR(vrm, S_IRUGO | S_IWUSR, show_vrm, set_vrm); |
| 359 | |
| 360 | /* Alarms */ |
| 361 | static ssize_t show_alarm(struct device *dev, struct device_attribute *attr, |
| 362 | char *buf) |
| 363 | { |
| 364 | struct sensor_device_attribute *sensor_attr = to_sensor_dev_attr(attr); |
| 365 | int nr = sensor_attr->index; |
| 366 | struct smsc47m192_data *data = smsc47m192_update_device(dev); |
| 367 | return sprintf(buf, "%u\n", (data->alarms & nr) ? 1 : 0); |
| 368 | } |
| 369 | |
| 370 | static SENSOR_DEVICE_ATTR(temp1_alarm, S_IRUGO, show_alarm, NULL, 0x0010); |
| 371 | static SENSOR_DEVICE_ATTR(temp2_alarm, S_IRUGO, show_alarm, NULL, 0x0020); |
| 372 | static SENSOR_DEVICE_ATTR(temp3_alarm, S_IRUGO, show_alarm, NULL, 0x0040); |
Jean Delvare | 7817a39 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 373 | static SENSOR_DEVICE_ATTR(temp2_fault, S_IRUGO, show_alarm, NULL, 0x4000); |
| 374 | static SENSOR_DEVICE_ATTR(temp3_fault, S_IRUGO, show_alarm, NULL, 0x8000); |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 375 | static SENSOR_DEVICE_ATTR(in0_alarm, S_IRUGO, show_alarm, NULL, 0x0001); |
| 376 | static SENSOR_DEVICE_ATTR(in1_alarm, S_IRUGO, show_alarm, NULL, 0x0002); |
| 377 | static SENSOR_DEVICE_ATTR(in2_alarm, S_IRUGO, show_alarm, NULL, 0x0004); |
| 378 | static SENSOR_DEVICE_ATTR(in3_alarm, S_IRUGO, show_alarm, NULL, 0x0008); |
| 379 | static SENSOR_DEVICE_ATTR(in4_alarm, S_IRUGO, show_alarm, NULL, 0x0100); |
| 380 | static SENSOR_DEVICE_ATTR(in5_alarm, S_IRUGO, show_alarm, NULL, 0x0200); |
| 381 | static SENSOR_DEVICE_ATTR(in6_alarm, S_IRUGO, show_alarm, NULL, 0x0400); |
| 382 | static SENSOR_DEVICE_ATTR(in7_alarm, S_IRUGO, show_alarm, NULL, 0x0800); |
| 383 | |
Jean Delvare | ce8c6ce1 | 2006-09-24 21:25:12 +0200 | [diff] [blame] | 384 | static struct attribute *smsc47m192_attributes[] = { |
| 385 | &sensor_dev_attr_in0_input.dev_attr.attr, |
| 386 | &sensor_dev_attr_in0_min.dev_attr.attr, |
| 387 | &sensor_dev_attr_in0_max.dev_attr.attr, |
| 388 | &sensor_dev_attr_in0_alarm.dev_attr.attr, |
| 389 | &sensor_dev_attr_in1_input.dev_attr.attr, |
| 390 | &sensor_dev_attr_in1_min.dev_attr.attr, |
| 391 | &sensor_dev_attr_in1_max.dev_attr.attr, |
| 392 | &sensor_dev_attr_in1_alarm.dev_attr.attr, |
| 393 | &sensor_dev_attr_in2_input.dev_attr.attr, |
| 394 | &sensor_dev_attr_in2_min.dev_attr.attr, |
| 395 | &sensor_dev_attr_in2_max.dev_attr.attr, |
| 396 | &sensor_dev_attr_in2_alarm.dev_attr.attr, |
| 397 | &sensor_dev_attr_in3_input.dev_attr.attr, |
| 398 | &sensor_dev_attr_in3_min.dev_attr.attr, |
| 399 | &sensor_dev_attr_in3_max.dev_attr.attr, |
| 400 | &sensor_dev_attr_in3_alarm.dev_attr.attr, |
| 401 | &sensor_dev_attr_in5_input.dev_attr.attr, |
| 402 | &sensor_dev_attr_in5_min.dev_attr.attr, |
| 403 | &sensor_dev_attr_in5_max.dev_attr.attr, |
| 404 | &sensor_dev_attr_in5_alarm.dev_attr.attr, |
| 405 | &sensor_dev_attr_in6_input.dev_attr.attr, |
| 406 | &sensor_dev_attr_in6_min.dev_attr.attr, |
| 407 | &sensor_dev_attr_in6_max.dev_attr.attr, |
| 408 | &sensor_dev_attr_in6_alarm.dev_attr.attr, |
| 409 | &sensor_dev_attr_in7_input.dev_attr.attr, |
| 410 | &sensor_dev_attr_in7_min.dev_attr.attr, |
| 411 | &sensor_dev_attr_in7_max.dev_attr.attr, |
| 412 | &sensor_dev_attr_in7_alarm.dev_attr.attr, |
| 413 | |
| 414 | &sensor_dev_attr_temp1_input.dev_attr.attr, |
| 415 | &sensor_dev_attr_temp1_max.dev_attr.attr, |
| 416 | &sensor_dev_attr_temp1_min.dev_attr.attr, |
| 417 | &sensor_dev_attr_temp1_offset.dev_attr.attr, |
| 418 | &sensor_dev_attr_temp1_alarm.dev_attr.attr, |
| 419 | &sensor_dev_attr_temp2_input.dev_attr.attr, |
| 420 | &sensor_dev_attr_temp2_max.dev_attr.attr, |
| 421 | &sensor_dev_attr_temp2_min.dev_attr.attr, |
| 422 | &sensor_dev_attr_temp2_offset.dev_attr.attr, |
| 423 | &sensor_dev_attr_temp2_alarm.dev_attr.attr, |
Jean Delvare | 7817a39 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 424 | &sensor_dev_attr_temp2_fault.dev_attr.attr, |
Jean Delvare | ce8c6ce1 | 2006-09-24 21:25:12 +0200 | [diff] [blame] | 425 | &sensor_dev_attr_temp3_input.dev_attr.attr, |
| 426 | &sensor_dev_attr_temp3_max.dev_attr.attr, |
| 427 | &sensor_dev_attr_temp3_min.dev_attr.attr, |
| 428 | &sensor_dev_attr_temp3_offset.dev_attr.attr, |
| 429 | &sensor_dev_attr_temp3_alarm.dev_attr.attr, |
Jean Delvare | 7817a39 | 2007-06-09 10:11:16 -0400 | [diff] [blame] | 430 | &sensor_dev_attr_temp3_fault.dev_attr.attr, |
Jean Delvare | ce8c6ce1 | 2006-09-24 21:25:12 +0200 | [diff] [blame] | 431 | |
| 432 | &dev_attr_cpu0_vid.attr, |
| 433 | &dev_attr_vrm.attr, |
| 434 | NULL |
| 435 | }; |
| 436 | |
| 437 | static const struct attribute_group smsc47m192_group = { |
| 438 | .attrs = smsc47m192_attributes, |
| 439 | }; |
| 440 | |
| 441 | static struct attribute *smsc47m192_attributes_in4[] = { |
| 442 | &sensor_dev_attr_in4_input.dev_attr.attr, |
| 443 | &sensor_dev_attr_in4_min.dev_attr.attr, |
| 444 | &sensor_dev_attr_in4_max.dev_attr.attr, |
| 445 | &sensor_dev_attr_in4_alarm.dev_attr.attr, |
| 446 | NULL |
| 447 | }; |
| 448 | |
| 449 | static const struct attribute_group smsc47m192_group_in4 = { |
| 450 | .attrs = smsc47m192_attributes_in4, |
| 451 | }; |
| 452 | |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 453 | static void smsc47m192_init_client(struct i2c_client *client) |
| 454 | { |
| 455 | int i; |
| 456 | u8 config = i2c_smbus_read_byte_data(client, SMSC47M192_REG_CONFIG); |
| 457 | u8 sfr = i2c_smbus_read_byte_data(client, SMSC47M192_REG_SFR); |
| 458 | |
| 459 | /* select cycle mode (pause 1 sec between updates) */ |
| 460 | i2c_smbus_write_byte_data(client, SMSC47M192_REG_SFR, |
| 461 | (sfr & 0xfd) | 0x02); |
| 462 | if (!(config & 0x01)) { |
| 463 | /* initialize alarm limits */ |
| 464 | for (i=0; i<8; i++) { |
| 465 | i2c_smbus_write_byte_data(client, |
| 466 | SMSC47M192_REG_IN_MIN(i), 0); |
| 467 | i2c_smbus_write_byte_data(client, |
| 468 | SMSC47M192_REG_IN_MAX(i), 0xff); |
| 469 | } |
| 470 | for (i=0; i<3; i++) { |
| 471 | i2c_smbus_write_byte_data(client, |
| 472 | SMSC47M192_REG_TEMP_MIN[i], 0x80); |
| 473 | i2c_smbus_write_byte_data(client, |
| 474 | SMSC47M192_REG_TEMP_MAX[i], 0x7f); |
| 475 | } |
| 476 | |
| 477 | /* start monitoring */ |
| 478 | i2c_smbus_write_byte_data(client, SMSC47M192_REG_CONFIG, |
| 479 | (config & 0xf7) | 0x01); |
| 480 | } |
| 481 | } |
| 482 | |
Jean Delvare | 8fb597b | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 483 | /* Return 0 if detection is successful, -ENODEV otherwise */ |
| 484 | static int smsc47m192_detect(struct i2c_client *client, int kind, |
| 485 | struct i2c_board_info *info) |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 486 | { |
Jean Delvare | 8fb597b | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 487 | struct i2c_adapter *adapter = client->adapter; |
| 488 | int version; |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 489 | |
| 490 | if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) |
Jean Delvare | 8fb597b | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 491 | return -ENODEV; |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 492 | |
| 493 | /* Detection criteria from sensors_detect script */ |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 494 | version = i2c_smbus_read_byte_data(client, SMSC47M192_REG_VERSION); |
| 495 | if (i2c_smbus_read_byte_data(client, |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 496 | SMSC47M192_REG_COMPANY_ID) == 0x55 |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 497 | && (version & 0xf0) == 0x20 |
| 498 | && (i2c_smbus_read_byte_data(client, |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 499 | SMSC47M192_REG_VID) & 0x70) == 0x00 |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 500 | && (i2c_smbus_read_byte_data(client, |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 501 | SMSC47M192_REG_VID4) & 0xfe) == 0x80) { |
Jean Delvare | 52df644 | 2009-12-09 20:35:57 +0100 | [diff] [blame] | 502 | dev_info(&adapter->dev, |
| 503 | "found SMSC47M192 or compatible, " |
| 504 | "version 2, stepping A%d\n", version & 0x0f); |
| 505 | } else { |
| 506 | dev_dbg(&adapter->dev, |
| 507 | "SMSC47M192 detection failed at 0x%02x\n", |
| 508 | client->addr); |
| 509 | return -ENODEV; |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 510 | } |
| 511 | |
Jean Delvare | 8fb597b | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 512 | strlcpy(info->type, "smsc47m192", I2C_NAME_SIZE); |
| 513 | |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | static int smsc47m192_probe(struct i2c_client *client, |
| 518 | const struct i2c_device_id *id) |
| 519 | { |
| 520 | struct smsc47m192_data *data; |
| 521 | int config; |
| 522 | int err; |
| 523 | |
| 524 | data = kzalloc(sizeof(struct smsc47m192_data), GFP_KERNEL); |
| 525 | if (!data) { |
| 526 | err = -ENOMEM; |
| 527 | goto exit; |
| 528 | } |
| 529 | |
| 530 | i2c_set_clientdata(client, data); |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 531 | data->vrm = vid_which_vrm(); |
Jean Delvare | e4a7167 | 2007-06-09 10:11:15 -0400 | [diff] [blame] | 532 | mutex_init(&data->update_lock); |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 533 | |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 534 | /* Initialize the SMSC47M192 chip */ |
| 535 | smsc47m192_init_client(client); |
| 536 | |
| 537 | /* Register sysfs hooks */ |
Jean Delvare | ce8c6ce1 | 2006-09-24 21:25:12 +0200 | [diff] [blame] | 538 | if ((err = sysfs_create_group(&client->dev.kobj, &smsc47m192_group))) |
Jean Delvare | 8fb597b | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 539 | goto exit_free; |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 540 | |
| 541 | /* Pin 110 is either in4 (+12V) or VID4 */ |
| 542 | config = i2c_smbus_read_byte_data(client, SMSC47M192_REG_CONFIG); |
| 543 | if (!(config & 0x20)) { |
Jean Delvare | ce8c6ce1 | 2006-09-24 21:25:12 +0200 | [diff] [blame] | 544 | if ((err = sysfs_create_group(&client->dev.kobj, |
| 545 | &smsc47m192_group_in4))) |
| 546 | goto exit_remove_files; |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 547 | } |
Jean Delvare | ce8c6ce1 | 2006-09-24 21:25:12 +0200 | [diff] [blame] | 548 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 549 | data->hwmon_dev = hwmon_device_register(&client->dev); |
| 550 | if (IS_ERR(data->hwmon_dev)) { |
| 551 | err = PTR_ERR(data->hwmon_dev); |
Jean Delvare | ce8c6ce1 | 2006-09-24 21:25:12 +0200 | [diff] [blame] | 552 | goto exit_remove_files; |
| 553 | } |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 554 | |
| 555 | return 0; |
| 556 | |
Jean Delvare | ce8c6ce1 | 2006-09-24 21:25:12 +0200 | [diff] [blame] | 557 | exit_remove_files: |
| 558 | sysfs_remove_group(&client->dev.kobj, &smsc47m192_group); |
| 559 | sysfs_remove_group(&client->dev.kobj, &smsc47m192_group_in4); |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 560 | exit_free: |
| 561 | kfree(data); |
| 562 | exit: |
| 563 | return err; |
| 564 | } |
| 565 | |
Jean Delvare | 8fb597b | 2008-07-16 19:30:16 +0200 | [diff] [blame] | 566 | static int smsc47m192_remove(struct i2c_client *client) |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 567 | { |
| 568 | struct smsc47m192_data *data = i2c_get_clientdata(client); |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 569 | |
Tony Jones | 1beeffe | 2007-08-20 13:46:20 -0700 | [diff] [blame] | 570 | hwmon_device_unregister(data->hwmon_dev); |
Jean Delvare | ce8c6ce1 | 2006-09-24 21:25:12 +0200 | [diff] [blame] | 571 | sysfs_remove_group(&client->dev.kobj, &smsc47m192_group); |
| 572 | sysfs_remove_group(&client->dev.kobj, &smsc47m192_group_in4); |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 573 | |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 574 | kfree(data); |
| 575 | |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | static struct smsc47m192_data *smsc47m192_update_device(struct device *dev) |
| 580 | { |
| 581 | struct i2c_client *client = to_i2c_client(dev); |
| 582 | struct smsc47m192_data *data = i2c_get_clientdata(client); |
| 583 | int i, config; |
| 584 | |
Jean Delvare | e4a7167 | 2007-06-09 10:11:15 -0400 | [diff] [blame] | 585 | mutex_lock(&data->update_lock); |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 586 | |
| 587 | if (time_after(jiffies, data->last_updated + HZ + HZ / 2) |
| 588 | || !data->valid) { |
| 589 | u8 sfr = i2c_smbus_read_byte_data(client, SMSC47M192_REG_SFR); |
| 590 | |
| 591 | dev_dbg(&client->dev, "Starting smsc47m192 update\n"); |
| 592 | |
| 593 | for (i = 0; i <= 7; i++) { |
| 594 | data->in[i] = i2c_smbus_read_byte_data(client, |
| 595 | SMSC47M192_REG_IN(i)); |
| 596 | data->in_min[i] = i2c_smbus_read_byte_data(client, |
| 597 | SMSC47M192_REG_IN_MIN(i)); |
| 598 | data->in_max[i] = i2c_smbus_read_byte_data(client, |
| 599 | SMSC47M192_REG_IN_MAX(i)); |
| 600 | } |
| 601 | for (i = 0; i < 3; i++) { |
| 602 | data->temp[i] = i2c_smbus_read_byte_data(client, |
| 603 | SMSC47M192_REG_TEMP[i]); |
| 604 | data->temp_max[i] = i2c_smbus_read_byte_data(client, |
| 605 | SMSC47M192_REG_TEMP_MAX[i]); |
| 606 | data->temp_min[i] = i2c_smbus_read_byte_data(client, |
| 607 | SMSC47M192_REG_TEMP_MIN[i]); |
| 608 | } |
| 609 | for (i = 1; i < 3; i++) |
| 610 | data->temp_offset[i] = i2c_smbus_read_byte_data(client, |
| 611 | SMSC47M192_REG_TEMP_OFFSET(i)); |
| 612 | /* first offset is temp_offset[0] if SFR bit 4 is set, |
| 613 | temp_offset[1] otherwise */ |
| 614 | if (sfr & 0x10) { |
| 615 | data->temp_offset[0] = data->temp_offset[1]; |
| 616 | data->temp_offset[1] = 0; |
| 617 | } else |
| 618 | data->temp_offset[0] = 0; |
| 619 | |
| 620 | data->vid = i2c_smbus_read_byte_data(client, SMSC47M192_REG_VID) |
| 621 | & 0x0f; |
| 622 | config = i2c_smbus_read_byte_data(client, |
| 623 | SMSC47M192_REG_CONFIG); |
| 624 | if (config & 0x20) |
| 625 | data->vid |= (i2c_smbus_read_byte_data(client, |
| 626 | SMSC47M192_REG_VID4) & 0x01) << 4; |
| 627 | data->alarms = i2c_smbus_read_byte_data(client, |
| 628 | SMSC47M192_REG_ALARM1) | |
| 629 | (i2c_smbus_read_byte_data(client, |
| 630 | SMSC47M192_REG_ALARM2) << 8); |
| 631 | |
| 632 | data->last_updated = jiffies; |
| 633 | data->valid = 1; |
| 634 | } |
| 635 | |
Jean Delvare | e4a7167 | 2007-06-09 10:11:15 -0400 | [diff] [blame] | 636 | mutex_unlock(&data->update_lock); |
Hartmut Rick | 59ac836 | 2006-03-23 16:37:23 +0100 | [diff] [blame] | 637 | |
| 638 | return data; |
| 639 | } |
| 640 | |
| 641 | static int __init smsc47m192_init(void) |
| 642 | { |
| 643 | return i2c_add_driver(&smsc47m192_driver); |
| 644 | } |
| 645 | |
| 646 | static void __exit smsc47m192_exit(void) |
| 647 | { |
| 648 | i2c_del_driver(&smsc47m192_driver); |
| 649 | } |
| 650 | |
| 651 | MODULE_AUTHOR("Hartmut Rick <linux@rick.claranet.de>"); |
| 652 | MODULE_DESCRIPTION("SMSC47M192 driver"); |
| 653 | MODULE_LICENSE("GPL"); |
| 654 | |
| 655 | module_init(smsc47m192_init); |
| 656 | module_exit(smsc47m192_exit); |