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/bitops.h>
34 #include <linux/slab.h>
35 #include <linux/jiffies.h>
36 #include <linux/i2c.h>
37 #include <linux/hwmon.h>
38 #include <linux/hwmon-sysfs.h>
39 #include <linux/err.h>
40 #include <linux/mutex.h>
41 #include <linux/sysfs.h>
43 /* Addresses to scan */
44 static const unsigned short normal_i2c[] = { 0x4c, 0x4d, 0x4e, I2C_CLIENT_END };
46 enum chips { tmp401, tmp411, tmp431 };
49 * The TMP401 registers, note some registers have different addresses for
52 #define TMP401_STATUS 0x02
53 #define TMP401_CONFIG_READ 0x03
54 #define TMP401_CONFIG_WRITE 0x09
55 #define TMP401_CONVERSION_RATE_READ 0x04
56 #define TMP401_CONVERSION_RATE_WRITE 0x0A
57 #define TMP401_TEMP_CRIT_HYST 0x21
58 #define TMP401_MANUFACTURER_ID_REG 0xFE
59 #define TMP401_DEVICE_ID_REG 0xFF
61 static const u8 TMP401_TEMP_MSB[2] = { 0x00, 0x01 };
62 static const u8 TMP401_TEMP_LSB[2] = { 0x15, 0x10 };
63 static const u8 TMP401_TEMP_LOW_LIMIT_MSB_READ[2] = { 0x06, 0x08 };
64 static const u8 TMP401_TEMP_LOW_LIMIT_MSB_WRITE[2] = { 0x0C, 0x0E };
65 static const u8 TMP401_TEMP_LOW_LIMIT_LSB[2] = { 0x17, 0x14 };
66 static const u8 TMP401_TEMP_HIGH_LIMIT_MSB_READ[2] = { 0x05, 0x07 };
67 static const u8 TMP401_TEMP_HIGH_LIMIT_MSB_WRITE[2] = { 0x0B, 0x0D };
68 static const u8 TMP401_TEMP_HIGH_LIMIT_LSB[2] = { 0x16, 0x13 };
69 /* These are called the THERM limit / hysteresis / mask in the datasheet */
70 static const u8 TMP401_TEMP_CRIT_LIMIT[2] = { 0x20, 0x19 };
72 static const u8 TMP411_TEMP_LOWEST_MSB[2] = { 0x30, 0x34 };
73 static const u8 TMP411_TEMP_LOWEST_LSB[2] = { 0x31, 0x35 };
74 static const u8 TMP411_TEMP_HIGHEST_MSB[2] = { 0x32, 0x36 };
75 static const u8 TMP411_TEMP_HIGHEST_LSB[2] = { 0x33, 0x37 };
78 #define TMP401_CONFIG_RANGE BIT(2)
79 #define TMP401_CONFIG_SHUTDOWN BIT(6)
80 #define TMP401_STATUS_LOCAL_CRIT BIT(0)
81 #define TMP401_STATUS_REMOTE_CRIT BIT(1)
82 #define TMP401_STATUS_REMOTE_OPEN BIT(2)
83 #define TMP401_STATUS_REMOTE_LOW BIT(3)
84 #define TMP401_STATUS_REMOTE_HIGH BIT(4)
85 #define TMP401_STATUS_LOCAL_LOW BIT(5)
86 #define TMP401_STATUS_LOCAL_HIGH BIT(6)
88 /* Manufacturer / Device ID's */
89 #define TMP401_MANUFACTURER_ID 0x55
90 #define TMP401_DEVICE_ID 0x11
91 #define TMP411A_DEVICE_ID 0x12
92 #define TMP411B_DEVICE_ID 0x13
93 #define TMP411C_DEVICE_ID 0x10
94 #define TMP431_DEVICE_ID 0x31
97 * Driver data (common to all clients)
100 static const struct i2c_device_id tmp401_id[] = {
101 { "tmp401", tmp401 },
102 { "tmp411", tmp411 },
103 { "tmp431", tmp431 },
106 MODULE_DEVICE_TABLE(i2c, tmp401_id);
109 * Client data (each client gets its own)
113 struct device *hwmon_dev;
114 struct mutex update_lock;
115 char valid; /* zero until following fields are valid */
116 unsigned long last_updated; /* in jiffies */
119 /* register values */
132 * Sysfs attr show / store functions
135 static int tmp401_register_to_temp(u16 reg, u8 config)
139 if (config & TMP401_CONFIG_RANGE)
142 return (temp * 625 + 80) / 160;
145 static u16 tmp401_temp_to_register(long temp, u8 config)
147 if (config & TMP401_CONFIG_RANGE) {
148 temp = clamp_val(temp, -64000, 191000);
151 temp = clamp_val(temp, 0, 127000);
153 return (temp * 160 + 312) / 625;
156 static int tmp401_crit_register_to_temp(u8 reg, u8 config)
160 if (config & TMP401_CONFIG_RANGE)
166 static u8 tmp401_crit_temp_to_register(long temp, u8 config)
168 if (config & TMP401_CONFIG_RANGE) {
169 temp = clamp_val(temp, -64000, 191000);
172 temp = clamp_val(temp, 0, 127000);
174 return (temp + 500) / 1000;
177 static struct tmp401_data *tmp401_update_device_reg16(
178 struct i2c_client *client, struct tmp401_data *data)
182 for (i = 0; i < 2; i++) {
184 * High byte must be read first immediately followed
187 data->temp[i] = i2c_smbus_read_byte_data(client,
188 TMP401_TEMP_MSB[i]) << 8;
189 data->temp[i] |= i2c_smbus_read_byte_data(client,
191 data->temp_low[i] = i2c_smbus_read_byte_data(client,
192 TMP401_TEMP_LOW_LIMIT_MSB_READ[i]) << 8;
193 data->temp_low[i] |= i2c_smbus_read_byte_data(client,
194 TMP401_TEMP_LOW_LIMIT_LSB[i]);
195 data->temp_high[i] = i2c_smbus_read_byte_data(client,
196 TMP401_TEMP_HIGH_LIMIT_MSB_READ[i]) << 8;
197 data->temp_high[i] |= i2c_smbus_read_byte_data(client,
198 TMP401_TEMP_HIGH_LIMIT_LSB[i]);
199 data->temp_crit[i] = i2c_smbus_read_byte_data(client,
200 TMP401_TEMP_CRIT_LIMIT[i]);
202 if (data->kind == tmp411) {
203 data->temp_lowest[i] = i2c_smbus_read_byte_data(client,
204 TMP411_TEMP_LOWEST_MSB[i]) << 8;
205 data->temp_lowest[i] |= i2c_smbus_read_byte_data(
206 client, TMP411_TEMP_LOWEST_LSB[i]);
208 data->temp_highest[i] = i2c_smbus_read_byte_data(
209 client, TMP411_TEMP_HIGHEST_MSB[i]) << 8;
210 data->temp_highest[i] |= i2c_smbus_read_byte_data(
211 client, TMP411_TEMP_HIGHEST_LSB[i]);
217 static struct tmp401_data *tmp401_update_device(struct device *dev)
219 struct i2c_client *client = to_i2c_client(dev);
220 struct tmp401_data *data = i2c_get_clientdata(client);
222 mutex_lock(&data->update_lock);
224 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
225 data->status = i2c_smbus_read_byte_data(client, TMP401_STATUS);
226 data->config = i2c_smbus_read_byte_data(client,
228 tmp401_update_device_reg16(client, data);
230 data->temp_crit_hyst = i2c_smbus_read_byte_data(client,
231 TMP401_TEMP_CRIT_HYST);
233 data->last_updated = jiffies;
237 mutex_unlock(&data->update_lock);
242 static ssize_t show_temp_value(struct device *dev,
243 struct device_attribute *devattr, char *buf)
245 int index = to_sensor_dev_attr(devattr)->index;
246 struct tmp401_data *data = tmp401_update_device(dev);
248 return sprintf(buf, "%d\n",
249 tmp401_register_to_temp(data->temp[index], data->config));
252 static ssize_t show_temp_min(struct device *dev,
253 struct device_attribute *devattr, char *buf)
255 int index = to_sensor_dev_attr(devattr)->index;
256 struct tmp401_data *data = tmp401_update_device(dev);
258 return sprintf(buf, "%d\n",
259 tmp401_register_to_temp(data->temp_low[index], data->config));
262 static ssize_t show_temp_max(struct device *dev,
263 struct device_attribute *devattr, char *buf)
265 int index = to_sensor_dev_attr(devattr)->index;
266 struct tmp401_data *data = tmp401_update_device(dev);
268 return sprintf(buf, "%d\n",
269 tmp401_register_to_temp(data->temp_high[index], data->config));
272 static ssize_t show_temp_crit(struct device *dev,
273 struct device_attribute *devattr, char *buf)
275 int index = to_sensor_dev_attr(devattr)->index;
276 struct tmp401_data *data = tmp401_update_device(dev);
278 return sprintf(buf, "%d\n",
279 tmp401_crit_register_to_temp(data->temp_crit[index],
283 static ssize_t show_temp_crit_hyst(struct device *dev,
284 struct device_attribute *devattr, char *buf)
286 int temp, index = to_sensor_dev_attr(devattr)->index;
287 struct tmp401_data *data = tmp401_update_device(dev);
289 mutex_lock(&data->update_lock);
290 temp = tmp401_crit_register_to_temp(data->temp_crit[index],
292 temp -= data->temp_crit_hyst * 1000;
293 mutex_unlock(&data->update_lock);
295 return sprintf(buf, "%d\n", temp);
298 static ssize_t show_temp_lowest(struct device *dev,
299 struct device_attribute *devattr, char *buf)
301 int index = to_sensor_dev_attr(devattr)->index;
302 struct tmp401_data *data = tmp401_update_device(dev);
304 return sprintf(buf, "%d\n",
305 tmp401_register_to_temp(data->temp_lowest[index],
309 static ssize_t show_temp_highest(struct device *dev,
310 struct device_attribute *devattr, char *buf)
312 int index = to_sensor_dev_attr(devattr)->index;
313 struct tmp401_data *data = tmp401_update_device(dev);
315 return sprintf(buf, "%d\n",
316 tmp401_register_to_temp(data->temp_highest[index],
320 static ssize_t show_status(struct device *dev,
321 struct device_attribute *devattr, char *buf)
323 int mask = to_sensor_dev_attr(devattr)->index;
324 struct tmp401_data *data = tmp401_update_device(dev);
326 if (data->status & mask)
327 return sprintf(buf, "1\n");
329 return sprintf(buf, "0\n");
332 static ssize_t store_temp_min(struct device *dev, struct device_attribute
333 *devattr, const char *buf, size_t count)
335 int index = to_sensor_dev_attr(devattr)->index;
336 struct tmp401_data *data = tmp401_update_device(dev);
340 if (kstrtol(buf, 10, &val))
343 reg = tmp401_temp_to_register(val, data->config);
345 mutex_lock(&data->update_lock);
347 i2c_smbus_write_byte_data(to_i2c_client(dev),
348 TMP401_TEMP_LOW_LIMIT_MSB_WRITE[index], reg >> 8);
349 i2c_smbus_write_byte_data(to_i2c_client(dev),
350 TMP401_TEMP_LOW_LIMIT_LSB[index], reg & 0xFF);
352 data->temp_low[index] = reg;
354 mutex_unlock(&data->update_lock);
359 static ssize_t store_temp_max(struct device *dev, struct device_attribute
360 *devattr, const char *buf, size_t count)
362 int index = to_sensor_dev_attr(devattr)->index;
363 struct tmp401_data *data = tmp401_update_device(dev);
367 if (kstrtol(buf, 10, &val))
370 reg = tmp401_temp_to_register(val, data->config);
372 mutex_lock(&data->update_lock);
374 i2c_smbus_write_byte_data(to_i2c_client(dev),
375 TMP401_TEMP_HIGH_LIMIT_MSB_WRITE[index], reg >> 8);
376 i2c_smbus_write_byte_data(to_i2c_client(dev),
377 TMP401_TEMP_HIGH_LIMIT_LSB[index], reg & 0xFF);
379 data->temp_high[index] = reg;
381 mutex_unlock(&data->update_lock);
386 static ssize_t store_temp_crit(struct device *dev, struct device_attribute
387 *devattr, const char *buf, size_t count)
389 int index = to_sensor_dev_attr(devattr)->index;
390 struct tmp401_data *data = tmp401_update_device(dev);
394 if (kstrtol(buf, 10, &val))
397 reg = tmp401_crit_temp_to_register(val, data->config);
399 mutex_lock(&data->update_lock);
401 i2c_smbus_write_byte_data(to_i2c_client(dev),
402 TMP401_TEMP_CRIT_LIMIT[index], reg);
404 data->temp_crit[index] = reg;
406 mutex_unlock(&data->update_lock);
411 static ssize_t store_temp_crit_hyst(struct device *dev, struct device_attribute
412 *devattr, const char *buf, size_t count)
414 int temp, index = to_sensor_dev_attr(devattr)->index;
415 struct tmp401_data *data = tmp401_update_device(dev);
419 if (kstrtol(buf, 10, &val))
422 if (data->config & TMP401_CONFIG_RANGE)
423 val = clamp_val(val, -64000, 191000);
425 val = clamp_val(val, 0, 127000);
427 mutex_lock(&data->update_lock);
428 temp = tmp401_crit_register_to_temp(data->temp_crit[index],
430 val = clamp_val(val, temp - 255000, temp);
431 reg = ((temp - val) + 500) / 1000;
433 i2c_smbus_write_byte_data(to_i2c_client(dev),
434 TMP401_TEMP_CRIT_HYST, reg);
436 data->temp_crit_hyst = reg;
438 mutex_unlock(&data->update_lock);
444 * Resets the historical measurements of minimum and maximum temperatures.
445 * This is done by writing any value to any of the minimum/maximum registers
448 static ssize_t reset_temp_history(struct device *dev,
449 struct device_attribute *devattr, const char *buf, size_t count)
453 if (kstrtol(buf, 10, &val))
458 "temp_reset_history value %ld not supported. Use 1 to reset the history!\n",
462 i2c_smbus_write_byte_data(to_i2c_client(dev),
463 TMP411_TEMP_LOWEST_MSB[0], val);
468 static struct sensor_device_attribute tmp401_attr[] = {
469 SENSOR_ATTR(temp1_input, S_IRUGO, show_temp_value, NULL, 0),
470 SENSOR_ATTR(temp1_min, S_IWUSR | S_IRUGO, show_temp_min,
472 SENSOR_ATTR(temp1_max, S_IWUSR | S_IRUGO, show_temp_max,
474 SENSOR_ATTR(temp1_crit, S_IWUSR | S_IRUGO, show_temp_crit,
476 SENSOR_ATTR(temp1_crit_hyst, S_IWUSR | S_IRUGO, show_temp_crit_hyst,
477 store_temp_crit_hyst, 0),
478 SENSOR_ATTR(temp1_min_alarm, S_IRUGO, show_status, NULL,
479 TMP401_STATUS_LOCAL_LOW),
480 SENSOR_ATTR(temp1_max_alarm, S_IRUGO, show_status, NULL,
481 TMP401_STATUS_LOCAL_HIGH),
482 SENSOR_ATTR(temp1_crit_alarm, S_IRUGO, show_status, NULL,
483 TMP401_STATUS_LOCAL_CRIT),
484 SENSOR_ATTR(temp2_input, S_IRUGO, show_temp_value, NULL, 1),
485 SENSOR_ATTR(temp2_min, S_IWUSR | S_IRUGO, show_temp_min,
487 SENSOR_ATTR(temp2_max, S_IWUSR | S_IRUGO, show_temp_max,
489 SENSOR_ATTR(temp2_crit, S_IWUSR | S_IRUGO, show_temp_crit,
491 SENSOR_ATTR(temp2_crit_hyst, S_IRUGO, show_temp_crit_hyst, NULL, 1),
492 SENSOR_ATTR(temp2_fault, S_IRUGO, show_status, NULL,
493 TMP401_STATUS_REMOTE_OPEN),
494 SENSOR_ATTR(temp2_min_alarm, S_IRUGO, show_status, NULL,
495 TMP401_STATUS_REMOTE_LOW),
496 SENSOR_ATTR(temp2_max_alarm, S_IRUGO, show_status, NULL,
497 TMP401_STATUS_REMOTE_HIGH),
498 SENSOR_ATTR(temp2_crit_alarm, S_IRUGO, show_status, NULL,
499 TMP401_STATUS_REMOTE_CRIT),
503 * Additional features of the TMP411 chip.
504 * The TMP411 stores the minimum and maximum
505 * temperature measured since power-on, chip-reset, or
506 * minimum and maximum register reset for both the local
507 * and remote channels.
509 static struct sensor_device_attribute tmp411_attr[] = {
510 SENSOR_ATTR(temp1_highest, S_IRUGO, show_temp_highest, NULL, 0),
511 SENSOR_ATTR(temp1_lowest, S_IRUGO, show_temp_lowest, NULL, 0),
512 SENSOR_ATTR(temp2_highest, S_IRUGO, show_temp_highest, NULL, 1),
513 SENSOR_ATTR(temp2_lowest, S_IRUGO, show_temp_lowest, NULL, 1),
514 SENSOR_ATTR(temp_reset_history, S_IWUSR, NULL, reset_temp_history, 0),
518 * Begin non sysfs callback code (aka Real code)
521 static void tmp401_init_client(struct i2c_client *client)
523 int config, config_orig;
525 /* Set the conversion rate to 2 Hz */
526 i2c_smbus_write_byte_data(client, TMP401_CONVERSION_RATE_WRITE, 5);
528 /* Start conversions (disable shutdown if necessary) */
529 config = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
531 dev_warn(&client->dev, "Initialization failed!\n");
535 config_orig = config;
536 config &= ~TMP401_CONFIG_SHUTDOWN;
538 if (config != config_orig)
539 i2c_smbus_write_byte_data(client, TMP401_CONFIG_WRITE, config);
542 static int tmp401_detect(struct i2c_client *client,
543 struct i2c_board_info *info)
546 struct i2c_adapter *adapter = client->adapter;
549 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
552 /* Detect and identify the chip */
553 reg = i2c_smbus_read_byte_data(client, TMP401_MANUFACTURER_ID_REG);
554 if (reg != TMP401_MANUFACTURER_ID)
557 reg = i2c_smbus_read_byte_data(client, TMP401_DEVICE_ID_REG);
560 case TMP401_DEVICE_ID:
561 if (client->addr != 0x4c)
565 case TMP411A_DEVICE_ID:
566 if (client->addr != 0x4c)
570 case TMP411B_DEVICE_ID:
571 if (client->addr != 0x4d)
575 case TMP411C_DEVICE_ID:
576 if (client->addr != 0x4e)
580 case TMP431_DEVICE_ID:
581 if (client->addr == 0x4e)
589 reg = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
593 reg = i2c_smbus_read_byte_data(client, TMP401_CONVERSION_RATE_READ);
594 /* Datasheet says: 0x1-0x6 */
598 strlcpy(info->type, tmp401_id[kind].name, I2C_NAME_SIZE);
603 static int tmp401_remove(struct i2c_client *client)
605 struct tmp401_data *data = i2c_get_clientdata(client);
609 hwmon_device_unregister(data->hwmon_dev);
611 for (i = 0; i < ARRAY_SIZE(tmp401_attr); i++)
612 device_remove_file(&client->dev, &tmp401_attr[i].dev_attr);
614 if (data->kind == tmp411) {
615 for (i = 0; i < ARRAY_SIZE(tmp411_attr); i++)
616 device_remove_file(&client->dev,
617 &tmp411_attr[i].dev_attr);
623 static int tmp401_probe(struct i2c_client *client,
624 const struct i2c_device_id *id)
627 struct tmp401_data *data;
628 const char *names[] = { "TMP401", "TMP411", "TMP431" };
630 data = devm_kzalloc(&client->dev, sizeof(struct tmp401_data),
635 i2c_set_clientdata(client, data);
636 mutex_init(&data->update_lock);
637 data->kind = id->driver_data;
639 /* Initialize the TMP401 chip */
640 tmp401_init_client(client);
642 /* Register sysfs hooks */
643 for (i = 0; i < ARRAY_SIZE(tmp401_attr); i++) {
644 err = device_create_file(&client->dev,
645 &tmp401_attr[i].dev_attr);
650 /* Register additional tmp411 sysfs hooks */
651 if (data->kind == tmp411) {
652 for (i = 0; i < ARRAY_SIZE(tmp411_attr); i++) {
653 err = device_create_file(&client->dev,
654 &tmp411_attr[i].dev_attr);
660 data->hwmon_dev = hwmon_device_register(&client->dev);
661 if (IS_ERR(data->hwmon_dev)) {
662 err = PTR_ERR(data->hwmon_dev);
663 data->hwmon_dev = NULL;
667 dev_info(&client->dev, "Detected TI %s chip\n", names[data->kind]);
672 tmp401_remove(client);
676 static struct i2c_driver tmp401_driver = {
677 .class = I2C_CLASS_HWMON,
681 .probe = tmp401_probe,
682 .remove = tmp401_remove,
683 .id_table = tmp401_id,
684 .detect = tmp401_detect,
685 .address_list = normal_i2c,
688 module_i2c_driver(tmp401_driver);
690 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
691 MODULE_DESCRIPTION("Texas Instruments TMP401 temperature sensor driver");
692 MODULE_LICENSE("GPL");