3 * Copyright (C) 2007,2008 Hans de Goede <hdegoede@redhat.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * Driver for the Texas Instruments TMP401 SMBUS temperature sensor IC.
23 * Note this IC is in some aspect similar to the LM90, but it has quite a
24 * few differences too, for example the local temp has a higher resolution
25 * and thus has 16 bits registers for its value and limit instead of 8 bits.
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.h>
34 #include <linux/hwmon-sysfs.h>
35 #include <linux/err.h>
36 #include <linux/mutex.h>
37 #include <linux/sysfs.h>
39 /* Addresses to scan */
40 static const unsigned short normal_i2c[] = { 0x4c, I2C_CLIENT_END };
42 /* Insmod parameters */
43 I2C_CLIENT_INSMOD_1(tmp401);
47 * The TMP401 registers, note some registers have different addresses for
50 #define TMP401_STATUS 0x02
51 #define TMP401_CONFIG_READ 0x03
52 #define TMP401_CONFIG_WRITE 0x09
53 #define TMP401_CONVERSION_RATE_READ 0x04
54 #define TMP401_CONVERSION_RATE_WRITE 0x0A
55 #define TMP401_TEMP_CRIT_HYST 0x21
56 #define TMP401_CONSECUTIVE_ALERT 0x22
57 #define TMP401_MANUFACTURER_ID_REG 0xFE
58 #define TMP401_DEVICE_ID_REG 0xFF
60 static const u8 TMP401_TEMP_MSB[2] = { 0x00, 0x01 };
61 static const u8 TMP401_TEMP_LSB[2] = { 0x15, 0x10 };
62 static const u8 TMP401_TEMP_LOW_LIMIT_MSB_READ[2] = { 0x06, 0x08 };
63 static const u8 TMP401_TEMP_LOW_LIMIT_MSB_WRITE[2] = { 0x0C, 0x0E };
64 static const u8 TMP401_TEMP_LOW_LIMIT_LSB[2] = { 0x17, 0x14 };
65 static const u8 TMP401_TEMP_HIGH_LIMIT_MSB_READ[2] = { 0x05, 0x07 };
66 static const u8 TMP401_TEMP_HIGH_LIMIT_MSB_WRITE[2] = { 0x0B, 0x0D };
67 static const u8 TMP401_TEMP_HIGH_LIMIT_LSB[2] = { 0x16, 0x13 };
68 /* These are called the THERM limit / hysteresis / mask in the datasheet */
69 static const u8 TMP401_TEMP_CRIT_LIMIT[2] = { 0x20, 0x19 };
72 #define TMP401_CONFIG_RANGE 0x04
73 #define TMP401_CONFIG_SHUTDOWN 0x40
74 #define TMP401_STATUS_LOCAL_CRIT 0x01
75 #define TMP401_STATUS_REMOTE_CRIT 0x02
76 #define TMP401_STATUS_REMOTE_OPEN 0x04
77 #define TMP401_STATUS_REMOTE_LOW 0x08
78 #define TMP401_STATUS_REMOTE_HIGH 0x10
79 #define TMP401_STATUS_LOCAL_LOW 0x20
80 #define TMP401_STATUS_LOCAL_HIGH 0x40
82 /* Manufacturer / Device ID's */
83 #define TMP401_MANUFACTURER_ID 0x55
84 #define TMP401_DEVICE_ID 0x11
87 * Functions declarations
90 static int tmp401_probe(struct i2c_client *client,
91 const struct i2c_device_id *id);
92 static int tmp401_detect(struct i2c_client *client, int kind,
93 struct i2c_board_info *info);
94 static int tmp401_remove(struct i2c_client *client);
95 static struct tmp401_data *tmp401_update_device(struct device *dev);
98 * Driver data (common to all clients)
101 static const struct i2c_device_id tmp401_id[] = {
102 { "tmp401", tmp401 },
105 MODULE_DEVICE_TABLE(i2c, tmp401_id);
107 static struct i2c_driver tmp401_driver = {
108 .class = I2C_CLASS_HWMON,
112 .probe = tmp401_probe,
113 .remove = tmp401_remove,
114 .id_table = tmp401_id,
115 .detect = tmp401_detect,
116 .address_data = &addr_data,
120 * Client data (each client gets its own)
124 struct device *hwmon_dev;
125 struct mutex update_lock;
126 char valid; /* zero until following fields are valid */
127 unsigned long last_updated; /* in jiffies */
129 /* register values */
140 * Sysfs attr show / store functions
143 static int tmp401_register_to_temp(u16 reg, u8 config)
147 if (config & TMP401_CONFIG_RANGE)
150 return (temp * 625 + 80) / 160;
153 static u16 tmp401_temp_to_register(long temp, u8 config)
155 if (config & TMP401_CONFIG_RANGE) {
156 temp = SENSORS_LIMIT(temp, -64000, 191000);
159 temp = SENSORS_LIMIT(temp, 0, 127000);
161 return (temp * 160 + 312) / 625;
164 static int tmp401_crit_register_to_temp(u8 reg, u8 config)
168 if (config & TMP401_CONFIG_RANGE)
174 static u8 tmp401_crit_temp_to_register(long temp, u8 config)
176 if (config & TMP401_CONFIG_RANGE) {
177 temp = SENSORS_LIMIT(temp, -64000, 191000);
180 temp = SENSORS_LIMIT(temp, 0, 127000);
182 return (temp + 500) / 1000;
185 static ssize_t show_temp_value(struct device *dev,
186 struct device_attribute *devattr, char *buf)
188 int index = to_sensor_dev_attr(devattr)->index;
189 struct tmp401_data *data = tmp401_update_device(dev);
191 return sprintf(buf, "%d\n",
192 tmp401_register_to_temp(data->temp[index], data->config));
195 static ssize_t show_temp_min(struct device *dev,
196 struct device_attribute *devattr, char *buf)
198 int index = to_sensor_dev_attr(devattr)->index;
199 struct tmp401_data *data = tmp401_update_device(dev);
201 return sprintf(buf, "%d\n",
202 tmp401_register_to_temp(data->temp_low[index], data->config));
205 static ssize_t show_temp_max(struct device *dev,
206 struct device_attribute *devattr, char *buf)
208 int index = to_sensor_dev_attr(devattr)->index;
209 struct tmp401_data *data = tmp401_update_device(dev);
211 return sprintf(buf, "%d\n",
212 tmp401_register_to_temp(data->temp_high[index], data->config));
215 static ssize_t show_temp_crit(struct device *dev,
216 struct device_attribute *devattr, char *buf)
218 int index = to_sensor_dev_attr(devattr)->index;
219 struct tmp401_data *data = tmp401_update_device(dev);
221 return sprintf(buf, "%d\n",
222 tmp401_crit_register_to_temp(data->temp_crit[index],
226 static ssize_t show_temp_crit_hyst(struct device *dev,
227 struct device_attribute *devattr, char *buf)
229 int temp, index = to_sensor_dev_attr(devattr)->index;
230 struct tmp401_data *data = tmp401_update_device(dev);
232 mutex_lock(&data->update_lock);
233 temp = tmp401_crit_register_to_temp(data->temp_crit[index],
235 temp -= data->temp_crit_hyst * 1000;
236 mutex_unlock(&data->update_lock);
238 return sprintf(buf, "%d\n", temp);
241 static ssize_t show_status(struct device *dev,
242 struct device_attribute *devattr, char *buf)
244 int mask = to_sensor_dev_attr(devattr)->index;
245 struct tmp401_data *data = tmp401_update_device(dev);
247 if (data->status & mask)
248 return sprintf(buf, "1\n");
250 return sprintf(buf, "0\n");
253 static ssize_t store_temp_min(struct device *dev, struct device_attribute
254 *devattr, const char *buf, size_t count)
256 int index = to_sensor_dev_attr(devattr)->index;
257 struct tmp401_data *data = tmp401_update_device(dev);
261 if (strict_strtol(buf, 10, &val))
264 reg = tmp401_temp_to_register(val, data->config);
266 mutex_lock(&data->update_lock);
268 i2c_smbus_write_byte_data(to_i2c_client(dev),
269 TMP401_TEMP_LOW_LIMIT_MSB_WRITE[index], reg >> 8);
270 i2c_smbus_write_byte_data(to_i2c_client(dev),
271 TMP401_TEMP_LOW_LIMIT_LSB[index], reg & 0xFF);
273 data->temp_low[index] = reg;
275 mutex_unlock(&data->update_lock);
280 static ssize_t store_temp_max(struct device *dev, struct device_attribute
281 *devattr, const char *buf, size_t count)
283 int index = to_sensor_dev_attr(devattr)->index;
284 struct tmp401_data *data = tmp401_update_device(dev);
288 if (strict_strtol(buf, 10, &val))
291 reg = tmp401_temp_to_register(val, data->config);
293 mutex_lock(&data->update_lock);
295 i2c_smbus_write_byte_data(to_i2c_client(dev),
296 TMP401_TEMP_HIGH_LIMIT_MSB_WRITE[index], reg >> 8);
297 i2c_smbus_write_byte_data(to_i2c_client(dev),
298 TMP401_TEMP_HIGH_LIMIT_LSB[index], reg & 0xFF);
300 data->temp_high[index] = reg;
302 mutex_unlock(&data->update_lock);
307 static ssize_t store_temp_crit(struct device *dev, struct device_attribute
308 *devattr, const char *buf, size_t count)
310 int index = to_sensor_dev_attr(devattr)->index;
311 struct tmp401_data *data = tmp401_update_device(dev);
315 if (strict_strtol(buf, 10, &val))
318 reg = tmp401_crit_temp_to_register(val, data->config);
320 mutex_lock(&data->update_lock);
322 i2c_smbus_write_byte_data(to_i2c_client(dev),
323 TMP401_TEMP_CRIT_LIMIT[index], reg);
325 data->temp_crit[index] = reg;
327 mutex_unlock(&data->update_lock);
332 static ssize_t store_temp_crit_hyst(struct device *dev, struct device_attribute
333 *devattr, const char *buf, size_t count)
335 int temp, index = to_sensor_dev_attr(devattr)->index;
336 struct tmp401_data *data = tmp401_update_device(dev);
340 if (strict_strtol(buf, 10, &val))
343 if (data->config & TMP401_CONFIG_RANGE)
344 val = SENSORS_LIMIT(val, -64000, 191000);
346 val = SENSORS_LIMIT(val, 0, 127000);
348 mutex_lock(&data->update_lock);
349 temp = tmp401_crit_register_to_temp(data->temp_crit[index],
351 val = SENSORS_LIMIT(val, temp - 255000, temp);
352 reg = ((temp - val) + 500) / 1000;
354 i2c_smbus_write_byte_data(to_i2c_client(dev),
355 TMP401_TEMP_CRIT_HYST, reg);
357 data->temp_crit_hyst = reg;
359 mutex_unlock(&data->update_lock);
364 static struct sensor_device_attribute tmp401_attr[] = {
365 SENSOR_ATTR(temp1_input, 0444, show_temp_value, NULL, 0),
366 SENSOR_ATTR(temp1_min, 0644, show_temp_min, store_temp_min, 0),
367 SENSOR_ATTR(temp1_max, 0644, show_temp_max, store_temp_max, 0),
368 SENSOR_ATTR(temp1_crit, 0644, show_temp_crit, store_temp_crit, 0),
369 SENSOR_ATTR(temp1_crit_hyst, 0644, show_temp_crit_hyst,
370 store_temp_crit_hyst, 0),
371 SENSOR_ATTR(temp1_min_alarm, 0444, show_status, NULL,
372 TMP401_STATUS_LOCAL_LOW),
373 SENSOR_ATTR(temp1_max_alarm, 0444, show_status, NULL,
374 TMP401_STATUS_LOCAL_HIGH),
375 SENSOR_ATTR(temp1_crit_alarm, 0444, show_status, NULL,
376 TMP401_STATUS_LOCAL_CRIT),
377 SENSOR_ATTR(temp2_input, 0444, show_temp_value, NULL, 1),
378 SENSOR_ATTR(temp2_min, 0644, show_temp_min, store_temp_min, 1),
379 SENSOR_ATTR(temp2_max, 0644, show_temp_max, store_temp_max, 1),
380 SENSOR_ATTR(temp2_crit, 0644, show_temp_crit, store_temp_crit, 1),
381 SENSOR_ATTR(temp2_crit_hyst, 0444, show_temp_crit_hyst, NULL, 1),
382 SENSOR_ATTR(temp2_fault, 0444, show_status, NULL,
383 TMP401_STATUS_REMOTE_OPEN),
384 SENSOR_ATTR(temp2_min_alarm, 0444, show_status, NULL,
385 TMP401_STATUS_REMOTE_LOW),
386 SENSOR_ATTR(temp2_max_alarm, 0444, show_status, NULL,
387 TMP401_STATUS_REMOTE_HIGH),
388 SENSOR_ATTR(temp2_crit_alarm, 0444, show_status, NULL,
389 TMP401_STATUS_REMOTE_CRIT),
393 * Begin non sysfs callback code (aka Real code)
396 static void tmp401_init_client(struct i2c_client *client)
398 int config, config_orig;
400 /* Set the conversion rate to 2 Hz */
401 i2c_smbus_write_byte_data(client, TMP401_CONVERSION_RATE_WRITE, 5);
403 /* Start conversions (disable shutdown if necessary) */
404 config = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
406 dev_warn(&client->dev, "Initialization failed!\n");
410 config_orig = config;
411 config &= ~TMP401_CONFIG_SHUTDOWN;
413 if (config != config_orig)
414 i2c_smbus_write_byte_data(client, TMP401_CONFIG_WRITE, config);
417 static int tmp401_detect(struct i2c_client *client, int kind,
418 struct i2c_board_info *info)
420 struct i2c_adapter *adapter = client->adapter;
422 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
425 /* Detect and identify the chip */
429 reg = i2c_smbus_read_byte_data(client,
430 TMP401_MANUFACTURER_ID_REG);
431 if (reg != TMP401_MANUFACTURER_ID)
434 reg = i2c_smbus_read_byte_data(client, TMP401_DEVICE_ID_REG);
435 if (reg != TMP401_DEVICE_ID)
438 reg = i2c_smbus_read_byte_data(client, TMP401_CONFIG_READ);
442 reg = i2c_smbus_read_byte_data(client,
443 TMP401_CONVERSION_RATE_READ);
447 strlcpy(info->type, "tmp401", I2C_NAME_SIZE);
452 static int tmp401_probe(struct i2c_client *client,
453 const struct i2c_device_id *id)
456 struct tmp401_data *data;
458 data = kzalloc(sizeof(struct tmp401_data), GFP_KERNEL);
462 i2c_set_clientdata(client, data);
463 mutex_init(&data->update_lock);
465 /* Initialize the TMP401 chip */
466 tmp401_init_client(client);
468 /* Register sysfs hooks */
469 for (i = 0; i < ARRAY_SIZE(tmp401_attr); i++) {
470 err = device_create_file(&client->dev,
471 &tmp401_attr[i].dev_attr);
476 data->hwmon_dev = hwmon_device_register(&client->dev);
477 if (IS_ERR(data->hwmon_dev)) {
478 err = PTR_ERR(data->hwmon_dev);
479 data->hwmon_dev = NULL;
483 dev_info(&client->dev, "Detected TI TMP401 chip\n");
488 tmp401_remove(client); /* will also free data for us */
492 static int tmp401_remove(struct i2c_client *client)
494 struct tmp401_data *data = i2c_get_clientdata(client);
498 hwmon_device_unregister(data->hwmon_dev);
500 for (i = 0; i < ARRAY_SIZE(tmp401_attr); i++)
501 device_remove_file(&client->dev, &tmp401_attr[i].dev_attr);
507 static struct tmp401_data *tmp401_update_device(struct device *dev)
509 struct i2c_client *client = to_i2c_client(dev);
510 struct tmp401_data *data = i2c_get_clientdata(client);
513 mutex_lock(&data->update_lock);
515 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
516 data->status = i2c_smbus_read_byte_data(client, TMP401_STATUS);
517 data->config = i2c_smbus_read_byte_data(client,
519 for (i = 0; i < 2; i++) {
520 /* High byte must be read first immediately followed
522 data->temp[i] = i2c_smbus_read_byte_data(client,
523 TMP401_TEMP_MSB[i]) << 8;
524 data->temp[i] |= i2c_smbus_read_byte_data(client,
526 data->temp_low[i] = i2c_smbus_read_byte_data(client,
527 TMP401_TEMP_LOW_LIMIT_MSB_READ[i]) << 8;
528 data->temp_low[i] |= i2c_smbus_read_byte_data(client,
529 TMP401_TEMP_LOW_LIMIT_LSB[i]);
530 data->temp_high[i] = i2c_smbus_read_byte_data(client,
531 TMP401_TEMP_HIGH_LIMIT_MSB_READ[i]) << 8;
532 data->temp_high[i] |= i2c_smbus_read_byte_data(client,
533 TMP401_TEMP_HIGH_LIMIT_LSB[i]);
534 data->temp_crit[i] = i2c_smbus_read_byte_data(client,
535 TMP401_TEMP_CRIT_LIMIT[i]);
538 data->temp_crit_hyst = i2c_smbus_read_byte_data(client,
539 TMP401_TEMP_CRIT_HYST);
541 data->last_updated = jiffies;
545 mutex_unlock(&data->update_lock);
550 static int __init tmp401_init(void)
552 return i2c_add_driver(&tmp401_driver);
555 static void __exit tmp401_exit(void)
557 i2c_del_driver(&tmp401_driver);
560 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
561 MODULE_DESCRIPTION("Texas Instruments TMP401 temperature sensor driver");
562 MODULE_LICENSE("GPL");
564 module_init(tmp401_init);
565 module_exit(tmp401_exit);