1 /* 2 adm1021.c - Part of lm_sensors, Linux kernel modules for hardware 3 monitoring 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 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> 27 #include <linux/hwmon.h> 28 #include <linux/hwmon-sysfs.h> 29 #include <linux/err.h> 30 #include <linux/mutex.h> 31 32 33 /* Addresses to scan */ 34 static const unsigned short normal_i2c[] = { 35 0x18, 0x19, 0x1a, 0x29, 0x2a, 0x2b, 0x4c, 0x4d, 0x4e, I2C_CLIENT_END }; 36 37 /* Insmod parameters */ 38 I2C_CLIENT_INSMOD_8(adm1021, adm1023, max1617, max1617a, thmc10, lm84, gl523sm, 39 mc1066); 40 41 /* adm1021 constants specified below */ 42 43 /* The adm1021 registers */ 44 /* Read-only */ 45 /* For nr in 0-1 */ 46 #define ADM1021_REG_TEMP(nr) (nr) 47 #define ADM1021_REG_STATUS 0x02 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 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 */ 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 63 /* limits */ 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)) 69 /* write-only */ 70 #define ADM1021_REG_ONESHOT 0x0F 71 72 /* Initial values */ 73 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 77 clearing it. Weird, ey? --Phil */ 78 79 /* Each client has this additional data */ 80 struct adm1021_data { 81 struct device *hwmon_dev; 82 enum chips type; 83 84 struct mutex update_lock; 85 char valid; /* !=0 if following fields are valid */ 86 unsigned long last_updated; /* In jiffies */ 87 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; 98 }; 99 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); 104 static void adm1021_init_client(struct i2c_client *client); 105 static int adm1021_remove(struct i2c_client *client); 106 static struct adm1021_data *adm1021_update_device(struct device *dev); 107 108 /* (amalysh) read only mode, otherwise any limit's writing confuse BIOS */ 109 static int read_only; 110 111 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 125 /* This is the driver that will be inserted */ 126 static struct i2c_driver adm1021_driver = { 127 .class = I2C_CLASS_HWMON, 128 .driver = { 129 .name = "adm1021", 130 }, 131 .probe = adm1021_probe, 132 .remove = adm1021_remove, 133 .id_table = adm1021_id, 134 .detect = adm1021_detect, 135 .address_data = &addr_data, 136 }; 137 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]); 145 } 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 } 164 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 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); 179 } 180 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; 198 } 199 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); 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 235 static DEVICE_ATTR(alarms, S_IRUGO, show_alarms, NULL); 236 237 static struct attribute *adm1021_attributes[] = { 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, 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, 249 &dev_attr_alarms.attr, 250 NULL 251 }; 252 253 static const struct attribute_group adm1021_group = { 254 .attrs = adm1021_attributes, 255 }; 256 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) 260 { 261 struct i2c_adapter *adapter = client->adapter; 262 int i; 263 const char *type_name = ""; 264 int conv_rate, status, config; 265 266 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) { 267 pr_debug("adm1021: detect failed, " 268 "smbus byte data not supported!\n"); 269 return -ENODEV; 270 } 271 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); 276 277 /* Now, we do the remaining detection. */ 278 if (kind < 0) { 279 if ((status & 0x03) != 0x00 || (config & 0x3F) != 0x00 280 || (conv_rate & 0xF8) != 0x00) { 281 pr_debug("adm1021: detect failed, " 282 "chip not detected!\n"); 283 return -ENODEV; 284 } 285 } 286 287 /* Determine the chip type. */ 288 if (kind <= 0) { 289 i = i2c_smbus_read_byte_data(client, ADM1021_REG_MAN_ID); 290 if (i == 0x41) 291 if ((i2c_smbus_read_byte_data(client, 292 ADM1021_REG_DEV_ID) & 0xF0) == 0x30) 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) && 301 (i2c_smbus_read_byte_data(client, 302 ADM1021_REG_DEV_ID) == 0x01)) 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 */ 307 else if (conv_rate == 0x00 308 && (kind == 0 /* skip extra detection */ 309 || ((config & 0x7F) == 0x00 310 && (status & 0xAB) == 0x00))) 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 } 333 pr_debug("adm1021: Detected chip %s at adapter %d, address 0x%02x.\n", 334 type_name, i2c_adapter_id(adapter), client->addr); 335 strlcpy(info->type, type_name, I2C_NAME_SIZE); 336 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; 355 mutex_init(&data->update_lock); 356 357 /* Initialize the ADM1021 chip */ 358 if (data->type != lm84 && !read_only) 359 adm1021_init_client(client); 360 361 /* Register sysfs hooks */ 362 if ((err = sysfs_create_group(&client->dev.kobj, &adm1021_group))) 363 goto error1; 364 365 data->hwmon_dev = hwmon_device_register(&client->dev); 366 if (IS_ERR(data->hwmon_dev)) { 367 err = PTR_ERR(data->hwmon_dev); 368 goto error3; 369 } 370 371 return 0; 372 373 error3: 374 sysfs_remove_group(&client->dev.kobj, &adm1021_group); 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 */ 384 i2c_smbus_write_byte_data(client, ADM1021_REG_CONFIG_W, 385 i2c_smbus_read_byte_data(client, ADM1021_REG_CONFIG_R) & 0xBF); 386 /* Set Conversion rate to 1/sec (this can be tinkered with) */ 387 i2c_smbus_write_byte_data(client, ADM1021_REG_CONV_RATE_W, 0x04); 388 } 389 390 static int adm1021_remove(struct i2c_client *client) 391 { 392 struct adm1021_data *data = i2c_get_clientdata(client); 393 394 hwmon_device_unregister(data->hwmon_dev); 395 sysfs_remove_group(&client->dev.kobj, &adm1021_group); 396 397 kfree(data); 398 return 0; 399 } 400 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 406 mutex_lock(&data->update_lock); 407 408 if (time_after(jiffies, data->last_updated + HZ + HZ / 2) 409 || !data->valid) { 410 int i; 411 412 dev_dbg(&client->dev, "Starting adm1021 update\n"); 413 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 } 422 data->alarms = i2c_smbus_read_byte_data(client, 423 ADM1021_REG_STATUS) & 0x7c; 424 if (data->type == adm1023) { 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); 440 } 441 data->last_updated = jiffies; 442 data->valid = 1; 443 } 444 445 mutex_unlock(&data->update_lock); 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) 470