1 /* 2 * adm1029.c - Part of lm_sensors, Linux kernel modules for hardware monitoring 3 * 4 * Copyright (C) 2006 Corentin LABBE <corentin.labbe@geomatys.fr> 5 * 6 * Based on LM83 Driver by Jean Delvare <khali@linux-fr.org> 7 * 8 * Give only processor, motherboard temperatures and fan tachs 9 * Very rare chip please let me know if you use it 10 * 11 * http://www.analog.com/UploadedFiles/Data_Sheets/ADM1029.pdf 12 * 13 * 14 * This program is free software; you can redistribute it and/or modify 15 * it under the terms of the GNU General Public License as published by 16 * the Free Software Foundation version 2 of the License 17 * 18 * This program is distributed in the hope that it will be useful, 19 * but WITHOUT ANY WARRANTY; without even the implied warranty of 20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21 * GNU General Public License for more details. 22 * 23 * You should have received a copy of the GNU General Public License 24 * along with this program; if not, write to the Free Software 25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 26 */ 27 28 #include <linux/module.h> 29 #include <linux/init.h> 30 #include <linux/slab.h> 31 #include <linux/jiffies.h> 32 #include <linux/i2c.h> 33 #include <linux/hwmon-sysfs.h> 34 #include <linux/hwmon.h> 35 #include <linux/err.h> 36 #include <linux/mutex.h> 37 38 /* 39 * Addresses to scan 40 */ 41 42 static const unsigned short normal_i2c[] = { 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 43 0x2e, 0x2f, I2C_CLIENT_END 44 }; 45 46 /* 47 * Insmod parameters 48 */ 49 50 I2C_CLIENT_INSMOD_1(adm1029); 51 52 /* 53 * The ADM1029 registers 54 * Manufacturer ID is 0x41 for Analog Devices 55 */ 56 57 #define ADM1029_REG_MAN_ID 0x0D 58 #define ADM1029_REG_CHIP_ID 0x0E 59 #define ADM1029_REG_CONFIG 0x01 60 #define ADM1029_REG_NB_FAN_SUPPORT 0x02 61 62 #define ADM1029_REG_TEMP_DEVICES_INSTALLED 0x06 63 64 #define ADM1029_REG_LOCAL_TEMP 0xA0 65 #define ADM1029_REG_REMOTE1_TEMP 0xA1 66 #define ADM1029_REG_REMOTE2_TEMP 0xA2 67 68 #define ADM1029_REG_LOCAL_TEMP_HIGH 0x90 69 #define ADM1029_REG_REMOTE1_TEMP_HIGH 0x91 70 #define ADM1029_REG_REMOTE2_TEMP_HIGH 0x92 71 72 #define ADM1029_REG_LOCAL_TEMP_LOW 0x98 73 #define ADM1029_REG_REMOTE1_TEMP_LOW 0x99 74 #define ADM1029_REG_REMOTE2_TEMP_LOW 0x9A 75 76 #define ADM1029_REG_FAN1 0x70 77 #define ADM1029_REG_FAN2 0x71 78 79 #define ADM1029_REG_FAN1_MIN 0x78 80 #define ADM1029_REG_FAN2_MIN 0x79 81 82 #define ADM1029_REG_FAN1_CONFIG 0x68 83 #define ADM1029_REG_FAN2_CONFIG 0x69 84 85 #define TEMP_FROM_REG(val) ((val) * 1000) 86 87 #define DIV_FROM_REG(val) ( 1 << (((val) >> 6) - 1)) 88 89 /* Registers to be checked by adm1029_update_device() */ 90 static const u8 ADM1029_REG_TEMP[] = { 91 ADM1029_REG_LOCAL_TEMP, 92 ADM1029_REG_REMOTE1_TEMP, 93 ADM1029_REG_REMOTE2_TEMP, 94 ADM1029_REG_LOCAL_TEMP_HIGH, 95 ADM1029_REG_REMOTE1_TEMP_HIGH, 96 ADM1029_REG_REMOTE2_TEMP_HIGH, 97 ADM1029_REG_LOCAL_TEMP_LOW, 98 ADM1029_REG_REMOTE1_TEMP_LOW, 99 ADM1029_REG_REMOTE2_TEMP_LOW, 100 }; 101 102 static const u8 ADM1029_REG_FAN[] = { 103 ADM1029_REG_FAN1, 104 ADM1029_REG_FAN2, 105 ADM1029_REG_FAN1_MIN, 106 ADM1029_REG_FAN2_MIN, 107 }; 108 109 static const u8 ADM1029_REG_FAN_DIV[] = { 110 ADM1029_REG_FAN1_CONFIG, 111 ADM1029_REG_FAN2_CONFIG, 112 }; 113 114 /* 115 * Functions declaration 116 */ 117 118 static int adm1029_probe(struct i2c_client *client, 119 const struct i2c_device_id *id); 120 static int adm1029_detect(struct i2c_client *client, int kind, 121 struct i2c_board_info *info); 122 static int adm1029_remove(struct i2c_client *client); 123 static struct adm1029_data *adm1029_update_device(struct device *dev); 124 static int adm1029_init_client(struct i2c_client *client); 125 126 /* 127 * Driver data (common to all clients) 128 */ 129 130 static const struct i2c_device_id adm1029_id[] = { 131 { "adm1029", adm1029 }, 132 { } 133 }; 134 MODULE_DEVICE_TABLE(i2c, adm1029_id); 135 136 static struct i2c_driver adm1029_driver = { 137 .class = I2C_CLASS_HWMON, 138 .driver = { 139 .name = "adm1029", 140 }, 141 .probe = adm1029_probe, 142 .remove = adm1029_remove, 143 .id_table = adm1029_id, 144 .detect = adm1029_detect, 145 .address_data = &addr_data, 146 }; 147 148 /* 149 * Client data (each client gets its own) 150 */ 151 152 struct adm1029_data { 153 struct device *hwmon_dev; 154 struct mutex update_lock; 155 char valid; /* zero until following fields are valid */ 156 unsigned long last_updated; /* in jiffies */ 157 158 /* registers values, signed for temperature, unsigned for other stuff */ 159 s8 temp[ARRAY_SIZE(ADM1029_REG_TEMP)]; 160 u8 fan[ARRAY_SIZE(ADM1029_REG_FAN)]; 161 u8 fan_div[ARRAY_SIZE(ADM1029_REG_FAN_DIV)]; 162 }; 163 164 /* 165 * Sysfs stuff 166 */ 167 168 static ssize_t 169 show_temp(struct device *dev, struct device_attribute *devattr, char *buf) 170 { 171 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 172 struct adm1029_data *data = adm1029_update_device(dev); 173 return sprintf(buf, "%d\n", TEMP_FROM_REG(data->temp[attr->index])); 174 } 175 176 static ssize_t 177 show_fan(struct device *dev, struct device_attribute *devattr, char *buf) 178 { 179 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 180 struct adm1029_data *data = adm1029_update_device(dev); 181 u16 val; 182 if (data->fan[attr->index] == 0 || data->fan_div[attr->index] == 0 183 || data->fan[attr->index] == 255) { 184 return sprintf(buf, "0\n"); 185 } 186 187 val = 1880 * 120 / DIV_FROM_REG(data->fan_div[attr->index]) 188 / data->fan[attr->index]; 189 return sprintf(buf, "%d\n", val); 190 } 191 192 static ssize_t 193 show_fan_div(struct device *dev, struct device_attribute *devattr, char *buf) 194 { 195 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 196 struct adm1029_data *data = adm1029_update_device(dev); 197 if (data->fan_div[attr->index] == 0) 198 return sprintf(buf, "0\n"); 199 return sprintf(buf, "%d\n", DIV_FROM_REG(data->fan_div[attr->index])); 200 } 201 202 static ssize_t set_fan_div(struct device *dev, 203 struct device_attribute *devattr, const char *buf, size_t count) 204 { 205 struct i2c_client *client = to_i2c_client(dev); 206 struct adm1029_data *data = i2c_get_clientdata(client); 207 struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); 208 long val = simple_strtol(buf, NULL, 10); 209 u8 reg; 210 211 mutex_lock(&data->update_lock); 212 213 /*Read actual config */ 214 reg = i2c_smbus_read_byte_data(client, 215 ADM1029_REG_FAN_DIV[attr->index]); 216 217 switch (val) { 218 case 1: 219 val = 1; 220 break; 221 case 2: 222 val = 2; 223 break; 224 case 4: 225 val = 3; 226 break; 227 default: 228 mutex_unlock(&data->update_lock); 229 dev_err(&client->dev, "fan_div value %ld not " 230 "supported. Choose one of 1, 2 or 4!\n", val); 231 return -EINVAL; 232 } 233 /* Update the value */ 234 reg = (reg & 0x3F) | (val << 6); 235 236 /* Write value */ 237 i2c_smbus_write_byte_data(client, 238 ADM1029_REG_FAN_DIV[attr->index], reg); 239 mutex_unlock(&data->update_lock); 240 241 return count; 242 } 243 244 /* 245 Access rights on sysfs, S_IRUGO stand for Is Readable by User, Group and Others 246 S_IWUSR stand for Is Writable by User 247 */ 248 static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, show_temp, NULL, 0); 249 static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, show_temp, NULL, 1); 250 static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, show_temp, NULL, 2); 251 252 static SENSOR_DEVICE_ATTR(temp1_max, S_IRUGO, show_temp, NULL, 3); 253 static SENSOR_DEVICE_ATTR(temp2_max, S_IRUGO, show_temp, NULL, 4); 254 static SENSOR_DEVICE_ATTR(temp3_max, S_IRUGO, show_temp, NULL, 5); 255 256 static SENSOR_DEVICE_ATTR(temp1_min, S_IRUGO, show_temp, NULL, 6); 257 static SENSOR_DEVICE_ATTR(temp2_min, S_IRUGO, show_temp, NULL, 7); 258 static SENSOR_DEVICE_ATTR(temp3_min, S_IRUGO, show_temp, NULL, 8); 259 260 static SENSOR_DEVICE_ATTR(fan1_input, S_IRUGO, show_fan, NULL, 0); 261 static SENSOR_DEVICE_ATTR(fan2_input, S_IRUGO, show_fan, NULL, 1); 262 263 static SENSOR_DEVICE_ATTR(fan1_min, S_IRUGO, show_fan, NULL, 2); 264 static SENSOR_DEVICE_ATTR(fan2_min, S_IRUGO, show_fan, NULL, 3); 265 266 static SENSOR_DEVICE_ATTR(fan1_div, S_IRUGO | S_IWUSR, 267 show_fan_div, set_fan_div, 0); 268 static SENSOR_DEVICE_ATTR(fan2_div, S_IRUGO | S_IWUSR, 269 show_fan_div, set_fan_div, 1); 270 271 static struct attribute *adm1029_attributes[] = { 272 &sensor_dev_attr_temp1_input.dev_attr.attr, 273 &sensor_dev_attr_temp1_min.dev_attr.attr, 274 &sensor_dev_attr_temp1_max.dev_attr.attr, 275 &sensor_dev_attr_temp2_input.dev_attr.attr, 276 &sensor_dev_attr_temp2_min.dev_attr.attr, 277 &sensor_dev_attr_temp2_max.dev_attr.attr, 278 &sensor_dev_attr_temp3_input.dev_attr.attr, 279 &sensor_dev_attr_temp3_min.dev_attr.attr, 280 &sensor_dev_attr_temp3_max.dev_attr.attr, 281 &sensor_dev_attr_fan1_input.dev_attr.attr, 282 &sensor_dev_attr_fan2_input.dev_attr.attr, 283 &sensor_dev_attr_fan1_min.dev_attr.attr, 284 &sensor_dev_attr_fan2_min.dev_attr.attr, 285 &sensor_dev_attr_fan1_div.dev_attr.attr, 286 &sensor_dev_attr_fan2_div.dev_attr.attr, 287 NULL 288 }; 289 290 static const struct attribute_group adm1029_group = { 291 .attrs = adm1029_attributes, 292 }; 293 294 /* 295 * Real code 296 */ 297 298 /* Return 0 if detection is successful, -ENODEV otherwise */ 299 static int adm1029_detect(struct i2c_client *client, int kind, 300 struct i2c_board_info *info) 301 { 302 struct i2c_adapter *adapter = client->adapter; 303 304 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 305 return -ENODEV; 306 307 /* Now we do the detection and identification. A negative kind 308 * means that the driver was loaded with no force parameter 309 * (default), so we must both detect and identify the chip 310 * (actually there is only one possible kind of chip for now, adm1029). 311 * A zero kind means that the driver was loaded with the force 312 * parameter, the detection step shall be skipped. A positive kind 313 * means that the driver was loaded with the force parameter and a 314 * given kind of chip is requested, so both the detection and the 315 * identification steps are skipped. */ 316 317 /* Default to an adm1029 if forced */ 318 if (kind == 0) 319 kind = adm1029; 320 321 /* ADM1029 doesn't have CHIP ID, check just MAN ID 322 * For better detection we check also ADM1029_TEMP_DEVICES_INSTALLED, 323 * ADM1029_REG_NB_FAN_SUPPORT and compare it with possible values 324 * documented 325 */ 326 327 if (kind <= 0) { /* identification */ 328 u8 man_id, chip_id, temp_devices_installed, nb_fan_support; 329 330 man_id = i2c_smbus_read_byte_data(client, ADM1029_REG_MAN_ID); 331 chip_id = i2c_smbus_read_byte_data(client, ADM1029_REG_CHIP_ID); 332 temp_devices_installed = i2c_smbus_read_byte_data(client, 333 ADM1029_REG_TEMP_DEVICES_INSTALLED); 334 nb_fan_support = i2c_smbus_read_byte_data(client, 335 ADM1029_REG_NB_FAN_SUPPORT); 336 /* 0x41 is Analog Devices */ 337 if (man_id == 0x41 && (temp_devices_installed & 0xf9) == 0x01 338 && nb_fan_support == 0x03) { 339 if ((chip_id & 0xF0) == 0x00) { 340 kind = adm1029; 341 } else { 342 /* There are no "official" CHIP ID, so actually 343 * we use Major/Minor revision for that */ 344 printk(KERN_INFO 345 "adm1029: Unknown major revision %x, " 346 "please let us know\n", chip_id); 347 } 348 } 349 350 if (kind <= 0) { /* identification failed */ 351 pr_debug("adm1029: Unsupported chip (man_id=0x%02X, " 352 "chip_id=0x%02X)\n", man_id, chip_id); 353 return -ENODEV; 354 } 355 } 356 strlcpy(info->type, "adm1029", I2C_NAME_SIZE); 357 358 return 0; 359 } 360 361 static int adm1029_probe(struct i2c_client *client, 362 const struct i2c_device_id *id) 363 { 364 struct adm1029_data *data; 365 int err; 366 367 data = kzalloc(sizeof(struct adm1029_data), GFP_KERNEL); 368 if (!data) { 369 err = -ENOMEM; 370 goto exit; 371 } 372 373 i2c_set_clientdata(client, data); 374 mutex_init(&data->update_lock); 375 376 /* 377 * Initialize the ADM1029 chip 378 * Check config register 379 */ 380 if (adm1029_init_client(client) == 0) { 381 err = -ENODEV; 382 goto exit_free; 383 } 384 385 /* Register sysfs hooks */ 386 if ((err = sysfs_create_group(&client->dev.kobj, &adm1029_group))) 387 goto exit_free; 388 389 data->hwmon_dev = hwmon_device_register(&client->dev); 390 if (IS_ERR(data->hwmon_dev)) { 391 err = PTR_ERR(data->hwmon_dev); 392 goto exit_remove_files; 393 } 394 395 return 0; 396 397 exit_remove_files: 398 sysfs_remove_group(&client->dev.kobj, &adm1029_group); 399 exit_free: 400 kfree(data); 401 exit: 402 return err; 403 } 404 405 static int adm1029_init_client(struct i2c_client *client) 406 { 407 u8 config; 408 config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG); 409 if ((config & 0x10) == 0) { 410 i2c_smbus_write_byte_data(client, ADM1029_REG_CONFIG, 411 config | 0x10); 412 } 413 /* recheck config */ 414 config = i2c_smbus_read_byte_data(client, ADM1029_REG_CONFIG); 415 if ((config & 0x10) == 0) { 416 dev_err(&client->dev, "Initialization failed!\n"); 417 return 0; 418 } 419 return 1; 420 } 421 422 static int adm1029_remove(struct i2c_client *client) 423 { 424 struct adm1029_data *data = i2c_get_clientdata(client); 425 426 hwmon_device_unregister(data->hwmon_dev); 427 sysfs_remove_group(&client->dev.kobj, &adm1029_group); 428 429 kfree(data); 430 return 0; 431 } 432 433 /* 434 function that update the status of the chips (temperature for exemple) 435 */ 436 static struct adm1029_data *adm1029_update_device(struct device *dev) 437 { 438 struct i2c_client *client = to_i2c_client(dev); 439 struct adm1029_data *data = i2c_get_clientdata(client); 440 441 mutex_lock(&data->update_lock); 442 /* 443 * Use the "cache" Luke, don't recheck values 444 * if there are already checked not a long time later 445 */ 446 if (time_after(jiffies, data->last_updated + HZ * 2) 447 || !data->valid) { 448 int nr; 449 450 dev_dbg(&client->dev, "Updating adm1029 data\n"); 451 452 for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_TEMP); nr++) { 453 data->temp[nr] = 454 i2c_smbus_read_byte_data(client, 455 ADM1029_REG_TEMP[nr]); 456 } 457 for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN); nr++) { 458 data->fan[nr] = 459 i2c_smbus_read_byte_data(client, 460 ADM1029_REG_FAN[nr]); 461 } 462 for (nr = 0; nr < ARRAY_SIZE(ADM1029_REG_FAN_DIV); nr++) { 463 data->fan_div[nr] = 464 i2c_smbus_read_byte_data(client, 465 ADM1029_REG_FAN_DIV[nr]); 466 } 467 468 data->last_updated = jiffies; 469 data->valid = 1; 470 } 471 472 mutex_unlock(&data->update_lock); 473 474 return data; 475 } 476 477 /* 478 Common module stuff 479 */ 480 static int __init sensors_adm1029_init(void) 481 { 482 483 return i2c_add_driver(&adm1029_driver); 484 } 485 486 static void __exit sensors_adm1029_exit(void) 487 { 488 489 i2c_del_driver(&adm1029_driver); 490 } 491 492 MODULE_AUTHOR("Corentin LABBE <corentin.labbe@geomatys.fr>"); 493 MODULE_DESCRIPTION("adm1029 driver"); 494 MODULE_LICENSE("GPL v2"); 495 496 module_init(sensors_adm1029_init); 497 module_exit(sensors_adm1029_exit); 498