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, I2C_CLIENT_END
};
45 enum chips
{ tmp401
, tmp411
};
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 TMP411_DEVICE_ID 0x12
95 * Functions declarations
98 static int tmp401_probe(struct i2c_client
*client
,
99 const struct i2c_device_id
*id
);
100 static int tmp401_detect(struct i2c_client
*client
,
101 struct i2c_board_info
*info
);
102 static int tmp401_remove(struct i2c_client
*client
);
103 static struct tmp401_data
*tmp401_update_device(struct device
*dev
);
106 * Driver data (common to all clients)
109 static const struct i2c_device_id tmp401_id
[] = {
110 { "tmp401", tmp401
},
111 { "tmp411", tmp411
},
114 MODULE_DEVICE_TABLE(i2c
, tmp401_id
);
116 static struct i2c_driver tmp401_driver
= {
117 .class = I2C_CLASS_HWMON
,
121 .probe
= tmp401_probe
,
122 .remove
= tmp401_remove
,
123 .id_table
= tmp401_id
,
124 .detect
= tmp401_detect
,
125 .address_list
= normal_i2c
,
129 * Client data (each client gets its own)
133 struct device
*hwmon_dev
;
134 struct mutex update_lock
;
135 char valid
; /* zero until following fields are valid */
136 unsigned long last_updated
; /* in jiffies */
139 /* register values */
152 * Sysfs attr show / store functions
155 static int tmp401_register_to_temp(u16 reg
, u8 config
)
159 if (config
& TMP401_CONFIG_RANGE
)
162 return (temp
* 625 + 80) / 160;
165 static u16
tmp401_temp_to_register(long temp
, u8 config
)
167 if (config
& TMP401_CONFIG_RANGE
) {
168 temp
= SENSORS_LIMIT(temp
, -64000, 191000);
171 temp
= SENSORS_LIMIT(temp
, 0, 127000);
173 return (temp
* 160 + 312) / 625;
176 static int tmp401_crit_register_to_temp(u8 reg
, u8 config
)
180 if (config
& TMP401_CONFIG_RANGE
)
186 static u8
tmp401_crit_temp_to_register(long temp
, u8 config
)
188 if (config
& TMP401_CONFIG_RANGE
) {
189 temp
= SENSORS_LIMIT(temp
, -64000, 191000);
192 temp
= SENSORS_LIMIT(temp
, 0, 127000);
194 return (temp
+ 500) / 1000;
197 static ssize_t
show_temp_value(struct device
*dev
,
198 struct device_attribute
*devattr
, char *buf
)
200 int index
= to_sensor_dev_attr(devattr
)->index
;
201 struct tmp401_data
*data
= tmp401_update_device(dev
);
203 return sprintf(buf
, "%d\n",
204 tmp401_register_to_temp(data
->temp
[index
], data
->config
));
207 static ssize_t
show_temp_min(struct device
*dev
,
208 struct device_attribute
*devattr
, char *buf
)
210 int index
= to_sensor_dev_attr(devattr
)->index
;
211 struct tmp401_data
*data
= tmp401_update_device(dev
);
213 return sprintf(buf
, "%d\n",
214 tmp401_register_to_temp(data
->temp_low
[index
], data
->config
));
217 static ssize_t
show_temp_max(struct device
*dev
,
218 struct device_attribute
*devattr
, char *buf
)
220 int index
= to_sensor_dev_attr(devattr
)->index
;
221 struct tmp401_data
*data
= tmp401_update_device(dev
);
223 return sprintf(buf
, "%d\n",
224 tmp401_register_to_temp(data
->temp_high
[index
], data
->config
));
227 static ssize_t
show_temp_crit(struct device
*dev
,
228 struct device_attribute
*devattr
, char *buf
)
230 int index
= to_sensor_dev_attr(devattr
)->index
;
231 struct tmp401_data
*data
= tmp401_update_device(dev
);
233 return sprintf(buf
, "%d\n",
234 tmp401_crit_register_to_temp(data
->temp_crit
[index
],
238 static ssize_t
show_temp_crit_hyst(struct device
*dev
,
239 struct device_attribute
*devattr
, char *buf
)
241 int temp
, index
= to_sensor_dev_attr(devattr
)->index
;
242 struct tmp401_data
*data
= tmp401_update_device(dev
);
244 mutex_lock(&data
->update_lock
);
245 temp
= tmp401_crit_register_to_temp(data
->temp_crit
[index
],
247 temp
-= data
->temp_crit_hyst
* 1000;
248 mutex_unlock(&data
->update_lock
);
250 return sprintf(buf
, "%d\n", temp
);
253 static ssize_t
show_temp_lowest(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_lowest
[index
],
264 static ssize_t
show_temp_highest(struct device
*dev
,
265 struct device_attribute
*devattr
, char *buf
)
267 int index
= to_sensor_dev_attr(devattr
)->index
;
268 struct tmp401_data
*data
= tmp401_update_device(dev
);
270 return sprintf(buf
, "%d\n",
271 tmp401_register_to_temp(data
->temp_highest
[index
],
275 static ssize_t
show_status(struct device
*dev
,
276 struct device_attribute
*devattr
, char *buf
)
278 int mask
= to_sensor_dev_attr(devattr
)->index
;
279 struct tmp401_data
*data
= tmp401_update_device(dev
);
281 if (data
->status
& mask
)
282 return sprintf(buf
, "1\n");
284 return sprintf(buf
, "0\n");
287 static ssize_t
store_temp_min(struct device
*dev
, struct device_attribute
288 *devattr
, const char *buf
, size_t count
)
290 int index
= to_sensor_dev_attr(devattr
)->index
;
291 struct tmp401_data
*data
= tmp401_update_device(dev
);
295 if (strict_strtol(buf
, 10, &val
))
298 reg
= tmp401_temp_to_register(val
, data
->config
);
300 mutex_lock(&data
->update_lock
);
302 i2c_smbus_write_byte_data(to_i2c_client(dev
),
303 TMP401_TEMP_LOW_LIMIT_MSB_WRITE
[index
], reg
>> 8);
304 i2c_smbus_write_byte_data(to_i2c_client(dev
),
305 TMP401_TEMP_LOW_LIMIT_LSB
[index
], reg
& 0xFF);
307 data
->temp_low
[index
] = reg
;
309 mutex_unlock(&data
->update_lock
);
314 static ssize_t
store_temp_max(struct device
*dev
, struct device_attribute
315 *devattr
, const char *buf
, size_t count
)
317 int index
= to_sensor_dev_attr(devattr
)->index
;
318 struct tmp401_data
*data
= tmp401_update_device(dev
);
322 if (strict_strtol(buf
, 10, &val
))
325 reg
= tmp401_temp_to_register(val
, data
->config
);
327 mutex_lock(&data
->update_lock
);
329 i2c_smbus_write_byte_data(to_i2c_client(dev
),
330 TMP401_TEMP_HIGH_LIMIT_MSB_WRITE
[index
], reg
>> 8);
331 i2c_smbus_write_byte_data(to_i2c_client(dev
),
332 TMP401_TEMP_HIGH_LIMIT_LSB
[index
], reg
& 0xFF);
334 data
->temp_high
[index
] = reg
;
336 mutex_unlock(&data
->update_lock
);
341 static ssize_t
store_temp_crit(struct device
*dev
, struct device_attribute
342 *devattr
, const char *buf
, size_t count
)
344 int index
= to_sensor_dev_attr(devattr
)->index
;
345 struct tmp401_data
*data
= tmp401_update_device(dev
);
349 if (strict_strtol(buf
, 10, &val
))
352 reg
= tmp401_crit_temp_to_register(val
, data
->config
);
354 mutex_lock(&data
->update_lock
);
356 i2c_smbus_write_byte_data(to_i2c_client(dev
),
357 TMP401_TEMP_CRIT_LIMIT
[index
], reg
);
359 data
->temp_crit
[index
] = reg
;
361 mutex_unlock(&data
->update_lock
);
366 static ssize_t
store_temp_crit_hyst(struct device
*dev
, struct device_attribute
367 *devattr
, const char *buf
, size_t count
)
369 int temp
, index
= to_sensor_dev_attr(devattr
)->index
;
370 struct tmp401_data
*data
= tmp401_update_device(dev
);
374 if (strict_strtol(buf
, 10, &val
))
377 if (data
->config
& TMP401_CONFIG_RANGE
)
378 val
= SENSORS_LIMIT(val
, -64000, 191000);
380 val
= SENSORS_LIMIT(val
, 0, 127000);
382 mutex_lock(&data
->update_lock
);
383 temp
= tmp401_crit_register_to_temp(data
->temp_crit
[index
],
385 val
= SENSORS_LIMIT(val
, temp
- 255000, temp
);
386 reg
= ((temp
- val
) + 500) / 1000;
388 i2c_smbus_write_byte_data(to_i2c_client(dev
),
389 TMP401_TEMP_CRIT_HYST
, reg
);
391 data
->temp_crit_hyst
= reg
;
393 mutex_unlock(&data
->update_lock
);
399 * Resets the historical measurements of minimum and maximum temperatures.
400 * This is done by writing any value to any of the minimum/maximum registers
403 static ssize_t
reset_temp_history(struct device
*dev
,
404 struct device_attribute
*devattr
, const char *buf
, size_t count
)
408 if (strict_strtol(buf
, 10, &val
))
412 dev_err(dev
, "temp_reset_history value %ld not"
413 " supported. Use 1 to reset the history!\n", val
);
416 i2c_smbus_write_byte_data(to_i2c_client(dev
),
417 TMP411_TEMP_LOWEST_MSB
[0], val
);
422 static struct sensor_device_attribute tmp401_attr
[] = {
423 SENSOR_ATTR(temp1_input
, 0444, show_temp_value
, NULL
, 0),
424 SENSOR_ATTR(temp1_min
, 0644, show_temp_min
, store_temp_min
, 0),
425 SENSOR_ATTR(temp1_max
, 0644, show_temp_max
, store_temp_max
, 0),
426 SENSOR_ATTR(temp1_crit
, 0644, show_temp_crit
, store_temp_crit
, 0),
427 SENSOR_ATTR(temp1_crit_hyst
, 0644, show_temp_crit_hyst
,
428 store_temp_crit_hyst
, 0),
429 SENSOR_ATTR(temp1_min_alarm
, 0444, show_status
, NULL
,
430 TMP401_STATUS_LOCAL_LOW
),
431 SENSOR_ATTR(temp1_max_alarm
, 0444, show_status
, NULL
,
432 TMP401_STATUS_LOCAL_HIGH
),
433 SENSOR_ATTR(temp1_crit_alarm
, 0444, show_status
, NULL
,
434 TMP401_STATUS_LOCAL_CRIT
),
435 SENSOR_ATTR(temp2_input
, 0444, show_temp_value
, NULL
, 1),
436 SENSOR_ATTR(temp2_min
, 0644, show_temp_min
, store_temp_min
, 1),
437 SENSOR_ATTR(temp2_max
, 0644, show_temp_max
, store_temp_max
, 1),
438 SENSOR_ATTR(temp2_crit
, 0644, show_temp_crit
, store_temp_crit
, 1),
439 SENSOR_ATTR(temp2_crit_hyst
, 0444, show_temp_crit_hyst
, NULL
, 1),
440 SENSOR_ATTR(temp2_fault
, 0444, show_status
, NULL
,
441 TMP401_STATUS_REMOTE_OPEN
),
442 SENSOR_ATTR(temp2_min_alarm
, 0444, show_status
, NULL
,
443 TMP401_STATUS_REMOTE_LOW
),
444 SENSOR_ATTR(temp2_max_alarm
, 0444, show_status
, NULL
,
445 TMP401_STATUS_REMOTE_HIGH
),
446 SENSOR_ATTR(temp2_crit_alarm
, 0444, show_status
, NULL
,
447 TMP401_STATUS_REMOTE_CRIT
),
451 * Additional features of the TMP411 chip.
452 * The TMP411 stores the minimum and maximum
453 * temperature measured since power-on, chip-reset, or
454 * minimum and maximum register reset for both the local
455 * and remote channels.
457 static struct sensor_device_attribute tmp411_attr
[] = {
458 SENSOR_ATTR(temp1_highest
, 0444, show_temp_highest
, NULL
, 0),
459 SENSOR_ATTR(temp1_lowest
, 0444, show_temp_lowest
, NULL
, 0),
460 SENSOR_ATTR(temp2_highest
, 0444, show_temp_highest
, NULL
, 1),
461 SENSOR_ATTR(temp2_lowest
, 0444, show_temp_lowest
, NULL
, 1),
462 SENSOR_ATTR(temp_reset_history
, 0200, NULL
, reset_temp_history
, 0),
466 * Begin non sysfs callback code (aka Real code)
469 static void tmp401_init_client(struct i2c_client
*client
)
471 int config
, config_orig
;
473 /* Set the conversion rate to 2 Hz */
474 i2c_smbus_write_byte_data(client
, TMP401_CONVERSION_RATE_WRITE
, 5);
476 /* Start conversions (disable shutdown if necessary) */
477 config
= i2c_smbus_read_byte_data(client
, TMP401_CONFIG_READ
);
479 dev_warn(&client
->dev
, "Initialization failed!\n");
483 config_orig
= config
;
484 config
&= ~TMP401_CONFIG_SHUTDOWN
;
486 if (config
!= config_orig
)
487 i2c_smbus_write_byte_data(client
, TMP401_CONFIG_WRITE
, config
);
490 static int tmp401_detect(struct i2c_client
*client
,
491 struct i2c_board_info
*info
)
494 struct i2c_adapter
*adapter
= client
->adapter
;
497 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
500 /* Detect and identify the chip */
501 reg
= i2c_smbus_read_byte_data(client
, TMP401_MANUFACTURER_ID_REG
);
502 if (reg
!= TMP401_MANUFACTURER_ID
)
505 reg
= i2c_smbus_read_byte_data(client
, TMP401_DEVICE_ID_REG
);
508 case TMP401_DEVICE_ID
:
511 case TMP411_DEVICE_ID
:
518 reg
= i2c_smbus_read_byte_data(client
, TMP401_CONFIG_READ
);
522 reg
= i2c_smbus_read_byte_data(client
, TMP401_CONVERSION_RATE_READ
);
523 /* Datasheet says: 0x1-0x6 */
527 strlcpy(info
->type
, tmp401_id
[kind
].name
, I2C_NAME_SIZE
);
532 static int tmp401_probe(struct i2c_client
*client
,
533 const struct i2c_device_id
*id
)
536 struct tmp401_data
*data
;
537 const char *names
[] = { "TMP401", "TMP411" };
539 data
= kzalloc(sizeof(struct tmp401_data
), GFP_KERNEL
);
543 i2c_set_clientdata(client
, data
);
544 mutex_init(&data
->update_lock
);
545 data
->kind
= id
->driver_data
;
547 /* Initialize the TMP401 chip */
548 tmp401_init_client(client
);
550 /* Register sysfs hooks */
551 for (i
= 0; i
< ARRAY_SIZE(tmp401_attr
); i
++) {
552 err
= device_create_file(&client
->dev
,
553 &tmp401_attr
[i
].dev_attr
);
558 /* Register aditional tmp411 sysfs hooks */
559 if (data
->kind
== tmp411
) {
560 for (i
= 0; i
< ARRAY_SIZE(tmp411_attr
); i
++) {
561 err
= device_create_file(&client
->dev
,
562 &tmp411_attr
[i
].dev_attr
);
568 data
->hwmon_dev
= hwmon_device_register(&client
->dev
);
569 if (IS_ERR(data
->hwmon_dev
)) {
570 err
= PTR_ERR(data
->hwmon_dev
);
571 data
->hwmon_dev
= NULL
;
575 dev_info(&client
->dev
, "Detected TI %s chip\n", names
[data
->kind
]);
580 tmp401_remove(client
); /* will also free data for us */
584 static int tmp401_remove(struct i2c_client
*client
)
586 struct tmp401_data
*data
= i2c_get_clientdata(client
);
590 hwmon_device_unregister(data
->hwmon_dev
);
592 for (i
= 0; i
< ARRAY_SIZE(tmp401_attr
); i
++)
593 device_remove_file(&client
->dev
, &tmp401_attr
[i
].dev_attr
);
595 if (data
->kind
== tmp411
) {
596 for (i
= 0; i
< ARRAY_SIZE(tmp411_attr
); i
++)
597 device_remove_file(&client
->dev
,
598 &tmp411_attr
[i
].dev_attr
);
605 static struct tmp401_data
*tmp401_update_device_reg16(
606 struct i2c_client
*client
, struct tmp401_data
*data
)
610 for (i
= 0; i
< 2; i
++) {
612 * High byte must be read first immediately followed
615 data
->temp
[i
] = i2c_smbus_read_byte_data(client
,
616 TMP401_TEMP_MSB
[i
]) << 8;
617 data
->temp
[i
] |= i2c_smbus_read_byte_data(client
,
619 data
->temp_low
[i
] = i2c_smbus_read_byte_data(client
,
620 TMP401_TEMP_LOW_LIMIT_MSB_READ
[i
]) << 8;
621 data
->temp_low
[i
] |= i2c_smbus_read_byte_data(client
,
622 TMP401_TEMP_LOW_LIMIT_LSB
[i
]);
623 data
->temp_high
[i
] = i2c_smbus_read_byte_data(client
,
624 TMP401_TEMP_HIGH_LIMIT_MSB_READ
[i
]) << 8;
625 data
->temp_high
[i
] |= i2c_smbus_read_byte_data(client
,
626 TMP401_TEMP_HIGH_LIMIT_LSB
[i
]);
627 data
->temp_crit
[i
] = i2c_smbus_read_byte_data(client
,
628 TMP401_TEMP_CRIT_LIMIT
[i
]);
630 if (data
->kind
== tmp411
) {
631 data
->temp_lowest
[i
] = i2c_smbus_read_byte_data(client
,
632 TMP411_TEMP_LOWEST_MSB
[i
]) << 8;
633 data
->temp_lowest
[i
] |= i2c_smbus_read_byte_data(
634 client
, TMP411_TEMP_LOWEST_LSB
[i
]);
636 data
->temp_highest
[i
] = i2c_smbus_read_byte_data(
637 client
, TMP411_TEMP_HIGHEST_MSB
[i
]) << 8;
638 data
->temp_highest
[i
] |= i2c_smbus_read_byte_data(
639 client
, TMP411_TEMP_HIGHEST_LSB
[i
]);
645 static struct tmp401_data
*tmp401_update_device(struct device
*dev
)
647 struct i2c_client
*client
= to_i2c_client(dev
);
648 struct tmp401_data
*data
= i2c_get_clientdata(client
);
650 mutex_lock(&data
->update_lock
);
652 if (time_after(jiffies
, data
->last_updated
+ HZ
) || !data
->valid
) {
653 data
->status
= i2c_smbus_read_byte_data(client
, TMP401_STATUS
);
654 data
->config
= i2c_smbus_read_byte_data(client
,
656 tmp401_update_device_reg16(client
, data
);
658 data
->temp_crit_hyst
= i2c_smbus_read_byte_data(client
,
659 TMP401_TEMP_CRIT_HYST
);
661 data
->last_updated
= jiffies
;
665 mutex_unlock(&data
->update_lock
);
670 static int __init
tmp401_init(void)
672 return i2c_add_driver(&tmp401_driver
);
675 static void __exit
tmp401_exit(void)
677 i2c_del_driver(&tmp401_driver
);
680 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
681 MODULE_DESCRIPTION("Texas Instruments TMP401 temperature sensor driver");
682 MODULE_LICENSE("GPL");
684 module_init(tmp401_init
);
685 module_exit(tmp401_exit
);