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>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * Driver for the Texas Instruments TMP401 SMBUS temperature sensor IC.
26 * Note this IC is in some aspect similar to the LM90, but it has quite a
27 * few differences too, for example the local temp has a higher resolution
28 * and thus has 16 bits registers for its value and limit instead of 8 bits.
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/slab.h>
34 #include <linux/jiffies.h>
35 #include <linux/i2c.h>
36 #include <linux/hwmon.h>
37 #include <linux/hwmon-sysfs.h>
38 #include <linux/err.h>
39 #include <linux/mutex.h>
40 #include <linux/sysfs.h>
42 /* Addresses to scan */
43 static const unsigned short normal_i2c[] = { 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
45 enum chips { tmp401, tmp411, tmp431 };
48 * The TMP401 registers, note some registers have different addresses for
51 #define TMP401_STATUS 0x02
52 #define TMP401_CONFIG_READ 0x03
53 #define TMP401_CONFIG_WRITE 0x09
54 #define TMP401_CONVERSION_RATE_READ 0x04
55 #define TMP401_CONVERSION_RATE_WRITE 0x0A
56 #define TMP401_TEMP_CRIT_HYST 0x21
57 #define TMP401_CONSECUTIVE_ALERT 0x22
58 #define TMP401_MANUFACTURER_ID_REG 0xFE
59 #define TMP401_DEVICE_ID_REG 0xFF
60 #define TMP411_N_FACTOR_REG 0x18
62 static const u8 TMP401_TEMP_MSB[2] = { 0x00, 0x01 };
63 static const u8 TMP401_TEMP_LSB[2] = { 0x15, 0x10 };
64 static const u8 TMP401_TEMP_LOW_LIMIT_MSB_READ[2] = { 0x06, 0x08 };
65 static const u8 TMP401_TEMP_LOW_LIMIT_MSB_WRITE[2] = { 0x0C, 0x0E };
66 static const u8 TMP401_TEMP_LOW_LIMIT_LSB[2] = { 0x17, 0x14 };
67 static const u8 TMP401_TEMP_HIGH_LIMIT_MSB_READ[2] = { 0x05, 0x07 };
68 static const u8 TMP401_TEMP_HIGH_LIMIT_MSB_WRITE[2] = { 0x0B, 0x0D };
69 static const u8 TMP401_TEMP_HIGH_LIMIT_LSB[2] = { 0x16, 0x13 };
70 /* These are called the THERM limit / hysteresis / mask in the datasheet */
71 static const u8 TMP401_TEMP_CRIT_LIMIT[2] = { 0x20, 0x19 };
73 static const u8 TMP411_TEMP_LOWEST_MSB[2] = { 0x30, 0x34 };
74 static const u8 TMP411_TEMP_LOWEST_LSB[2] = { 0x31, 0x35 };
75 static const u8 TMP411_TEMP_HIGHEST_MSB[2] = { 0x32, 0x36 };
76 static const u8 TMP411_TEMP_HIGHEST_LSB[2] = { 0x33, 0x37 };
79 #define TMP401_CONFIG_RANGE 0x04
80 #define TMP401_CONFIG_SHUTDOWN 0x40
81 #define TMP401_STATUS_LOCAL_CRIT 0x01
82 #define TMP401_STATUS_REMOTE_CRIT 0x02
83 #define TMP401_STATUS_REMOTE_OPEN 0x04
84 #define TMP401_STATUS_REMOTE_LOW 0x08
85 #define TMP401_STATUS_REMOTE_HIGH 0x10
86 #define TMP401_STATUS_LOCAL_LOW 0x20
87 #define TMP401_STATUS_LOCAL_HIGH 0x40
89 /* Manufacturer / Device ID's */
90 #define TMP401_MANUFACTURER_ID 0x55
91 #define TMP401_DEVICE_ID 0x11
92 #define TMP411A_DEVICE_ID 0x12
93 #define TMP411B_DEVICE_ID 0x13
94 #define TMP411C_DEVICE_ID 0x10
95 #define TMP431_DEVICE_ID 0x31
98 * Driver data (common to all clients)
101 static const struct i2c_device_id tmp401_id[] = {
102 { "tmp401", tmp401 },
103 { "tmp411", tmp411 },
104 { "tmp431", tmp431 },
107 MODULE_DEVICE_TABLE(i2c, tmp401_id);
110 * Client data (each client gets its own)
114 struct device *hwmon_dev;
115 struct mutex update_lock;
116 char valid; /* zero until following fields are valid */
117 unsigned long last_updated; /* in jiffies */
120 /* register values */
133 * Sysfs attr show / store functions
136 static int tmp401_register_to_temp(u16 reg, u8 config)
140 if (config & TMP401_CONFIG_RANGE)
143 return (temp * 625 + 80) / 160;
146 static u16 tmp401_temp_to_register(long temp, u8 config)
148 if (config & TMP401_CONFIG_RANGE) {
149 temp = clamp_val(temp, -64000, 191000);
152 temp = clamp_val(temp, 0, 127000);
154 return (temp * 160 + 312) / 625;
157 static int tmp401_crit_register_to_temp(u8 reg, u8 config)
161 if (config & TMP401_CONFIG_RANGE)
167 static u8 tmp401_crit_temp_to_register(long temp, u8 config)
169 if (config & TMP401_CONFIG_RANGE) {
170 temp = clamp_val(temp, -64000, 191000);
173 temp = clamp_val(temp, 0, 127000);
175 return (temp + 500) / 1000;
178 static struct tmp401_data *tmp401_update_device_reg16(
179 struct i2c_client *client, struct tmp401_data *data)
183 for (i = 0; i < 2; i++) {
185 * High byte must be read first immediately followed
188 data->temp[i] = i2c_smbus_read_byte_data(client,
189 TMP401_TEMP_MSB[i]) << 8;
190 data->temp[i] |= i2c_smbus_read_byte_data(client,
192 data->temp_low[i] = i2c_smbus_read_byte_data(client,
193 TMP401_TEMP_LOW_LIMIT_MSB_READ[i]) << 8;
194 data->temp_low[i] |= i2c_smbus_read_byte_data(client,
195 TMP401_TEMP_LOW_LIMIT_LSB[i]);
196 data->temp_high[i] = i2c_smbus_read_byte_data(client,
197 TMP401_TEMP_HIGH_LIMIT_MSB_READ[i]) << 8;
198 data->temp_high[i] |= i2c_smbus_read_byte_data(client,
199 TMP401_TEMP_HIGH_LIMIT_LSB[i]);
200 data->temp_crit[i] = i2c_smbus_read_byte_data(client,
201 TMP401_TEMP_CRIT_LIMIT[i]);
203 if (data->kind == tmp411) {
204 data->temp_lowest[i] = i2c_smbus_read_byte_data(client,
205 TMP411_TEMP_LOWEST_MSB[i]) << 8;
206 data->temp_lowest[i] |= i2c_smbus_read_byte_data(
207 client, TMP411_TEMP_LOWEST_LSB[i]);
209 data->temp_highest[i] = i2c_smbus_read_byte_data(
210 client, TMP411_TEMP_HIGHEST_MSB[i]) << 8;
211 data->temp_highest[i] |= i2c_smbus_read_byte_data(
212 client, TMP411_TEMP_HIGHEST_LSB[i]);
218 static struct tmp401_data *tmp401_update_device(struct device *dev)
220 struct i2c_client *client = to_i2c_client(dev);
221 struct tmp401_data *data = i2c_get_clientdata(client);
223 mutex_lock(&data->update_lock);
225 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
226 data->status = i2c_smbus_read_byte_data(client, TMP401_STATUS);
227 data->config = i2c_smbus_read_byte_data(client,
229 tmp401_update_device_reg16(client, data);
231 data->temp_crit_hyst = i2c_smbus_read_byte_data(client,
232 TMP401_TEMP_CRIT_HYST);
234 data->last_updated = jiffies;
238 mutex_unlock(&data->update_lock);
243 static ssize_t show_temp_value(struct device *dev,
244 struct device_attribute *devattr, char *buf)
246 int index = to_sensor_dev_attr(devattr)->index;
247 struct tmp401_data *data = tmp401_update_device(dev);
249 return sprintf(buf, "%d\n",
250 tmp401_register_to_temp(data->temp[index], data->config));
253 static ssize_t show_temp_min(struct device *dev,
254 struct device_attribute *devattr, char *buf)
256 int index = to_sensor_dev_attr(devattr)->index;
257 struct tmp401_data *data = tmp401_update_device(dev);
259 return sprintf(buf, "%d\n",
260 tmp401_register_to_temp(data->temp_low[index], data->config));
263 static ssize_t show_temp_max(struct device *dev,
264 struct device_attribute *devattr, char *buf)
266 int index = to_sensor_dev_attr(devattr)->index;
267 struct tmp401_data *data = tmp401_update_device(dev);
269 return sprintf(buf, "%d\n",
270 tmp401_register_to_temp(data->temp_high[index], data->config));
273 static ssize_t show_temp_crit(struct device *dev,
274 struct device_attribute *devattr, char *buf)
276 int index = to_sensor_dev_attr(devattr)->index;
277 struct tmp401_data *data = tmp401_update_device(dev);
279 return sprintf(buf, "%d\n",
280 tmp401_crit_register_to_temp(data->temp_crit[index],
284 static ssize_t show_temp_crit_hyst(struct device *dev,
285 struct device_attribute *devattr, char *buf)
287 int temp, index = to_sensor_dev_attr(devattr)->index;
288 struct tmp401_data *data = tmp401_update_device(dev);
290 mutex_lock(&data->update_lock);
291 temp = tmp401_crit_register_to_temp(data->temp_crit[index],
293 temp -= data->temp_crit_hyst * 1000;
294 mutex_unlock(&data->update_lock);
296 return sprintf(buf, "%d\n", temp);
299 static ssize_t show_temp_lowest(struct device *dev,
300 struct device_attribute *devattr, char *buf)
302 int index = to_sensor_dev_attr(devattr)->index;
303 struct tmp401_data *data = tmp401_update_device(dev);
305 return sprintf(buf, "%d\n",
306 tmp401_register_to_temp(data->temp_lowest[index],
310 static ssize_t show_temp_highest(struct device *dev,
311 struct device_attribute *devattr, char *buf)
313 int index = to_sensor_dev_attr(devattr)->index;
314 struct tmp401_data *data = tmp401_update_device(dev);
316 return sprintf(buf, "%d\n",
317 tmp401_register_to_temp(data->temp_highest[index],
321 static ssize_t show_status(struct device *dev,
322 struct device_attribute *devattr, char *buf)
324 int mask = to_sensor_dev_attr(devattr)->index;
325 struct tmp401_data *data = tmp401_update_device(dev);
327 if (data->status & mask)
328 return sprintf(buf, "1\n");
330 return sprintf(buf, "0\n");
333 static ssize_t store_temp_min(struct device *dev, struct device_attribute
334 *devattr, const char *buf, size_t count)
336 int index = to_sensor_dev_attr(devattr)->index;
337 struct tmp401_data *data = tmp401_update_device(dev);
341 if (kstrtol(buf, 10, &val))
344 reg = tmp401_temp_to_register(val, data->config);
346 mutex_lock(&data->update_lock);
348 i2c_smbus_write_byte_data(to_i2c_client(dev),
349 TMP401_TEMP_LOW_LIMIT_MSB_WRITE[index], reg >> 8);
350 i2c_smbus_write_byte_data(to_i2c_client(dev),
351 TMP401_TEMP_LOW_LIMIT_LSB[index], reg & 0xFF);
353 data->temp_low[index] = reg;
355 mutex_unlock(&data->update_lock);
360 static ssize_t store_temp_max(struct device *dev, struct device_attribute
361 *devattr, const char *buf, size_t count)
363 int index = to_sensor_dev_attr(devattr)->index;
364 struct tmp401_data *data = tmp401_update_device(dev);
368 if (kstrtol(buf, 10, &val))
371 reg = tmp401_temp_to_register(val, data->config);
373 mutex_lock(&data->update_lock);
375 i2c_smbus_write_byte_data(to_i2c_client(dev),
376 TMP401_TEMP_HIGH_LIMIT_MSB_WRITE[index], reg >> 8);
377 i2c_smbus_write_byte_data(to_i2c_client(dev),
378 TMP401_TEMP_HIGH_LIMIT_LSB[index], reg & 0xFF);
380 data->temp_high[index] = reg;
382 mutex_unlock(&data->update_lock);
387 static ssize_t store_temp_crit(struct device *dev, struct device_attribute
388 *devattr, const char *buf, size_t count)
390 int index = to_sensor_dev_attr(devattr)->index;
391 struct tmp401_data *data = tmp401_update_device(dev);
395 if (kstrtol(buf, 10, &val))
398 reg = tmp401_crit_temp_to_register(val, data->config);
400 mutex_lock(&data->update_lock);
402 i2c_smbus_write_byte_data(to_i2c_client(dev),
403 TMP401_TEMP_CRIT_LIMIT[index], reg);
405 data->temp_crit[index] = reg;
407 mutex_unlock(&data->update_lock);
412 static ssize_t store_temp_crit_hyst(struct device *dev, struct device_attribute
413 *devattr, const char *buf, size_t count)
415 int temp, index = to_sensor_dev_attr(devattr)->index;
416 struct tmp401_data *data = tmp401_update_device(dev);
420 if (kstrtol(buf, 10, &val))
423 if (data->config & TMP401_CONFIG_RANGE)
424 val = clamp_val(val, -64000, 191000);
426 val = clamp_val(val, 0, 127000);
428 mutex_lock(&data->update_lock);
429 temp = tmp401_crit_register_to_temp(data->temp_crit[index],
431 val = clamp_val(val, temp - 255000, temp);
432 reg = ((temp - val) + 500) / 1000;
434 i2c_smbus_write_byte_data(to_i2c_client(dev),
435 TMP401_TEMP_CRIT_HYST, reg);
437 data->temp_crit_hyst = reg;
439 mutex_unlock(&data->update_lock);
445 * Resets the historical measurements of minimum and maximum temperatures.
446 * This is done by writing any value to any of the minimum/maximum registers
449 static ssize_t reset_temp_history(struct device *dev,
450 struct device_attribute *devattr, const char *buf, size_t count)
454 if (kstrtol(buf, 10, &val))
459 "temp_reset_history value %ld not supported. Use 1 to reset the history!\n",
463 i2c_smbus_write_byte_data(to_i2c_client(dev),
464 TMP411_TEMP_LOWEST_MSB[0], val);
469 static struct sensor_device_attribute tmp401_attr[] = {
470 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp_value, NULL, 0),
471 SENSOR_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
473 SENSOR_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
475 SENSOR_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_crit,
477 SENSOR_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_crit_hyst,
478 store_temp_crit_hyst, 0),
479 SENSOR_ATTR(temp1_min_alarm, S_IRUGO, show_status, NULL,
480 TMP401_STATUS_LOCAL_LOW),
481 SENSOR_ATTR(temp1_max_alarm, S_IRUGO, show_status, NULL,
482 TMP401_STATUS_LOCAL_HIGH),
483 SENSOR_ATTR(temp1_crit_alarm, S_IRUGO, show_status, NULL,
484 TMP401_STATUS_LOCAL_CRIT),
485 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp_value, NULL, 1),
486 SENSOR_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
488 SENSOR_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
490 SENSOR_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp_crit,
492 SENSOR_ATTR(temp2_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL, 1),
493 SENSOR_ATTR(temp2_fault, S_IRUGO, show_status, NULL,
494 TMP401_STATUS_REMOTE_OPEN),
495 SENSOR_ATTR(temp2_min_alarm, S_IRUGO, show_status, NULL,
496 TMP401_STATUS_REMOTE_LOW),
497 SENSOR_ATTR(temp2_max_alarm, S_IRUGO, show_status, NULL,
498 TMP401_STATUS_REMOTE_HIGH),
499 SENSOR_ATTR(temp2_crit_alarm, S_IRUGO, show_status, NULL,
500 TMP401_STATUS_REMOTE_CRIT),
504 * Additional features of the TMP411 chip.
505 * The TMP411 stores the minimum and maximum
506 * temperature measured since power-on, chip-reset, or
507 * minimum and maximum register reset for both the local
508 * and remote channels.
510 static struct sensor_device_attribute tmp411_attr[] = {
511 SENSOR_ATTR(temp1_highest, S_IRUGO, show_temp_highest, NULL, 0),
512 SENSOR_ATTR(temp1_lowest, S_IRUGO, show_temp_lowest, NULL, 0),
513 SENSOR_ATTR(temp2_highest, S_IRUGO, show_temp_highest, NULL, 1),
514 SENSOR_ATTR(temp2_lowest, S_IRUGO, show_temp_lowest, NULL, 1),
515 SENSOR_ATTR(temp_reset_history, S_IWUSR, NULL, reset_temp_history, 0),
519 * Begin non sysfs callback code (aka Real code)
522 static void tmp401_init_client(struct i2c_client *client)
524 int config, config_orig;
526 /* Set the conversion rate to 2 Hz */
527 i2c_smbus_write_byte_data(client, TMP401_CONVERSION_RATE_WRITE, 5);
529 /* Start conversions (disable shutdown if necessary) */
530 config = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
532 dev_warn(&client->dev, "Initialization failed!\n");
536 config_orig = config;
537 config &= ~TMP401_CONFIG_SHUTDOWN;
539 if (config != config_orig)
540 i2c_smbus_write_byte_data(client, TMP401_CONFIG_WRITE, config);
543 static int tmp401_detect(struct i2c_client *client,
544 struct i2c_board_info *info)
547 struct i2c_adapter *adapter = client->adapter;
550 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
553 /* Detect and identify the chip */
554 reg = i2c_smbus_read_byte_data(client, TMP401_MANUFACTURER_ID_REG);
555 if (reg != TMP401_MANUFACTURER_ID)
558 reg = i2c_smbus_read_byte_data(client, TMP401_DEVICE_ID_REG);
561 case TMP401_DEVICE_ID:
562 if (client->addr != 0x4c)
566 case TMP411A_DEVICE_ID:
567 if (client->addr != 0x4c)
571 case TMP411B_DEVICE_ID:
572 if (client->addr != 0x4d)
576 case TMP411C_DEVICE_ID:
577 if (client->addr != 0x4e)
581 case TMP431_DEVICE_ID:
582 if (client->addr == 0x4e)
590 reg = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
594 reg = i2c_smbus_read_byte_data(client, TMP401_CONVERSION_RATE_READ);
595 /* Datasheet says: 0x1-0x6 */
599 strlcpy(info->type, tmp401_id[kind].name, I2C_NAME_SIZE);
604 static int tmp401_remove(struct i2c_client *client)
606 struct tmp401_data *data = i2c_get_clientdata(client);
610 hwmon_device_unregister(data->hwmon_dev);
612 for (i = 0; i < ARRAY_SIZE(tmp401_attr); i++)
613 device_remove_file(&client->dev, &tmp401_attr[i].dev_attr);
615 if (data->kind == tmp411) {
616 for (i = 0; i < ARRAY_SIZE(tmp411_attr); i++)
617 device_remove_file(&client->dev,
618 &tmp411_attr[i].dev_attr);
624 static int tmp401_probe(struct i2c_client *client,
625 const struct i2c_device_id *id)
628 struct tmp401_data *data;
629 const char *names[] = { "TMP401", "TMP411", "TMP431" };
631 data = devm_kzalloc(&client->dev, sizeof(struct tmp401_data),
636 i2c_set_clientdata(client, data);
637 mutex_init(&data->update_lock);
638 data->kind = id->driver_data;
640 /* Initialize the TMP401 chip */
641 tmp401_init_client(client);
643 /* Register sysfs hooks */
644 for (i = 0; i < ARRAY_SIZE(tmp401_attr); i++) {
645 err = device_create_file(&client->dev,
646 &tmp401_attr[i].dev_attr);
651 /* Register additional tmp411 sysfs hooks */
652 if (data->kind == tmp411) {
653 for (i = 0; i < ARRAY_SIZE(tmp411_attr); i++) {
654 err = device_create_file(&client->dev,
655 &tmp411_attr[i].dev_attr);
661 data->hwmon_dev = hwmon_device_register(&client->dev);
662 if (IS_ERR(data->hwmon_dev)) {
663 err = PTR_ERR(data->hwmon_dev);
664 data->hwmon_dev = NULL;
668 dev_info(&client->dev, "Detected TI %s chip\n", names[data->kind]);
673 tmp401_remove(client);
677 static struct i2c_driver tmp401_driver = {
678 .class = I2C_CLASS_HWMON,
682 .probe = tmp401_probe,
683 .remove = tmp401_remove,
684 .id_table = tmp401_id,
685 .detect = tmp401_detect,
686 .address_list = normal_i2c,
689 module_i2c_driver(tmp401_driver);
691 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
692 MODULE_DESCRIPTION("Texas Instruments TMP401 temperature sensor driver");
693 MODULE_LICENSE("GPL");