Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

hwmon: (tmp401) Add support for TI's TMP411 sensors chip

This adds support for TI's TMP411 sensor chip.

Preliminary support were done by Gabriel Konat, Sander Leget and
Wouter Willems. The chip is compatible with TI's TMP401 sensor
chip. It has additional support for historical minimun/maximum
measurements.

Signed-off-by: Andre Prendel <andre.prendel@gmx.de>
Acked-by: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Jean Delvare <khali@linux-fr.org>

authored by

Andre Prendel and committed by
Jean Delvare
fce0758f ab2b79d5

+149 -24
+149 -24
drivers/hwmon/tmp401.c
··· 1 1 /* tmp401.c 2 2 * 3 3 * Copyright (C) 2007,2008 Hans de Goede <hdegoede@redhat.com> 4 + * Preliminary tmp411 support by: 5 + * Gabriel Konat, Sander Leget, Wouter Willems 6 + * Copyright (C) 2009 Andre Prendel <andre.prendel@gmx.de> 4 7 * 5 8 * This program is free software; you can redistribute it and/or modify 6 9 * it under the terms of the GNU General Public License as published by ··· 43 40 static const unsigned short normal_i2c[] = { 0x4c, I2C_CLIENT_END }; 44 41 45 42 /* Insmod parameters */ 46 - I2C_CLIENT_INSMOD_1(tmp401); 47 - 43 + I2C_CLIENT_INSMOD_2(tmp401, tmp411); 48 44 49 45 /* 50 46 * The TMP401 registers, note some registers have different addresses for ··· 58 56 #define TMP401_CONSECUTIVE_ALERT 0x22 59 57 #define TMP401_MANUFACTURER_ID_REG 0xFE 60 58 #define TMP401_DEVICE_ID_REG 0xFF 59 + #define TMP411_N_FACTOR_REG 0x18 61 60 62 61 static const u8 TMP401_TEMP_MSB[2] = { 0x00, 0x01 }; 63 62 static const u8 TMP401_TEMP_LSB[2] = { 0x15, 0x10 }; ··· 70 67 static const u8 TMP401_TEMP_HIGH_LIMIT_LSB[2] = { 0x16, 0x13 }; 71 68 /* These are called the THERM limit / hysteresis / mask in the datasheet */ 72 69 static const u8 TMP401_TEMP_CRIT_LIMIT[2] = { 0x20, 0x19 }; 70 + 71 + static const u8 TMP411_TEMP_LOWEST_MSB[2] = { 0x30, 0x34 }; 72 + static const u8 TMP411_TEMP_LOWEST_LSB[2] = { 0x31, 0x35 }; 73 + static const u8 TMP411_TEMP_HIGHEST_MSB[2] = { 0x32, 0x36 }; 74 + static const u8 TMP411_TEMP_HIGHEST_LSB[2] = { 0x33, 0x37 }; 73 75 74 76 /* Flags */ 75 77 #define TMP401_CONFIG_RANGE 0x04 ··· 90 82 /* Manufacturer / Device ID's */ 91 83 #define TMP401_MANUFACTURER_ID 0x55 92 84 #define TMP401_DEVICE_ID 0x11 85 + #define TMP411_DEVICE_ID 0x12 93 86 94 87 /* 95 88 * Functions declarations ··· 109 100 110 101 static const struct i2c_device_id tmp401_id[] = { 111 102 { "tmp401", tmp401 }, 103 + { "tmp411", tmp411 }, 112 104 { } 113 105 }; 114 106 MODULE_DEVICE_TABLE(i2c, tmp401_id); ··· 135 125 struct mutex update_lock; 136 126 char valid; /* zero until following fields are valid */ 137 127 unsigned long last_updated; /* in jiffies */ 128 + int kind; 138 129 139 130 /* register values */ 140 131 u8 status; ··· 145 134 u16 temp_high[2]; 146 135 u8 temp_crit[2]; 147 136 u8 temp_crit_hyst; 137 + u16 temp_lowest[2]; 138 + u16 temp_highest[2]; 148 139 }; 149 140 150 141 /* ··· 249 236 mutex_unlock(&data->update_lock); 250 237 251 238 return sprintf(buf, "%d\n", temp); 239 + } 240 + 241 + static ssize_t show_temp_lowest(struct device *dev, 242 + struct device_attribute *devattr, char *buf) 243 + { 244 + int index = to_sensor_dev_attr(devattr)->index; 245 + struct tmp401_data *data = tmp401_update_device(dev); 246 + 247 + return sprintf(buf, "%d\n", 248 + tmp401_register_to_temp(data->temp_lowest[index], 249 + data->config)); 250 + } 251 + 252 + static ssize_t show_temp_highest(struct device *dev, 253 + struct device_attribute *devattr, char *buf) 254 + { 255 + int index = to_sensor_dev_attr(devattr)->index; 256 + struct tmp401_data *data = tmp401_update_device(dev); 257 + 258 + return sprintf(buf, "%d\n", 259 + tmp401_register_to_temp(data->temp_highest[index], 260 + data->config)); 252 261 } 253 262 254 263 static ssize_t show_status(struct device *dev, ··· 396 361 return count; 397 362 } 398 363 364 + /* 365 + * Resets the historical measurements of minimum and maximum temperatures. 366 + * This is done by writing any value to any of the minimum/maximum registers 367 + * (0x30-0x37). 368 + */ 369 + static ssize_t reset_temp_history(struct device *dev, 370 + struct device_attribute *devattr, const char *buf, size_t count) 371 + { 372 + long val; 373 + 374 + if (strict_strtol(buf, 10, &val)) 375 + return -EINVAL; 376 + 377 + if (val != 1) { 378 + dev_err(dev, "temp_reset_history value %ld not" 379 + " supported. Use 1 to reset the history!\n", val); 380 + return -EINVAL; 381 + } 382 + i2c_smbus_write_byte_data(to_i2c_client(dev), 383 + TMP411_TEMP_LOWEST_MSB[0], val); 384 + 385 + return count; 386 + } 387 + 399 388 static struct sensor_device_attribute tmp401_attr[] = { 400 389 SENSOR_ATTR(temp1_input, 0444, show_temp_value, NULL, 0), 401 390 SENSOR_ATTR(temp1_min, 0644, show_temp_min, store_temp_min, 0), ··· 446 387 TMP401_STATUS_REMOTE_HIGH), 447 388 SENSOR_ATTR(temp2_crit_alarm, 0444, show_status, NULL, 448 389 TMP401_STATUS_REMOTE_CRIT), 390 + }; 391 + 392 + /* 393 + * Additional features of the TMP411 chip. 394 + * The TMP411 stores the minimum and maximum 395 + * temperature measured since power-on, chip-reset, or 396 + * minimum and maximum register reset for both the local 397 + * and remote channels. 398 + */ 399 + static struct sensor_device_attribute tmp411_attr[] = { 400 + SENSOR_ATTR(temp1_highest, 0444, show_temp_highest, NULL, 0), 401 + SENSOR_ATTR(temp1_lowest, 0444, show_temp_lowest, NULL, 0), 402 + SENSOR_ATTR(temp2_highest, 0444, show_temp_highest, NULL, 1), 403 + SENSOR_ATTR(temp2_lowest, 0444, show_temp_lowest, NULL, 1), 404 + SENSOR_ATTR(temp_reset_history, 0200, NULL, reset_temp_history, 0), 449 405 }; 450 406 451 407 /* ··· 506 432 return -ENODEV; 507 433 508 434 reg = i2c_smbus_read_byte_data(client, TMP401_DEVICE_ID_REG); 509 - if (reg != TMP401_DEVICE_ID) 435 + 436 + switch (reg) { 437 + case TMP401_DEVICE_ID: 438 + kind = tmp401; 439 + break; 440 + case TMP411_DEVICE_ID: 441 + kind = tmp411; 442 + break; 443 + default: 510 444 return -ENODEV; 445 + } 511 446 512 447 reg = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ); 513 448 if (reg & 0x1b) ··· 524 441 525 442 reg = i2c_smbus_read_byte_data(client, 526 443 TMP401_CONVERSION_RATE_READ); 444 + /* Datasheet says: 0x1-0x6 */ 527 445 if (reg > 15) 528 446 return -ENODEV; 529 447 } 530 - strlcpy(info->type, "tmp401", I2C_NAME_SIZE); 448 + strlcpy(info->type, tmp401_id[kind - 1].name, I2C_NAME_SIZE); 531 449 532 450 return 0; 533 451 } ··· 538 454 { 539 455 int i, err = 0; 540 456 struct tmp401_data *data; 457 + const char *names[] = { "TMP401", "TMP411" }; 541 458 542 459 data = kzalloc(sizeof(struct tmp401_data), GFP_KERNEL); 543 460 if (!data) ··· 546 461 547 462 i2c_set_clientdata(client, data); 548 463 mutex_init(&data->update_lock); 464 + data->kind = id->driver_data; 549 465 550 466 /* Initialize the TMP401 chip */ 551 467 tmp401_init_client(client); ··· 559 473 goto exit_remove; 560 474 } 561 475 476 + /* Register aditional tmp411 sysfs hooks */ 477 + if (data->kind == tmp411) { 478 + for (i = 0; i < ARRAY_SIZE(tmp411_attr); i++) { 479 + err = device_create_file(&client->dev, 480 + &tmp411_attr[i].dev_attr); 481 + if (err) 482 + goto exit_remove; 483 + } 484 + } 485 + 562 486 data->hwmon_dev = hwmon_device_register(&client->dev); 563 487 if (IS_ERR(data->hwmon_dev)) { 564 488 err = PTR_ERR(data->hwmon_dev); ··· 576 480 goto exit_remove; 577 481 } 578 482 579 - dev_info(&client->dev, "Detected TI TMP401 chip\n"); 483 + dev_info(&client->dev, "Detected TI %s chip\n", 484 + names[data->kind - 1]); 580 485 581 486 return 0; 582 487 ··· 597 500 for (i = 0; i < ARRAY_SIZE(tmp401_attr); i++) 598 501 device_remove_file(&client->dev, &tmp401_attr[i].dev_attr); 599 502 503 + if (data->kind == tmp411) { 504 + for (i = 0; i < ARRAY_SIZE(tmp411_attr); i++) 505 + device_remove_file(&client->dev, 506 + &tmp411_attr[i].dev_attr); 507 + } 508 + 600 509 kfree(data); 601 510 return 0; 511 + } 512 + 513 + static struct tmp401_data *tmp401_update_device_reg16( 514 + struct i2c_client *client, struct tmp401_data *data) 515 + { 516 + int i; 517 + 518 + for (i = 0; i < 2; i++) { 519 + /* 520 + * High byte must be read first immediately followed 521 + * by the low byte 522 + */ 523 + data->temp[i] = i2c_smbus_read_byte_data(client, 524 + TMP401_TEMP_MSB[i]) << 8; 525 + data->temp[i] |= i2c_smbus_read_byte_data(client, 526 + TMP401_TEMP_LSB[i]); 527 + data->temp_low[i] = i2c_smbus_read_byte_data(client, 528 + TMP401_TEMP_LOW_LIMIT_MSB_READ[i]) << 8; 529 + data->temp_low[i] |= i2c_smbus_read_byte_data(client, 530 + TMP401_TEMP_LOW_LIMIT_LSB[i]); 531 + data->temp_high[i] = i2c_smbus_read_byte_data(client, 532 + TMP401_TEMP_HIGH_LIMIT_MSB_READ[i]) << 8; 533 + data->temp_high[i] |= i2c_smbus_read_byte_data(client, 534 + TMP401_TEMP_HIGH_LIMIT_LSB[i]); 535 + data->temp_crit[i] = i2c_smbus_read_byte_data(client, 536 + TMP401_TEMP_CRIT_LIMIT[i]); 537 + 538 + if (data->kind == tmp411) { 539 + data->temp_lowest[i] = i2c_smbus_read_byte_data(client, 540 + TMP411_TEMP_LOWEST_MSB[i]) << 8; 541 + data->temp_lowest[i] |= i2c_smbus_read_byte_data( 542 + client, TMP411_TEMP_LOWEST_LSB[i]); 543 + 544 + data->temp_highest[i] = i2c_smbus_read_byte_data( 545 + client, TMP411_TEMP_HIGHEST_MSB[i]) << 8; 546 + data->temp_highest[i] |= i2c_smbus_read_byte_data( 547 + client, TMP411_TEMP_HIGHEST_LSB[i]); 548 + } 549 + } 550 + return data; 602 551 } 603 552 604 553 static struct tmp401_data *tmp401_update_device(struct device *dev) 605 554 { 606 555 struct i2c_client *client = to_i2c_client(dev); 607 556 struct tmp401_data *data = i2c_get_clientdata(client); 608 - int i; 609 557 610 558 mutex_lock(&data->update_lock); 611 559 ··· 658 516 data->status = i2c_smbus_read_byte_data(client, TMP401_STATUS); 659 517 data->config = i2c_smbus_read_byte_data(client, 660 518 TMP401_CONFIG_READ); 661 - for (i = 0; i < 2; i++) { 662 - /* High byte must be read first immediately followed 663 - by the low byte */ 664 - data->temp[i] = i2c_smbus_read_byte_data(client, 665 - TMP401_TEMP_MSB[i]) << 8; 666 - data->temp[i] |= i2c_smbus_read_byte_data(client, 667 - TMP401_TEMP_LSB[i]); 668 - data->temp_low[i] = i2c_smbus_read_byte_data(client, 669 - TMP401_TEMP_LOW_LIMIT_MSB_READ[i]) << 8; 670 - data->temp_low[i] |= i2c_smbus_read_byte_data(client, 671 - TMP401_TEMP_LOW_LIMIT_LSB[i]); 672 - data->temp_high[i] = i2c_smbus_read_byte_data(client, 673 - TMP401_TEMP_HIGH_LIMIT_MSB_READ[i]) << 8; 674 - data->temp_high[i] |= i2c_smbus_read_byte_data(client, 675 - TMP401_TEMP_HIGH_LIMIT_LSB[i]); 676 - data->temp_crit[i] = i2c_smbus_read_byte_data(client, 677 - TMP401_TEMP_CRIT_LIMIT[i]); 678 - } 519 + tmp401_update_device_reg16(client, data); 679 520 680 521 data->temp_crit_hyst = i2c_smbus_read_byte_data(client, 681 522 TMP401_TEMP_CRIT_HYST);